ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ld2410s.h
Go to the documentation of this file.
1#pragma once
2
8#ifdef USE_BINARY_SENSOR
10#endif
11#ifdef USE_SENSOR
13#endif
15#ifdef USE_LD2410S_VERSION
17#endif
18#if defined(USE_NUMBER) && defined(USE_LD2410S_PARAMETERS)
20#endif
21#if defined(USE_SELECT) && defined(USE_LD2410S_PARAMETERS)
23#endif
24
25namespace esphome::ld2410s {
26
27// Largest frame read here is the standard data frame: 4 header + 2 length + payload + 4 footer
28static constexpr size_t RX_BUFFER_SIZE = 96;
29static_assert(RX_BUFFER_SIZE <= 255, "frame lengths are kept in a byte");
30// Minimal output: header, state, distance low, distance high, footer
32
43static constexpr uint8_t PARAMETER_COUNT = static_cast<uint8_t>(Parameter::PARAMETER_COUNT);
44// Response speed values, in the order of the select's options (Normal, Fast)
45static constexpr uint8_t RESPONSE_SPEEDS[] PROGMEM = {5, 10};
46static constexpr uint8_t RESPONSE_SPEED_COUNT = sizeof(RESPONSE_SPEEDS);
47
48class LD2410SComponent final : public Component, public uart::UARTDevice {
49#ifdef USE_BINARY_SENSOR
50 SUB_BINARY_SENSOR(target)
51#endif
52#ifdef USE_SENSOR
53 SUB_SENSOR_WITH_DEDUP(distance, uint16_t)
54#endif
55#ifdef USE_LD2410S_VERSION
56 SUB_TEXT_SENSOR(version)
57#endif
58#if defined(USE_SELECT) && defined(USE_LD2410S_PARAMETERS)
59 SUB_SELECT(response_speed)
60#endif
61
62 public:
63 void setup() override { this->status_set_warning(); } // cleared once the module acknowledged the init sequence
64 void loop() override;
65 void dump_config() override;
66
69#ifdef USE_LD2410S_PARAMETERS
70 bool set_parameter(Parameter parameter, uint8_t value);
71#ifdef USE_NUMBER
72 void set_number(Parameter parameter, number::Number *number) {
73 this->numbers_[static_cast<uint8_t>(parameter)] = number;
74 }
76 void control_number(number::Number *number, float value);
77#endif
78#ifdef USE_SELECT
79 void control_response_speed(select::Select *select, size_t index);
80#endif
81#endif
82
83 protected:
85 bool handle_frame_(const uint8_t *frame, uint8_t len);
87 bool handle_data_frame_(const uint8_t *payload, uint16_t len, bool standard);
88 bool 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 }
100 void start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now);
101 void restart_init_(uint32_t at);
102 void run_sequence_(uint32_t now);
103 uint16_t current_command_() const;
104 void send_command_(uint16_t command);
105 void publish_target_(bool target, uint16_t distance);
106#ifdef USE_LD2410S_PARAMETERS
107 void publish_parameters_();
108#endif
109
110#if defined(USE_NUMBER) && defined(USE_LD2410S_PARAMETERS)
111 number::Number *numbers_[PARAMETER_COUNT]{}; // null for parameters without a number
112#endif
113
114 const uint16_t *sequence_{nullptr}; // PROGMEM; null while nothing is being sent
118#ifdef USE_LD2410S_PARAMETERS
119 uint8_t parameters_[PARAMETER_COUNT]{}; // all fit a byte: gates, seconds, tenths of a hertz, speed
120#endif
122 uint8_t sequence_len_{0};
123 uint8_t step_{0};
124 uint8_t step_timeouts_{0};
125 uint8_t dropped_frames_{0}; // since the last accepted data frame, saturates at 255
126 bool awaiting_ack_{false};
127 bool init_done_{false};
128#ifdef USE_LD2410S_PARAMETERS
129 bool parameters_known_{false}; // the module answered the parameter read at least once
130 bool parameters_dirty_{false};
131#endif
132};
133
134} // 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
Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the...
bool handle_version_(const uint8_t *values, uint16_t len)
Definition ld2410s.cpp:391
void start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now)
Definition ld2410s.cpp:177
uint8_t parameters_[PARAMETER_COUNT]
Definition ld2410s.h:119
void publish_target_(bool target, uint16_t distance)
Definition ld2410s.cpp:406
bool handle_frame_(const uint8_t *frame, uint8_t len)
Returns false for bytes that are not a real frame.
Definition ld2410s.cpp:247
bool handle_data_frame_(const uint8_t *payload, uint16_t len, bool standard)
Returns false for a data frame whose state byte is impossible, so it counts as dropped.
Definition ld2410s.cpp:270
number::Number * numbers_[PARAMETER_COUNT]
Definition ld2410s.h:111
LengthFrameReader< RX_BUFFER_SIZE, ld24xx::CmdFrameFormat, ld24xx::DataFrameFormat, ShortDataFrameFormat > reader_
Definition ld2410s.h:121
void send_command_(uint16_t command)
Definition ld2410s.cpp:209
bool handle_parameters_(const uint8_t *values, uint16_t len)
Definition ld2410s.cpp:355
bool handle_command_ack_(const uint8_t *payload, uint16_t len)
Definition ld2410s.cpp:295
void run_sequence_(uint32_t now)
Definition ld2410s.cpp:188
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:34
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
static void uint32_t
A frame type that always has LEN bytes.
One frame type for LengthFrameReader: a FrameHeader, a length rule from above, then any of the option...