ESPHome  2024.11.1
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%08" PRIx32, this->can_id_);
20  } else {
21  ESP_LOGCONFIG(TAG, "config standard id=0x%03" PRIx32, 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%08" PRIx32 " rtr=%s size=%d", can_id, TRUEFALSE(remote_transmission_request),
32  size);
33  } else {
34  ESP_LOGD(TAG, "send standard id=0x%03" PRIx32 " rtr=%s size=%d", can_id, TRUEFALSE(remote_transmission_request),
35  size);
36  }
37  if (size > CAN_MAX_DATA_LENGTH)
38  size = CAN_MAX_DATA_LENGTH;
39  can_message.can_data_length_code = size;
40  can_message.can_id = can_id;
41  can_message.use_extended_id = use_extended_id;
42  can_message.remote_transmission_request = remote_transmission_request;
43 
44  for (int i = 0; i < size; i++) {
45  can_message.data[i] = data[i];
46  ESP_LOGVV(TAG, " data[%d]=%02x", i, can_message.data[i]);
47  }
48 
49  this->send_message(&can_message);
50 }
51 
53  if (trigger->use_extended_id_) {
54  ESP_LOGVV(TAG, "add trigger for extended canid=0x%08" PRIx32, trigger->can_id_);
55  } else {
56  ESP_LOGVV(TAG, "add trigger for std canid=0x%03" PRIx32, trigger->can_id_);
57  }
58  this->triggers_.push_back(trigger);
59 };
60 
61 void Canbus::loop() {
62  struct CanFrame can_message;
63  // read all messages until queue is empty
64  int message_counter = 0;
65  while (this->read_message(&can_message) == canbus::ERROR_OK) {
66  message_counter++;
67  if (can_message.use_extended_id) {
68  ESP_LOGD(TAG, "received can message (#%d) extended can_id=0x%" PRIx32 " size=%d", message_counter,
69  can_message.can_id, can_message.can_data_length_code);
70  } else {
71  ESP_LOGD(TAG, "received can message (#%d) std can_id=0x%" PRIx32 " size=%d", message_counter, can_message.can_id,
72  can_message.can_data_length_code);
73  }
74 
75  std::vector<uint8_t> data;
76 
77  // show data received
78  for (int i = 0; i < can_message.can_data_length_code; i++) {
79  ESP_LOGV(TAG, " can_message.data[%d]=%02x", i, can_message.data[i]);
80  data.push_back(can_message.data[i]);
81  }
82 
83  // fire all triggers
84  for (auto *trigger : this->triggers_) {
85  if ((trigger->can_id_ == (can_message.can_id & trigger->can_id_mask_)) &&
86  (trigger->use_extended_id_ == can_message.use_extended_id) &&
87  (!trigger->remote_transmission_request_.has_value() ||
88  trigger->remote_transmission_request_.value() == can_message.remote_transmission_request)) {
89  trigger->trigger(data, can_message.can_id, can_message.remote_transmission_request);
90  }
91  }
92  }
93 }
94 
95 } // namespace canbus
96 } // namespace esphome
void setup() override
Definition: canbus.cpp:9
void loop() override
Definition: canbus.cpp:61
void add_trigger(CanbusTrigger *trigger)
Definition: canbus.cpp:52
uint8_t can_data_length_code
Definition: canbus.h:61
bool remote_transmission_request
Definition: canbus.h:59
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:118
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
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:87
void dump_config() override
Definition: canbus.cpp:17