ESPHome 2026.2.1
Loading...
Searching...
No Matches
ld2410.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::ld2410 {
33
34using namespace ld24xx;
35
36// Engineering data frame is 45 bytes; +1 for null terminator, +4 so that a frame footer always
37// lands inside the buffer during footer-based resynchronization after losing sync.
38static constexpr uint8_t MAX_LINE_LENGTH = 50;
39static constexpr uint8_t TOTAL_GATES = 9; // Total number of gates supported by the LD2410
40
42#ifdef USE_BINARY_SENSOR
43 SUB_BINARY_SENSOR(out_pin_presence_status)
44 SUB_BINARY_SENSOR(moving_target)
45 SUB_BINARY_SENSOR(still_target)
46 SUB_BINARY_SENSOR(target)
47#endif
48#ifdef USE_SENSOR
49 SUB_SENSOR_WITH_DEDUP(light, uint8_t)
50 SUB_SENSOR_WITH_DEDUP(detection_distance, int)
51 SUB_SENSOR_WITH_DEDUP(moving_target_distance, int)
52 SUB_SENSOR_WITH_DEDUP(moving_target_energy, uint8_t)
53 SUB_SENSOR_WITH_DEDUP(still_target_distance, int)
54 SUB_SENSOR_WITH_DEDUP(still_target_energy, uint8_t)
55#endif
56#ifdef USE_TEXT_SENSOR
57 SUB_TEXT_SENSOR(version)
58 SUB_TEXT_SENSOR(mac)
59#endif
60#ifdef USE_NUMBER
61 SUB_NUMBER(light_threshold)
62 SUB_NUMBER(max_move_distance_gate)
63 SUB_NUMBER(max_still_distance_gate)
64 SUB_NUMBER(timeout)
65#endif
66#ifdef USE_SELECT
67 SUB_SELECT(baud_rate)
68 SUB_SELECT(distance_resolution)
69 SUB_SELECT(light_function)
70 SUB_SELECT(out_pin_level)
71#endif
72#ifdef USE_SWITCH
73 SUB_SWITCH(bluetooth)
74 SUB_SWITCH(engineering_mode)
75#endif
76#ifdef USE_BUTTON
77 SUB_BUTTON(factory_reset)
78 SUB_BUTTON(query)
79 SUB_BUTTON(restart)
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#ifdef USE_NUMBER
88 void set_gate_still_threshold_number(uint8_t gate, number::Number *n);
89 void set_gate_move_threshold_number(uint8_t gate, number::Number *n);
90 void set_max_distances_timeout();
91 void set_gate_threshold(uint8_t gate);
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_bluetooth_password(const std::string &password);
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 char *state);
103 void set_baud_rate(const char *state);
104 void factory_reset();
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_();
118
119 uint8_t light_function_ = 0;
120 uint8_t light_threshold_ = 0;
121 uint8_t out_pin_level_ = 0;
122 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
123 uint8_t buffer_data_[MAX_LINE_LENGTH];
124 uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
125 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
126 bool bluetooth_on_{false};
127#ifdef USE_NUMBER
128 std::array<number::Number *, TOTAL_GATES> gate_move_threshold_numbers_{};
129 std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
130#endif
131#ifdef USE_SENSOR
132 std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_move_sensors_{};
133 std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_still_sensors_{};
134#endif
135};
136
137} // namespace esphome::ld2410
virtual void setup()
Where the component's initialization should happen.
std::array< number::Number *, TOTAL_GATES > gate_still_threshold_numbers_
Definition ld2410.h:129
void set_config_mode_(bool enable)
Definition ld2410.cpp:636
std::array< number::Number *, TOTAL_GATES > gate_move_threshold_numbers_
Definition ld2410.h:128
std::array< SensorWithDedup< uint8_t > *, TOTAL_GATES > gate_still_sensors_
Definition ld2410.h:133
std::array< SensorWithDedup< uint8_t > *, TOTAL_GATES > gate_move_sensors_
Definition ld2410.h:132
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2410.cpp:294
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2410.h:123
Base-class for all numbers.
Definition number.h:29
Base-class for all sensors.
Definition sensor.h:43
bool state
Definition fan.h:2