ESPHome  2022.12.8
canbus.cpp
Go to the documentation of this file.
1 #include "canbus.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace canbus {
6 
7 static const char *const TAG = "canbus";
8 
9 void Canbus::setup() {
10  ESP_LOGCONFIG(TAG, "Setting up Canbus...");
11  if (!this->setup_internal()) {
12  ESP_LOGE(TAG, "setup error!");
13  this->mark_failed();
14  }
15 }
16 
18  if (this->use_extended_id_) {
19  ESP_LOGCONFIG(TAG, "config extended id=0x%08x", this->can_id_);
20  } else {
21  ESP_LOGCONFIG(TAG, "config standard id=0x%03x", this->can_id_);
22  }
23 }
24 
25 void Canbus::send_data(uint32_t can_id, bool use_extended_id, bool remote_transmission_request,
26  const std::vector<uint8_t> &data) {
27  struct CanFrame can_message;
28 
29  uint8_t size = static_cast<uint8_t>(data.size());
30  if (use_extended_id) {
31  ESP_LOGD(TAG, "send extended id=0x%08x rtr=%s size=%d", can_id, TRUEFALSE(remote_transmission_request), size);
32  } else {
33  ESP_LOGD(TAG, "send extended id=0x%03x rtr=%s size=%d", can_id, TRUEFALSE(remote_transmission_request), size);
34  }
35  if (size > CAN_MAX_DATA_LENGTH)
36  size = CAN_MAX_DATA_LENGTH;
37  can_message.can_data_length_code = size;
38  can_message.can_id = can_id;
39  can_message.use_extended_id = use_extended_id;
40  can_message.remote_transmission_request = remote_transmission_request;
41 
42  for (int i = 0; i < size; i++) {
43  can_message.data[i] = data[i];
44  ESP_LOGVV(TAG, " data[%d]=%02x", i, can_message.data[i]);
45  }
46 
47  this->send_message(&can_message);
48 }
49 
51  if (trigger->use_extended_id_) {
52  ESP_LOGVV(TAG, "add trigger for extended canid=0x%08x", trigger->can_id_);
53  } else {
54  ESP_LOGVV(TAG, "add trigger for std canid=0x%03x", trigger->can_id_);
55  }
56  this->triggers_.push_back(trigger);
57 };
58 
59 void Canbus::loop() {
60  struct CanFrame can_message;
61  // read all messages until queue is empty
62  int message_counter = 0;
63  while (this->read_message(&can_message) == canbus::ERROR_OK) {
64  message_counter++;
65  if (can_message.use_extended_id) {
66  ESP_LOGD(TAG, "received can message (#%d) extended can_id=0x%x size=%d", message_counter, can_message.can_id,
67  can_message.can_data_length_code);
68  } else {
69  ESP_LOGD(TAG, "received can message (#%d) std can_id=0x%x size=%d", message_counter, can_message.can_id,
70  can_message.can_data_length_code);
71  }
72 
73  std::vector<uint8_t> data;
74 
75  // show data received
76  for (int i = 0; i < can_message.can_data_length_code; i++) {
77  ESP_LOGV(TAG, " can_message.data[%d]=%02x", i, can_message.data[i]);
78  data.push_back(can_message.data[i]);
79  }
80 
81  // fire all triggers
82  for (auto *trigger : this->triggers_) {
83  if ((trigger->can_id_ == (can_message.can_id & trigger->can_id_mask_)) &&
84  (trigger->use_extended_id_ == can_message.use_extended_id) &&
85  (!trigger->remote_transmission_request_.has_value() ||
86  trigger->remote_transmission_request_.value() == can_message.remote_transmission_request)) {
87  trigger->trigger(data, can_message.can_id, can_message.remote_transmission_request);
88  }
89  }
90  }
91 }
92 
93 } // namespace canbus
94 } // namespace esphome
void setup() override
Definition: canbus.cpp:9
void loop() override
Definition: canbus.cpp:59
void add_trigger(CanbusTrigger *trigger)
Definition: canbus.cpp:50
uint8_t can_data_length_code
Definition: canbus.h:55
bool remote_transmission_request
Definition: canbus.h:53
virtual Error read_message(struct CanFrame *frame)
virtual bool setup_internal()
virtual Error send_message(struct CanFrame *frame)
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:112
Definition: a4988.cpp:4
void send_data(uint32_t can_id, bool use_extended_id, bool remote_transmission_request, const std::vector< uint8_t > &data)
Definition: canbus.cpp:25
std::vector< CanbusTrigger * > triggers_
Definition: canbus.h:81
void dump_config() override
Definition: canbus.cpp:17