ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld2410.h
Go to the documentation of this file.
1#pragma once
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
28
29#include <array>
30
31namespace esphome::ld2410 {
32
33using namespace ld24xx;
34
35// Engineering data frame is 45 bytes; +1 for null terminator, +4 so that a frame footer always
36// lands inside the buffer during footer-based resynchronization after losing sync.
37static constexpr uint8_t MAX_LINE_LENGTH = 50;
38static constexpr uint8_t TOTAL_GATES = 9; // Total number of gates supported by the LD2410
39
50
55
56// Device values of the distance resolution select options, in option order; in flash on ESP8266
57static constexpr uint8_t DISTANCE_RESOLUTIONS[] PROGMEM = {DISTANCE_RESOLUTION_0_2, DISTANCE_RESOLUTION_0_75};
58
59#ifdef USE_LD2410_BG_CORRECTION
60// Order matches BgCorrectionResultStrings
69#endif
70
71class LD2410Component final : public Component, public uart::UARTDevice {
72#ifdef USE_BINARY_SENSOR
73 SUB_BINARY_SENSOR(out_pin_presence_status)
74 SUB_BINARY_SENSOR(moving_target)
75 SUB_BINARY_SENSOR(still_target)
76 SUB_BINARY_SENSOR(target)
77 SUB_BINARY_SENSOR(calibration)
78#endif
79#ifdef USE_SENSOR
80 SUB_SENSOR_WITH_DEDUP(light, uint8_t)
81 SUB_SENSOR_WITH_DEDUP(detection_distance, int)
82 SUB_SENSOR_WITH_DEDUP(moving_target_distance, int)
83 SUB_SENSOR_WITH_DEDUP(moving_target_energy, uint8_t)
84 SUB_SENSOR_WITH_DEDUP(still_target_distance, int)
85 SUB_SENSOR_WITH_DEDUP(still_target_energy, uint8_t)
86#endif
87#ifdef USE_TEXT_SENSOR
88 SUB_TEXT_SENSOR(version)
89 SUB_TEXT_SENSOR(mac)
90 SUB_TEXT_SENSOR(calibration_result)
91#endif
92#ifdef USE_NUMBER
93 SUB_NUMBER(light_threshold)
94 SUB_NUMBER(max_move_distance_gate)
95 SUB_NUMBER(max_still_distance_gate)
96 SUB_NUMBER(timeout)
97#endif
98#ifdef USE_SELECT
99 SUB_SELECT(baud_rate)
100 SUB_SELECT(distance_resolution)
101 SUB_SELECT(light_function)
102 SUB_SELECT(out_pin_level)
103#endif
104#ifdef USE_SWITCH
105 SUB_SWITCH(bluetooth)
106 SUB_SWITCH(engineering_mode)
107#endif
108#ifdef USE_BUTTON
109 SUB_BUTTON(factory_reset)
110 SUB_BUTTON(query)
111 SUB_BUTTON(restart)
112 SUB_BUTTON(background_correction)
113#endif
114
115 public:
116 void setup() override;
117 void dump_config() override;
118 void loop() override;
119 void set_light_out_control();
120#ifdef USE_NUMBER
121 void set_gate_still_threshold_number(uint8_t gate, number::Number *n);
122 void set_gate_move_threshold_number(uint8_t gate, number::Number *n);
123 void set_max_distances_timeout();
124 void set_gate_threshold(number::Number *n);
125#endif
126#ifdef USE_SENSOR
127 void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
128 void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
129#endif
130 void set_bluetooth_password(const std::string &password);
131 void set_engineering_mode(bool enable);
132 void read_all_info();
133 void restart_and_read_all_info();
134 void set_bluetooth(bool enable);
135 void set_distance_resolution(const char *state);
136 void set_distance_resolution(DistanceResolution resolution);
137 void set_baud_rate(const char *state);
138 void set_baud_rate(BaudRate rate);
139 void factory_reset();
140#ifdef USE_LD2410_BG_CORRECTION
141 void set_background_correction_duration(uint8_t seconds) { this->bg_correction_duration_ = seconds; }
142 void start_background_correction();
143#endif
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 void handle_ack_data_();
150 bool handle_frame_(uint8_t frame_len);
151 void readline_(int readch);
152 void query_parameters_();
153 void get_version_();
154 void get_mac_();
157 void restart_();
158#ifdef USE_LD2410_BG_CORRECTION
160 void publish_calibration_running_(bool running);
162#endif
163
164 uint8_t light_function_ = 0;
165 uint8_t light_threshold_ = 0;
166 uint8_t out_pin_level_ = 0;
167 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
168 uint8_t buffer_data_[MAX_LINE_LENGTH];
169 uint8_t mac_address_[MAC_ADDRESS_SIZE] = {0, 0, 0, 0, 0, 0};
170 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
171 bool bluetooth_on_{false};
172#ifdef USE_LD2410_BG_CORRECTION
173 uint8_t bg_correction_duration_{10}; // seconds
176#endif
177#ifdef USE_NUMBER
178 std::array<number::Number *, TOTAL_GATES> gate_move_threshold_numbers_{};
179 std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
180#endif
181#ifdef USE_SENSOR
182 std::array<sensor::SensorWithDedup<uint8_t>, TOTAL_GATES> gate_move_sensors_{};
183 std::array<sensor::SensorWithDedup<uint8_t>, TOTAL_GATES> gate_still_sensors_{};
184#endif
185};
186
187} // namespace esphome::ld2410
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
std::array< number::Number *, TOTAL_GATES > gate_still_threshold_numbers_
Definition ld2410.h:179
void publish_calibration_running_(bool running)
Definition ld2410.cpp:709
BgCorrectionResult bg_correction_result_
Definition ld2410.h:175
void set_config_mode_(bool enable)
Definition ld2410.cpp:575
std::array< sensor::SensorWithDedup< uint8_t >, TOTAL_GATES > gate_move_sensors_
Definition ld2410.h:182
std::array< number::Number *, TOTAL_GATES > gate_move_threshold_numbers_
Definition ld2410.h:178
std::array< sensor::SensorWithDedup< uint8_t >, TOTAL_GATES > gate_still_sensors_
Definition ld2410.h:183
void finish_background_correction_(BgCorrectionResult result)
Definition ld2410.cpp:691
uint8_t mac_address_[MAC_ADDRESS_SIZE]
Definition ld2410.h:169
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2410.cpp:237
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2410.h:168
void publish_calibration_result_(BgCorrectionResult result)
Definition ld2410.cpp:715
bool handle_frame_(uint8_t frame_len)
Definition ld2410.cpp:556
Base-class for all numbers.
Definition number.h:29
Base-class for all sensors.
Definition sensor.h:48
bool state
Definition fan.h:2
Resolution resolution
Definition msa3xx.h:1
@ DISTANCE_RESOLUTION_0_2
Definition ld2410.h:52
@ DISTANCE_RESOLUTION_0_75
Definition ld2410.h:53