ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld2410s.cpp
Go to the documentation of this file.
1#include "ld2410s.h"
2#include <cstring>
4#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7
8namespace esphome::ld2410s {
9
10ESPHOME_LOG_TAG(TAG, "ld2410s");
11
12static constexpr uint8_t SHORT_DATA_FRAME_HEADER = 0x6E;
13static constexpr uint8_t SHORT_DATA_FRAME_FOOTER[] PROGMEM = {0x62};
14static constexpr uint8_t SHORT_DATA_FRAME_SIZE = 5; // header, state, distance low, distance high, footer
15static constexpr uint8_t STD_DATA_FRAME_HEADER[] PROGMEM = {0xF4, 0xF3, 0xF2, 0xF1};
16static constexpr uint8_t STD_DATA_FRAME_FOOTER[] PROGMEM = {0xF8, 0xF7, 0xF6, 0xF5};
17static constexpr uint8_t CMD_FRAME_HEADER[] PROGMEM = {0xFD, 0xFC, 0xFB, 0xFA};
18static constexpr uint8_t CMD_FRAME_FOOTER[] PROGMEM = {0x04, 0x03, 0x02, 0x01};
19static constexpr uint8_t LONG_HEADER_SIZE = 4;
20static constexpr uint8_t LENGTH_FIELD_SIZE = 2;
21static constexpr uint8_t LONG_PAYLOAD_POS = LONG_HEADER_SIZE + LENGTH_FIELD_SIZE;
22
23static constexpr uint16_t CMD_CONFIRMATION = 0x0100; // set in the command word of an acknowledgement
24static constexpr uint16_t CONFIG_MODE_START_CMD = 0x00FF;
25static constexpr uint16_t CONFIG_MODE_START_VALUE = 0x0001;
26static constexpr uint16_t CONFIG_MODE_END_CMD = 0x00FE;
27static constexpr uint16_t VERSION_READ_CMD = 0x0000;
28static constexpr uint16_t PARAMETERS_WRITE_CMD = 0x0070;
29static constexpr uint16_t PARAMETERS_READ_CMD = 0x0071;
30static constexpr uint16_t OUTPUT_MODE_SWITCH_CMD = 0x007A;
31// Minimal output mode (short frames carrying presence and distance only) is selected by six zero bytes
32static constexpr uint8_t OUTPUT_MODE_MINIMAL_SIZE = 6;
33// The parameter write carries a 16-bit key and a 32-bit value per parameter, the read only the keys
34static constexpr uint8_t PARAMETER_KEY_SIZE = 2;
35static constexpr uint8_t PARAMETER_VALUE_SIZE = 4;
36static constexpr uint8_t PARAMETERS_WRITE_SIZE = PARAMETER_COUNT * (PARAMETER_KEY_SIZE + PARAMETER_VALUE_SIZE);
37static constexpr uint8_t CMD_FRAME_MAX_SIZE = LONG_PAYLOAD_POS + 2 + PARAMETERS_WRITE_SIZE + sizeof(CMD_FRAME_FOOTER);
38// Keys of the parameters, in Parameter order
39#ifdef USE_LD2410S_PARAMETERS
40static constexpr uint16_t PARAMETER_KEYS[] PROGMEM = {0x0005, 0x000A, 0x0006, 0x0002, 0x000C, 0x000B};
41static_assert(sizeof(PARAMETER_KEYS) / sizeof(PARAMETER_KEYS[0]) == PARAMETER_COUNT, "one key per parameter");
42#ifdef USE_NUMBER
43// Module units to number units: 0.7 m gates, seconds, tenths of a hertz (number.py uses the same)
44static constexpr float PARAMETER_SCALES[] PROGMEM = {0.7f, 0.7f, 1.0f, 0.1f, 0.1f, 1.0f};
45static_assert(sizeof(PARAMETER_SCALES) / sizeof(PARAMETER_SCALES[0]) == PARAMETER_COUNT, "one scale per parameter");
46#endif
47#endif
48// The version answer: 4 bytes device type, then major, minor and patch as 16-bit words
49static constexpr uint8_t VERSION_TYPE_SIZE = 4;
50static constexpr uint8_t VERSION_SIZE = VERSION_TYPE_SIZE + 3 * 2;
51
52// Everything the module needs at start: configuration mode on, read the version and parameters when
53// an entity shows them, pick the output mode, configuration mode off
54static constexpr uint16_t INIT_SEQUENCE[] PROGMEM = {
55 CONFIG_MODE_START_CMD,
56#ifdef USE_LD2410S_VERSION
57 VERSION_READ_CMD,
58#endif
59#ifdef USE_LD2410S_PARAMETERS
60 PARAMETERS_READ_CMD,
61#endif
62 OUTPUT_MODE_SWITCH_CMD, CONFIG_MODE_END_CMD,
63};
64#ifdef USE_LD2410S_PARAMETERS
65static constexpr uint16_t WRITE_SEQUENCE[] PROGMEM = {CONFIG_MODE_START_CMD, PARAMETERS_WRITE_CMD, CONFIG_MODE_END_CMD};
66#endif
67static constexpr uint8_t INIT_SEQUENCE_LENGTH = sizeof(INIT_SEQUENCE) / sizeof(INIT_SEQUENCE[0]);
68#ifdef USE_LD2410S_PARAMETERS
69static constexpr uint8_t WRITE_SEQUENCE_LENGTH = sizeof(WRITE_SEQUENCE) / sizeof(WRITE_SEQUENCE[0]);
70#endif
71static constexpr uint8_t STEP_MAX_TIMEOUTS = 2;
72static constexpr uint32_t ACK_TIMEOUT_MS = 300;
73static constexpr uint32_t COMMAND_GAP_MS = 300; // pause after an acknowledgement before the next command
74static constexpr uint32_t REINIT_PAUSE_MS = 15000; // pause before starting over after the module stopped answering
75static constexpr uint32_t LINK_TIMEOUT_MS = 5000; // the module streams frames continuously; silence means it is gone
76
77static constexpr uint8_t DATA_TYPE_STANDARD = 0x01;
78
79void LD2410SComponent::dump_config() {
80 ESP_LOGCONFIG(TAG, "LD2410S:");
81#ifdef USE_BINARY_SENSOR
82 LOG_BINARY_SENSOR(" ", "Has Target", this->target_binary_sensor_);
83#endif
84#ifdef USE_SENSOR
85 LOG_SENSOR_WITH_DEDUP(" ", "Distance", this->distance_sensor_);
86#endif
87#ifdef USE_LD2410S_VERSION
88 LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_);
89#endif
90#if defined(USE_NUMBER) && defined(USE_LD2410S_PARAMETERS)
91 for (auto *number : this->numbers_) {
92 LOG_NUMBER(" ", "Setting", number);
93 }
94#endif
95#if defined(USE_SELECT) && defined(USE_LD2410S_PARAMETERS)
96 LOG_SELECT(" ", "Response Speed", this->response_speed_select_);
97#endif
98}
99
101 if (!this->read_available<16>([this](uint8_t b) { this->receive_byte_(b); })) {
102 ESP_LOGV(TAG, "UART read failed");
103 }
105 if (this->sequence_ == nullptr) {
106 if (!this->init_done_) {
107 this->start_sequence_(INIT_SEQUENCE, INIT_SEQUENCE_LENGTH, now);
108#ifdef USE_LD2410S_PARAMETERS
109 } else if (this->parameters_dirty_) {
110 this->parameters_dirty_ = false;
111 // Keep the pause after the last acknowledgement; a start time further ahead than that is long past,
112 // and after 2^31 ms the run_sequence_() check would read it as the future
113 const bool in_gap = this->next_send_at_ - now <= COMMAND_GAP_MS;
114 this->start_sequence_(WRITE_SEQUENCE, WRITE_SEQUENCE_LENGTH, in_gap ? this->next_send_at_ : now);
115#endif
116 }
117 }
118 this->run_sequence_(now);
119 if (this->init_done_ && now - this->last_frame_at_ > LINK_TIMEOUT_MS) {
120 // The count tells real silence apart from frames that never parse (wrong baud rate or firmware)
121 ESP_LOGW(TAG, "No data for %" PRIu32 " s (%u frames dropped), starting over", LINK_TIMEOUT_MS / 1000,
122 this->dropped_frames_);
123 this->dropped_frames_ = 0;
124 this->restart_init_(now);
125 }
126}
127
128#ifdef USE_LD2410S_PARAMETERS
129bool LD2410SComponent::set_parameter(Parameter parameter, uint8_t value) {
130 // All six are written together, so the other five must be the module's own values
131 if (!this->init_done_) {
132 ESP_LOGW(TAG, "Parameters not read yet, change ignored");
133 return false;
134 }
135 if (!this->parameters_known_) {
136 ESP_LOGW(TAG, "The module reported a parameter out of range, changes are disabled");
137 return false;
138 }
139 this->parameters_[static_cast<uint8_t>(parameter)] = value;
140 this->parameters_dirty_ = true; // written by loop() once no sequence is running
141 return true;
142}
143
144#ifdef USE_NUMBER
145void LD2410SComponent::control_number(number::Number *number, float value) {
146 for (uint8_t i = 0; i < PARAMETER_COUNT; i++) {
147 if (this->numbers_[i] != number)
148 continue;
149 // Show the step the module got, not a value between two steps
150 const uint8_t raw = lroundf(value / PARAMETER_SCALES[i]); // the schema keeps every parameter within a byte
151 if (this->set_parameter(static_cast<Parameter>(i), raw))
152 number->publish_state(raw * PARAMETER_SCALES[i]);
153 return;
154 }
155}
156#endif
157
158#ifdef USE_SELECT
159void LD2410SComponent::control_response_speed(select::Select *select, size_t index) {
160 if (this->set_parameter(Parameter::PARAMETER_RESPONSE_SPEED, progmem_read_byte(&RESPONSE_SPEEDS[index])))
161 select->publish_state(index);
162}
163#endif
164#endif
165
167 this->init_done_ = false;
168 this->status_set_warning(); // cleared again when the new init sequence is acknowledged
169#ifdef USE_LD2410S_PARAMETERS
170 // The module's own values count again once it is set up anew
171 if (this->parameters_dirty_) {
172 ESP_LOGW(TAG, "Dropping the pending parameter change, the module's values are read back");
173 }
174 this->parameters_dirty_ = false;
175 this->parameters_known_ = false; // a refused value may sit in parameters_ until the read replaces it
176#endif
177 this->start_sequence_(INIT_SEQUENCE, INIT_SEQUENCE_LENGTH, at);
178}
179
180void LD2410SComponent::start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now) {
181 this->sequence_ = sequence;
182 this->sequence_len_ = length;
183 this->step_ = 0;
184 this->step_timeouts_ = 0;
185 this->awaiting_ack_ = false;
186 this->next_send_at_ = now;
187}
188
190
192 if (this->sequence_ == nullptr || static_cast<int32_t>(now - this->next_send_at_) < 0) {
193 return;
194 }
195 const uint16_t command = this->current_command_();
196 if (this->awaiting_ack_) {
197 if (++this->step_timeouts_ > STEP_MAX_TIMEOUTS) {
198 ESP_LOGW(TAG, "No acknowledgement for command %04X, starting over in %" PRIu32 " s", command,
199 REINIT_PAUSE_MS / 1000);
200 // Leave configuration mode whatever state the module is in, then begin again
201 this->send_command_(CONFIG_MODE_END_CMD);
202 this->restart_init_(now + REINIT_PAUSE_MS);
203 return;
204 }
205 ESP_LOGD(TAG, "No acknowledgement for command %04X, resending", command);
206 }
207 this->send_command_(command);
208 this->awaiting_ack_ = true;
209 this->next_send_at_ = now + ACK_TIMEOUT_MS;
210}
211
212void LD2410SComponent::send_command_(uint16_t command) {
213 uint8_t frame[CMD_FRAME_MAX_SIZE];
214 progmem_memcpy(frame, CMD_FRAME_HEADER, LONG_HEADER_SIZE);
215 uint8_t pos = LONG_PAYLOAD_POS;
216 frame[pos++] = command & 0xFF;
217 frame[pos++] = command >> 8;
218 switch (command) {
219 case OUTPUT_MODE_SWITCH_CMD:
220 memset(&frame[pos], 0, OUTPUT_MODE_MINIMAL_SIZE);
221 pos += OUTPUT_MODE_MINIMAL_SIZE;
222 break;
223 case CONFIG_MODE_START_CMD:
224 frame[pos++] = CONFIG_MODE_START_VALUE & 0xFF;
225 frame[pos++] = CONFIG_MODE_START_VALUE >> 8;
226 break;
227#ifdef USE_LD2410S_PARAMETERS
228 case PARAMETERS_READ_CMD:
229 case PARAMETERS_WRITE_CMD:
230 for (uint8_t i = 0; i < PARAMETER_COUNT; i++) {
231 const uint16_t key = progmem_read_uint16(&PARAMETER_KEYS[i]);
232 frame[pos++] = key & 0xFF;
233 frame[pos++] = key >> 8;
234 if (command == PARAMETERS_WRITE_CMD) {
235 const uint32_t value = this->parameters_[i]; // a 32-bit field on the wire
236 for (uint8_t shift = 0; shift < 32; shift += 8) {
237 frame[pos++] = (value >> shift) & 0xFF;
238 }
239 }
240 }
241 break;
242#endif
243 default:
244 break;
245 }
246 frame[LONG_HEADER_SIZE] = pos - LONG_PAYLOAD_POS; // payload length, little-endian
247 frame[LONG_HEADER_SIZE + 1] = 0;
248 progmem_memcpy(&frame[pos], CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
249 pos += sizeof(CMD_FRAME_FOOTER);
250 this->write_array(frame, pos);
251#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERY_VERBOSE
252 char hex_buf[format_hex_pretty_size(CMD_FRAME_MAX_SIZE)];
253 ESP_LOGVV(TAG, "> %s", format_hex_pretty_to(hex_buf, frame, pos, ' '));
254#endif
255}
256
258 if (this->rx_len_ >= RX_BUFFER_SIZE) {
259 this->reset_frame_();
260 }
261 this->rx_buffer_[this->rx_len_++] = byte;
262
263 if (this->frame_type_ == FrameType::NONE) {
264 if (this->rx_len_ == 1 && byte == SHORT_DATA_FRAME_HEADER) {
266 this->expected_len_ = SHORT_DATA_FRAME_SIZE;
267 return;
268 }
269 // The two long headers are told apart once all four bytes are in
270 uint8_t std_header[LONG_HEADER_SIZE];
271 uint8_t cmd_header[LONG_HEADER_SIZE];
272 progmem_memcpy(std_header, STD_DATA_FRAME_HEADER, LONG_HEADER_SIZE);
273 progmem_memcpy(cmd_header, CMD_FRAME_HEADER, LONG_HEADER_SIZE);
274 const bool std_prefix = memcmp(this->rx_buffer_, std_header, this->rx_len_) == 0;
275 const bool cmd_prefix = memcmp(this->rx_buffer_, cmd_header, this->rx_len_) == 0;
276 if (std_prefix || cmd_prefix) {
277 if (this->rx_len_ == LONG_HEADER_SIZE) {
279 }
280 return;
281 }
282 // Not a header: drop what was collected, and try this byte as the start of the next frame
283 const bool retry = this->rx_len_ > 1;
284 this->reset_frame_();
285 if (retry) {
286 this->receive_byte_(byte);
287 }
288 return;
289 }
290
291 if (this->expected_len_ == 0) {
292 if (this->rx_len_ < LONG_PAYLOAD_POS) {
293 return;
294 }
295 // Standard data and command frames carry a little-endian payload length after the header
296 const uint16_t payload_len =
297 encode_uint16(this->rx_buffer_[LONG_HEADER_SIZE + 1], this->rx_buffer_[LONG_HEADER_SIZE]);
298 if (payload_len > RX_BUFFER_SIZE - LONG_PAYLOAD_POS - sizeof(CMD_FRAME_FOOTER)) {
299 ESP_LOGV(TAG, "Dropping frame with a %u byte payload, larger than the receive buffer", payload_len);
300 this->count_dropped_frame_();
301 this->reset_frame_();
302 return;
303 }
304 this->expected_len_ = LONG_PAYLOAD_POS + payload_len + sizeof(CMD_FRAME_FOOTER);
305 return;
306 }
307
309 return;
310 }
311 if (this->handle_frame_() || this->rescanning_) {
312 this->reset_frame_();
313 return;
314 }
315 // A truncated long frame swallowed the bytes that followed it; try them again as new frames, once
316 uint8_t rest[RX_BUFFER_SIZE];
317 const uint8_t rest_len = this->rx_len_ - 1;
318 memcpy(rest, &this->rx_buffer_[1], rest_len);
319 this->reset_frame_();
320 this->rescanning_ = true;
321 for (uint8_t i = 0; i < rest_len; i++) {
322 this->receive_byte_(rest[i]);
323 }
324 this->rescanning_ = false;
325}
326
328 const uint8_t *footer_src;
329 uint8_t footer_size = sizeof(CMD_FRAME_FOOTER);
330 uint8_t payload_pos = LONG_PAYLOAD_POS;
331 switch (this->frame_type_) {
333 footer_src = SHORT_DATA_FRAME_FOOTER;
334 footer_size = sizeof(SHORT_DATA_FRAME_FOOTER);
335 payload_pos = 1;
336 break;
338 footer_src = STD_DATA_FRAME_FOOTER;
339 break;
340 default:
341 footer_src = CMD_FRAME_FOOTER;
342 break;
343 }
344 uint8_t footer[sizeof(CMD_FRAME_FOOTER)];
345 progmem_memcpy(footer, footer_src, footer_size);
346 if (memcmp(&this->rx_buffer_[this->rx_len_ - footer_size], footer, footer_size) != 0) {
347#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
348 char hex_buf[format_hex_pretty_size(RX_BUFFER_SIZE)];
349 ESP_LOGV(TAG, "Footer does not match header: %s",
350 format_hex_pretty_to(hex_buf, this->rx_buffer_, this->rx_len_, ' '));
351#endif
352 this->count_dropped_frame_();
353 return false;
354 }
355 const uint8_t *payload = &this->rx_buffer_[payload_pos];
356 const uint16_t payload_len = this->rx_len_ - payload_pos - footer_size;
357 switch (this->frame_type_) {
360 if (!this->handle_data_frame_(payload, payload_len)) {
361 this->count_dropped_frame_();
362 break;
363 }
365 this->dropped_frames_ = 0;
366 break;
367 default:
368 this->handle_command_ack_(payload, payload_len);
369 break;
370 }
371 return true;
372}
373
374bool LD2410SComponent::handle_data_frame_(const uint8_t *payload, uint16_t len) {
375 // Short frames carry [state][distance low][distance high]; standard frames put a type byte first, and the
376 // module only sends them before the init sequence switches it to minimal output
377 const uint8_t *data = payload;
378 if (this->frame_type_ == FrameType::STD_DATA) {
379 if (len < 4 || payload[0] != DATA_TYPE_STANDARD) {
380 if (len >= 1) {
381 ESP_LOGV(TAG, "Ignoring data frame type %02X", payload[0]);
382 }
383 return true;
384 }
385 data = payload + 1;
386 }
387 const uint8_t state = data[0];
388 const uint16_t distance = encode_uint16(data[2], data[1]);
389 // States 0 and 1 mean no target, 2 and 3 a target; anything else is line noise that happened to
390 // carry the header and footer bytes
391 if (state > 3) {
392 ESP_LOGV(TAG, "Dropping frame with impossible state %u", state);
393 return false;
394 }
395 this->publish_target_(state > 1, distance);
396 return true;
397}
398
399void LD2410SComponent::handle_command_ack_(const uint8_t *payload, uint16_t len) {
400 if (len < 4) {
401 ESP_LOGV(TAG, "Acknowledgement too short (%u bytes)", len);
402 return;
403 }
404 const uint16_t command_word = encode_uint16(payload[1], payload[0]);
405 const uint16_t ack = encode_uint16(payload[3], payload[2]);
406 if (!this->awaiting_ack_) {
407 ESP_LOGV(TAG, "Unexpected acknowledgement %04X", command_word);
408 return;
409 }
410 const uint16_t command = this->current_command_();
411 if (command_word != (command | CMD_CONFIRMATION)) {
412 ESP_LOGV(TAG, "Unexpected acknowledgement %04X", command_word);
413 return;
414 }
415#ifdef USE_LD2410S_PARAMETERS
416 if (ack != 0 && command == PARAMETERS_WRITE_CMD) {
417 // Resending the same values would be rejected again; leave configuration mode and read the
418 // module's own values back, which republishes them
419 ESP_LOGW(TAG, "Module rejected the parameters, ack %04X", ack);
420 this->send_command_(CONFIG_MODE_END_CMD);
421 this->restart_init_(App.get_loop_component_start_time() + COMMAND_GAP_MS);
422 return;
423 }
424#endif
425 if (ack != 0) {
426 // Leave the command pending so the timeout resends it
427 ESP_LOGW(TAG, "Module rejected command %04X, ack %04X", command, ack);
428 return;
429 }
430 // A short answer leaves the read pending the same way, so init cannot finish without the values
431#ifdef USE_LD2410S_PARAMETERS
432 if (command == PARAMETERS_READ_CMD && !this->handle_parameters_(&payload[4], len - 4))
433 return;
434#endif
435#ifdef USE_LD2410S_VERSION
436 if (command == VERSION_READ_CMD && !this->handle_version_(&payload[4], len - 4))
437 return;
438#endif
439 this->awaiting_ack_ = false;
440 this->step_timeouts_ = 0;
442 this->next_send_at_ = now + COMMAND_GAP_MS;
443 if (++this->step_ < this->sequence_len_) {
444 return;
445 }
446 const bool was_init = this->sequence_ == INIT_SEQUENCE;
447 this->sequence_ = nullptr;
448 if (was_init) {
449 ESP_LOGD(TAG, "Setup done");
450 this->init_done_ = true;
451 this->last_frame_at_ = now;
452 this->status_clear_warning();
453 }
454}
455
456#ifdef USE_LD2410S_PARAMETERS
457// The answer lists the parameter values in key order, 32 bits each; returns false when it is cut short
458bool LD2410SComponent::handle_parameters_(const uint8_t *values, uint16_t len) {
459 if (len < PARAMETER_COUNT * PARAMETER_VALUE_SIZE) {
460 ESP_LOGW(TAG, "Parameter answer too short (%u bytes)", len);
461 return false;
462 }
463 // Every parameter must fit the low byte of its little-endian value; otherwise nothing is stored or
464 // published and writes stay refused, so a cut-down value is never shown or sent back
465 for (uint8_t i = 0; i < PARAMETER_COUNT; i++) {
466 const uint8_t *v = &values[i * PARAMETER_VALUE_SIZE];
467 if ((v[1] | v[2] | v[3]) != 0) {
468 ESP_LOGW(TAG, "Parameter %u out of range: %" PRIu32 ", changes stay disabled", i,
469 encode_uint32(v[3], v[2], v[1], v[0]));
470 this->parameters_known_ = false;
471#ifdef USE_NUMBER
472 // A value a number showed before the module answered is not confirmed
473 for (auto *number : this->numbers_) {
474 PUBLISH_STATE(number, NAN);
475 }
476#endif
477 return true;
478 }
479 }
480 for (uint8_t i = 0; i < PARAMETER_COUNT; i++) {
481 this->parameters_[i] = values[i * PARAMETER_VALUE_SIZE];
482 }
483 this->parameters_known_ = true;
484 this->publish_parameters_();
485 ESP_LOGD(TAG, "Parameters: gates %u to %u, no delay %u s, frequencies %u/%u dHz, response speed %u",
486 this->parameters_[1], this->parameters_[0], this->parameters_[2], this->parameters_[3], this->parameters_[4],
487 this->parameters_[5]);
488 return true;
489}
490#endif
491
492#ifdef USE_LD2410S_VERSION
493// Returns false when the answer is cut short
494bool LD2410SComponent::handle_version_(const uint8_t *values, uint16_t len) {
495 if (len < VERSION_SIZE) {
496 ESP_LOGW(TAG, "Version answer too short (%u bytes)", len);
497 return false;
498 }
499 const uint8_t *v = &values[VERSION_TYPE_SIZE];
500 char version[18];
501 snprintf(version, sizeof(version), "%u.%u.%u", encode_uint16(v[1], v[0]), encode_uint16(v[3], v[2]),
502 encode_uint16(v[5], v[4]));
503 ESP_LOGD(TAG, "Firmware %s", version);
504 PUBLISH_STATE(this->version_text_sensor_, version);
505 return true;
506}
507#endif
508
509void LD2410SComponent::publish_target_(bool target, uint16_t distance) {
510#ifdef USE_BINARY_SENSOR
511 PUBLISH_STATE(this->target_binary_sensor_, target);
512#endif
513#ifdef USE_SENSOR
514 // The module keeps reporting the last distance after the target left
515 SAFE_PUBLISH_SENSOR(this->distance_sensor_, target ? distance : 0);
516#endif
517}
518
519#ifdef USE_LD2410S_PARAMETERS
521#ifdef USE_NUMBER
522 for (uint8_t i = 0; i < PARAMETER_COUNT; i++) {
523 PUBLISH_STATE(this->numbers_[i], this->parameters_[i] * PARAMETER_SCALES[i]);
524 }
525#endif
526#ifdef USE_SELECT
527 if (this->response_speed_select_ != nullptr) {
528 const uint8_t speed = this->parameters_[static_cast<uint8_t>(Parameter::PARAMETER_RESPONSE_SPEED)];
529 for (uint8_t i = 0; i < RESPONSE_SPEED_COUNT; i++) {
530 if (progmem_read_byte(&RESPONSE_SPEEDS[i]) == speed) {
531 this->response_speed_select_->publish_state(i);
532 return;
533 }
534 }
535 ESP_LOGV(TAG, "Unknown response speed %u", speed);
536 }
537#endif
538}
539#endif
540
541} // namespace esphome::ld2410s
uint8_t raw[35]
Definition bl0939.h:0
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:86
void status_clear_warning()
Definition component.h:309
void handle_command_ack_(const uint8_t *payload, uint16_t len)
Definition ld2410s.cpp:399
bool handle_version_(const uint8_t *values, uint16_t len)
Definition ld2410s.cpp:494
bool handle_frame_()
Returns false when the footer does not match, so the bytes can be scanned again.
Definition ld2410s.cpp:327
void start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now)
Definition ld2410s.cpp:180
uint8_t parameters_[PARAMETER_COUNT]
Definition ld2410s.h:123
void publish_target_(bool target, uint16_t distance)
Definition ld2410s.cpp:509
number::Number * numbers_[PARAMETER_COUNT]
Definition ld2410s.h:116
void send_command_(uint16_t command)
Definition ld2410s.cpp:212
uint8_t rx_buffer_[RX_BUFFER_SIZE]
Definition ld2410s.h:125
bool handle_parameters_(const uint8_t *values, uint16_t len)
Definition ld2410s.cpp:458
bool handle_data_frame_(const uint8_t *payload, uint16_t len)
Returns false for a data frame whose state byte is impossible, so it counts as dropped.
Definition ld2410s.cpp:374
void receive_byte_(uint8_t byte)
Definition ld2410s.cpp:257
void run_sequence_(uint32_t now)
Definition ld2410s.cpp:191
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
const char * key
int speed
Definition fan.h:3
bool state
Definition fan.h:2
constexpr float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM
Definition bme680.cpp:28
ESPHOME_LOG_TAG(TAG, "ld2410s")
Parameter
The module's common parameters, in the order the parameter read answers them.
Definition ld2410s.h:31
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:498
size_t size_t pos
Definition helpers.h:1333
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition hal.h:53
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1687
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:1051
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t
uint8_t ack
uint16_t length
Definition tt21100.cpp:0