ESPHome 2026.2.3
Loading...
Searching...
No Matches
ld2450.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_SENSOR
8#endif
9#ifdef USE_NUMBER
11#endif
12#ifdef USE_SWITCH
14#endif
15#ifdef USE_BUTTON
17#endif
18#ifdef USE_SELECT
20#endif
21#ifdef USE_TEXT_SENSOR
23#endif
24#ifdef USE_BINARY_SENSOR
26#endif
27
32
33#include <array>
34
35namespace esphome::ld2450 {
36
37using namespace ld24xx;
38
39// Constants
40static constexpr uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset presense status 5 sec.
41// Zone query response is 40 bytes; +1 for null terminator, +4 so that a frame footer always
42// lands inside the buffer during footer-based resynchronization after losing sync.
43static constexpr uint8_t MAX_LINE_LENGTH = 45;
44static constexpr uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450
45static constexpr uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450
46
54
55// Target coordinate struct
56struct Target {
57 int16_t x;
58 int16_t y;
60};
61
62// Zone coordinate struct
63struct Zone {
64 int16_t x1 = 0;
65 int16_t y1 = 0;
66 int16_t x2 = 0;
67 int16_t y2 = 0;
68};
69
70#ifdef USE_NUMBER
72 number::Number *x1 = nullptr;
73 number::Number *y1 = nullptr;
74 number::Number *x2 = nullptr;
75 number::Number *y2 = nullptr;
76};
77#endif
78
80#ifdef USE_BINARY_SENSOR
81 SUB_BINARY_SENSOR(moving_target)
82 SUB_BINARY_SENSOR(still_target)
83 SUB_BINARY_SENSOR(target)
84#endif
85#ifdef USE_SENSOR
86 SUB_SENSOR_WITH_DEDUP(moving_target_count, uint8_t)
87 SUB_SENSOR_WITH_DEDUP(still_target_count, uint8_t)
88 SUB_SENSOR_WITH_DEDUP(target_count, uint8_t)
89#endif
90#ifdef USE_TEXT_SENSOR
91 SUB_TEXT_SENSOR(mac)
92 SUB_TEXT_SENSOR(version)
93#endif
94#ifdef USE_NUMBER
95 SUB_NUMBER(presence_timeout)
96#endif
97#ifdef USE_SELECT
98 SUB_SELECT(baud_rate)
99 SUB_SELECT(zone_type)
100#endif
101#ifdef USE_SWITCH
102 SUB_SWITCH(bluetooth)
103 SUB_SWITCH(multi_target)
104#endif
105#ifdef USE_BUTTON
106 SUB_BUTTON(factory_reset)
107 SUB_BUTTON(restart)
108#endif
109
110 public:
111 void setup() override;
112 void dump_config() override;
113 void loop() override;
114 void set_presence_timeout();
115 void read_all_info();
116 void query_zone_info();
117 void restart_and_read_all_info();
118 void set_bluetooth(bool enable);
119 void set_multi_target(bool enable);
120 void set_baud_rate(const char *state);
121 void set_zone_type(const char *state);
122 void publish_zone_type();
123 void factory_reset();
124#ifdef USE_TEXT_SENSOR
125 void set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s);
126#endif
127#ifdef USE_NUMBER
128 void set_zone_coordinate(uint8_t zone);
129 void set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2, number::Number *y2);
130#endif
131#ifdef USE_SENSOR
132 void set_move_x_sensor(uint8_t target, sensor::Sensor *s);
133 void set_move_y_sensor(uint8_t target, sensor::Sensor *s);
134 void set_move_speed_sensor(uint8_t target, sensor::Sensor *s);
135 void set_move_angle_sensor(uint8_t target, sensor::Sensor *s);
136 void set_move_distance_sensor(uint8_t target, sensor::Sensor *s);
137 void set_move_resolution_sensor(uint8_t target, sensor::Sensor *s);
138 void set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s);
139 void set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s);
140 void set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s);
141#endif
142 void reset_radar_zone();
143 void set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2, int32_t zone1_y2,
144 int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1,
145 int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2);
146
148 void add_on_data_callback(std::function<void()> &&callback);
149
150 protected:
151 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
152 void set_config_mode_(bool enable);
154 bool handle_ack_data_();
155 void process_zone_();
156 void readline_(int readch);
157 void get_version_();
158 void get_mac_();
160 void query_zone_();
161 void restart_();
163 void save_to_flash_(float value);
164 float restore_from_flash_();
165 bool get_timeout_status_(uint32_t check_millis);
166 uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving);
167
168 uint32_t presence_millis_ = 0;
171 uint16_t timeout_ = 5;
172 uint8_t buffer_data_[MAX_LINE_LENGTH];
173 uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
174 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
175 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
176 uint8_t zone_type_ = 0;
177 bool bluetooth_on_{false};
178 Target target_info_[MAX_TARGETS];
179 Zone zone_config_[MAX_ZONES];
180
181#ifdef USE_NUMBER
182 ESPPreferenceObject pref_; // only used when numbers are in use
184#endif
185#ifdef USE_SENSOR
186 std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_x_sensors_{};
187 std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_y_sensors_{};
188 std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_speed_sensors_{};
189 std::array<SensorWithDedup<float> *, MAX_TARGETS> move_angle_sensors_{};
190 std::array<SensorWithDedup<uint16_t> *, MAX_TARGETS> move_distance_sensors_{};
191 std::array<SensorWithDedup<uint16_t> *, MAX_TARGETS> move_resolution_sensors_{};
192 std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_target_count_sensors_{};
193 std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_still_target_count_sensors_{};
194 std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_moving_target_count_sensors_{};
195#endif
196#ifdef USE_TEXT_SENSOR
197 std::array<text_sensor::TextSensor *, 3> direction_text_sensors_{};
198#endif
199
201};
202
203class LD2450DataTrigger : public Trigger<> {
204 public:
206 parent->add_on_data_callback([this]() { this->trigger(); });
207 }
208};
209
210} // namespace esphome::ld2450
virtual void setup()
Where the component's initialization should happen.
void trigger(const Ts &...x)
Definition automation.h:279
void save_to_flash_(float value)
Definition ld2450.cpp:959
uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving)
Definition ld2450.cpp:296
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_speed_sensors_
Definition ld2450.h:188
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_x_sensors_
Definition ld2450.h:186
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2450.cpp:421
std::array< text_sensor::TextSensor *, 3 > direction_text_sensors_
Definition ld2450.h:197
std::array< SensorWithDedup< float > *, MAX_TARGETS > move_angle_sensors_
Definition ld2450.h:189
void set_config_mode_(bool enable)
Definition ld2450.cpp:806
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_still_target_count_sensors_
Definition ld2450.h:193
std::array< SensorWithDedup< uint16_t > *, MAX_TARGETS > move_resolution_sensors_
Definition ld2450.h:191
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_target_count_sensors_
Definition ld2450.h:192
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2450.h:172
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_moving_target_count_sensors_
Definition ld2450.h:194
Target target_info_[MAX_TARGETS]
Definition ld2450.h:178
Zone zone_config_[MAX_ZONES]
Definition ld2450.h:179
std::array< SensorWithDedup< uint16_t > *, MAX_TARGETS > move_distance_sensors_
Definition ld2450.h:190
ESPPreferenceObject pref_
Definition ld2450.h:182
LazyCallbackManager< void()> data_callback_
Definition ld2450.h:200
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_y_sensors_
Definition ld2450.h:187
ZoneOfNumbers zone_numbers_[MAX_ZONES]
Definition ld2450.h:183
bool get_timeout_status_(uint32_t check_millis)
Definition ld2450.cpp:353
LD2450DataTrigger(LD2450Component *parent)
Definition ld2450.h:205
Base-class for all numbers.
Definition number.h:29
Base-class for all sensors.
Definition sensor.h:43
bool state
Definition fan.h:2
@ DIRECTION_MOVING_AWAY
Definition ld2450.h:49
@ DIRECTION_APPROACHING
Definition ld2450.h:48
@ DIRECTION_UNDEFINED
Definition ld2450.h:52
@ DIRECTION_STATIONARY
Definition ld2450.h:50
number::Number * y2
Definition ld2450.h:75
number::Number * x2
Definition ld2450.h:74
number::Number * x1
Definition ld2450.h:72
number::Number * y1
Definition ld2450.h:73