ESPHome 2026.5.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
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) { this->gate_move_threshold_numbers_[gate] = n; }
89 void set_gate_still_threshold_number(uint8_t gate, number::Number *n) {
90 this->gate_still_threshold_numbers_[gate] = n;
91 }
92 void set_gate_threshold();
93 void get_gate_threshold();
94#endif
95#ifdef USE_SENSOR
96 void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
97 void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
98#endif
99 void set_engineering_mode(bool enable);
100 void read_all_info();
101 void restart_and_read_all_info();
102 void set_bluetooth(bool enable);
103 void set_distance_resolution(const char *state);
104 void set_baud_rate(const char *state);
105 void factory_reset();
106 void start_dynamic_background_correction();
107
108 protected:
109 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
110 void set_config_mode_(bool enable);
112 bool handle_ack_data_();
113 void readline_(int readch);
114 void query_parameters_();
115 void get_version_();
116 void get_mac_();
119 void restart_();
121
122 uint8_t light_function_ = 0;
123 uint8_t light_threshold_ = 0;
124 uint8_t out_pin_level_ = 0;
125 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
126 uint8_t buffer_data_[MAX_LINE_LENGTH];
127 uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
128 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
129 bool bluetooth_on_{false};
131#ifdef USE_NUMBER
132 std::array<number::Number *, TOTAL_GATES> gate_move_threshold_numbers_{};
133 std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
134#endif
135#ifdef USE_SENSOR
136 std::array<SensorWithDedup<uint8_t>, TOTAL_GATES> gate_move_sensors_{};
137 std::array<SensorWithDedup<uint8_t>, TOTAL_GATES> gate_still_sensors_{};
138#endif
139};
140
141} // namespace esphome::ld2412
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:89
void set_config_mode_(bool enable)
Definition ld2412.cpp:694
std::array< number::Number *, TOTAL_GATES > gate_move_threshold_numbers_
Definition ld2412.h:132
std::array< number::Number *, TOTAL_GATES > gate_still_threshold_numbers_
Definition ld2412.h:133
std::array< SensorWithDedup< uint8_t >, TOTAL_GATES > gate_move_sensors_
Definition ld2412.h:136
std::array< SensorWithDedup< uint8_t >, TOTAL_GATES > gate_still_sensors_
Definition ld2412.h:137
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2412.h:126
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2412.cpp:327
Base-class for all numbers.
Definition number.h:29
Base-class for all sensors.
Definition sensor.h:47
bool state
Definition fan.h:2