ESPHome 2026.5.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
31
32#include <array>
33
34namespace esphome::ld2450 {
35
36using namespace ld24xx;
37
38// Constants
39static constexpr uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset presense status 5 sec.
40// Zone query response is 40 bytes; +1 for null terminator, +4 so that a frame footer always
41// lands inside the buffer during footer-based resynchronization after losing sync.
42static constexpr uint8_t MAX_LINE_LENGTH = 45;
43static constexpr uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450
44static constexpr uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450
45
53
54// Target coordinate struct
55struct Target {
56 int16_t x;
57 int16_t y;
59};
60
61// Zone coordinate struct
62struct Zone {
63 int16_t x1 = 0;
64 int16_t y1 = 0;
65 int16_t x2 = 0;
66 int16_t y2 = 0;
67};
68
69#ifdef USE_NUMBER
71 number::Number *x1 = nullptr;
72 number::Number *y1 = nullptr;
73 number::Number *x2 = nullptr;
74 number::Number *y2 = nullptr;
75};
76#endif
77
79#ifdef USE_BINARY_SENSOR
80 SUB_BINARY_SENSOR(moving_target)
81 SUB_BINARY_SENSOR(still_target)
82 SUB_BINARY_SENSOR(target)
83#endif
84#ifdef USE_SENSOR
85 SUB_SENSOR_WITH_DEDUP(moving_target_count, uint8_t)
86 SUB_SENSOR_WITH_DEDUP(still_target_count, uint8_t)
87 SUB_SENSOR_WITH_DEDUP(target_count, uint8_t)
88#endif
89#ifdef USE_TEXT_SENSOR
90 SUB_TEXT_SENSOR(mac)
91 SUB_TEXT_SENSOR(version)
92#endif
93#ifdef USE_NUMBER
94 SUB_NUMBER(presence_timeout)
95#endif
96#ifdef USE_SELECT
97 SUB_SELECT(baud_rate)
98 SUB_SELECT(zone_type)
99#endif
100#ifdef USE_SWITCH
101 SUB_SWITCH(bluetooth)
102 SUB_SWITCH(multi_target)
103#endif
104#ifdef USE_BUTTON
105 SUB_BUTTON(factory_reset)
106 SUB_BUTTON(restart)
107#endif
108
109 public:
110 void setup() override;
111 void dump_config() override;
112 void loop() override;
113 void set_presence_timeout();
114 void read_all_info();
115 void query_zone_info();
116 void restart_and_read_all_info();
117 void set_bluetooth(bool enable);
118 void set_multi_target(bool enable);
119 void set_baud_rate(const char *state);
120 void set_zone_type(const char *state);
121 void publish_zone_type();
122 void factory_reset();
123#ifdef USE_TEXT_SENSOR
124 void set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s);
125#endif
126#ifdef USE_NUMBER
127 void set_zone_coordinate(uint8_t zone);
128 void set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2, number::Number *y2);
129#endif
130#ifdef USE_SENSOR
131 void set_move_x_sensor(uint8_t target, sensor::Sensor *s);
132 void set_move_y_sensor(uint8_t target, sensor::Sensor *s);
133 void set_move_speed_sensor(uint8_t target, sensor::Sensor *s);
134 void set_move_angle_sensor(uint8_t target, sensor::Sensor *s);
135 void set_move_distance_sensor(uint8_t target, sensor::Sensor *s);
136 void set_move_resolution_sensor(uint8_t target, sensor::Sensor *s);
137 void set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s);
138 void set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s);
139 void set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s);
140#endif
141 void reset_radar_zone();
142 void set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2, int32_t zone1_y2,
143 int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1,
144 int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2);
145
147 template<typename F> void add_on_data_callback(F &&callback) { this->data_callback_.add(std::forward<F>(callback)); }
148
149 protected:
150 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
151 void set_config_mode_(bool enable);
153 bool handle_ack_data_();
154 void process_zone_();
155 void readline_(int readch);
156 void get_version_();
157 void get_mac_();
159 void query_zone_();
160 void restart_();
162 void save_to_flash_(float value);
163 float restore_from_flash_();
164 bool get_timeout_status_(uint32_t check_millis);
165 void count_targets_in_zone_(const Zone &zone, uint8_t &still, uint8_t &moving);
166
171 uint8_t buffer_data_[MAX_LINE_LENGTH];
172 uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
173 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
174 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
175 uint8_t zone_type_ = 0;
176 bool bluetooth_on_{false};
177 Target target_info_[MAX_TARGETS];
178 Zone zone_config_[MAX_ZONES];
179
180#ifdef USE_NUMBER
181 ESPPreferenceObject pref_; // only used when numbers are in use
183#endif
184#ifdef USE_SENSOR
185 std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_x_sensors_{};
186 std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_y_sensors_{};
187 std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_speed_sensors_{};
188 std::array<SensorWithDedup<float>, MAX_TARGETS> move_angle_sensors_{};
189 std::array<SensorWithDedup<uint16_t>, MAX_TARGETS> move_distance_sensors_{};
190 std::array<SensorWithDedup<uint16_t>, MAX_TARGETS> move_resolution_sensors_{};
191 std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_target_count_sensors_{};
192 std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_still_target_count_sensors_{};
193 std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_moving_target_count_sensors_{};
194#endif
195#ifdef USE_TEXT_SENSOR
196 std::array<text_sensor::TextSensor *, MAX_TARGETS> direction_text_sensors_{};
197 std::array<Deduplicator<uint8_t>, MAX_TARGETS> direction_dedup_{};
198#endif
199
201};
202
203} // namespace esphome::ld2450
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:89
void save_to_flash_(float value)
Definition ld2450.cpp:955
std::array< SensorWithDedup< int16_t >, MAX_TARGETS > move_y_sensors_
Definition ld2450.h:186
std::array< SensorWithDedup< uint8_t >, MAX_ZONES > zone_moving_target_count_sensors_
Definition ld2450.h:193
std::array< Deduplicator< uint8_t >, MAX_TARGETS > direction_dedup_
Definition ld2450.h:197
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2450.cpp:412
std::array< SensorWithDedup< uint8_t >, MAX_ZONES > zone_target_count_sensors_
Definition ld2450.h:191
void set_config_mode_(bool enable)
Definition ld2450.cpp:803
std::array< text_sensor::TextSensor *, MAX_TARGETS > direction_text_sensors_
Definition ld2450.h:196
void count_targets_in_zone_(const Zone &zone, uint8_t &still, uint8_t &moving)
Definition ld2450.cpp:288
std::array< SensorWithDedup< uint16_t >, MAX_TARGETS > move_resolution_sensors_
Definition ld2450.h:190
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2450.h:171
Target target_info_[MAX_TARGETS]
Definition ld2450.h:177
Zone zone_config_[MAX_ZONES]
Definition ld2450.h:178
ESPPreferenceObject pref_
Definition ld2450.h:181
std::array< SensorWithDedup< uint16_t >, MAX_TARGETS > move_distance_sensors_
Definition ld2450.h:189
std::array< SensorWithDedup< uint8_t >, MAX_ZONES > zone_still_target_count_sensors_
Definition ld2450.h:192
LazyCallbackManager< void()> data_callback_
Definition ld2450.h:200
std::array< SensorWithDedup< int16_t >, MAX_TARGETS > move_speed_sensors_
Definition ld2450.h:187
std::array< SensorWithDedup< float >, MAX_TARGETS > move_angle_sensors_
Definition ld2450.h:188
ZoneOfNumbers zone_numbers_[MAX_ZONES]
Definition ld2450.h:182
std::array< SensorWithDedup< int16_t >, MAX_TARGETS > move_x_sensors_
Definition ld2450.h:185
bool get_timeout_status_(uint32_t check_millis)
Definition ld2450.cpp:348
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:48
@ DIRECTION_APPROACHING
Definition ld2450.h:47
@ DIRECTION_UNDEFINED
Definition ld2450.h:51
@ DIRECTION_STATIONARY
Definition ld2450.h:49
static void uint32_t
number::Number * y2
Definition ld2450.h:74
number::Number * x2
Definition ld2450.h:73
number::Number * x1
Definition ld2450.h:71
number::Number * y1
Definition ld2450.h:72