ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld2410s.h
Go to the documentation of this file.
1#pragma once
2
7#ifdef USE_BINARY_SENSOR
9#endif
10#ifdef USE_SENSOR
13#endif
14#ifdef USE_LD2410S_VERSION
16#endif
17#if defined(USE_NUMBER) && defined(USE_LD2410S_PARAMETERS)
19#endif
20#if defined(USE_SELECT) && defined(USE_LD2410S_PARAMETERS)
22#endif
23
24namespace esphome::ld2410s {
25
26// Largest frame read here is the standard data frame: 4 header + 2 length + payload + 4 footer
27static constexpr size_t RX_BUFFER_SIZE = 96;
28static_assert(RX_BUFFER_SIZE <= 255, "frame lengths are kept in a byte");
29
40static constexpr uint8_t PARAMETER_COUNT = static_cast<uint8_t>(Parameter::PARAMETER_COUNT);
41// Response speed values, in the order of the select's options (Normal, Fast)
42static constexpr uint8_t RESPONSE_SPEEDS[] PROGMEM = {5, 10};
43static constexpr uint8_t RESPONSE_SPEED_COUNT = sizeof(RESPONSE_SPEEDS);
44
45class LD2410SComponent final : public Component, public uart::UARTDevice {
46#ifdef USE_BINARY_SENSOR
47 SUB_BINARY_SENSOR(target)
48#endif
49#ifdef USE_SENSOR
50 SUB_SENSOR_WITH_DEDUP(distance, uint16_t)
51#endif
52#ifdef USE_LD2410S_VERSION
53 SUB_TEXT_SENSOR(version)
54#endif
55#if defined(USE_SELECT) && defined(USE_LD2410S_PARAMETERS)
56 SUB_SELECT(response_speed)
57#endif
58
59 public:
60 void setup() override { this->status_set_warning(); } // cleared once the module acknowledged the init sequence
61 void loop() override;
62 void dump_config() override;
63
66#ifdef USE_LD2410S_PARAMETERS
67 bool set_parameter(Parameter parameter, uint8_t value);
68#ifdef USE_NUMBER
69 void set_number(Parameter parameter, number::Number *number) {
70 this->numbers_[static_cast<uint8_t>(parameter)] = number;
71 }
73 void control_number(number::Number *number, float value);
74#endif
75#ifdef USE_SELECT
76 void control_response_speed(select::Select *select, size_t index);
77#endif
78#endif
79
80 protected:
81 enum class FrameType : uint8_t { NONE, SHORT_DATA, STD_DATA, COMMAND };
82
83 void receive_byte_(uint8_t byte);
85 bool handle_frame_();
87 bool handle_data_frame_(const uint8_t *payload, uint16_t len);
88 void handle_command_ack_(const uint8_t *payload, uint16_t len);
89#ifdef USE_LD2410S_PARAMETERS
90 bool handle_parameters_(const uint8_t *values, uint16_t len);
91#endif
92#ifdef USE_LD2410S_VERSION
93 bool handle_version_(const uint8_t *values, uint16_t len);
94#endif
96 if (this->dropped_frames_ < UINT8_MAX) {
97 this->dropped_frames_++;
98 }
99 }
101 this->rx_len_ = 0;
102 this->expected_len_ = 0;
104 }
105 void start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now);
106 void restart_init_(uint32_t at);
107 void run_sequence_(uint32_t now);
108 uint16_t current_command_() const;
109 void send_command_(uint16_t command);
110 void publish_target_(bool target, uint16_t distance);
111#ifdef USE_LD2410S_PARAMETERS
112 void publish_parameters_();
113#endif
114
115#if defined(USE_NUMBER) && defined(USE_LD2410S_PARAMETERS)
116 number::Number *numbers_[PARAMETER_COUNT]{}; // null for parameters without a number
117#endif
118
119 const uint16_t *sequence_{nullptr}; // PROGMEM; null while nothing is being sent
122#ifdef USE_LD2410S_PARAMETERS
123 uint8_t parameters_[PARAMETER_COUNT]{}; // all fit a byte: gates, seconds, tenths of a hertz, speed
124#endif
125 uint8_t rx_buffer_[RX_BUFFER_SIZE];
126 uint8_t rx_len_{0};
127 uint8_t expected_len_{0};
128 uint8_t sequence_len_{0};
129 uint8_t step_{0};
130 uint8_t step_timeouts_{0};
131 uint8_t dropped_frames_{0}; // since the last accepted data frame, saturates at 255
133 bool awaiting_ack_{false};
134 bool rescanning_{false};
135 bool init_done_{false};
136#ifdef USE_LD2410S_PARAMETERS
137 bool parameters_known_{false}; // the module answered the parameter read at least once
138 bool parameters_dirty_{false};
139#endif
140};
141
142} // namespace esphome::ld2410s
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
virtual void loop()
This method will be called repeatedly.
Definition component.cpp:86
void handle_command_ack_(const uint8_t *payload, uint16_t len)
Definition ld2410s.cpp:399
bool handle_version_(const uint8_t *values, uint16_t len)
Definition ld2410s.cpp:494
bool handle_frame_()
Returns false when the footer does not match, so the bytes can be scanned again.
Definition ld2410s.cpp:327
void start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now)
Definition ld2410s.cpp:180
uint8_t parameters_[PARAMETER_COUNT]
Definition ld2410s.h:123
void publish_target_(bool target, uint16_t distance)
Definition ld2410s.cpp:509
number::Number * numbers_[PARAMETER_COUNT]
Definition ld2410s.h:116
void send_command_(uint16_t command)
Definition ld2410s.cpp:212
uint8_t rx_buffer_[RX_BUFFER_SIZE]
Definition ld2410s.h:125
bool handle_parameters_(const uint8_t *values, uint16_t len)
Definition ld2410s.cpp:458
bool handle_data_frame_(const uint8_t *payload, uint16_t len)
Returns false for a data frame whose state byte is impossible, so it counts as dropped.
Definition ld2410s.cpp:374
void receive_byte_(uint8_t byte)
Definition ld2410s.cpp:257
void run_sequence_(uint32_t now)
Definition ld2410s.cpp:191
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:28
Parameter
The module's common parameters, in the order the parameter read answers them.
Definition ld2410s.h:31
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
static void uint32_t