ESPHome  2024.4.0
rdm6300.cpp
Go to the documentation of this file.
1 #include "rdm6300.h"
2 #include "esphome/core/log.h"
3 
4 namespace esphome {
5 namespace rdm6300 {
6 
7 static const char *const TAG = "rdm6300";
8 
9 static const uint8_t RDM6300_START_BYTE = 0x02;
10 static const uint8_t RDM6300_END_BYTE = 0x03;
11 static const int8_t RDM6300_STATE_WAITING_FOR_START = -1;
12 
14  while (this->available() > 0) {
15  uint8_t data;
16  if (!this->read_byte(&data)) {
17  ESP_LOGW(TAG, "Reading data from RDM6300 failed!");
18  this->status_set_warning();
19  return;
20  }
21 
22  if (this->read_state_ == RDM6300_STATE_WAITING_FOR_START) {
23  if (data == RDM6300_START_BYTE) {
24  this->read_state_ = 0;
25  } else {
26  // Not start byte, probably not synced up correctly.
27  }
28  } else if (this->read_state_ < 12) {
29  uint8_t value = (data > '9') ? data - '7' : data - '0';
30  if (this->read_state_ % 2 == 0) {
31  this->buffer_[this->read_state_ / 2] = value << 4;
32  } else {
33  this->buffer_[this->read_state_ / 2] += value;
34  }
35  this->read_state_++;
36  } else if (data != RDM6300_END_BYTE) {
37  ESP_LOGW(TAG, "Invalid end byte from RDM6300!");
38  this->read_state_ = RDM6300_STATE_WAITING_FOR_START;
39  } else {
40  uint8_t checksum = 0;
41  for (uint8_t i = 0; i < 5; i++)
42  checksum ^= this->buffer_[i];
43  this->read_state_ = RDM6300_STATE_WAITING_FOR_START;
44  if (checksum != this->buffer_[5]) {
45  ESP_LOGW(TAG, "Checksum from RDM6300 doesn't match! (0x%02X!=0x%02X)", checksum, this->buffer_[5]);
46  } else {
47  // Valid data
48  this->status_clear_warning();
49  const uint32_t result = encode_uint32(this->buffer_[1], this->buffer_[2], this->buffer_[3], this->buffer_[4]);
50  bool report = result != last_id_;
51  for (auto *card : this->cards_) {
52  if (card->process(result)) {
53  report = false;
54  }
55  }
56  for (auto *trig : this->triggers_)
57  trig->process(result);
58 
59  if (report) {
60  ESP_LOGD(TAG, "Found new tag with ID %" PRIu32, result);
61  }
62  }
63  }
64  }
65 }
66 
67 } // namespace rdm6300
68 } // namespace esphome
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition: helpers.h:186
uint8_t checksum
Definition: bl0939.h:35
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7