ESPHome 2026.11.0-dev
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
57
58// Target coordinate struct
59struct Target {
60 int16_t x;
61 int16_t y;
63};
64
65// Zone coordinate struct
66struct Zone {
67 int16_t x1 = 0;
68 int16_t y1 = 0;
69 int16_t x2 = 0;
70 int16_t y2 = 0;
71};
72
73#ifdef USE_NUMBER
75 number::Number *x1 = nullptr;
76 number::Number *y1 = nullptr;
77 number::Number *x2 = nullptr;
78 number::Number *y2 = nullptr;
79};
80#endif
81
83#ifdef USE_BINARY_SENSOR
84 SUB_BINARY_SENSOR(moving_target)
85 SUB_BINARY_SENSOR(still_target)
86 SUB_BINARY_SENSOR(target)
87#endif
88#ifdef USE_SENSOR
89 SUB_SENSOR_WITH_DEDUP(moving_target_count, uint8_t)
90 SUB_SENSOR_WITH_DEDUP(still_target_count, uint8_t)
91 SUB_SENSOR_WITH_DEDUP(target_count, uint8_t)
92#endif
93#ifdef USE_TEXT_SENSOR
94 SUB_TEXT_SENSOR(mac)
95 SUB_TEXT_SENSOR(version)
96#endif
97#ifdef USE_NUMBER
98 SUB_NUMBER(presence_timeout)
99#endif
100#ifdef USE_SELECT
101 SUB_SELECT(baud_rate)
102 SUB_SELECT(zone_type)
103#endif
104#ifdef USE_SWITCH
105 SUB_SWITCH(bluetooth)
106 SUB_SWITCH(multi_target)
107#endif
108#ifdef USE_BUTTON
109 SUB_BUTTON(factory_reset)
110 SUB_BUTTON(restart)
111#endif
112
113 public:
114 void setup() override;
115 void dump_config() override;
116 void loop() override;
117 void set_presence_timeout();
118 void read_all_info();
119 void query_zone_info();
120 void restart_and_read_all_info();
121 void set_bluetooth(bool enable);
122 void set_multi_target(bool enable);
123 void set_baud_rate(const char *state);
124 void set_zone_type(const char *state);
125 void publish_zone_type();
126 void factory_reset();
127#ifdef USE_TEXT_SENSOR
128 void set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s);
129#endif
130#ifdef USE_NUMBER
131 void set_zone_coordinate(number::Number *n);
132 void set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2, number::Number *y2);
133#endif
134#ifdef USE_SENSOR
135 void set_move_x_sensor(uint8_t target, sensor::Sensor *s);
136 void set_move_y_sensor(uint8_t target, sensor::Sensor *s);
137 void set_move_speed_sensor(uint8_t target, sensor::Sensor *s);
138 void set_move_angle_sensor(uint8_t target, sensor::Sensor *s);
139 void set_move_distance_sensor(uint8_t target, sensor::Sensor *s);
140 void set_move_resolution_sensor(uint8_t target, sensor::Sensor *s);
141 void set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s);
142 void set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s);
143 void set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s);
144#endif
145#ifdef LD2450_POLYGON_ZONE_COUNT
146 void register_polygon_zone(PolygonZone *zone) { this->polygon_zones_.push_back(zone); }
147#endif
148 void reset_radar_zone();
149 void set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2, int32_t zone1_y2,
150 int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1,
151 int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2);
152
154 template<typename F> void add_on_data_callback(F &&callback) { this->data_callback_.add(std::forward<F>(callback)); }
155
156 protected:
157 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
158 void set_config_mode_(bool enable);
160 bool handle_ack_data_();
161 void process_zone_();
162 void readline_(int readch);
163 void get_version_();
164 void get_mac_();
166 void query_zone_();
167 void restart_();
169 void save_to_flash_(float value);
170 float restore_from_flash_();
171 bool get_timeout_status_(uint32_t check_millis);
172 void count_targets_in_zone_(const Zone &zone, uint8_t &still, uint8_t &moving);
173
177 uint32_t timeout_ = DEFAULT_PRESENCE_TIMEOUT * 1000; // presence_timeout, in ms
178 uint8_t buffer_data_[MAX_LINE_LENGTH];
179 uint8_t mac_address_[MAC_ADDRESS_SIZE] = {0, 0, 0, 0, 0, 0};
180 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
181 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
182 uint8_t zone_type_ = 0;
183 bool bluetooth_on_{false};
184 Target target_info_[MAX_TARGETS];
185 Zone zone_config_[MAX_ZONES];
186#ifdef USE_TEXT_SENSOR
187 // Byte aligned, so it packs after zone_config_ instead of padding before data_callback_
188 std::array<Deduplicator<uint8_t>, MAX_TARGETS> direction_dedup_{};
189#endif
190
191#ifdef USE_NUMBER
192 ESPPreferenceObject pref_; // only used when numbers are in use
194#endif
195#ifdef USE_SENSOR
196 std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_x_sensors_{};
197 std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_y_sensors_{};
198 std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_speed_sensors_{};
199 std::array<SensorWithDedup<float>, MAX_TARGETS> move_angle_sensors_{};
200 std::array<SensorWithDedup<uint16_t>, MAX_TARGETS> move_distance_sensors_{};
201 std::array<SensorWithDedup<uint16_t>, MAX_TARGETS> move_resolution_sensors_{};
202 std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_target_count_sensors_{};
203 std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_still_target_count_sensors_{};
204 std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_moving_target_count_sensors_{};
205#endif
206#ifdef USE_TEXT_SENSOR
207 std::array<text_sensor::TextSensor *, MAX_TARGETS> direction_text_sensors_{};
208#endif
209
210#ifdef LD2450_POLYGON_ZONE_COUNT
212#endif
213
215};
216
217} // 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:286
void save_to_flash_(float value)
Definition ld2450.cpp:1009
std::array< SensorWithDedup< int16_t >, MAX_TARGETS > move_y_sensors_
Definition ld2450.h:197
std::array< SensorWithDedup< uint8_t >, MAX_ZONES > zone_moving_target_count_sensors_
Definition ld2450.h:204
std::array< Deduplicator< uint8_t >, MAX_TARGETS > direction_dedup_
Definition ld2450.h:188
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2450.cpp:432
std::array< SensorWithDedup< uint8_t >, MAX_ZONES > zone_target_count_sensors_
Definition ld2450.h:202
void set_config_mode_(bool enable)
Definition ld2450.cpp:851
std::array< text_sensor::TextSensor *, MAX_TARGETS > direction_text_sensors_
Definition ld2450.h:207
void count_targets_in_zone_(const Zone &zone, uint8_t &still, uint8_t &moving)
Definition ld2450.cpp:308
StaticVector< PolygonZone *, LD2450_POLYGON_ZONE_COUNT > polygon_zones_
Definition ld2450.h:211
std::array< SensorWithDedup< uint16_t >, MAX_TARGETS > move_resolution_sensors_
Definition ld2450.h:201
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2450.h:178
uint8_t mac_address_[MAC_ADDRESS_SIZE]
Definition ld2450.h:179
Target target_info_[MAX_TARGETS]
Definition ld2450.h:184
Zone zone_config_[MAX_ZONES]
Definition ld2450.h:185
ESPPreferenceObject pref_
Definition ld2450.h:192
std::array< SensorWithDedup< uint16_t >, MAX_TARGETS > move_distance_sensors_
Definition ld2450.h:200
std::array< SensorWithDedup< uint8_t >, MAX_ZONES > zone_still_target_count_sensors_
Definition ld2450.h:203
LazyCallbackManager< void()> data_callback_
Definition ld2450.h:214
std::array< SensorWithDedup< int16_t >, MAX_TARGETS > move_speed_sensors_
Definition ld2450.h:198
std::array< SensorWithDedup< float >, MAX_TARGETS > move_angle_sensors_
Definition ld2450.h:199
ZoneOfNumbers zone_numbers_[MAX_ZONES]
Definition ld2450.h:193
std::array< SensorWithDedup< int16_t >, MAX_TARGETS > move_x_sensors_
Definition ld2450.h:196
bool get_timeout_status_(uint32_t check_millis)
Definition ld2450.cpp:368
A zone with any polygon shape.
Base-class for all numbers.
Definition number.h:29
Base-class for all sensors.
Definition sensor.h:47
bool state
Definition fan.h:2
@ DIRECTION_MOVING_AWAY
Definition ld2450.h:52
@ DIRECTION_APPROACHING
Definition ld2450.h:51
@ DIRECTION_UNDEFINED
Definition ld2450.h:55
@ DIRECTION_STATIONARY
Definition ld2450.h:53
static void uint32_t
number::Number * y2
Definition ld2450.h:78
number::Number * x2
Definition ld2450.h:77
number::Number * x1
Definition ld2450.h:75
number::Number * y1
Definition ld2450.h:76