ESPHome  2024.11.1
wl_134.cpp
Go to the documentation of this file.
1 #include "wl_134.h"
2 #include "esphome/core/log.h"
3 
4 #include <cinttypes>
5 
6 namespace esphome {
7 namespace wl_134 {
8 
9 static const char *const TAG = "wl_134.sensor";
10 static const uint8_t ASCII_CR = 0x0D;
11 static const uint8_t ASCII_NBSP = 0xFF;
12 static const int MAX_DATA_LENGTH_BYTES = 6;
13 
14 void Wl134Component::setup() { this->publish_state(""); }
15 
17  while (this->available() >= RFID134_PACKET_SIZE) {
18  Wl134Component::Rfid134Error error = this->read_packet_();
19  if (error != RFID134_ERROR_NONE) {
20  ESP_LOGW(TAG, "Error: %d", error);
21  }
22  }
23 }
24 
25 Wl134Component::Rfid134Error Wl134Component::read_packet_() {
26  uint8_t packet[RFID134_PACKET_SIZE];
27  packet[RFID134_PACKET_START_CODE] = this->read();
28 
29  // check for the first byte being the packet start code
30  if (packet[RFID134_PACKET_START_CODE] != 0x02) {
31  // just out of sync, ignore until we are synced up
32  return RFID134_ERROR_NONE;
33  }
34 
35  if (!this->read_array(&(packet[RFID134_PACKET_ID]), RFID134_PACKET_SIZE - 1)) {
37  }
38 
39  if (packet[RFID134_PACKET_END_CODE] != 0x03) {
41  }
42 
43  // calculate checksum
44  uint8_t checksum = 0;
45  for (uint8_t i = RFID134_PACKET_ID; i < RFID134_PACKET_CHECKSUM; i++) {
46  checksum = checksum ^ packet[i];
47  }
48 
49  // test checksum
50  if (checksum != packet[RFID134_PACKET_CHECKSUM]) {
52  }
53 
54  if (static_cast<uint8_t>(~checksum) != static_cast<uint8_t>(packet[RFID134_PACKET_CHECKSUM_INVERT])) {
56  }
57 
58  Rfid134Reading reading;
59 
60  // convert packet into the reading struct
61  reading.id = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_ID]), RFID134_PACKET_COUNTRY - RFID134_PACKET_ID);
62  reading.country = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_COUNTRY]),
63  RFID134_PACKET_DATA_FLAG - RFID134_PACKET_COUNTRY);
64  reading.isData = packet[RFID134_PACKET_DATA_FLAG] == '1';
65  reading.isAnimal = packet[RFID134_PACKET_ANIMAL_FLAG] == '1';
66  reading.reserved0 = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_RESERVED0]),
67  RFID134_PACKET_RESERVED1 - RFID134_PACKET_RESERVED0);
68  reading.reserved1 = this->hex_lsb_ascii_to_uint64_(&(packet[RFID134_PACKET_RESERVED1]),
69  RFID134_PACKET_CHECKSUM - RFID134_PACKET_RESERVED1);
70 
71  ESP_LOGV(TAG, "Tag id: %012lld", reading.id);
72  ESP_LOGV(TAG, "Country: %03d", reading.country);
73  ESP_LOGV(TAG, "isData: %s", reading.isData ? "true" : "false");
74  ESP_LOGV(TAG, "isAnimal: %s", reading.isAnimal ? "true" : "false");
75  ESP_LOGV(TAG, "Reserved0: %d", reading.reserved0);
76  ESP_LOGV(TAG, "Reserved1: %" PRId32, reading.reserved1);
77 
78  char buf[20];
79  sprintf(buf, "%03d%012lld", reading.country, reading.id);
80  this->publish_state(buf);
81  if (this->do_reset_) {
82  this->set_timeout(1000, [this]() { this->publish_state(""); });
83  }
84 
85  return RFID134_ERROR_NONE;
86 }
87 
88 uint64_t Wl134Component::hex_lsb_ascii_to_uint64_(const uint8_t *text, uint8_t text_size) {
89  uint64_t value = 0;
90  uint8_t i = text_size;
91  do {
92  i--;
93 
94  uint8_t digit = text[i];
95  if (digit >= 'A') {
96  digit = digit - 'A' + 10;
97  } else {
98  digit = digit - '0';
99  }
100  value = (value << 4) + digit;
101  } while (i != 0);
102 
103  return value;
104 }
105 
107  ESP_LOGCONFIG(TAG, "WL-134 Sensor:");
108  LOG_TEXT_SENSOR("", "Tag", this);
109  // As specified in the sensor's data sheet
111 }
112 } // namespace wl_134
113 } // namespace esphome
optional< std::array< uint8_t, N > > read_array()
Definition: uart.h:33
uint8_t checksum
Definition: bl0906.h:210
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition: component.cpp:69
void dump_config() override
Definition: wl_134.cpp:106
void publish_state(const std::string &state)
Definition: text_sensor.cpp:9
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition: uart.cpp:13
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7