ESPHome  2022.6.3
canbus.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace canbus {
9 
10 enum Error : uint8_t {
11  ERROR_OK = 0,
17 };
18 
19 enum CanSpeed : uint8_t {
36 };
37 
38 class CanbusTrigger;
39 template<typename... Ts> class CanbusSendAction;
40 
41 /* CAN payload length definitions according to ISO 11898-1 */
42 static const uint8_t CAN_MAX_DATA_LENGTH = 8;
43 
44 /*
45 Can Frame describes a normative CAN Frame
46 The RTR = Remote Transmission Request is implemented in every CAN controller but rarely used
47 So currently the flag is passed to and from the hardware but currently ignored to the user application.
48 */
49 struct CanFrame {
50  bool use_extended_id = false;
52  uint32_t can_id; /* 29 or 11 bit CAN_ID */
53  uint8_t can_data_length_code; /* frame payload length in byte (0 .. CAN_MAX_DATA_LENGTH) */
54  uint8_t data[CAN_MAX_DATA_LENGTH] __attribute__((aligned(8)));
55 };
56 
57 class Canbus : public Component {
58  public:
59  Canbus(){};
60  void setup() override;
61  void dump_config() override;
62  float get_setup_priority() const override { return setup_priority::HARDWARE; }
63  void loop() override;
64 
65  void send_data(uint32_t can_id, bool use_extended_id, bool remote_transmission_request,
66  const std::vector<uint8_t> &data);
67  void send_data(uint32_t can_id, bool use_extended_id, const std::vector<uint8_t> &data) {
68  // for backwards compatibility only
69  this->send_data(can_id, use_extended_id, false, data);
70  }
71  void set_can_id(uint32_t can_id) { this->can_id_ = can_id; }
72  void set_use_extended_id(bool use_extended_id) { this->use_extended_id_ = use_extended_id; }
73  void set_bitrate(CanSpeed bit_rate) { this->bit_rate_ = bit_rate; }
74 
75  void add_trigger(CanbusTrigger *trigger);
76 
77  protected:
78  template<typename... Ts> friend class CanbusSendAction;
79  std::vector<CanbusTrigger *> triggers_{};
80  uint32_t can_id_;
83 
84  virtual bool setup_internal();
85  virtual Error send_message(struct CanFrame *frame);
86  virtual Error read_message(struct CanFrame *frame);
87 };
88 
89 template<typename... Ts> class CanbusSendAction : public Action<Ts...>, public Parented<Canbus> {
90  public:
91  void set_data_template(const std::function<std::vector<uint8_t>(Ts...)> func) {
92  this->data_func_ = func;
93  this->static_ = false;
94  }
95  void set_data_static(const std::vector<uint8_t> &data) {
96  this->data_static_ = data;
97  this->static_ = true;
98  }
99 
100  void set_can_id(uint32_t can_id) { this->can_id_ = can_id; }
101 
102  void set_use_extended_id(bool use_extended_id) { this->use_extended_id_ = use_extended_id; }
103 
104  void set_remote_transmission_request(bool remote_transmission_request) {
105  this->remote_transmission_request_ = remote_transmission_request;
106  }
107 
108  void play(Ts... x) override {
109  auto can_id = this->can_id_.has_value() ? *this->can_id_ : this->parent_->can_id_;
110  auto use_extended_id =
111  this->use_extended_id_.has_value() ? *this->use_extended_id_ : this->parent_->use_extended_id_;
112  if (this->static_) {
113  this->parent_->send_data(can_id, use_extended_id, this->remote_transmission_request_, this->data_static_);
114  } else {
115  auto val = this->data_func_(x...);
116  this->parent_->send_data(can_id, use_extended_id, this->remote_transmission_request_, val);
117  }
118  }
119 
120  protected:
122  optional<bool> use_extended_id_{};
123  bool remote_transmission_request_{false};
124  bool static_{false};
125  std::function<std::vector<uint8_t>(Ts...)> data_func_{};
126  std::vector<uint8_t> data_static_{};
127 };
128 
129 class CanbusTrigger : public Trigger<std::vector<uint8_t>, uint32_t, bool>, public Component {
130  friend class Canbus;
131 
132  public:
133  explicit CanbusTrigger(Canbus *parent, const std::uint32_t can_id, const std::uint32_t can_id_mask,
134  const bool use_extended_id)
135  : parent_(parent), can_id_(can_id), can_id_mask_(can_id_mask), use_extended_id_(use_extended_id){};
136 
137  void set_remote_transmission_request(bool remote_transmission_request) {
138  this->remote_transmission_request_ = remote_transmission_request;
139  }
140 
141  void setup() override { this->parent_->add_trigger(this); }
142 
143  protected:
145  uint32_t can_id_;
146  uint32_t can_id_mask_;
148  optional<bool> remote_transmission_request_{};
149 };
150 
151 } // namespace canbus
152 } // namespace esphome
void setup()
void send_data(uint32_t can_id, bool use_extended_id, const std::vector< uint8_t > &data)
Definition: canbus.h:67
void loop()
void set_data_static(const std::vector< uint8_t > &data)
Definition: canbus.h:95
float get_setup_priority() const override
Definition: canbus.h:62
uint8_t can_data_length_code
Definition: canbus.h:53
void set_bitrate(CanSpeed bit_rate)
Definition: canbus.h:73
CanbusTrigger(Canbus *parent, const std::uint32_t can_id, const std::uint32_t can_id_mask, const bool use_extended_id)
Definition: canbus.h:133
void set_can_id(uint32_t can_id)
Definition: canbus.h:100
CanSpeed bit_rate_
Definition: canbus.h:82
void setup() override
Definition: canbus.h:141
bool remote_transmission_request
Definition: canbus.h:51
void set_use_extended_id(bool use_extended_id)
Definition: canbus.h:102
void set_data_template(const std::function< std::vector< uint8_t >(Ts...)> func)
Definition: canbus.h:91
void set_remote_transmission_request(bool remote_transmission_request)
Definition: canbus.h:137
void set_use_extended_id(bool use_extended_id)
Definition: canbus.h:72
void play(Ts... x) override
Definition: canbus.h:108
uint8_t data [CAN_MAX_DATA_LENGTH] __attribute__((aligned(8)))
const float HARDWARE
For components that deal with hardware and are very important like GPIO switch.
Definition: component.cpp:17
void set_can_id(uint32_t can_id)
Definition: canbus.h:71
Definition: a4988.cpp:4
uint32_t val
Definition: datatypes.h:85
Helper class to easily give an object a parent of type T.
Definition: helpers.h:494
void set_remote_transmission_request(bool remote_transmission_request)
Definition: canbus.h:104