ESPHome 2025.7.4
Loading...
Searching...
No Matches
rdm6300.cpp
Go to the documentation of this file.
1#include "rdm6300.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace rdm6300 {
7
8static const char *const TAG = "rdm6300";
9
10static const uint8_t RDM6300_START_BYTE = 0x02;
11static const uint8_t RDM6300_END_BYTE = 0x03;
12static const int8_t RDM6300_STATE_WAITING_FOR_START = -1;
13
15 while (this->available() > 0) {
16 uint8_t data;
17 if (!this->read_byte(&data)) {
18 ESP_LOGW(TAG, "Reading data from RDM6300 failed!");
19 this->status_set_warning();
20 return;
21 }
22
23 if (this->read_state_ == RDM6300_STATE_WAITING_FOR_START) {
24 if (data == RDM6300_START_BYTE) {
25 this->read_state_ = 0;
26 } else {
27 // Not start byte, probably not synced up correctly.
28 }
29 } else if (this->read_state_ < 12) {
30 uint8_t value = (data > '9') ? data - '7' : data - '0';
31 if (this->read_state_ % 2 == 0) {
32 this->buffer_[this->read_state_ / 2] = value << 4;
33 } else {
34 this->buffer_[this->read_state_ / 2] += value;
35 }
36 this->read_state_++;
37 } else if (data != RDM6300_END_BYTE) {
38 ESP_LOGW(TAG, "Invalid end byte from RDM6300!");
39 this->read_state_ = RDM6300_STATE_WAITING_FOR_START;
40 } else {
41 uint8_t checksum = 0;
42 for (uint8_t i = 0; i < 5; i++)
43 checksum ^= this->buffer_[i];
44 this->read_state_ = RDM6300_STATE_WAITING_FOR_START;
45 if (checksum != this->buffer_[5]) {
46 ESP_LOGW(TAG, "Checksum from RDM6300 doesn't match! (0x%02X!=0x%02X)", checksum, this->buffer_[5]);
47 } else {
48 // Valid data
49 this->status_clear_warning();
50 const uint32_t result = encode_uint32(this->buffer_[1], this->buffer_[2], this->buffer_[3], this->buffer_[4]);
51 bool report = result != last_id_;
52 for (auto *card : this->cards_) {
53 if (card->process(result)) {
54 report = false;
55 }
56 }
57 for (auto *trig : this->triggers_)
58 trig->process(result);
59
60 if (report) {
61 ESP_LOGD(TAG, "Found new tag with ID %" PRIu32, result);
62 }
63 }
64 }
65 }
66}
67
68} // namespace rdm6300
69} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:134