ESPHome 2025.9.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 {
33namespace ld2412 {
34
35using namespace ld24xx;
36
37static constexpr uint8_t MAX_LINE_LENGTH = 54; // Max characters for serial buffer
38static constexpr uint8_t TOTAL_GATES = 14; // Total number of gates supported by the LD2412
39
41#ifdef USE_BINARY_SENSOR
42 SUB_BINARY_SENSOR(dynamic_background_correction_status)
43 SUB_BINARY_SENSOR(moving_target)
44 SUB_BINARY_SENSOR(still_target)
45 SUB_BINARY_SENSOR(target)
46#endif
47#ifdef USE_SENSOR
48 SUB_SENSOR_WITH_DEDUP(light, uint8_t)
49 SUB_SENSOR_WITH_DEDUP(detection_distance, int)
50 SUB_SENSOR_WITH_DEDUP(moving_target_distance, int)
51 SUB_SENSOR_WITH_DEDUP(moving_target_energy, uint8_t)
52 SUB_SENSOR_WITH_DEDUP(still_target_distance, int)
53 SUB_SENSOR_WITH_DEDUP(still_target_energy, uint8_t)
54#endif
55#ifdef USE_TEXT_SENSOR
56 SUB_TEXT_SENSOR(mac)
57 SUB_TEXT_SENSOR(version)
58#endif
59#ifdef USE_NUMBER
60 SUB_NUMBER(light_threshold)
61 SUB_NUMBER(max_distance_gate)
62 SUB_NUMBER(min_distance_gate)
63 SUB_NUMBER(timeout)
64#endif
65#ifdef USE_SELECT
66 SUB_SELECT(baud_rate)
67 SUB_SELECT(distance_resolution)
68 SUB_SELECT(light_function)
69 SUB_SELECT(out_pin_level)
70#endif
71#ifdef USE_SWITCH
72 SUB_SWITCH(bluetooth)
73 SUB_SWITCH(engineering_mode)
74#endif
75#ifdef USE_BUTTON
76 SUB_BUTTON(factory_reset)
77 SUB_BUTTON(query)
78 SUB_BUTTON(restart)
79 SUB_BUTTON(start_dynamic_background_correction)
80#endif
81
82 public:
83 void setup() override;
84 void dump_config() override;
85 void loop() override;
86 void set_light_out_control();
87 void set_basic_config();
88#ifdef USE_NUMBER
89 void set_gate_move_threshold_number(uint8_t gate, number::Number *n);
90 void set_gate_still_threshold_number(uint8_t gate, number::Number *n);
91 void set_gate_threshold();
92 void get_gate_threshold();
93#endif
94#ifdef USE_SENSOR
95 void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
96 void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
97#endif
98 void set_engineering_mode(bool enable);
99 void read_all_info();
100 void restart_and_read_all_info();
101 void set_bluetooth(bool enable);
102 void set_distance_resolution(const std::string &state);
103 void set_baud_rate(const std::string &state);
104 void factory_reset();
105 void start_dynamic_background_correction();
106
107 protected:
108 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
109 void set_config_mode_(bool enable);
111 bool handle_ack_data_();
112 void readline_(int readch);
113 void query_parameters_();
114 void get_version_();
115 void get_mac_();
118 void restart_();
120
121 uint8_t light_function_ = 0;
122 uint8_t light_threshold_ = 0;
123 uint8_t out_pin_level_ = 0;
124 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
125 uint8_t buffer_data_[MAX_LINE_LENGTH];
126 uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
127 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
128 bool bluetooth_on_{false};
130#ifdef USE_NUMBER
131 std::array<number::Number *, TOTAL_GATES> gate_move_threshold_numbers_{};
132 std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
133#endif
134#ifdef USE_SENSOR
135 std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_move_sensors_{};
136 std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_still_sensors_{};
137#endif
138};
139
140} // namespace ld2412
141} // namespace esphome
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:85
void set_config_mode_(bool enable)
Definition ld2412.cpp:689
std::array< number::Number *, TOTAL_GATES > gate_move_threshold_numbers_
Definition ld2412.h:131
std::array< SensorWithDedup< uint8_t > *, TOTAL_GATES > gate_move_sensors_
Definition ld2412.h:135
std::array< SensorWithDedup< uint8_t > *, TOTAL_GATES > gate_still_sensors_
Definition ld2412.h:136
std::array< number::Number *, TOTAL_GATES > gate_still_threshold_numbers_
Definition ld2412.h:132
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2412.h:125
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2412.cpp:318
Base-class for all numbers.
Definition number.h:39
Base-class for all sensors.
Definition sensor.h:59
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7