ESPHome  2025.3.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
ld2450.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iomanip>
4 #include <map>
7 #include "esphome/core/defines.h"
8 #include "esphome/core/helpers.h"
10 #ifdef USE_SENSOR
12 #endif
13 #ifdef USE_NUMBER
15 #endif
16 #ifdef USE_SWITCH
18 #endif
19 #ifdef USE_BUTTON
21 #endif
22 #ifdef USE_SELECT
24 #endif
25 #ifdef USE_TEXT_SENSOR
27 #endif
28 #ifdef USE_BINARY_SENSOR
30 #endif
31 
32 #ifndef M_PI
33 #define M_PI 3.14
34 #endif
35 
36 namespace esphome {
37 namespace ld2450 {
38 
39 // Constants
40 static const uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset presense status 5 sec.
41 static const uint8_t MAX_LINE_LENGTH = 60; // Max characters for serial buffer
42 static const uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450
43 static const uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450
44 
45 // Target coordinate struct
46 struct Target {
47  int16_t x;
48  int16_t y;
49  bool is_moving;
50 };
51 
52 // Zone coordinate struct
53 struct Zone {
54  int16_t x1 = 0;
55  int16_t y1 = 0;
56  int16_t x2 = 0;
57  int16_t y2 = 0;
58 };
59 
60 #ifdef USE_NUMBER
61 struct ZoneOfNumbers {
62  number::Number *x1 = nullptr;
63  number::Number *y1 = nullptr;
64  number::Number *x2 = nullptr;
65  number::Number *y2 = nullptr;
66 };
67 #endif
68 
69 enum BaudRateStructure : uint8_t {
78 };
79 
80 // Convert baud rate enum to int
81 static const std::map<std::string, uint8_t> BAUD_RATE_ENUM_TO_INT{
82  {"9600", BAUD_RATE_9600}, {"19200", BAUD_RATE_19200}, {"38400", BAUD_RATE_38400},
83  {"57600", BAUD_RATE_57600}, {"115200", BAUD_RATE_115200}, {"230400", BAUD_RATE_230400},
84  {"256000", BAUD_RATE_256000}, {"460800", BAUD_RATE_460800}};
85 
86 // Zone type struct
87 enum ZoneTypeStructure : uint8_t { ZONE_DISABLED = 0, ZONE_DETECTION = 1, ZONE_FILTER = 2 };
88 
89 // Convert zone type int to enum
90 static const std::map<ZoneTypeStructure, std::string> ZONE_TYPE_INT_TO_ENUM{
91  {ZONE_DISABLED, "Disabled"}, {ZONE_DETECTION, "Detection"}, {ZONE_FILTER, "Filter"}};
92 
93 // Convert zone type enum to int
94 static const std::map<std::string, uint8_t> ZONE_TYPE_ENUM_TO_INT{
95  {"Disabled", ZONE_DISABLED}, {"Detection", ZONE_DETECTION}, {"Filter", ZONE_FILTER}};
96 
97 // LD2450 serial command header & footer
98 static const uint8_t CMD_FRAME_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA};
99 static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01};
100 
101 enum PeriodicDataStructure : uint8_t {
102  TARGET_X = 4,
103  TARGET_Y = 6,
106 };
107 
108 enum PeriodicDataValue : uint8_t { HEAD = 0XAA, END = 0x55, CHECK = 0x00 };
109 
110 enum AckDataStructure : uint8_t { COMMAND = 6, COMMAND_STATUS = 7 };
111 
113 #ifdef USE_SENSOR
114  SUB_SENSOR(target_count)
115  SUB_SENSOR(still_target_count)
116  SUB_SENSOR(moving_target_count)
117 #endif
118 #ifdef USE_BINARY_SENSOR
119  SUB_BINARY_SENSOR(target)
120  SUB_BINARY_SENSOR(moving_target)
121  SUB_BINARY_SENSOR(still_target)
122 #endif
123 #ifdef USE_TEXT_SENSOR
124  SUB_TEXT_SENSOR(version)
125  SUB_TEXT_SENSOR(mac)
126 #endif
127 #ifdef USE_SELECT
128  SUB_SELECT(baud_rate)
129  SUB_SELECT(zone_type)
130 #endif
131 #ifdef USE_SWITCH
132  SUB_SWITCH(bluetooth)
133  SUB_SWITCH(multi_target)
134 #endif
135 #ifdef USE_BUTTON
136  SUB_BUTTON(reset)
137  SUB_BUTTON(restart)
138 #endif
139 #ifdef USE_NUMBER
140  SUB_NUMBER(presence_timeout)
141 #endif
142 
143  public:
144  LD2450Component();
145  void setup() override;
146  void dump_config() override;
147  void loop() override;
148  void set_presence_timeout();
149  void set_throttle(uint16_t value) { this->throttle_ = value; };
150  void read_all_info();
151  void query_zone_info();
152  void restart_and_read_all_info();
153  void set_bluetooth(bool enable);
154  void set_multi_target(bool enable);
155  void set_baud_rate(const std::string &state);
156  void set_zone_type(const std::string &state);
157  void publish_zone_type();
158  void factory_reset();
159 #ifdef USE_TEXT_SENSOR
160  void set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s);
161 #endif
162 #ifdef USE_NUMBER
163  void set_zone_coordinate(uint8_t zone);
164  void set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2, number::Number *y2);
165 #endif
166 #ifdef USE_SENSOR
167  void set_move_x_sensor(uint8_t target, sensor::Sensor *s);
168  void set_move_y_sensor(uint8_t target, sensor::Sensor *s);
169  void set_move_speed_sensor(uint8_t target, sensor::Sensor *s);
170  void set_move_angle_sensor(uint8_t target, sensor::Sensor *s);
171  void set_move_distance_sensor(uint8_t target, sensor::Sensor *s);
172  void set_move_resolution_sensor(uint8_t target, sensor::Sensor *s);
173  void set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s);
174  void set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s);
175  void set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s);
176 #endif
177  void reset_radar_zone();
178  void set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2, int32_t zone1_y2,
179  int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1,
180  int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2);
181 
182  protected:
183  void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
184  void set_config_mode_(bool enable);
185  void handle_periodic_data_(uint8_t *buffer, uint8_t len);
186  bool handle_ack_data_(uint8_t *buffer, uint8_t len);
187  void process_zone_(uint8_t *buffer);
188  void readline_(int readch, uint8_t *buffer, uint8_t len);
189  void get_version_();
190  void get_mac_();
191  void query_target_tracking_mode_();
192  void query_zone_();
193  void restart_();
194  void send_set_zone_command_();
195  void save_to_flash_(float value);
196  float restore_from_flash_();
197  bool get_timeout_status_(uint32_t check_millis);
198  uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving);
199 
200  Target target_info_[MAX_TARGETS];
201  Zone zone_config_[MAX_ZONES];
202  uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
203  uint8_t buffer_data_[MAX_LINE_LENGTH];
204  uint32_t last_periodic_millis_ = 0;
205  uint32_t presence_millis_ = 0;
206  uint32_t still_presence_millis_ = 0;
207  uint32_t moving_presence_millis_ = 0;
208  uint16_t throttle_ = 0;
209  uint16_t timeout_ = 5;
210  uint8_t zone_type_ = 0;
211  std::string version_{};
212  std::string mac_{};
213 #ifdef USE_NUMBER
214  ESPPreferenceObject pref_; // only used when numbers are in use
215  ZoneOfNumbers zone_numbers_[MAX_ZONES];
216 #endif
217 #ifdef USE_SENSOR
218  std::vector<sensor::Sensor *> move_x_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
219  std::vector<sensor::Sensor *> move_y_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
220  std::vector<sensor::Sensor *> move_speed_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
221  std::vector<sensor::Sensor *> move_angle_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
222  std::vector<sensor::Sensor *> move_distance_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
223  std::vector<sensor::Sensor *> move_resolution_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
224  std::vector<sensor::Sensor *> zone_target_count_sensors_ = std::vector<sensor::Sensor *>(MAX_ZONES);
225  std::vector<sensor::Sensor *> zone_still_target_count_sensors_ = std::vector<sensor::Sensor *>(MAX_ZONES);
226  std::vector<sensor::Sensor *> zone_moving_target_count_sensors_ = std::vector<sensor::Sensor *>(MAX_ZONES);
227 #endif
228 #ifdef USE_TEXT_SENSOR
229  std::vector<text_sensor::TextSensor *> direction_text_sensors_ = std::vector<text_sensor::TextSensor *>(3);
230 #endif
231 };
232 
233 } // namespace ld2450
234 } // namespace esphome
void setup()
bool state
Definition: fan.h:34
void loop()
Base-class for all numbers.
Definition: number.h:39
uint16_t reset
Definition: ina226.h:39
std::string size_t len
Definition: helpers.h:301
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Base-class for all sensors.
Definition: sensor.h:57
ESPPreferenceObject pref_
Definition: ld2450.h:214