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