ESPHome 2026.1.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.
40static constexpr uint8_t MAX_LINE_LENGTH = 41; // Max characters for serial buffer
41static constexpr uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450
42static constexpr uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450
43
51
52// Target coordinate struct
53struct Target {
54 int16_t x;
55 int16_t y;
57};
58
59// Zone coordinate struct
60struct Zone {
61 int16_t x1 = 0;
62 int16_t y1 = 0;
63 int16_t x2 = 0;
64 int16_t y2 = 0;
65};
66
67#ifdef USE_NUMBER
69 number::Number *x1 = nullptr;
70 number::Number *y1 = nullptr;
71 number::Number *x2 = nullptr;
72 number::Number *y2 = nullptr;
73};
74#endif
75
77#ifdef USE_BINARY_SENSOR
78 SUB_BINARY_SENSOR(moving_target)
79 SUB_BINARY_SENSOR(still_target)
80 SUB_BINARY_SENSOR(target)
81#endif
82#ifdef USE_SENSOR
83 SUB_SENSOR_WITH_DEDUP(moving_target_count, uint8_t)
84 SUB_SENSOR_WITH_DEDUP(still_target_count, uint8_t)
85 SUB_SENSOR_WITH_DEDUP(target_count, uint8_t)
86#endif
87#ifdef USE_TEXT_SENSOR
88 SUB_TEXT_SENSOR(mac)
89 SUB_TEXT_SENSOR(version)
90#endif
91#ifdef USE_NUMBER
92 SUB_NUMBER(presence_timeout)
93#endif
94#ifdef USE_SELECT
95 SUB_SELECT(baud_rate)
96 SUB_SELECT(zone_type)
97#endif
98#ifdef USE_SWITCH
99 SUB_SWITCH(bluetooth)
100 SUB_SWITCH(multi_target)
101#endif
102#ifdef USE_BUTTON
103 SUB_BUTTON(factory_reset)
104 SUB_BUTTON(restart)
105#endif
106
107 public:
108 void setup() override;
109 void dump_config() override;
110 void loop() override;
111 void set_presence_timeout();
112 void read_all_info();
113 void query_zone_info();
114 void restart_and_read_all_info();
115 void set_bluetooth(bool enable);
116 void set_multi_target(bool enable);
117 void set_baud_rate(const char *state);
118 void set_zone_type(const char *state);
119 void publish_zone_type();
120 void factory_reset();
121#ifdef USE_TEXT_SENSOR
122 void set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s);
123#endif
124#ifdef USE_NUMBER
125 void set_zone_coordinate(uint8_t zone);
126 void set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2, number::Number *y2);
127#endif
128#ifdef USE_SENSOR
129 void set_move_x_sensor(uint8_t target, sensor::Sensor *s);
130 void set_move_y_sensor(uint8_t target, sensor::Sensor *s);
131 void set_move_speed_sensor(uint8_t target, sensor::Sensor *s);
132 void set_move_angle_sensor(uint8_t target, sensor::Sensor *s);
133 void set_move_distance_sensor(uint8_t target, sensor::Sensor *s);
134 void set_move_resolution_sensor(uint8_t target, sensor::Sensor *s);
135 void set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s);
136 void set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s);
137 void set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s);
138#endif
139 void reset_radar_zone();
140 void set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2, int32_t zone1_y2,
141 int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1,
142 int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2);
143
144 protected:
145 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
146 void set_config_mode_(bool enable);
148 bool handle_ack_data_();
149 void process_zone_();
150 void readline_(int readch);
151 void get_version_();
152 void get_mac_();
154 void query_zone_();
155 void restart_();
157 void save_to_flash_(float value);
158 float restore_from_flash_();
159 bool get_timeout_status_(uint32_t check_millis);
160 uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving);
161
162 uint32_t presence_millis_ = 0;
165 uint16_t timeout_ = 5;
166 uint8_t buffer_data_[MAX_LINE_LENGTH];
167 uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
168 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
169 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
170 uint8_t zone_type_ = 0;
171 bool bluetooth_on_{false};
172 Target target_info_[MAX_TARGETS];
173 Zone zone_config_[MAX_ZONES];
174
175#ifdef USE_NUMBER
176 ESPPreferenceObject pref_; // only used when numbers are in use
178#endif
179#ifdef USE_SENSOR
180 std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_x_sensors_{};
181 std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_y_sensors_{};
182 std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_speed_sensors_{};
183 std::array<SensorWithDedup<float> *, MAX_TARGETS> move_angle_sensors_{};
184 std::array<SensorWithDedup<uint16_t> *, MAX_TARGETS> move_distance_sensors_{};
185 std::array<SensorWithDedup<uint16_t> *, MAX_TARGETS> move_resolution_sensors_{};
186 std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_target_count_sensors_{};
187 std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_still_target_count_sensors_{};
188 std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_moving_target_count_sensors_{};
189#endif
190#ifdef USE_TEXT_SENSOR
191 std::array<text_sensor::TextSensor *, 3> direction_text_sensors_{};
192#endif
193};
194
195} // namespace esphome::ld2450
virtual void setup()
Where the component's initialization should happen.
void save_to_flash_(float value)
Definition ld2450.cpp:929
uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving)
Definition ld2450.cpp:282
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_speed_sensors_
Definition ld2450.h:182
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_x_sensors_
Definition ld2450.h:180
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2450.cpp:402
std::array< text_sensor::TextSensor *, 3 > direction_text_sensors_
Definition ld2450.h:191
std::array< SensorWithDedup< float > *, MAX_TARGETS > move_angle_sensors_
Definition ld2450.h:183
void set_config_mode_(bool enable)
Definition ld2450.cpp:775
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_still_target_count_sensors_
Definition ld2450.h:187
std::array< SensorWithDedup< uint16_t > *, MAX_TARGETS > move_resolution_sensors_
Definition ld2450.h:185
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_target_count_sensors_
Definition ld2450.h:186
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2450.h:166
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_moving_target_count_sensors_
Definition ld2450.h:188
Target target_info_[MAX_TARGETS]
Definition ld2450.h:172
Zone zone_config_[MAX_ZONES]
Definition ld2450.h:173
std::array< SensorWithDedup< uint16_t > *, MAX_TARGETS > move_distance_sensors_
Definition ld2450.h:184
ESPPreferenceObject pref_
Definition ld2450.h:176
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_y_sensors_
Definition ld2450.h:181
ZoneOfNumbers zone_numbers_[MAX_ZONES]
Definition ld2450.h:177
bool get_timeout_status_(uint32_t check_millis)
Definition ld2450.cpp:339
Base-class for all numbers.
Definition number.h:29
Base-class for all sensors.
Definition sensor.h:43
bool state
Definition fan.h:0
@ DIRECTION_MOVING_AWAY
Definition ld2450.h:46
@ DIRECTION_APPROACHING
Definition ld2450.h:45
@ DIRECTION_UNDEFINED
Definition ld2450.h:49
@ DIRECTION_STATIONARY
Definition ld2450.h:47
number::Number * y2
Definition ld2450.h:72
number::Number * x2
Definition ld2450.h:71
number::Number * x1
Definition ld2450.h:69
number::Number * y1
Definition ld2450.h:70