ESPHome  2024.11.1
ld2410.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
4 #ifdef USE_BINARY_SENSOR
6 #endif
7 #ifdef USE_SENSOR
9 #endif
10 #ifdef USE_NUMBER
12 #endif
13 #ifdef USE_SWITCH
15 #endif
16 #ifdef USE_BUTTON
18 #endif
19 #ifdef USE_SELECT
21 #endif
22 #ifdef USE_TEXT_SENSOR
24 #endif
27 #include "esphome/core/helpers.h"
28 
29 #include <map>
30 
31 namespace esphome {
32 namespace ld2410 {
33 
34 #define CHECK_BIT(var, pos) (((var) >> (pos)) & 1)
35 
36 // Commands
37 static const uint8_t CMD_ENABLE_CONF = 0x00FF;
38 static const uint8_t CMD_DISABLE_CONF = 0x00FE;
39 static const uint8_t CMD_ENABLE_ENG = 0x0062;
40 static const uint8_t CMD_DISABLE_ENG = 0x0063;
41 static const uint8_t CMD_MAXDIST_DURATION = 0x0060;
42 static const uint8_t CMD_QUERY = 0x0061;
43 static const uint8_t CMD_GATE_SENS = 0x0064;
44 static const uint8_t CMD_VERSION = 0x00A0;
45 static const uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0x00AB;
46 static const uint8_t CMD_SET_DISTANCE_RESOLUTION = 0x00AA;
47 static const uint8_t CMD_QUERY_LIGHT_CONTROL = 0x00AE;
48 static const uint8_t CMD_SET_LIGHT_CONTROL = 0x00AD;
49 static const uint8_t CMD_SET_BAUD_RATE = 0x00A1;
50 static const uint8_t CMD_BT_PASSWORD = 0x00A9;
51 static const uint8_t CMD_MAC = 0x00A5;
52 static const uint8_t CMD_RESET = 0x00A2;
53 static const uint8_t CMD_RESTART = 0x00A3;
54 static const uint8_t CMD_BLUETOOTH = 0x00A4;
55 
56 enum BaudRateStructure : uint8_t {
65 };
66 
67 static const std::map<std::string, uint8_t> BAUD_RATE_ENUM_TO_INT{
68  {"9600", BAUD_RATE_9600}, {"19200", BAUD_RATE_19200}, {"38400", BAUD_RATE_38400},
69  {"57600", BAUD_RATE_57600}, {"115200", BAUD_RATE_115200}, {"230400", BAUD_RATE_230400},
70  {"256000", BAUD_RATE_256000}, {"460800", BAUD_RATE_460800}};
71 
73 
74 static const std::map<std::string, uint8_t> DISTANCE_RESOLUTION_ENUM_TO_INT{{"0.2m", DISTANCE_RESOLUTION_0_2},
75  {"0.75m", DISTANCE_RESOLUTION_0_75}};
76 static const std::map<uint8_t, std::string> DISTANCE_RESOLUTION_INT_TO_ENUM{{DISTANCE_RESOLUTION_0_2, "0.2m"},
77  {DISTANCE_RESOLUTION_0_75, "0.75m"}};
78 
79 enum LightFunctionStructure : uint8_t {
83 };
84 
85 static const std::map<std::string, uint8_t> LIGHT_FUNCTION_ENUM_TO_INT{
86  {"off", LIGHT_FUNCTION_OFF}, {"below", LIGHT_FUNCTION_BELOW}, {"above", LIGHT_FUNCTION_ABOVE}};
87 static const std::map<uint8_t, std::string> LIGHT_FUNCTION_INT_TO_ENUM{
88  {LIGHT_FUNCTION_OFF, "off"}, {LIGHT_FUNCTION_BELOW, "below"}, {LIGHT_FUNCTION_ABOVE, "above"}};
89 
90 enum OutPinLevelStructure : uint8_t { OUT_PIN_LEVEL_LOW = 0x00, OUT_PIN_LEVEL_HIGH = 0x01 };
91 
92 static const std::map<std::string, uint8_t> OUT_PIN_LEVEL_ENUM_TO_INT{{"low", OUT_PIN_LEVEL_LOW},
93  {"high", OUT_PIN_LEVEL_HIGH}};
94 static const std::map<uint8_t, std::string> OUT_PIN_LEVEL_INT_TO_ENUM{{OUT_PIN_LEVEL_LOW, "low"},
95  {OUT_PIN_LEVEL_HIGH, "high"}};
96 
97 // Commands values
98 static const uint8_t CMD_MAX_MOVE_VALUE = 0x0000;
99 static const uint8_t CMD_MAX_STILL_VALUE = 0x0001;
100 static const uint8_t CMD_DURATION_VALUE = 0x0002;
101 // Command Header & Footer
102 static const uint8_t CMD_FRAME_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA};
103 static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01};
104 // Data Header & Footer
105 static const uint8_t DATA_FRAME_HEADER[4] = {0xF4, 0xF3, 0xF2, 0xF1};
106 static const uint8_t DATA_FRAME_END[4] = {0xF8, 0xF7, 0xF6, 0xF5};
107 /*
108 Data Type: 6th byte
109 Target states: 9th byte
110  Moving target distance: 10~11th bytes
111  Moving target energy: 12th byte
112  Still target distance: 13~14th bytes
113  Still target energy: 15th byte
114  Detect distance: 16~17th bytes
115 */
116 enum PeriodicDataStructure : uint8_t {
131 };
132 enum PeriodicDataValue : uint8_t { HEAD = 0XAA, END = 0x55, CHECK = 0x00 };
133 
134 enum AckDataStructure : uint8_t { COMMAND = 6, COMMAND_STATUS = 7 };
135 
136 // char cmd[2] = {enable ? 0xFF : 0xFE, 0x00};
138 #ifdef USE_SENSOR
139  SUB_SENSOR(moving_target_distance)
140  SUB_SENSOR(still_target_distance)
141  SUB_SENSOR(moving_target_energy)
142  SUB_SENSOR(still_target_energy)
143  SUB_SENSOR(light)
144  SUB_SENSOR(detection_distance)
145 #endif
146 #ifdef USE_BINARY_SENSOR
147  SUB_BINARY_SENSOR(target)
148  SUB_BINARY_SENSOR(moving_target)
149  SUB_BINARY_SENSOR(still_target)
150  SUB_BINARY_SENSOR(out_pin_presence_status)
151 #endif
152 #ifdef USE_TEXT_SENSOR
153  SUB_TEXT_SENSOR(version)
154  SUB_TEXT_SENSOR(mac)
155 #endif
156 #ifdef USE_SELECT
157  SUB_SELECT(distance_resolution)
158  SUB_SELECT(baud_rate)
159  SUB_SELECT(light_function)
160  SUB_SELECT(out_pin_level)
161 #endif
162 #ifdef USE_SWITCH
163  SUB_SWITCH(engineering_mode)
164  SUB_SWITCH(bluetooth)
165 #endif
166 #ifdef USE_BUTTON
167  SUB_BUTTON(reset)
168  SUB_BUTTON(restart)
169  SUB_BUTTON(query)
170 #endif
171 #ifdef USE_NUMBER
172  SUB_NUMBER(max_still_distance_gate)
173  SUB_NUMBER(max_move_distance_gate)
174  SUB_NUMBER(timeout)
175  SUB_NUMBER(light_threshold)
176 #endif
177 
178  public:
179  LD2410Component();
180  void setup() override;
181  void dump_config() override;
182  void loop() override;
183  void set_light_out_control();
184 #ifdef USE_NUMBER
185  void set_gate_still_threshold_number(int gate, number::Number *n);
186  void set_gate_move_threshold_number(int gate, number::Number *n);
187  void set_max_distances_timeout();
188  void set_gate_threshold(uint8_t gate);
189 #endif
190 #ifdef USE_SENSOR
191  void set_gate_move_sensor(int gate, sensor::Sensor *s);
192  void set_gate_still_sensor(int gate, sensor::Sensor *s);
193 #endif
194  void set_throttle(uint16_t value) { this->throttle_ = value; };
195  void set_bluetooth_password(const std::string &password);
196  void set_engineering_mode(bool enable);
197  void read_all_info();
198  void restart_and_read_all_info();
199  void set_bluetooth(bool enable);
200  void set_distance_resolution(const std::string &state);
201  void set_baud_rate(const std::string &state);
202  void factory_reset();
203 
204  protected:
205  int two_byte_to_int_(char firstbyte, char secondbyte) { return (int16_t) (secondbyte << 8) + firstbyte; }
206  void send_command_(uint8_t command_str, const uint8_t *command_value, int command_value_len);
207  void set_config_mode_(bool enable);
208  void handle_periodic_data_(uint8_t *buffer, int len);
209  bool handle_ack_data_(uint8_t *buffer, int len);
210  void readline_(int readch, uint8_t *buffer, int len);
211  void query_parameters_();
212  void get_version_();
213  void get_mac_();
215  void get_light_control_();
216  void restart_();
217 
220  uint16_t throttle_;
221  std::string version_;
222  std::string mac_;
223  std::string out_pin_level_;
224  std::string light_function_;
225  float light_threshold_ = -1;
226 #ifdef USE_NUMBER
227  std::vector<number::Number *> gate_still_threshold_numbers_ = std::vector<number::Number *>(9);
228  std::vector<number::Number *> gate_move_threshold_numbers_ = std::vector<number::Number *>(9);
229 #endif
230 #ifdef USE_SENSOR
231  std::vector<sensor::Sensor *> gate_still_sensors_ = std::vector<sensor::Sensor *>(9);
232  std::vector<sensor::Sensor *> gate_move_sensors_ = std::vector<sensor::Sensor *>(9);
233 #endif
234 };
235 
236 } // namespace ld2410
237 } // namespace esphome
void set_config_mode_(bool enable)
Definition: ld2410.cpp:506
DistanceResolutionStructure
Definition: ld2410.h:72
void readline_(int readch, uint8_t *buffer, int len)
Definition: ld2410.cpp:478
LightFunctionStructure
Definition: ld2410.h:79
std::vector< number::Number * > gate_move_threshold_numbers_
Definition: ld2410.h:228
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
int32_t last_engineering_mode_change_millis_
Definition: ld2410.h:219
Base-class for all numbers.
Definition: number.h:39
std::vector< number::Number * > gate_still_threshold_numbers_
Definition: ld2410.h:227
uint16_t reset
Definition: ina226.h:39
bool handle_ack_data_(uint8_t *buffer, int len)
Definition: ld2410.cpp:329
std::vector< sensor::Sensor * > gate_move_sensors_
Definition: ld2410.h:232
OutPinLevelStructure
Definition: ld2410.h:90
std::string size_t len
Definition: helpers.h:293
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< sensor::Sensor * > gate_still_sensors_
Definition: ld2410.h:231
void send_command_(uint8_t command_str, const uint8_t *command_value, int command_value_len)
Definition: ld2410.cpp:119
int two_byte_to_int_(char firstbyte, char secondbyte)
Definition: ld2410.h:205
void handle_periodic_data_(uint8_t *buffer, int len)
Definition: ld2410.cpp:146
Base-class for all sensors.
Definition: sensor.h:57
bool state
Definition: fan.h:34