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