ESPHome 2026.11.0-dev
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
38
39class LD2412Component final : public Component, public uart::UARTDevice {
40#ifdef USE_BINARY_SENSOR
41 SUB_BINARY_SENSOR(dynamic_background_correction_status)
42 SUB_BINARY_SENSOR(moving_target)
43 SUB_BINARY_SENSOR(still_target)
44 SUB_BINARY_SENSOR(target)
45#endif
46#ifdef USE_SENSOR
47 SUB_SENSOR_WITH_DEDUP(light, uint8_t)
48 SUB_SENSOR_WITH_DEDUP(detection_distance, int)
49 SUB_SENSOR_WITH_DEDUP(moving_target_distance, int)
50 SUB_SENSOR_WITH_DEDUP(moving_target_energy, uint8_t)
51 SUB_SENSOR_WITH_DEDUP(still_target_distance, int)
52 SUB_SENSOR_WITH_DEDUP(still_target_energy, uint8_t)
53#endif
54#ifdef USE_TEXT_SENSOR
55 SUB_TEXT_SENSOR(mac)
56 SUB_TEXT_SENSOR(version)
57#endif
58#ifdef USE_NUMBER
59 SUB_NUMBER(light_threshold)
60 SUB_NUMBER(max_distance_gate)
61 SUB_NUMBER(min_distance_gate)
62 SUB_NUMBER(timeout)
63#endif
64#ifdef USE_SELECT
65 SUB_SELECT(baud_rate)
66 SUB_SELECT(distance_resolution)
67 SUB_SELECT(light_function)
68 SUB_SELECT(out_pin_level)
69#endif
70#ifdef USE_SWITCH
71 SUB_SWITCH(bluetooth)
72 SUB_SWITCH(engineering_mode)
73#endif
74#ifdef USE_BUTTON
75 SUB_BUTTON(factory_reset)
76 SUB_BUTTON(query)
77 SUB_BUTTON(restart)
78 SUB_BUTTON(start_dynamic_background_correction)
79#endif
80
81 public:
82 void setup() override;
83 void dump_config() override;
84 void loop() override;
85 void set_light_out_control();
86 void set_basic_config();
87#ifdef USE_NUMBER
88 void set_gate_move_threshold_number(uint8_t gate, number::Number *n) {
89 this->gate_move_thresholds_.numbers[gate] = n;
90 }
91 void set_gate_still_threshold_number(uint8_t gate, number::Number *n) {
92 this->gate_still_thresholds_.numbers[gate] = n;
93 }
94 void set_gate_threshold(number::Number *n);
95 void get_gate_threshold();
96#endif
97#ifdef USE_SENSOR
98 void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
99 void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
100#endif
101 void set_engineering_mode(bool enable);
102 void read_all_info();
103 void restart_and_read_all_info();
104 void set_bluetooth(bool enable);
105 void set_distance_resolution(const char *state);
106 void set_baud_rate(const char *state);
107 void factory_reset();
108 void start_dynamic_background_correction();
109
110 protected:
111 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
112 void set_config_mode_(bool enable);
114 bool handle_ack_data_();
115 void readline_(int readch);
116 void query_parameters_();
117 void get_version_();
118 void get_mac_();
121 void restart_();
123#ifdef USE_NUMBER
124 // One command carries all 14 gates, so unconfigured gates get the module's last value
126 std::array<number::Number *, TOTAL_GATES> numbers{};
127 std::array<uint8_t, TOTAL_GATES> last_read{};
128 bool read{false}; // answered since boot or the last factory reset
129 };
131 return motion ? this->gate_move_thresholds_ : this->gate_still_thresholds_;
132 }
133 void send_gate_thresholds_(bool motion);
134 void gate_query_timeout_(bool motion);
135#endif
136
137 uint8_t light_function_ = 0;
138 uint8_t light_threshold_ = 0;
139 uint8_t out_pin_level_ = 0;
140 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
141 uint8_t buffer_data_[MAX_LINE_LENGTH];
142 uint8_t mac_address_[MAC_ADDRESS_SIZE] = {0, 0, 0, 0, 0, 0};
143 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
144 bool bluetooth_on_{false};
146#ifdef USE_NUMBER
149#endif
150#ifdef USE_SENSOR
151 std::array<SensorWithDedup<uint8_t>, TOTAL_GATES> gate_move_sensors_{};
152 std::array<SensorWithDedup<uint8_t>, TOTAL_GATES> gate_still_sensors_{};
153#endif
154};
155
156} // namespace esphome::ld2412
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
void set_config_mode_(bool enable)
Definition ld2412.cpp:718
void send_gate_thresholds_(bool motion)
Definition ld2412.cpp:837
std::array< SensorWithDedup< uint8_t >, TOTAL_GATES > gate_move_sensors_
Definition ld2412.h:151
GateThresholds & gate_thresholds_(bool motion)
Definition ld2412.h:130
void gate_query_timeout_(bool motion)
Definition ld2412.cpp:862
GateThresholds gate_still_thresholds_
Definition ld2412.h:148
std::array< SensorWithDedup< uint8_t >, TOTAL_GATES > gate_still_sensors_
Definition ld2412.h:152
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2412.h:141
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2412.cpp:335
uint8_t mac_address_[MAC_ADDRESS_SIZE]
Definition ld2412.h:142
GateThresholds gate_move_thresholds_
Definition ld2412.h:147
Base-class for all numbers.
Definition number.h:29
Base-class for all sensors.
Definition sensor.h:47
bool state
Definition fan.h:2
std::array< uint8_t, TOTAL_GATES > last_read
Definition ld2412.h:127
std::array< number::Number *, TOTAL_GATES > numbers
Definition ld2412.h:126