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