ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld2412.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
29
30#include <array>
31
32namespace esphome::ld2412 {
33
34using namespace ld24xx;
35
36static constexpr uint8_t MAX_LINE_LENGTH = 54; // Max characters for serial buffer
37static constexpr uint8_t TOTAL_GATES = 14; // Total number of gates supported by the LD2412
38static constexpr uint16_t DEFAULT_PRESENCE_TIMEOUT = 5;
39
50
56
57// Device values of the distance resolution select options, in option order; in flash on ESP8266
58static constexpr uint8_t DISTANCE_RESOLUTIONS[] PROGMEM = {DISTANCE_RESOLUTION_0_2, DISTANCE_RESOLUTION_0_5,
60
61class LD2412Component final : public Component, public uart::UARTDevice {
62#ifdef USE_BINARY_SENSOR
63 SUB_BINARY_SENSOR(dynamic_background_correction_status)
64 SUB_BINARY_SENSOR(moving_target)
65 SUB_BINARY_SENSOR(still_target)
66 SUB_BINARY_SENSOR(target)
67#endif
68#ifdef USE_SENSOR
69 SUB_SENSOR_WITH_DEDUP(light, uint8_t)
70 SUB_SENSOR_WITH_DEDUP(detection_distance, int)
71 SUB_SENSOR_WITH_DEDUP(moving_target_distance, int)
72 SUB_SENSOR_WITH_DEDUP(moving_target_energy, uint8_t)
73 SUB_SENSOR_WITH_DEDUP(still_target_distance, int)
74 SUB_SENSOR_WITH_DEDUP(still_target_energy, uint8_t)
75#endif
76#ifdef USE_TEXT_SENSOR
77 SUB_TEXT_SENSOR(mac)
78 SUB_TEXT_SENSOR(version)
79#endif
80#ifdef USE_NUMBER
81 SUB_NUMBER(light_threshold)
82 SUB_NUMBER(max_distance_gate)
83 SUB_NUMBER(min_distance_gate)
84 SUB_NUMBER(timeout)
85#endif
86#ifdef USE_SELECT
87 SUB_SELECT(baud_rate)
88 SUB_SELECT(distance_resolution)
89 SUB_SELECT(light_function)
90 SUB_SELECT(out_pin_level)
91#endif
92#ifdef USE_SWITCH
93 SUB_SWITCH(bluetooth)
94 SUB_SWITCH(engineering_mode)
95#endif
96#ifdef USE_BUTTON
97 SUB_BUTTON(factory_reset)
98 SUB_BUTTON(query)
99 SUB_BUTTON(restart)
100 SUB_BUTTON(start_dynamic_background_correction)
101#endif
102
103 public:
104 void setup() override;
105 void dump_config() override;
106 void loop() override;
107 void set_light_out_control();
108 void set_basic_config();
109#ifdef USE_NUMBER
110 void set_gate_move_threshold_number(uint8_t gate, number::Number *n) {
111 this->gate_move_thresholds_.numbers[gate] = n;
112 }
113 void set_gate_still_threshold_number(uint8_t gate, number::Number *n) {
114 this->gate_still_thresholds_.numbers[gate] = n;
115 }
116 void set_gate_threshold(number::Number *n);
117 void get_gate_threshold();
118#endif
119#ifdef USE_SENSOR
120 void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
121 void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
122#endif
123 void set_engineering_mode(bool enable);
124 void read_all_info();
125 void restart_and_read_all_info();
126 void set_bluetooth(bool enable);
127 void set_distance_resolution(const char *state);
128 void set_distance_resolution(DistanceResolution resolution);
129 void set_baud_rate(const char *state);
130 void set_baud_rate(BaudRate rate);
131 void factory_reset();
132 void start_dynamic_background_correction();
133
134 protected:
135 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
136 void set_config_mode_(bool enable);
138 void handle_ack_data_();
139 bool handle_frame_(uint8_t frame_len);
140 void basic_conf_timeout_();
141 void readline_(int readch);
142 void query_parameters_();
143 void get_version_();
144 void get_mac_();
147 void restart_();
149#ifdef USE_NUMBER
150 // One command carries all 14 gates, so unconfigured gates get the module's last value
152 std::array<number::Number *, TOTAL_GATES> numbers{};
153 std::array<uint8_t, TOTAL_GATES> last_read{};
154 bool read{false}; // answered since boot or the last factory reset
155 };
157 return motion ? this->gate_move_thresholds_ : this->gate_still_thresholds_;
158 }
159 void send_gate_thresholds_(bool motion);
160 void gate_query_timeout_(bool motion);
161#endif
162
163 uint8_t light_function_ = 0;
164 uint8_t light_threshold_ = 0;
165 // Basic configuration as last reported by the module; resent for entities that are not configured
166 bool basic_conf_read_{false}; // answered since boot or the last factory reset
167 uint16_t timeout_ = DEFAULT_PRESENCE_TIMEOUT;
168 uint8_t min_gate_ = 1;
169 uint8_t max_gate_ = TOTAL_GATES;
170 uint8_t out_pin_level_ = 1;
171 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
172 uint8_t buffer_data_[MAX_LINE_LENGTH];
173 uint8_t mac_address_[MAC_ADDRESS_SIZE] = {0, 0, 0, 0, 0, 0};
174 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
175 bool bluetooth_on_{false};
177#ifdef USE_NUMBER
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::ld2412
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
bool handle_frame_(uint8_t frame_len)
Definition ld2412.cpp:584
void set_config_mode_(bool enable)
Definition ld2412.cpp:603
void send_gate_thresholds_(bool motion)
Definition ld2412.cpp:777
std::array< sensor::SensorWithDedup< uint8_t >, TOTAL_GATES > gate_still_sensors_
Definition ld2412.h:183
GateThresholds & gate_thresholds_(bool motion)
Definition ld2412.h:156
std::array< sensor::SensorWithDedup< uint8_t >, TOTAL_GATES > gate_move_sensors_
Definition ld2412.h:182
void gate_query_timeout_(bool motion)
Definition ld2412.cpp:802
GateThresholds gate_still_thresholds_
Definition ld2412.h:179
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2412.h:172
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2412.cpp:240
uint8_t mac_address_[MAC_ADDRESS_SIZE]
Definition ld2412.h:173
GateThresholds gate_move_thresholds_
Definition ld2412.h:178
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_5
Definition ld2412.h:53
@ DISTANCE_RESOLUTION_0_2
Definition ld2412.h:52
@ DISTANCE_RESOLUTION_0_75
Definition ld2412.h:54
std::array< uint8_t, TOTAL_GATES > last_read
Definition ld2412.h:153
std::array< number::Number *, TOTAL_GATES > numbers
Definition ld2412.h:152