ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld2450.cpp
Go to the documentation of this file.
1#include "ld2450.h"
2
3#ifdef USE_NUMBER
5#endif
6#ifdef USE_SENSOR
8#endif
13
14#include <algorithm>
15#include <cinttypes>
16#include <cmath>
17#include <numbers>
18
19namespace esphome::ld2450 {
20
21ESPHOME_LOG_TAG(TAG, "ld2450");
22
23enum PeriodicData : uint8_t {
28};
29
30enum PeriodicDataValue : uint8_t {
31 HEADER = 0xAA,
32 FOOTER = 0x55,
33 CHECK = 0x00,
34};
35
36enum AckData : uint8_t {
40};
41
42// Index + 1 is the BaudRate value; the select options use the same order
43PROGMEM_STRING_TABLE(BaudRateStrings, "9600", "19200", "38400", "57600", "115200", "230400", "256000", "460800");
44static_assert(BAUD_RATE_9600 == 1 && BAUD_RATE_460800 == BaudRateStrings::COUNT, "BaudRate is index + 1");
45// Index is the Direction value
46PROGMEM_STRING_TABLE(DirectionStrings, "Approaching", "Moving away", "Stationary", "NA");
47static_assert(DIRECTION_APPROACHING == 0 && DIRECTION_MOVING_AWAY == 1 && DIRECTION_STATIONARY == 2 &&
48 DIRECTION_NA == 3,
49 "Direction is the table index");
50// Index is the ZoneType value; the select options use the same order
51PROGMEM_STRING_TABLE(ZoneTypeStrings, "Disabled", "Detection", "Filter");
52static_assert(ZONE_DISABLED == 0 && ZONE_DETECTION == 1 && ZONE_FILTER == 2, "ZoneType is the table index");
53
54// Baud rates in the same order as BaudRateStrings for index-based lookup
55static constexpr uint32_t BAUD_RATES[] PROGMEM = {9600, 19200, 38400, 57600, 115200, 230400, 256000, 460800};
56
57// LD2450 UART Serial Commands
58static constexpr uint8_t CMD_ENABLE_CONF = 0xFF;
59static constexpr uint8_t CMD_DISABLE_CONF = 0xFE;
60static constexpr uint8_t CMD_QUERY_VERSION = 0xA0;
61static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5;
62static constexpr uint8_t CMD_RESET = 0xA2;
63static constexpr uint8_t CMD_RESTART = 0xA3;
64static constexpr uint8_t CMD_BLUETOOTH = 0xA4;
65static constexpr uint8_t CMD_SINGLE_TARGET_MODE = 0x80;
66static constexpr uint8_t CMD_MULTI_TARGET_MODE = 0x90;
67static constexpr uint8_t CMD_QUERY_TARGET_MODE = 0x91;
68static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1;
69static constexpr uint8_t CMD_QUERY_ZONE = 0xC1;
70static constexpr uint8_t CMD_SET_ZONE = 0xC2;
71using ld24xx::CMD_FRAME_FOOTER;
72using ld24xx::CMD_FRAME_HEADER;
73using ld24xx::HEADER_FOOTER_SIZE;
75// Data Header & Footer
76static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xAA, 0xFF, 0x03, 0x00};
77static constexpr uint8_t DATA_FRAME_FOOTER[2] = {0x55, 0xCC};
78// Successful zone query ACK: header (4) + length (2) + command/status (2) + result (2) + zone type (2) +
79// 3 zones * 8 + footer (4).
80static constexpr uint8_t ZONE_ACK_FRAME_SIZE = 40;
81static constexpr uint8_t PERIODIC_FRAME_SIZE = 30; // header, three targets of 8 bytes, footer
82
83static inline uint32_t convert_seconds_to_ms(uint16_t value) { return (uint32_t) value * 1000; };
84
85static inline void convert_int_values_to_hex(const int *values, uint8_t *bytes) {
86 for (uint8_t i = 0; i < 4; i++) {
87 uint16_t val = values[i] & 0xFFFF;
88 bytes[i * 2] = val & 0xFF; // Store low byte first (little-endian)
89 bytes[i * 2 + 1] = (val >> 8) & 0xFF; // Store high byte second
90 }
91}
92
93static inline int16_t decode_coordinate(uint8_t low_byte, uint8_t high_byte) {
94 int16_t coordinate = (high_byte & 0x7F) << 8 | low_byte;
95 if ((high_byte & 0x80) == 0) {
96 coordinate = -coordinate;
97 }
98 return coordinate; // mm
99}
100
101static inline int16_t decode_speed(uint8_t low_byte, uint8_t high_byte) {
102 int16_t speed = (high_byte & 0x7F) << 8 | low_byte;
103 if ((high_byte & 0x80) == 0) {
104 speed = -speed;
105 }
106 return speed * 10; // mm/s
107}
108
109// Length of the frame that opens the buffer: periodic frames are fixed size, command frames carry their
110// payload length after the header. readline_ only buffers a valid header, so its first byte names it.
111// Returns 0 while the length field has not arrived yet.
112static inline uint32_t frame_length(const uint8_t *buffer, uint8_t len) {
113 if (len < HEADER_FOOTER_SIZE) {
114 return 0;
115 }
116 if (buffer[0] == DATA_FRAME_HEADER[0]) {
117 return PERIODIC_FRAME_SIZE;
118 }
120}
121
123 // The first frame is due link_timeout after the boot restart's grace window
126#ifdef USE_BINARY_SENSOR
127 if (this->connected_binary_sensor_ != nullptr) {
128 this->connected_binary_sensor_->publish_initial_state(false);
129 }
130#endif
131#ifdef USE_NUMBER
132 if (this->presence_timeout_number_ != nullptr) {
133 this->pref_ = this->presence_timeout_number_->make_entity_preference<float>();
134 this->set_presence_timeout();
135 }
136#endif
137#ifdef LD2450_POLYGON_ZONE_COUNT
138 for (PolygonZone *zone : this->polygon_zones_) {
139 zone->setup();
140 }
141#endif
142 this->restart_and_read_all_info();
143}
144
145void LD2450Component::dump_config() {
146 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
147 char version_s[20];
148 const char *mac_str = ld24xx::format_mac_str(this->mac_address_, mac_s);
149 ld24xx::format_version_str(this->version_, version_s);
150 ESP_LOGCONFIG(TAG,
151 "LD2450:\n"
152 " Firmware version: %s\n"
153 " MAC address: %s\n"
154 " Link timeout: %" PRIu32 " ms",
155 version_s, mac_str, this->link_timeout_ms_);
156#ifdef USE_BINARY_SENSOR
157 ESP_LOGCONFIG(TAG, "Binary Sensors:");
158 LOG_BINARY_SENSOR(" ", "Connected", this->connected_binary_sensor_);
159 LOG_BINARY_SENSOR(" ", "MovingTarget", this->moving_target_binary_sensor_);
160 LOG_BINARY_SENSOR(" ", "StillTarget", this->still_target_binary_sensor_);
161 LOG_BINARY_SENSOR(" ", "Target", this->target_binary_sensor_);
162#endif
163#ifdef USE_SENSOR
164 ESP_LOGCONFIG(TAG, "Sensors:");
165 LOG_SENSOR_WITH_DEDUP(" ", "MovingTargetCount", this->moving_target_count_sensor_);
166 LOG_SENSOR_WITH_DEDUP(" ", "StillTargetCount", this->still_target_count_sensor_);
167 LOG_SENSOR_WITH_DEDUP(" ", "TargetCount", this->target_count_sensor_);
168 for (auto &s : this->move_x_sensors_) {
169 LOG_SENSOR_WITH_DEDUP(" ", "TargetX", s);
170 }
171 for (auto &s : this->move_y_sensors_) {
172 LOG_SENSOR_WITH_DEDUP(" ", "TargetY", s);
173 }
174 for (auto &s : this->move_angle_sensors_) {
175 LOG_SENSOR_WITH_DEDUP(" ", "TargetAngle", s);
176 }
177 for (auto &s : this->move_distance_sensors_) {
178 LOG_SENSOR_WITH_DEDUP(" ", "TargetDistance", s);
179 }
180 for (auto &s : this->move_resolution_sensors_) {
181 LOG_SENSOR_WITH_DEDUP(" ", "TargetResolution", s);
182 }
183 for (auto &s : this->move_speed_sensors_) {
184 LOG_SENSOR_WITH_DEDUP(" ", "TargetSpeed", s);
185 }
186 for (auto &s : this->zone_target_count_sensors_) {
187 LOG_SENSOR_WITH_DEDUP(" ", "ZoneTargetCount", s);
188 }
189 for (auto &s : this->zone_moving_target_count_sensors_) {
190 LOG_SENSOR_WITH_DEDUP(" ", "ZoneMovingTargetCount", s);
191 }
192 for (auto &s : this->zone_still_target_count_sensors_) {
193 LOG_SENSOR_WITH_DEDUP(" ", "ZoneStillTargetCount", s);
194 }
195#endif
196#ifdef USE_TEXT_SENSOR
197 ESP_LOGCONFIG(TAG, "Text Sensors:");
198 LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_);
199 LOG_TEXT_SENSOR(" ", "MAC address", this->mac_text_sensor_);
200 for (text_sensor::TextSensor *s : this->direction_text_sensors_) {
201 LOG_TEXT_SENSOR(" ", "Direction", s);
202 }
203#ifdef USE_LD2450_TARGET_XY
204 for (text_sensor::TextSensor *s : this->target_xy_text_sensors_) {
205 LOG_TEXT_SENSOR(" ", "TargetXY", s);
206 }
207#endif
208#endif
209#ifdef USE_NUMBER
210 ESP_LOGCONFIG(TAG, "Numbers:");
211 LOG_NUMBER(" ", "PresenceTimeout", this->presence_timeout_number_);
212 for (auto n : this->zone_numbers_) {
213 LOG_NUMBER(" ", "ZoneX1", n.x1);
214 LOG_NUMBER(" ", "ZoneY1", n.y1);
215 LOG_NUMBER(" ", "ZoneX2", n.x2);
216 LOG_NUMBER(" ", "ZoneY2", n.y2);
217 }
218#endif
219#ifdef USE_SELECT
220 ESP_LOGCONFIG(TAG, "Selects:");
221 LOG_SELECT(" ", "BaudRate", this->baud_rate_select_);
222 LOG_SELECT(" ", "ZoneType", this->zone_type_select_);
223#endif
224#ifdef USE_SWITCH
225 ESP_LOGCONFIG(TAG, "Switches:");
226 LOG_SWITCH(" ", "Bluetooth", this->bluetooth_switch_);
227 LOG_SWITCH(" ", "MultiTarget", this->multi_target_switch_);
228#endif
229#ifdef USE_BUTTON
230 ESP_LOGCONFIG(TAG, "Buttons:");
231 LOG_BUTTON(" ", "FactoryReset", this->factory_reset_button_);
232 LOG_BUTTON(" ", "Restart", this->restart_button_);
233#endif
234#ifdef LD2450_POLYGON_ZONE_COUNT
235 ESP_LOGCONFIG(TAG, "Polygon Zones:");
236 for (PolygonZone *zone : this->polygon_zones_) {
237 zone->dump_config();
238 }
239#endif
240}
241
242void LD2450Component::loop() {
243 // Before reading, so a frame in this pass resets it
244 const size_t count = this->available();
245 if (count != 0) {
246 this->bytes_since_frame_ = true;
247 }
248 this->read_available<MAX_LINE_LENGTH>([this](uint8_t b) { this->readline_(b); }, count);
249 this->check_link_();
250}
251
252// Wrap-safe for loop clock times less than 2^31 ms apart
253static inline uint32_t later_ms(uint32_t a, uint32_t b) { return static_cast<int32_t>(b - a) > 0 ? b : a; }
254static inline uint32_t earlier_ms(uint32_t a, uint32_t b) { return static_cast<int32_t>(b - a) < 0 ? b : a; }
255
258 return; // the next valid frame restores the link
259 }
261 uint32_t end = earlier_ms(now + LINK_GRACE_MS, this->last_frame_ms_ + LINK_GRACE_CAP_MS);
263 end += this->link_timeout_ms_; // the first frame is due link_timeout after the boot window
264 }
265 this->link_deadline_ms_ = later_ms(this->link_deadline_ms_, end);
266}
267
270 if (this->link_state_ == LinkState::LINK_STATE_LOST || static_cast<int32_t>(now - this->link_deadline_ms_) < 0) {
271 return;
272 }
273 const bool was_up = this->link_state_ == LinkState::LINK_STATE_UP;
275 this->on_link_lost_(now - this->last_frame_ms_, was_up);
276}
277
280 const uint32_t due = now + this->link_timeout_ms_;
281 const LinkState previous = this->link_state_;
282 if (previous == LinkState::LINK_STATE_LOST) {
283 this->link_deadline_ms_ = due;
284 } else {
285 // Before the first frame the deadline holds link_timeout on top of the boot window
286 const uint32_t window =
287 this->link_deadline_ms_ - (previous == LinkState::LINK_STATE_WAITING ? this->link_timeout_ms_ : 0);
288 this->link_deadline_ms_ = later_ms(window, due);
289 }
290 this->last_frame_ms_ = now;
291 this->bytes_since_frame_ = false;
292 this->invalid_frames_ = 0;
293 if (previous != LinkState::LINK_STATE_UP) {
295 this->on_link_up_(previous == LinkState::LINK_STATE_LOST);
296 }
297}
298
299void LD2450Component::on_link_up_(bool restored) {
300 if (restored) {
301 ESP_LOGI(TAG, "Radar link restored");
302 } else {
303 ESP_LOGD(TAG, "Receiving radar frames");
304 }
305 this->status_clear_warning();
306#ifdef USE_BINARY_SENSOR
307 PUBLISH_STATE(this->connected_binary_sensor_, true);
308#endif
309}
310
311#ifdef USE_SENSOR
313 SAFE_PUBLISH_SENSOR_UNKNOWN(this->move_x_sensors_[target]);
314 SAFE_PUBLISH_SENSOR_UNKNOWN(this->move_y_sensors_[target]);
315 SAFE_PUBLISH_SENSOR_UNKNOWN(this->move_resolution_sensors_[target]);
316 SAFE_PUBLISH_SENSOR_UNKNOWN(this->move_speed_sensors_[target]);
317 SAFE_PUBLISH_SENSOR_UNKNOWN(this->move_distance_sensors_[target]);
318 SAFE_PUBLISH_SENSOR_UNKNOWN(this->move_angle_sensors_[target]);
319}
320#endif
321
322// Live values become unknown; settings and diagnostics keep theirs
323void LD2450Component::on_link_lost_(uint32_t silent_ms, bool was_up) {
324 ESP_LOGW(TAG, "Radar link lost: no valid frame for %" PRIu32 " ms (%s, %u invalid frames)", silent_ms,
325 this->bytes_since_frame_ ? LOG_STR_LITERAL("bytes received") : LOG_STR_LITERAL("no bytes received"),
326 static_cast<unsigned>(this->invalid_frames_));
327 this->status_set_warning();
328#ifdef USE_BINARY_SENSOR
329 PUBLISH_STATE(this->connected_binary_sensor_, false);
330#endif
331 if (!was_up) {
332 return; // no frame since boot, so the live values are still unknown
333 }
334#ifdef USE_BINARY_SENSOR
336 {this->target_binary_sensor_, this->moving_target_binary_sensor_, this->still_target_binary_sensor_}) {
337 if (s != nullptr) {
338 s->invalidate_state();
339 }
340 }
341#endif
342 // The first frame after the link returns publishes presence at once
343 this->presence_millis_ = 0;
344 this->still_presence_millis_ = 0;
345 this->moving_presence_millis_ = 0;
346#ifdef LD2450_POLYGON_ZONE_COUNT
347 for (PolygonZone *zone : this->polygon_zones_) {
348 zone->invalidate_presence();
349 }
350#endif
351 for (uint8_t i = 0; i < MAX_TARGETS; i++) {
352#ifdef USE_SENSOR
354#endif
355#ifdef USE_LD2450_TARGET_XY
356 PUBLISH_STATE(this->target_xy_text_sensors_[i], "unknown");
357 // No coordinate decodes to INT16_MIN, so the next position publishes
358 this->target_info_[i].x = INT16_MIN;
359 this->target_info_[i].y = INT16_MIN;
360#endif
361#ifdef USE_TEXT_SENSOR
362 // "unknown" reads as unknown in Home Assistant; "NA" still means no target
363 if (this->direction_dedup_[i].next_unknown() && this->direction_text_sensors_[i] != nullptr) {
364 this->direction_text_sensors_[i]->publish_state("unknown");
365 }
366#endif
367 }
368#ifdef USE_SENSOR
369 for (uint8_t i = 0; i < MAX_ZONES; i++) {
370 SAFE_PUBLISH_SENSOR_UNKNOWN(this->zone_target_count_sensors_[i]);
371 SAFE_PUBLISH_SENSOR_UNKNOWN(this->zone_still_target_count_sensors_[i]);
372 SAFE_PUBLISH_SENSOR_UNKNOWN(this->zone_moving_target_count_sensors_[i]);
373 }
374 SAFE_PUBLISH_SENSOR_UNKNOWN(this->target_count_sensor_);
375 SAFE_PUBLISH_SENSOR_UNKNOWN(this->still_target_count_sensor_);
376 SAFE_PUBLISH_SENSOR_UNKNOWN(this->moving_target_count_sensor_);
377#endif
378}
379
380// Count targets in zone (single pass for both still and moving)
381void LD2450Component::count_targets_in_zone_(const Zone &zone, uint8_t &still, uint8_t &moving) {
382 still = 0;
383 moving = 0;
384 for (auto &target : this->target_info_) {
385 if (target.x > zone.x1 && target.x < zone.x2 && target.y > zone.y1 && target.y < zone.y2) {
386 if (target.is_moving) {
387 moving++;
388 } else {
389 still++;
390 }
391 }
392 }
393}
394
395// Service reset_radar_zone
396void LD2450Component::reset_radar_zone() {
397 this->zone_type_ = 0;
398 for (auto &i : this->zone_config_) {
399 i.x1 = 0;
400 i.y1 = 0;
401 i.x2 = 0;
402 i.y2 = 0;
403 }
405}
406
407void LD2450Component::set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2,
408 int32_t zone1_y2, int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2,
409 int32_t zone2_y2, int32_t zone3_x1, int32_t zone3_y1, int32_t zone3_x2,
410 int32_t zone3_y2) {
411 this->zone_type_ = zone_type;
412 int zone_parameters[12] = {zone1_x1, zone1_y1, zone1_x2, zone1_y2, zone2_x1, zone2_y1,
413 zone2_x2, zone2_y2, zone3_x1, zone3_y1, zone3_x2, zone3_y2};
414 for (uint8_t i = 0; i < MAX_ZONES; i++) {
415 this->zone_config_[i].x1 = zone_parameters[i * 4];
416 this->zone_config_[i].y1 = zone_parameters[i * 4 + 1];
417 this->zone_config_[i].x2 = zone_parameters[i * 4 + 2];
418 this->zone_config_[i].y2 = zone_parameters[i * 4 + 3];
419 }
421}
422
423// Set Zone on LD2450 Sensor
425 uint8_t cmd_value[26] = {};
426 uint8_t zone_type_bytes[2] = {static_cast<uint8_t>(this->zone_type_), 0x00};
427 uint8_t area_config[24] = {};
428 for (uint8_t i = 0; i < MAX_ZONES; i++) {
429 int values[4] = {this->zone_config_[i].x1, this->zone_config_[i].y1, this->zone_config_[i].x2,
430 this->zone_config_[i].y2};
431 ld2450::convert_int_values_to_hex(values, area_config + (i * 8));
432 }
433 std::memcpy(cmd_value, zone_type_bytes, sizeof(zone_type_bytes));
434 std::memcpy(cmd_value + 2, area_config, sizeof(area_config));
435 this->set_config_mode_(true);
436 this->send_command_(CMD_SET_ZONE, cmd_value, sizeof(cmd_value));
437 this->set_config_mode_(false);
438}
439
440// Check presense timeout to reset presence status
442 if (check_millis == 0) {
443 return true;
444 }
445 return App.get_loop_component_start_time() - check_millis >= this->timeout_;
446}
447
448// Extract, store and publish zone details LD2450 buffer
450 uint8_t index, start;
451 for (index = 0; index < MAX_ZONES; index++) {
452 start = 12 + index * 8;
453 this->zone_config_[index].x1 = encode_value_le<int16_t>(&this->buffer_data_[start]);
454 this->zone_config_[index].y1 = encode_value_le<int16_t>(&this->buffer_data_[start + 2]);
455 this->zone_config_[index].x2 = encode_value_le<int16_t>(&this->buffer_data_[start + 4]);
456 this->zone_config_[index].y2 = encode_value_le<int16_t>(&this->buffer_data_[start + 6]);
457#ifdef USE_NUMBER
458 // only one null check as all coordinates are required for a single zone
459 if (this->zone_numbers_[index].x1 != nullptr) {
460 this->zone_numbers_[index].x1->publish_state(this->zone_config_[index].x1);
461 this->zone_numbers_[index].y1->publish_state(this->zone_config_[index].y1);
462 this->zone_numbers_[index].x2->publish_state(this->zone_config_[index].x2);
463 this->zone_numbers_[index].y2->publish_state(this->zone_config_[index].y2);
464 }
465#endif
466 }
467}
468
469// Read all info from LD2450 buffer
470void LD2450Component::read_all_info() {
471 this->open_link_grace_();
472 this->set_config_mode_(true);
473 this->get_version_();
474 this->get_mac_();
476 this->query_zone_();
477 this->set_config_mode_(false);
478#ifdef USE_SELECT
479 if (this->baud_rate_select_ != nullptr) {
480 if (auto index = ld24xx::find_index(BAUD_RATES, this->parent_->get_baud_rate())) {
481 this->baud_rate_select_->publish_state(*index);
482 }
483 }
484 this->publish_zone_type();
485#endif
486}
487
488// Read zone info from LD2450 buffer
489void LD2450Component::query_zone_info() {
490 this->set_config_mode_(true);
491 this->query_zone_();
492 this->set_config_mode_(false);
493}
494
495// Restart LD2450 and read all info from buffer
496void LD2450Component::restart_and_read_all_info() {
497 this->set_config_mode_(true);
498 this->restart_();
499 this->set_timeout(1500, [this]() { this->read_all_info(); });
500}
501
502// Send command with values to LD2450
503void LD2450Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
504 ESP_LOGV(TAG, "Sending COMMAND %02X", command);
505 // frame header bytes
506 this->write_array(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER));
507 // length bytes
508 uint8_t len = 2;
509 if (command_value != nullptr) {
510 len += command_value_len;
511 }
512 // 2 length bytes (low, high) + 2 command bytes (low, high)
513 uint8_t len_cmd[] = {len, 0x00, command, 0x00};
514 this->write_array(len_cmd, sizeof(len_cmd));
515 // command value bytes
516 if (command_value != nullptr) {
517 this->write_array(command_value, command_value_len);
518 }
519 // frame footer bytes
520 this->write_array(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
521
522 if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
523 delay(50); // NOLINT
524 }
525}
526
527// LD2450 Radar data message:
528// [AA FF 03 00] [0E 03 B1 86 10 00 40 01] [00 00 00 00 00 00 00 00] [00 00 00 00 00 00 00 00] [55 CC]
529// Header Target 1 Target 2 Target 3 End
531 int16_t target_count = 0;
532 int16_t still_target_count = 0;
533 int16_t moving_target_count = 0;
534 int16_t res = 0;
535 int16_t start = 0;
536 int16_t tx = 0;
537 int16_t ty = 0;
538 int16_t td = 0;
539 int16_t ts = 0;
540 float angle = 0;
541 uint8_t index = 0;
543 bool is_moving = false;
544
545#if defined(USE_BINARY_SENSOR) || defined(USE_SENSOR) || defined(USE_TEXT_SENSOR)
546 // Loop thru targets
547 for (index = 0; index < MAX_TARGETS; index++) {
548 // X
549 start = TARGET_X + index * 8;
550 is_moving = false;
551 // tx and ty are used for further calculations, so always need to be populated
552 tx = ld2450::decode_coordinate(this->buffer_data_[start], this->buffer_data_[start + 1]);
553 // Y
554 start = TARGET_Y + index * 8;
555 ty = ld2450::decode_coordinate(this->buffer_data_[start], this->buffer_data_[start + 1]);
556#ifdef USE_SENSOR
557 // RESOLUTION
558 start = TARGET_RESOLUTION + index * 8;
559 res = encode_value_le<uint16_t>(&this->buffer_data_[start]);
560#endif
561 // SPEED
562 start = TARGET_SPEED + index * 8;
563 ts = ld2450::decode_speed(this->buffer_data_[start], this->buffer_data_[start + 1]);
564 if (ts) {
565 is_moving = true;
566 moving_target_count++;
567 }
568 // DISTANCE
569 // Optimized: use already decoded tx and ty values, replace pow() with multiplication
570 int32_t x_squared = (int32_t) tx * tx;
571 int32_t y_squared = (int32_t) ty * ty;
572 td = (uint16_t) sqrtf(x_squared + y_squared);
573 if (td > 0) {
574 target_count++;
575 }
576#ifdef USE_SENSOR
577 if (td == 0) {
578 this->publish_target_unknown_(index);
579 } else {
580 SAFE_PUBLISH_SENSOR(this->move_x_sensors_[index], tx);
581 SAFE_PUBLISH_SENSOR(this->move_y_sensors_[index], ty);
582 SAFE_PUBLISH_SENSOR(this->move_resolution_sensors_[index], res);
583 SAFE_PUBLISH_SENSOR(this->move_speed_sensors_[index], ts);
584 SAFE_PUBLISH_SENSOR(this->move_distance_sensors_[index], td);
585 // ANGLE - atan2f computes angle from Y axis directly, no sqrt/division needed
586 angle = atan2f(static_cast<float>(-tx), static_cast<float>(ty)) * (180.0f / std::numbers::pi_v<float>);
587 SAFE_PUBLISH_SENSOR(this->move_angle_sensors_[index], angle);
588 }
589#endif
590#ifdef USE_TEXT_SENSOR
591 // DIRECTION
592 if (td == 0) {
594 } else if (ts > 0) {
596 } else if (ts < 0) {
598 } else {
600 }
601 if (this->direction_dedup_[index].next(direction)) {
602 PUBLISH_STATE(this->direction_text_sensors_[index],
603 DirectionStrings::get_progmem_str(direction, DirectionStrings::LAST_INDEX));
604 }
605#endif
606
607 // Store target info for zone target count. Zero out untracked targets (td==0)
608 // so stale coordinates don't produce ghost counts in count_targets_in_zone_().
609#ifdef USE_LD2450_TARGET_XY
610 const Target previous = this->target_info_[index];
611#endif
612 this->target_info_[index].x = (td > 0) ? tx : 0;
613 this->target_info_[index].y = (td > 0) ? ty : 0;
614 this->target_info_[index].is_moving = (td > 0) && is_moving;
615#ifdef USE_LD2450_TARGET_XY
616 this->update_target_xy_(index,
617 previous.x != this->target_info_[index].x || previous.y != this->target_info_[index].y);
618#endif
619
620 } // End loop thru targets
621
622 still_target_count = target_count - moving_target_count;
623#endif
624
625#ifdef LD2450_POLYGON_ZONE_COUNT
627 for (PolygonZone *zone : this->polygon_zones_) {
628 if (!zone->has_polygon())
629 continue;
630 bool target_inside = false;
631 for (const Target &target : this->target_info_) {
632 // Untracked targets are stored as (0, 0)
633 if ((target.x != 0 || target.y != 0) && zone->contains(target.x, target.y)) {
634 target_inside = true;
635 break;
636 }
637 }
638 zone->update(target_inside, now, this->get_timeout_status_(zone->get_last_inside_ms()));
639 }
640#endif
641
642#ifdef USE_SENSOR
643 // Loop thru zones
644 uint8_t zone_still_targets = 0;
645 uint8_t zone_moving_targets = 0;
646 uint8_t zone_all_targets = 0;
647 for (index = 0; index < MAX_ZONES; index++) {
648 this->count_targets_in_zone_(this->zone_config_[index], zone_still_targets, zone_moving_targets);
649 zone_all_targets = zone_still_targets + zone_moving_targets;
650
651 // Publish Still Target Count in Zones
652 SAFE_PUBLISH_SENSOR(this->zone_still_target_count_sensors_[index], zone_still_targets);
653 // Publish Moving Target Count in Zones
654 SAFE_PUBLISH_SENSOR(this->zone_moving_target_count_sensors_[index], zone_moving_targets);
655 // Publish All Target Count in Zones
656 SAFE_PUBLISH_SENSOR(this->zone_target_count_sensors_[index], zone_all_targets);
657 } // End loop thru zones
658
659 // Target Count
660 SAFE_PUBLISH_SENSOR(this->target_count_sensor_, target_count);
661 // Still Target Count
662 SAFE_PUBLISH_SENSOR(this->still_target_count_sensor_, still_target_count);
663 // Moving Target Count
664 SAFE_PUBLISH_SENSOR(this->moving_target_count_sensor_, moving_target_count);
665
666#endif
667
668#ifdef USE_BINARY_SENSOR
669 // Target Presence
670 if (this->target_binary_sensor_ != nullptr) {
671 if (target_count > 0) {
672 this->target_binary_sensor_->publish_state(true);
673 } else {
674 if (this->get_timeout_status_(this->presence_millis_)) {
675 this->target_binary_sensor_->publish_state(false);
676 } else {
677 ESP_LOGV(TAG, "Clear presence waiting timeout: %" PRIu32, this->timeout_);
678 }
679 }
680 }
681 // Moving Target Presence
682 if (this->moving_target_binary_sensor_ != nullptr) {
683 if (moving_target_count > 0) {
684 this->moving_target_binary_sensor_->publish_state(true);
685 } else {
687 this->moving_target_binary_sensor_->publish_state(false);
688 }
689 }
690 }
691 // Still Target Presence
692 if (this->still_target_binary_sensor_ != nullptr) {
693 if (still_target_count > 0) {
694 this->still_target_binary_sensor_->publish_state(true);
695 } else {
697 this->still_target_binary_sensor_->publish_state(false);
698 }
699 }
700 }
701#endif
702#ifdef USE_BINARY_SENSOR
703 // For presence timeout check
704 if (target_count > 0) {
706 }
707 if (moving_target_count > 0) {
709 }
710 if (still_target_count > 0) {
712 }
713#endif
714
715 this->data_callback_.call();
716}
717
719 ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", this->buffer_data_[COMMAND]);
720 if (this->buffer_data_[COMMAND_STATUS] != 0x01) {
721 ESP_LOGE(TAG, "Invalid status");
722 return;
723 }
724 if (this->buffer_data_[8] || this->buffer_data_[9]) {
725 ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]);
726 return;
727 }
728
729 switch (this->buffer_data_[COMMAND]) {
730 case CMD_ENABLE_CONF:
731 ESP_LOGV(TAG, "Enable conf");
732 break;
733
734 case CMD_DISABLE_CONF:
735 ESP_LOGV(TAG, "Disabled conf");
736 break;
737
738 case CMD_SET_BAUD_RATE:
739 ESP_LOGV(TAG, "Baud rate change");
740#ifdef USE_SELECT
741 if (this->baud_rate_select_ != nullptr) {
742 auto baud = this->baud_rate_select_->current_option();
743 ESP_LOGE(TAG, "Change baud rate to %.*s and reinstall", (int) baud.size(), baud.c_str());
744 }
745#endif
746 break;
747
748 case CMD_QUERY_VERSION: {
749 if (this->buffer_pos_ < 12 + sizeof(this->version_) + HEADER_FOOTER_SIZE) {
750 break; // too short to hold a version
751 }
752 std::memcpy(this->version_, &this->buffer_data_[12], sizeof(this->version_));
753 char version_s[20];
754 ld24xx::format_version_str(this->version_, version_s);
755 ESP_LOGV(TAG, "Firmware version: %s", version_s);
756#ifdef USE_TEXT_SENSOR
757 PUBLISH_STATE(this->version_text_sensor_, version_s);
758#endif
759 break;
760 }
761
762 case CMD_QUERY_MAC_ADDRESS: {
763 if (this->buffer_pos_ < 20) {
764 break; // too short to hold a MAC address
765 }
766
767 this->bluetooth_on_ = ld24xx::store_mac(&this->buffer_data_[10], this->mac_address_);
768
769 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
770 const char *mac_str = ld24xx::format_mac_str(this->mac_address_, mac_s);
771 ESP_LOGV(TAG, "MAC address: %s", mac_str);
772#ifdef USE_TEXT_SENSOR
773 PUBLISH_STATE(this->mac_text_sensor_, mac_str);
774#endif
775#ifdef USE_SWITCH
776 PUBLISH_STATE(this->bluetooth_switch_, this->bluetooth_on_);
777#endif
778 break;
779 }
780
781 case CMD_BLUETOOTH:
782 ESP_LOGV(TAG, "Bluetooth");
783 break;
784
785 case CMD_SINGLE_TARGET_MODE:
786 ESP_LOGV(TAG, "Single target conf");
787#ifdef USE_SWITCH
788 PUBLISH_STATE(this->multi_target_switch_, false);
789#endif
790 break;
791
792 case CMD_MULTI_TARGET_MODE:
793 ESP_LOGV(TAG, "Multi target conf");
794#ifdef USE_SWITCH
795 PUBLISH_STATE(this->multi_target_switch_, true);
796#endif
797 break;
798
799 case CMD_QUERY_TARGET_MODE:
800 if (this->buffer_pos_ < 11 + HEADER_FOOTER_SIZE) {
801 break; // too short to hold the mode
802 }
803 ESP_LOGV(TAG, "Query target tracking mode");
804#ifdef USE_SWITCH
805 PUBLISH_STATE(this->multi_target_switch_, this->buffer_data_[10] == 0x02);
806#endif
807 break;
808
809 case CMD_QUERY_ZONE:
810 if (this->buffer_pos_ < ZONE_ACK_FRAME_SIZE) {
811 break; // too short to hold the zones
812 }
813 ESP_LOGV(TAG, "Query zone conf");
814 this->zone_type_ = this->buffer_data_[10];
815 this->publish_zone_type();
816#ifdef USE_SELECT
817 if (this->zone_type_select_ != nullptr) {
818 auto zone = this->zone_type_select_->current_option();
819 ESP_LOGV(TAG, "Change zone type to: %.*s", (int) zone.size(), zone.c_str());
820 }
821#endif
822 if (this->buffer_data_[10] == 0x00) {
823 ESP_LOGV(TAG, "Zone: Disabled");
824 }
825 if (this->buffer_data_[10] == 0x01) {
826 ESP_LOGV(TAG, "Zone: Area detection");
827 }
828 if (this->buffer_data_[10] == 0x02) {
829 ESP_LOGV(TAG, "Zone: Area filter");
830 }
831 this->process_zone_();
832 break;
833
834 case CMD_SET_ZONE:
835 ESP_LOGV(TAG, "Set zone conf");
836 this->query_zone_info();
837 break;
838
839 default:
840 break;
841 }
842}
843
844// Read LD2450 buffer data
846 if (readch < 0) {
847 return; // No data available
848 }
850 this->buffer_data_, this->buffer_pos_, readch, DATA_FRAME_HEADER, ld2450::frame_length,
851 [this](uint8_t frame_len) { return this->handle_frame_(frame_len); },
852 [this]() {
853 if (this->invalid_frames_ < UINT16_MAX) {
854 this->invalid_frames_++;
855 }
856 ESP_LOGW(TAG, "Invalid header/footer");
857 });
858}
859
860// Dispatches the frame of frame_len bytes at the start of the buffer when its footer is in place
861bool LD2450Component::handle_frame_(uint8_t frame_len) {
862 this->buffer_pos_ = frame_len; // the handlers take the frame length from here
863 const uint8_t *end = &this->buffer_data_[frame_len];
864 if (this->buffer_data_[0] == DATA_FRAME_HEADER[0]) {
865 if (end[-2] != DATA_FRAME_FOOTER[0] || end[-1] != DATA_FRAME_FOOTER[1]) {
866 return false;
867 }
868 ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Handling Periodic Data: %s");
869 this->on_valid_frame_();
870 this->handle_periodic_data_();
871 return true;
872 }
873 if (!ld2450::validate_header_footer(CMD_FRAME_FOOTER, end - HEADER_FOOTER_SIZE)) {
874 return false;
875 }
876 ESP_LOGV_HEX(TAG, this->buffer_data_, this->buffer_pos_, MAX_LINE_LENGTH, "Handling Ack Data: %s");
877 this->handle_ack_data_();
878 return true;
879}
880
881// Set Config Mode - Pre-requisite sending commands
883 const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
884 const uint8_t cmd_value[2] = {0x01, 0x00};
885 this->send_command_(cmd, enable ? cmd_value : nullptr, sizeof(cmd_value));
886}
887
888// Set Bluetooth Enable/Disable
889void LD2450Component::set_bluetooth(bool enable) {
890 this->open_link_grace_();
891 this->set_config_mode_(true);
892 const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00};
893 this->send_command_(CMD_BLUETOOTH, cmd_value, sizeof(cmd_value));
894 this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
895}
896
897// Set Baud rate
898void LD2450Component::set_baud_rate(const char *state) {
899 const int16_t index = progmem_string_table_find<BaudRateStrings>(state, strlen(state));
900 if (index >= 0) {
901 this->set_baud_rate(static_cast<BaudRate>(index + 1));
902 }
903}
904
905void LD2450Component::set_baud_rate(BaudRate rate) {
906 this->open_link_grace_();
907 this->set_config_mode_(true);
908 const uint8_t cmd_value[2] = {rate, 0x00};
909 this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value));
910 this->set_timeout(200, [this]() { this->restart_(); });
911}
912
913// Set Zone Type - one of: Disabled, Detection, Filter
914void LD2450Component::set_zone_type(const char *state) {
915 ESP_LOGV(TAG, "Set zone type: %s", state);
916 const int16_t index = progmem_string_table_find<ZoneTypeStrings>(state, strlen(state));
917 if (index >= 0) {
918 this->set_zone_type(static_cast<ZoneType>(index));
919 }
920}
921
922void LD2450Component::set_zone_type(ZoneType type) {
923 this->zone_type_ = type;
925}
926
927// Publish Zone Type to Select component
928void LD2450Component::publish_zone_type() {
929#ifdef USE_SELECT
930 PUBLISH_STATE(this->zone_type_select_, static_cast<size_t>(this->zone_type_));
931#endif
932}
933
934// Set Single/Multiplayer target detection
935void LD2450Component::set_multi_target(bool enable) {
936 this->set_config_mode_(true);
937 uint8_t cmd = enable ? CMD_MULTI_TARGET_MODE : CMD_SINGLE_TARGET_MODE;
938 this->send_command_(cmd, nullptr, 0);
939 this->set_config_mode_(false);
940}
941
942// LD2450 factory reset
943void LD2450Component::factory_reset() {
944 this->open_link_grace_();
945 this->set_config_mode_(true);
946 this->send_command_(CMD_RESET, nullptr, 0);
947 this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
948}
949
950// Restart LD2450 module
952 this->open_link_grace_();
953 this->send_command_(CMD_RESTART, nullptr, 0);
954}
955
956// Get LD2450 firmware version
957void LD2450Component::get_version_() { this->send_command_(CMD_QUERY_VERSION, nullptr, 0); }
958
959// Get LD2450 mac address
961 uint8_t cmd_value[2] = {0x01, 0x00};
962 this->send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value, 2);
963}
964
965// Query for target tracking mode
966void LD2450Component::query_target_tracking_mode_() { this->send_command_(CMD_QUERY_TARGET_MODE, nullptr, 0); }
967
968// Query for zone info
969void LD2450Component::query_zone_() { this->send_command_(CMD_QUERY_ZONE, nullptr, 0); }
970
971#ifdef USE_SENSOR
972void LD2450Component::set_move_x_sensor(uint8_t target, sensor::Sensor *s) {
973 this->move_x_sensors_[target].set_sensor(s);
974}
975void LD2450Component::set_move_y_sensor(uint8_t target, sensor::Sensor *s) {
976 this->move_y_sensors_[target].set_sensor(s);
977}
978void LD2450Component::set_move_speed_sensor(uint8_t target, sensor::Sensor *s) {
979 this->move_speed_sensors_[target].set_sensor(s);
980}
981void LD2450Component::set_move_angle_sensor(uint8_t target, sensor::Sensor *s) {
982 this->move_angle_sensors_[target].set_sensor(s);
983}
984void LD2450Component::set_move_distance_sensor(uint8_t target, sensor::Sensor *s) {
985 this->move_distance_sensors_[target].set_sensor(s);
986}
987void LD2450Component::set_move_resolution_sensor(uint8_t target, sensor::Sensor *s) {
988 this->move_resolution_sensors_[target].set_sensor(s);
989}
990void LD2450Component::set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
991 this->zone_target_count_sensors_[zone].set_sensor(s);
992}
993void LD2450Component::set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
994 this->zone_still_target_count_sensors_[zone].set_sensor(s);
995}
996void LD2450Component::set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
997 this->zone_moving_target_count_sensors_[zone].set_sensor(s);
998}
999#endif
1000#ifdef USE_TEXT_SENSOR
1001void LD2450Component::set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s) {
1002 this->direction_text_sensors_[target] = s;
1003}
1004#endif
1005#ifdef USE_LD2450_TARGET_XY
1006void LD2450Component::set_target_xy_text_sensor(uint8_t target, text_sensor::TextSensor *s) {
1007 this->target_xy_text_sensors_[target] = s;
1008}
1009
1010// Publish the position of a target in one radar frame; the sensor's filters throttle it
1011void LD2450Component::update_target_xy_(uint8_t target, bool changed) {
1012 text_sensor::TextSensor *s = this->target_xy_text_sensors_[target];
1013 if (s == nullptr || (!changed && s->has_state()))
1014 return;
1015 const Target &pos = this->target_info_[target];
1016 if (pos.x == 0 && pos.y == 0) {
1017 s->publish_state(DirectionStrings::get_progmem_str(DIRECTION_NA, DirectionStrings::LAST_INDEX));
1018 return;
1019 }
1020 // Longest is "-32767,-32767"
1021 char text[16];
1022 const size_t len = buf_append_printf(text, sizeof(text), 0, "%d,%d", pos.x, pos.y);
1023 s->publish_state(text, len);
1024}
1025#endif
1026
1027// Send Zone coordinates data to LD2450
1028#ifdef USE_NUMBER
1029void LD2450Component::set_zone_coordinate(number::Number *n) {
1030 uint8_t zone = 0;
1031 while (zone < MAX_ZONES && this->zone_numbers_[zone].x1 != n && this->zone_numbers_[zone].y1 != n &&
1032 this->zone_numbers_[zone].x2 != n && this->zone_numbers_[zone].y2 != n)
1033 zone++;
1034 if (zone == MAX_ZONES)
1035 return;
1036 number::Number *x1sens = this->zone_numbers_[zone].x1;
1037 number::Number *y1sens = this->zone_numbers_[zone].y1;
1038 number::Number *x2sens = this->zone_numbers_[zone].x2;
1039 number::Number *y2sens = this->zone_numbers_[zone].y2;
1040 if (!x1sens->has_state() || !y1sens->has_state() || !x2sens->has_state() || !y2sens->has_state()) {
1041 return;
1042 }
1043 this->zone_config_[zone].x1 = static_cast<int>(x1sens->state);
1044 this->zone_config_[zone].y1 = static_cast<int>(y1sens->state);
1045 this->zone_config_[zone].x2 = static_cast<int>(x2sens->state);
1046 this->zone_config_[zone].y2 = static_cast<int>(y2sens->state);
1047 this->send_set_zone_command_();
1048}
1049
1050void LD2450Component::set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2,
1051 number::Number *y2) {
1052 if (zone < MAX_ZONES) {
1053 this->zone_numbers_[zone].x1 = x1;
1054 this->zone_numbers_[zone].y1 = y1;
1055 this->zone_numbers_[zone].x2 = x2;
1056 this->zone_numbers_[zone].y2 = y2;
1057 }
1058}
1059#endif
1060
1061// Set Presence Timeout load and save from flash
1062#ifdef USE_NUMBER
1063void LD2450Component::set_presence_timeout() {
1064 if (this->presence_timeout_number_ == nullptr)
1065 return;
1066 // Restore only while the number has no state yet: 0 is a valid presence_timeout (no hold)
1067 if (!this->presence_timeout_number_->has_state())
1068 this->presence_timeout_number_->publish_state(this->restore_from_flash_());
1069 const float timeout = this->presence_timeout_number_->state;
1070 this->save_to_flash_(timeout);
1071 this->timeout_ = ld2450::convert_seconds_to_ms(timeout);
1072}
1073
1074// Save Presence Timeout to flash
1075void LD2450Component::save_to_flash_(float value) { this->pref_.save(&value); }
1076
1077// Load Presence Timeout from flash
1079 float value;
1080 if (!this->pref_.load(&value)) {
1081 value = DEFAULT_PRESENCE_TIMEOUT;
1082 }
1083 return value;
1084}
1085#endif
1086
1087} // namespace esphome::ld2450
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 setup()
Where the component's initialization should happen.
Definition component.cpp:84
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void status_clear_warning()
Definition component.h:309
bool has_state() const
Base class for all binary_sensor-type classes.
void save_to_flash_(float value)
Definition ld2450.cpp:1075
std::array< sensor::SensorWithDedup< int16_t >, MAX_TARGETS > move_y_sensors_
Definition ld2450.h:250
std::array< sensor::SensorWithDedup< uint8_t >, MAX_ZONES > zone_target_count_sensors_
Definition ld2450.h:255
std::array< Deduplicator< uint8_t >, MAX_TARGETS > direction_dedup_
Definition ld2450.h:241
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2450.cpp:503
void on_link_lost_(uint32_t silent_ms, bool was_up)
Definition ld2450.cpp:323
void set_config_mode_(bool enable)
Definition ld2450.cpp:882
std::array< text_sensor::TextSensor *, MAX_TARGETS > direction_text_sensors_
Definition ld2450.h:260
void count_targets_in_zone_(const Zone &zone, uint8_t &still, uint8_t &moving)
Definition ld2450.cpp:381
std::array< text_sensor::TextSensor *, MAX_TARGETS > target_xy_text_sensors_
Definition ld2450.h:264
StaticVector< PolygonZone *, LD2450_POLYGON_ZONE_COUNT > polygon_zones_
Definition ld2450.h:268
std::array< sensor::SensorWithDedup< uint8_t >, MAX_ZONES > zone_moving_target_count_sensors_
Definition ld2450.h:257
bool handle_frame_(uint8_t frame_len)
Definition ld2450.cpp:861
std::array< sensor::SensorWithDedup< uint16_t >, MAX_TARGETS > move_distance_sensors_
Definition ld2450.h:253
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2450.h:228
std::array< sensor::SensorWithDedup< uint16_t >, MAX_TARGETS > move_resolution_sensors_
Definition ld2450.h:254
void update_target_xy_(uint8_t target, bool changed)
Definition ld2450.cpp:1011
std::array< sensor::SensorWithDedup< int16_t >, MAX_TARGETS > move_x_sensors_
Definition ld2450.h:249
uint8_t mac_address_[MAC_ADDRESS_SIZE]
Definition ld2450.h:229
Target target_info_[MAX_TARGETS]
Definition ld2450.h:237
std::array< sensor::SensorWithDedup< uint8_t >, MAX_ZONES > zone_still_target_count_sensors_
Definition ld2450.h:256
Zone zone_config_[MAX_ZONES]
Definition ld2450.h:238
ESPPreferenceObject pref_
Definition ld2450.h:245
void publish_target_unknown_(uint8_t target)
Definition ld2450.cpp:312
LazyCallbackManager< void()> data_callback_
Definition ld2450.h:271
void on_link_up_(bool restored)
Definition ld2450.cpp:299
std::array< sensor::SensorWithDedup< int16_t >, MAX_TARGETS > move_speed_sensors_
Definition ld2450.h:251
ZoneOfNumbers zone_numbers_[MAX_ZONES]
Definition ld2450.h:246
bool get_timeout_status_(uint32_t check_millis)
Definition ld2450.cpp:441
std::array< sensor::SensorWithDedup< float >, MAX_TARGETS > move_angle_sensors_
Definition ld2450.h:252
A zone with any polygon shape.
Base-class for all numbers.
Definition number.h:29
Base-class for all sensors.
Definition sensor.h:48
void publish_state(const std::string &state)
Definition text_sensor.h:42
UARTComponent * parent_
Definition uart.h:134
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
uint8_t type
NameCopy * next
FanDirection direction
Definition fan.h:5
int speed
Definition fan.h:3
bool state
Definition fan.h:2
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
mopeka_std_values val[3]
@ DIRECTION_MOVING_AWAY
Definition ld2450.h:63
@ DIRECTION_APPROACHING
Definition ld2450.h:62
@ DIRECTION_UNDEFINED
Definition ld2450.h:66
@ DIRECTION_STATIONARY
Definition ld2450.h:64
ESPHOME_LOG_TAG(TAG, "ld2450")
PROGMEM_STRING_TABLE(BaudRateStrings, "9600", "19200", "38400", "57600", "115200", "230400", "256000", "460800")
bool store_mac(const uint8_t *reported, uint8_t *mac_address)
Definition ld24xx.h:64
void format_version_str(const uint8_t *version, std::span< char, 20 > buffer)
Definition ld24xx.h:77
bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer)
Definition ld24xx.h:87
const char * format_mac_str(const uint8_t *mac_address, std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buffer)
Definition ld24xx.h:43
void feed_frame_byte(uint8_t(&buffer)[N], uint8_t &pos, uint8_t byte, const uint8_t *data_header, LengthFn frame_length, HandleFn handle_frame, DropFn on_drop)
Definition ld24xx.h:116
optional< size_t > find_index(const T(&arr)[N], T value)
Definition ld24xx.h:27
uint32_t cmd_frame_length(const uint8_t *buffer, uint8_t count)
Definition ld24xx.h:92
static float float b
std::vector< uint8_t > bytes
Definition sml_parser.h:12
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1084
int16_t progmem_string_table_find(const char *value, size_t len)
The index of the string value (len bytes, in RAM) in a PROGMEM_STRING_TABLE, or -1 when it is not one...
Definition progmem.h:160
size_t size_t pos
Definition helpers.h:1333
void HOT delay(uint32_t ms)
Definition hal.cpp:85
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint8_t end[39]
Definition sun_gtil2.cpp:17