ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld2450.h
Go to the documentation of this file.
1#pragma once
2
5#ifdef USE_SENSOR
7#endif
8#ifdef USE_NUMBER
10#endif
11#ifdef USE_SWITCH
13#endif
14#ifdef USE_BUTTON
16#endif
17#ifdef USE_SELECT
19#endif
20#ifdef USE_TEXT_SENSOR
22#endif
23#ifdef USE_BINARY_SENSOR
25#endif
26
27#ifdef LD2450_POLYGON_ZONE_COUNT
28#include "polygon_zone.h"
29#endif
30
35
36#include <array>
37
38namespace esphome::ld2450 {
39
40using namespace ld24xx;
41
42// Constants
43static constexpr uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset presense status 5 sec.
44// Zone query response is 40 bytes; +1 for null terminator, +4 so that a frame footer always
45// lands inside the buffer during footer-based resynchronization after losing sync.
46static constexpr uint8_t MAX_LINE_LENGTH = 45;
47static constexpr uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450
48static constexpr uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450
49
50// Silence after our own commands (restart, Bluetooth toggle, ...) is not a link loss; a restart takes about 2.3 s
51static constexpr uint32_t LINK_GRACE_MS = 5000;
52// Caps the windows, so retried commands cannot hide a real loss
53static constexpr uint32_t LINK_GRACE_CAP_MS = 15000;
54
55enum class LinkState : uint8_t {
56 LINK_STATE_WAITING, // no valid frame since boot
57 LINK_STATE_UP, // valid frames arrive
58 LINK_STATE_LOST, // no valid frame for the link timeout
59};
60
68
79
80enum ZoneType : uint8_t {
84};
85
86// Target coordinate struct
87struct Target {
88 int16_t x;
89 int16_t y;
91};
92
93// Zone coordinate struct
94struct Zone {
95 int16_t x1 = 0;
96 int16_t y1 = 0;
97 int16_t x2 = 0;
98 int16_t y2 = 0;
99};
100
101#ifdef USE_NUMBER
103 number::Number *x1 = nullptr;
104 number::Number *y1 = nullptr;
105 number::Number *x2 = nullptr;
106 number::Number *y2 = nullptr;
107};
108#endif
109
111#ifdef USE_BINARY_SENSOR
112 SUB_BINARY_SENSOR(connected)
113 SUB_BINARY_SENSOR(moving_target)
114 SUB_BINARY_SENSOR(still_target)
115 SUB_BINARY_SENSOR(target)
116#endif
117#ifdef USE_SENSOR
118 SUB_SENSOR_WITH_DEDUP(moving_target_count, uint8_t)
119 SUB_SENSOR_WITH_DEDUP(still_target_count, uint8_t)
120 SUB_SENSOR_WITH_DEDUP(target_count, uint8_t)
121#endif
122#ifdef USE_TEXT_SENSOR
123 SUB_TEXT_SENSOR(mac)
124 SUB_TEXT_SENSOR(version)
125#endif
126#ifdef USE_NUMBER
127 SUB_NUMBER(presence_timeout)
128#endif
129#ifdef USE_SELECT
130 SUB_SELECT(baud_rate)
131 SUB_SELECT(zone_type)
132#endif
133#ifdef USE_SWITCH
134 SUB_SWITCH(bluetooth)
135 SUB_SWITCH(multi_target)
136#endif
137#ifdef USE_BUTTON
138 SUB_BUTTON(factory_reset)
139 SUB_BUTTON(restart)
140#endif
141
142 public:
143 void setup() override;
144 void dump_config() override;
145 void loop() override;
146 void set_presence_timeout();
147 void set_link_timeout(uint32_t timeout_ms) { this->link_timeout_ms_ = timeout_ms; }
148 void read_all_info();
149 void query_zone_info();
150 void restart_and_read_all_info();
151 void set_bluetooth(bool enable);
152 void set_multi_target(bool enable);
153 void set_baud_rate(const char *state);
154 void set_baud_rate(BaudRate rate);
155 void set_zone_type(const char *state);
156 void set_zone_type(ZoneType type);
157 void publish_zone_type();
158 void factory_reset();
159#ifdef USE_TEXT_SENSOR
160 void set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s);
161#endif
162#ifdef USE_LD2450_TARGET_XY
163 void set_target_xy_text_sensor(uint8_t target, text_sensor::TextSensor *s);
164#endif
165#ifdef USE_NUMBER
166 void set_zone_coordinate(number::Number *n);
167 void set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2, number::Number *y2);
168#endif
169#ifdef USE_SENSOR
170 void set_move_x_sensor(uint8_t target, sensor::Sensor *s);
171 void set_move_y_sensor(uint8_t target, sensor::Sensor *s);
172 void set_move_speed_sensor(uint8_t target, sensor::Sensor *s);
173 void set_move_angle_sensor(uint8_t target, sensor::Sensor *s);
174 void set_move_distance_sensor(uint8_t target, sensor::Sensor *s);
175 void set_move_resolution_sensor(uint8_t target, sensor::Sensor *s);
176 void set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s);
177 void set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s);
178 void set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s);
179#endif
180#ifdef LD2450_POLYGON_ZONE_COUNT
181 void register_polygon_zone(PolygonZone *zone) { this->polygon_zones_.push_back(zone); }
182#endif
183 void reset_radar_zone();
184 void set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2, int32_t zone1_y2,
185 int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1,
186 int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2);
187
189 template<typename F> void add_on_data_callback(F &&callback) { this->data_callback_.add(std::forward<F>(callback)); }
190
191 protected:
192 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
193 void set_config_mode_(bool enable);
195 void handle_ack_data_();
196 bool handle_frame_(uint8_t frame_len);
197 void process_zone_();
198 void readline_(int readch);
199 void get_version_();
200 void get_mac_();
202 void query_zone_();
203 void restart_();
205 void save_to_flash_(float value);
206 float restore_from_flash_();
207 bool get_timeout_status_(uint32_t check_millis);
208 void open_link_grace_();
209 void check_link_();
210 void on_valid_frame_();
211 void on_link_lost_(uint32_t silent_ms, bool was_up);
212 void on_link_up_(bool restored);
213#ifdef USE_SENSOR
214 void publish_target_unknown_(uint8_t target);
215#endif
216#ifdef USE_LD2450_TARGET_XY
217 void update_target_xy_(uint8_t target, bool changed);
218#endif
219 void count_targets_in_zone_(const Zone &zone, uint8_t &still, uint8_t &moving);
220
224 uint32_t timeout_ = DEFAULT_PRESENCE_TIMEOUT * 1000; // presence_timeout, in ms
226 uint32_t last_frame_ms_{0}; // last valid periodic frame; until the first one, when setup() ran
227 uint32_t link_deadline_ms_{0}; // the link is lost when the loop clock reaches this
228 uint8_t buffer_data_[MAX_LINE_LENGTH];
229 uint8_t mac_address_[MAC_ADDRESS_SIZE] = {0, 0, 0, 0, 0, 0};
230 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
231 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
232 uint8_t zone_type_ = 0;
233 bool bluetooth_on_{false};
235 bool bytes_since_frame_{false}; // any bytes since the last valid frame
236 uint16_t invalid_frames_{0}; // since the last valid frame, saturating
237 Target target_info_[MAX_TARGETS];
238 Zone zone_config_[MAX_ZONES];
239#ifdef USE_TEXT_SENSOR
240 // Byte aligned, so it packs after zone_config_ instead of padding before data_callback_
241 std::array<Deduplicator<uint8_t>, MAX_TARGETS> direction_dedup_{};
242#endif
243
244#ifdef USE_NUMBER
245 ESPPreferenceObject pref_; // only used when numbers are in use
247#endif
248#ifdef USE_SENSOR
249 std::array<sensor::SensorWithDedup<int16_t>, MAX_TARGETS> move_x_sensors_{};
250 std::array<sensor::SensorWithDedup<int16_t>, MAX_TARGETS> move_y_sensors_{};
251 std::array<sensor::SensorWithDedup<int16_t>, MAX_TARGETS> move_speed_sensors_{};
252 std::array<sensor::SensorWithDedup<float>, MAX_TARGETS> move_angle_sensors_{};
253 std::array<sensor::SensorWithDedup<uint16_t>, MAX_TARGETS> move_distance_sensors_{};
254 std::array<sensor::SensorWithDedup<uint16_t>, MAX_TARGETS> move_resolution_sensors_{};
255 std::array<sensor::SensorWithDedup<uint8_t>, MAX_ZONES> zone_target_count_sensors_{};
256 std::array<sensor::SensorWithDedup<uint8_t>, MAX_ZONES> zone_still_target_count_sensors_{};
257 std::array<sensor::SensorWithDedup<uint8_t>, MAX_ZONES> zone_moving_target_count_sensors_{};
258#endif
259#ifdef USE_TEXT_SENSOR
260 std::array<text_sensor::TextSensor *, MAX_TARGETS> direction_text_sensors_{};
261#endif
262#ifdef USE_LD2450_TARGET_XY
263 // Publishes a target's position as "x,y" text, so both values always come from the same radar frame
264 std::array<text_sensor::TextSensor *, MAX_TARGETS> target_xy_text_sensors_{};
265#endif
266
267#ifdef LD2450_POLYGON_ZONE_COUNT
269#endif
270
272};
273
274} // namespace esphome::ld2450
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:304
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
uint8_t type
bool state
Definition fan.h:2
@ 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
uint32_t value
Definition hal.cpp:41
static void uint32_t