ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ld600x.h
Go to the documentation of this file.
1#pragma once
2
3// Shared base for the Hi-Link 60 GHz radars that speak the TinyFrame serial protocol: the LD6002B and
4// the LD6004. The LD6001A uses AT commands and another report format and is a separate component.
8#include "esphome/core/log.h"
12#ifdef USE_SENSOR
15#endif
16#ifdef USE_BINARY_SENSOR
18#endif
19#ifdef USE_TEXT_SENSOR
21#endif
22#ifdef USE_NUMBER
24#endif
25#ifdef USE_SELECT
27#endif
28#ifdef USE_SWITCH
30#endif
31
32#include <array>
33#include <cmath>
34
35namespace esphome::ld600x {
36
37static constexpr uint8_t AREA_COUNT = 4;
38// Interference areas own ids 0..AREA_COUNT-1 and detection areas the next four, so
39// this is the whole id space TYPE_SET_AREA accepts.
40static constexpr uint8_t AREA_ID_COUNT = AREA_COUNT * LD600X_AREA_KINDS;
41static constexpr uint16_t DEFAULT_MAX_DATA_LEN = 1024;
42static constexpr uint16_t DEFAULT_MAX_DATA_LEN_POINT_CLOUD = 4096;
43// Largest protocol payload is TYPE_SET_AREA: int32 area id + 6 floats = 28 bytes.
44static constexpr size_t CMD_MAX_DATA_LEN = 28;
45
46static constexpr uint8_t LD600X_MODEL_KIND_BASE = 0x80;
47
58
74
75#ifdef USE_SENSOR
77 sensor::Sensor *x{nullptr};
78 sensor::Sensor *y{nullptr};
79 sensor::Sensor *z{nullptr};
81 // A cluster id names a person, so like the counts it is published on change, not per frame.
83};
84
85// Indexed by AreaAxis.
86using AreaSensors = std::array<sensor::Sensor *, AREA_AXIS_COUNT>;
87#endif
88
89// Index of one area in the tables that hold every area of every kind.
90static constexpr uint8_t area_index(uint8_t kind, uint8_t area) { return kind * AREA_COUNT + area; }
91
92struct AreaConfig {
93 float x_min{NAN};
94 float x_max{NAN};
95 float y_min{NAN};
96 float y_max{NAN};
97 float z_min{NAN};
98 float z_max{NAN};
99};
100
102 char value[20];
103};
104
106 public:
107 // The tag comes from LOG_STR, so it stays in flash on ESP8266 like an ESPHOME_LOG_TAG. It keeps the
108 // model's own name on every log line, so a logger `logs:` filter on `ld6002b` keeps working.
109 explicit LD600XComponent(const LogString *log_tag) : log_tag_(LOG_STR_ARG(log_tag)) {}
110
111 void setup() override;
112 void loop() override;
113 void dump_config() override;
114
115 void set_frame_buffer(uint8_t *buffer, uint16_t capacity) { this->parser_.init(buffer, capacity); }
116
117#ifdef USE_SENSOR
119 SUB_SENSOR_WITH_DEDUP(point_count, uint32_t)
120
121 void set_target_x_sensor(uint8_t target, sensor::Sensor *sensor) {
122 if (target >= LD600X_MAX_TARGETS)
123 return;
124 this->targets_[target].x = sensor;
125 }
127 if (target >= LD600X_MAX_TARGETS)
128 return;
129 this->targets_[target].y = sensor;
130 }
132 if (target >= LD600X_MAX_TARGETS)
133 return;
134 this->targets_[target].z = sensor;
135 }
137 if (target >= LD600X_MAX_TARGETS)
138 return;
139 this->targets_[target].dop_idx = sensor;
140 }
142 if (target >= LD600X_MAX_TARGETS)
143 return;
144 this->targets_[target].cluster_id.set_sensor(sensor);
145 }
146 void set_area_sensor(uint8_t kind, uint8_t area, uint8_t axis, sensor::Sensor *sensor) {
147 if (kind < LD600X_AREA_KINDS && area < AREA_COUNT && axis < AREA_AXIS_COUNT)
148 this->areas_[area_index(kind, area)][axis] = sensor;
149 }
150#endif
151
152#ifdef USE_BINARY_SENSOR
154 void set_target_presence_binary_sensor(uint8_t target, binary_sensor::BinarySensor *sensor) {
155 if (target >= LD600X_MAX_TARGETS)
156 return;
157 this->target_presence_[target] = sensor;
158 }
160 if (area >= AREA_COUNT)
161 return;
162 this->area_presence_[area] = sensor;
163 }
164#endif
165
166#ifdef USE_TEXT_SENSOR
167 SUB_TEXT_SENSOR(work_mode)
168 SUB_TEXT_SENSOR(ota_version)
169#endif
170
171#ifdef USE_NUMBER
172 SUB_NUMBER(hold_delay)
173 SUB_NUMBER(z_min)
174 SUB_NUMBER(z_max)
175 SUB_NUMBER(low_power_sleep)
176
177 SUB_NUMBER(area_x_min)
178 SUB_NUMBER(area_x_max)
179 SUB_NUMBER(area_y_min)
180 SUB_NUMBER(area_y_max)
181 SUB_NUMBER(area_z_min)
182 SUB_NUMBER(area_z_max)
183#endif
184
185#ifdef USE_SELECT
186 SUB_SELECT(sensitivity)
187 SUB_SELECT(trigger_speed)
188 SUB_SELECT(installation)
189 SUB_SELECT(area_id)
190#endif
191
192#ifdef USE_SWITCH
193 SUB_SWITCH(point_cloud)
194 SUB_SWITCH(target_display)
195#endif
196
197 // Each setter picks its setting by entity pointer, so the entities share one generated class. A model
198 // with its own entities handles those first and passes the rest down.
199#ifdef USE_NUMBER
200 // Returns false when the value was rejected or its command not queued, so the number keeps its old value.
201 virtual bool set_number_value(number::Number *entity, float value);
202#endif
203#ifdef USE_SELECT
204 // Returns false when the command was not queued, so the select keeps its old option.
205 virtual bool set_select_value(select::Select *entity, size_t index);
206#endif
207#ifdef USE_SWITCH
208 // Returns false when the command was not queued, so the switch keeps its old state.
209 virtual bool set_switch_state(switch_::Switch *entity, bool state);
210#endif
211#ifdef USE_BUTTON
212 virtual void press_button(uint8_t kind);
213#endif
214
215 protected:
216 virtual bool handle_model_report(uint16_t type, const uint8_t *data, uint16_t len) { return false; }
217 // Whether one of the model's own entities reads the target stream, so boot switches it on.
218 virtual bool model_wants_target_stream() const { return false; }
219 // Called after every target or area presence report and whenever target presence is cleared.
220 virtual void on_presence_update() {}
221 // Lets the model hold a frame back (for example to wake the module first) and write it later with
222 // write_frame_(). Returns true when it took the frame.
223 virtual bool defer_frame_write(uint16_t type, const uint8_t *data, uint8_t len, bool track) { return false; }
224 virtual void setup_model() {}
225 virtual void dump_model_config() {}
226
227 const char *log_tag_;
228
230 uint16_t type{0};
231 uint8_t len{0};
232 std::array<uint8_t, CMD_MAX_DATA_LEN> data{};
233 };
234
235 void handle_frame_(uint16_t type, const uint8_t *data, uint16_t len);
236 void handle_target_report_(const uint8_t *data, uint16_t len);
237 void handle_point_cloud_(const uint8_t *data, uint16_t len);
238 void handle_area_presence_(const uint8_t *data, uint16_t len);
239 void handle_area_report_(uint8_t kind, const uint8_t *data, uint16_t len);
240 void handle_delay_report_(const uint8_t *data, uint16_t len);
241 void handle_sensitivity_report_(const uint8_t *data, uint16_t len);
242 void handle_trigger_speed_report_(const uint8_t *data, uint16_t len);
243 void handle_z_range_report_(const uint8_t *data, uint16_t len);
244 void handle_installation_report_(const uint8_t *data, uint16_t len);
245 void handle_low_power_sleep_report_(const uint8_t *data, uint16_t len);
246 void handle_version_report_(const uint8_t *data, uint16_t len);
247 // Drops every target-derived reading and the slot table they are indexed by.
248 void clear_target_state_();
252#ifdef USE_SENSOR
253 void clear_target_slot_(uint8_t index);
254#endif
255#ifdef USE_NUMBER
256 void publish_number_clamped_(number::Number *number, float value);
257#endif
258 void update_area_numbers_(const AreaConfig &area);
259 void update_area_numbers_for_id_(uint8_t area_id);
260 bool queue_area_config_(uint8_t area_id, const AreaConfig &desired);
261 void try_apply_pending_area_(uint8_t reported_kind);
262 void init_area_id_pref_();
263 void save_area_id_pref_(uint8_t value);
264 void init_version_pref_();
265 void save_version_pref_(const char *value);
266
267 // Returns whether the command was queued: it is dropped, with a log line, when
268 // the payload is too long or the ring is full.
269 bool queue_command_(uint16_t type, const uint8_t *data, uint8_t len);
271 void send_command_(uint16_t type, const uint8_t *data, uint8_t len);
272 void send_command_internal_(uint16_t type, const uint8_t *data, uint8_t len, bool track);
273 void write_frame_(uint16_t type, const uint8_t *data, uint8_t len, bool track);
274 // Returns whether the command reached the queue; see queue_command_.
275 bool send_control_command_(uint32_t command);
276 // Writes the candidate range; the caller stores it only on true. Returns false when it was not
277 // queued: crossed bounds or a full queue. A bound held until the other one is known counts as accepted.
278 bool send_z_range_(float z_min, float z_max);
279#ifdef USE_NUMBER
280 // Writes a whole-number setting as a uint32; returns false for a fractional value or a full queue.
281 bool queue_u32_setting_(uint16_t type, float value);
282#endif
283 void apply_area_config_();
284
285#ifdef USE_SENSOR
286 std::array<TargetSensors, LD600X_MAX_TARGETS> targets_{};
287 std::array<AreaSensors, AREA_COUNT * LD600X_AREA_KINDS> areas_{};
288#endif
289#ifdef USE_BINARY_SENSOR
290 std::array<binary_sensor::BinarySensor *, LD600X_MAX_TARGETS> target_presence_{};
291 std::array<binary_sensor::BinarySensor *, AREA_COUNT> area_presence_{};
292#endif
293#ifdef USE_TEXT_SENSOR
296#endif
297#ifdef USE_SELECT
300#endif
301
303 uint16_t next_frame_id_{0};
304
305 // Sized for the two bursts that reach it, both counted as what is still queued
306 // once the first command is dequeued: boot leaves 11 with every platform
307 // configured, and pressing all fourteen buttons before an ack lands leaves 15.
308 // Neither overflowed 16, but one free slot is not headroom, and overflowing is a
309 // dropped command with only a log line to show for it. Costs 256 bytes more per
310 // configured instance, and this component is MULTI_CONF.
311 static constexpr uint8_t CMD_QUEUE_SIZE = 24;
312 static constexpr uint32_t CMD_ACK_TIMEOUT_MS = 300;
313 // A sleeping module consumes the first frame to wake and answers only the one after it.
314 static constexpr uint32_t CMD_FIRST_ACK_TIMEOUT_MS = 600;
315 // How long the module stays awake after any frame, and so still answers the next one.
316 static constexpr uint32_t MODULE_AWAKE_MS = 10000;
317 static constexpr uint8_t CMD_MAX_RETRIES = 3;
318 // Free for one keyed timer of the model's own.
319 static constexpr uint32_t MODEL_TIMEOUT_ID = 0;
320 // Keyed so a burst of writes collapses to one read once they settle, rather than
321 // one read per write.
322 static constexpr uint32_t AREA_REFRESH_TIMEOUT_ID = 1;
323 // A reply cannot trail the frame that earned it for longer than this; the field worst case is ~726ms.
324 static constexpr uint32_t STALE_ACK_MAX_AGE_MS = 1000;
325
327 bool command_active_{false};
328 bool command_sent_{false};
330 uint8_t retries_left_{0};
332 // Last frame seen in either direction; any traffic keeps the module awake.
334 // Frames transmitted for the command in flight, including retries; drives the retry budget.
335 uint8_t attempts_sent_{0};
336 // Subset of those the module can actually answer: a frame that woke it is consumed, not replied to.
337 uint8_t acks_expected_{0};
338 // ACKs still owed for superseded attempts; they carry no id, only their arrival order.
339 uint16_t stale_ack_type_{0};
341 // When that debt was booked, so a debt no reply can still settle expires instead of eating a live ACK.
343 // Bumped whenever the active command changes, so a deferred send can tell it was retired.
345
346 float z_min_{NAN};
347 float z_max_{NAN};
348 float area_x_min_{NAN};
349 float area_x_max_{NAN};
350 float area_y_min_{NAN};
351 float area_y_max_{NAN};
352 float area_z_min_{NAN};
353 float area_z_max_{NAN};
354 // What the user has typed and not yet applied; NaN per axis means "nothing of
355 // mine here, take the module's value". Same sentinel shape as
356 // pending_area_updates_. Exactly two things empty it: the area_id select moving
357 // to another area, and an apply that was accepted. A write the bounds guard
358 // refused leaves it alone, and a deferred apply that had to be dropped hands its
359 // staged values back here -- but only while the user is still on the area they
360 // were staged for. Either way the values stay the user's to fix.
362 std::array<AreaConfig, AREA_COUNT * LD600X_AREA_KINDS> area_values_{};
363 uint8_t area_id_{0xFF};
364 bool area_id_set_{false};
365
366 // Which person owns each target_N slot, so a slot survives the module re-sorting its array.
367 std::array<int32_t, LD600X_MAX_TARGETS> slot_cluster_{};
368 std::array<bool, LD600X_MAX_TARGETS> slot_occupied_{};
369
371 // What the switches and setup asked the module for, which is not the same as
372 // what it is doing yet: a stream keeps sending until it acts on the command.
373 // The report handlers read these and drop anything a stopped stream still emits.
379 uint8_t pending_area_id_{0xFF};
381
382#ifdef USE_SENSOR
383 std::array<bool, LD600X_MAX_TARGETS> last_target_presence_{}; // one-shot NAN clear for target sensors
384#endif
385};
386
387} // namespace esphome::ld600x
Fixed-size circular buffer with FIFO semantics and iteration support.
Definition helpers.h:456
Base class for all binary_sensor-type classes.
void init(uint8_t *buffer, uint16_t capacity)
static constexpr uint8_t CMD_MAX_RETRIES
Definition ld600x.h:317
void set_frame_buffer(uint8_t *buffer, uint16_t capacity)
Definition ld600x.h:115
bool queue_u32_setting_(uint16_t type, float value)
Definition ld600x.cpp:950
PendingCommand active_command_
Definition ld600x.h:329
void set_target_y_sensor(uint8_t target, sensor::Sensor *sensor)
Definition ld600x.h:126
void set_area_presence_binary_sensor(uint8_t area, binary_sensor::BinarySensor *sensor)
Definition ld600x.h:159
static constexpr uint32_t CMD_ACK_TIMEOUT_MS
Definition ld600x.h:312
void update_area_numbers_for_id_(uint8_t area_id)
Definition ld600x.cpp:1110
std::array< AreaConfig, AREA_COUNT *LD600X_AREA_KINDS > area_values_
Definition ld600x.h:362
bool queue_area_config_(uint8_t area_id, const AreaConfig &desired)
Definition ld600x.cpp:1119
std::array< int32_t, LD600X_MAX_TARGETS > slot_cluster_
Definition ld600x.h:367
bool send_z_range_(float z_min, float z_max)
Definition ld600x.cpp:932
LD600XComponent(const LogString *log_tag)
Definition ld600x.h:109
void handle_sensitivity_report_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:673
void handle_z_range_report_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:699
uint32_t void set_target_x_sensor(uint8_t target, sensor::Sensor *sensor)
Definition ld600x.h:121
virtual bool set_switch_state(switch_::Switch *entity, bool state)
Definition ld600x.cpp:1374
void write_frame_(uint16_t type, const uint8_t *data, uint8_t len, bool track)
Definition ld600x.cpp:907
SUB_SENSOR_WITH_DEDUP(target_count, uint32_t) SUB_SENSOR_WITH_DEDUP(point_count
virtual void press_button(uint8_t kind)
Definition ld600x.cpp:1400
void handle_low_power_sleep_report_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:725
virtual bool model_wants_target_stream() const
Definition ld600x.h:218
SUB_BINARY_SENSOR(presence) void set_target_presence_binary_sensor(uint8_t target
void set_target_dop_idx_sensor(uint8_t target, sensor::Sensor *sensor)
Definition ld600x.h:136
static constexpr uint32_t AREA_REFRESH_TIMEOUT_ID
Definition ld600x.h:322
std::array< bool, LD600X_MAX_TARGETS > slot_occupied_
Definition ld600x.h:368
void handle_trigger_speed_report_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:686
std::array< bool, LD600X_MAX_TARGETS > last_target_presence_
Definition ld600x.h:383
std::array< AreaSensors, AREA_COUNT *LD600X_AREA_KINDS > areas_
Definition ld600x.h:287
virtual bool defer_frame_write(uint16_t type, const uint8_t *data, uint8_t len, bool track)
Definition ld600x.h:223
void clear_target_slot_(uint8_t index)
Definition ld600x.cpp:1264
bool queue_command_(uint16_t type, const uint8_t *data, uint8_t len)
Definition ld600x.cpp:782
void handle_area_presence_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:605
void set_target_z_sensor(uint8_t target, sensor::Sensor *sensor)
Definition ld600x.h:131
static constexpr uint32_t STALE_ACK_MAX_AGE_MS
Definition ld600x.h:324
void update_area_numbers_(const AreaConfig &area)
Definition ld600x.cpp:1077
void handle_target_report_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:490
void try_apply_pending_area_(uint8_t reported_kind)
Definition ld600x.cpp:1158
void handle_version_report_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:761
void handle_point_cloud_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:587
void publish_number_clamped_(number::Number *number, float value)
Definition ld600x.cpp:735
virtual bool handle_model_report(uint16_t type, const uint8_t *data, uint16_t len)
Definition ld600x.h:216
StaticRingBuffer< PendingCommand, CMD_QUEUE_SIZE > cmd_queue_
Definition ld600x.h:326
void set_area_sensor(uint8_t kind, uint8_t area, uint8_t axis, sensor::Sensor *sensor)
Definition ld600x.h:146
binary_sensor::BinarySensor * sensor
Definition ld600x.h:154
std::array< binary_sensor::BinarySensor *, AREA_COUNT > area_presence_
Definition ld600x.h:291
void handle_installation_report_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:712
virtual void on_presence_update()
Definition ld600x.h:220
void set_target_cluster_id_sensor(uint8_t target, sensor::Sensor *sensor)
Definition ld600x.h:141
std::array< TargetSensors, LD600X_MAX_TARGETS > targets_
Definition ld600x.h:286
void handle_area_report_(uint8_t kind, const uint8_t *data, uint16_t len)
Definition ld600x.cpp:627
static constexpr uint32_t CMD_FIRST_ACK_TIMEOUT_MS
Definition ld600x.h:314
void handle_frame_(uint16_t type, const uint8_t *data, uint16_t len)
Definition ld600x.cpp:397
virtual void dump_model_config()
Definition ld600x.h:225
ESPPreferenceObject version_pref_
Definition ld600x.h:294
ESPPreferenceObject area_id_pref_
Definition ld600x.h:298
void send_command_internal_(uint16_t type, const uint8_t *data, uint8_t len, bool track)
Definition ld600x.cpp:890
bool send_control_command_(uint32_t command)
Definition ld600x.cpp:926
static constexpr uint32_t MODEL_TIMEOUT_ID
Definition ld600x.h:319
virtual SUB_TEXT_SENSOR(work_mode) SUB_TEXT_SENSOR(ota_version) SUB_NUMBER(hold_delay) SUB_NUMBER(z_min) SUB_NUMBER(z_max) SUB_NUMBER(low_power_sleep) SUB_NUMBER(area_x_min) SUB_NUMBER(area_x_max) SUB_NUMBER(area_y_min) SUB_NUMBER(area_y_max) SUB_NUMBER(area_z_min) SUB_NUMBER(area_z_max) SUB_SELECT(sensitivity) SUB_SELECT(trigger_speed) SUB_SELECT(installation) SUB_SELECT(area_id) SUB_SWITCH(point_cloud) SUB_SWITCH(target_display) virtual bool set_number_value(number bool set_select_value(select::Select *entity, size_t index)
Definition ld600x.cpp:1061
static constexpr uint8_t CMD_QUEUE_SIZE
Definition ld600x.h:311
void handle_delay_report_(const uint8_t *data, uint16_t len)
Definition ld600x.cpp:664
static constexpr uint32_t MODULE_AWAKE_MS
Definition ld600x.h:316
void save_version_pref_(const char *value)
Definition ld600x.cpp:1251
void send_command_(uint16_t type, const uint8_t *data, uint8_t len)
Definition ld600x.cpp:886
void save_area_id_pref_(uint8_t value)
Definition ld600x.cpp:1226
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:28
Base-class for all sensors.
Definition sensor.h:48
A sensor pointer that may be null plus the last value it published, stored inline.
Base class for all switches.
Definition switch.h:43
uint8_t type
bool state
Definition fan.h:2
uint32_t buffer
Definition gif.cpp:50
@ AREA_KIND_DETECTION
Definition ld600x.h:48
@ AREA_KIND_INTERFERENCE
Definition ld600x.h:48
@ BUTTON_GET_LOW_POWER_MODE
Definition ld600x.h:70
@ BUTTON_CLEAR_INTERFERENCE
Definition ld600x.h:63
@ BUTTON_GET_Z_RANGE
Definition ld600x.h:68
@ BUTTON_GET_TRIGGER_SPEED
Definition ld600x.h:67
@ BUTTON_AUTO_INTERFERENCE
Definition ld600x.h:61
@ BUTTON_GET_LOW_POWER_SLEEP_TIME
Definition ld600x.h:71
@ BUTTON_RESET_UNATTENDED
Definition ld600x.h:72
@ BUTTON_RESET_DETECTION_AREA
Definition ld600x.h:64
@ BUTTON_GET_INSTALLATION
Definition ld600x.h:69
@ BUTTON_GET_SENSITIVITY
Definition ld600x.h:66
@ BUTTON_APPLY_AREA
Definition ld600x.h:60
std::array< sensor::Sensor *, AREA_AXIS_COUNT > AreaSensors
Definition ld600x.h:86
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
uint16_t uint16_t & capacity
Definition helpers.cpp:44
static void uint32_t
std::array< uint8_t, CMD_MAX_DATA_LEN > data
Definition ld600x.h:232
sensor::Sensor * dop_idx
Definition ld600x.h:80
sensor::SensorWithDedup< int32_t > cluster_id
Definition ld600x.h:82