ESPHome 2025.9.0-dev
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 {
33namespace ld2410 {
34
35using namespace ld24xx;
36
37static constexpr uint8_t MAX_LINE_LENGTH = 46; // Max characters for serial buffer
38static constexpr uint8_t TOTAL_GATES = 9; // Total number of gates supported by the LD2410
39
41#ifdef USE_BINARY_SENSOR
42 SUB_BINARY_SENSOR(out_pin_presence_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(version)
57 SUB_TEXT_SENSOR(mac)
58#endif
59#ifdef USE_NUMBER
60 SUB_NUMBER(light_threshold)
61 SUB_NUMBER(max_move_distance_gate)
62 SUB_NUMBER(max_still_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#endif
80
81 public:
82 void setup() override;
83 void dump_config() override;
84 void loop() override;
85 void set_light_out_control();
86#ifdef USE_NUMBER
87 void set_gate_still_threshold_number(uint8_t gate, number::Number *n);
88 void set_gate_move_threshold_number(uint8_t gate, number::Number *n);
89 void set_max_distances_timeout();
90 void set_gate_threshold(uint8_t gate);
91#endif
92#ifdef USE_SENSOR
93 void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
94 void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
95#endif
96 void set_bluetooth_password(const std::string &password);
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 std::string &state);
102 void set_baud_rate(const std::string &state);
103 void factory_reset();
104
105 protected:
106 void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
107 void set_config_mode_(bool enable);
109 bool handle_ack_data_();
110 void readline_(int readch);
111 void query_parameters_();
112 void get_version_();
113 void get_mac_();
116 void restart_();
117
118 uint8_t light_function_ = 0;
119 uint8_t light_threshold_ = 0;
120 uint8_t out_pin_level_ = 0;
121 uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
122 uint8_t buffer_data_[MAX_LINE_LENGTH];
123 uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
124 uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
125 bool bluetooth_on_{false};
126#ifdef USE_NUMBER
127 std::array<number::Number *, TOTAL_GATES> gate_move_threshold_numbers_{};
128 std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
129#endif
130#ifdef USE_SENSOR
131 std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_move_sensors_{};
132 std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_still_sensors_{};
133#endif
134};
135
136} // namespace ld2410
137} // namespace esphome
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:85
std::array< number::Number *, TOTAL_GATES > gate_still_threshold_numbers_
Definition ld2410.h:128
void set_config_mode_(bool enable)
Definition ld2410.cpp:616
std::array< number::Number *, TOTAL_GATES > gate_move_threshold_numbers_
Definition ld2410.h:127
std::array< SensorWithDedup< uint8_t > *, TOTAL_GATES > gate_still_sensors_
Definition ld2410.h:132
std::array< SensorWithDedup< uint8_t > *, TOTAL_GATES > gate_move_sensors_
Definition ld2410.h:131
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
Definition ld2410.cpp:283
uint8_t buffer_data_[MAX_LINE_LENGTH]
Definition ld2410.h:122
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