ESPHome 2026.11.0-dev
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_SELECT) && defined(USE_LD2410S_PARAMETERS)
19#endif
20
21namespace esphome::ld2410s {
22
23// Largest frame read here is the standard data frame: 4 header + 2 length + payload + 4 footer
24static constexpr size_t RX_BUFFER_SIZE = 96;
25static_assert(RX_BUFFER_SIZE <= 255, "frame lengths are kept in a byte");
26
37static constexpr uint8_t PARAMETER_COUNT = static_cast<uint8_t>(Parameter::PARAMETER_COUNT);
38// Response speed values, in the order of the select's options (Normal, Fast)
39static constexpr uint8_t RESPONSE_SPEEDS[] PROGMEM = {5, 10};
40static constexpr uint8_t RESPONSE_SPEED_COUNT = sizeof(RESPONSE_SPEEDS);
41
42#if defined(USE_NUMBER) && defined(USE_LD2410S_PARAMETERS)
43class LD2410SNumber;
44#endif
45
46class LD2410SComponent final : public Component, public uart::UARTDevice {
47#ifdef USE_BINARY_SENSOR
48 SUB_BINARY_SENSOR(target)
49#endif
50#ifdef USE_SENSOR
51 SUB_SENSOR_WITH_DEDUP(distance, uint16_t)
52#endif
53#ifdef USE_LD2410S_VERSION
54 SUB_TEXT_SENSOR(version)
55#endif
56#if defined(USE_SELECT) && defined(USE_LD2410S_PARAMETERS)
57 SUB_SELECT(response_speed)
58#endif
59
60 public:
61 void setup() override { this->status_set_warning(); } // cleared once the module acknowledged the init sequence
62 void loop() override;
63 void dump_config() override;
64
67#ifdef USE_LD2410S_PARAMETERS
68 bool set_parameter(Parameter parameter, uint8_t value);
69#ifdef USE_NUMBER
70 void set_number(Parameter parameter, LD2410SNumber *number) {
71 this->numbers_[static_cast<uint8_t>(parameter)] = number;
72 }
73#endif
74#endif
75
76 protected:
77 enum class FrameType : uint8_t { NONE, SHORT_DATA, STD_DATA, COMMAND };
78
79 void receive_byte_(uint8_t byte);
81 bool handle_frame_();
83 bool handle_data_frame_(const uint8_t *payload, uint16_t len);
84 void handle_command_ack_(const uint8_t *payload, uint16_t len);
85#ifdef USE_LD2410S_PARAMETERS
86 bool handle_parameters_(const uint8_t *values, uint16_t len);
87#endif
88#ifdef USE_LD2410S_VERSION
89 bool handle_version_(const uint8_t *values, uint16_t len);
90#endif
92 if (this->dropped_frames_ < UINT8_MAX) {
93 this->dropped_frames_++;
94 }
95 }
96 void reset_frame_() {
97 this->rx_len_ = 0;
98 this->expected_len_ = 0;
100 }
101 void start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now);
102 void restart_init_(uint32_t at);
103 void run_sequence_(uint32_t now);
104 uint16_t current_command_() const;
105 void send_command_(uint16_t command);
106 void publish_target_(bool target, uint16_t distance);
107#ifdef USE_LD2410S_PARAMETERS
108 void publish_parameters_();
109#endif
110
111#if defined(USE_NUMBER) && defined(USE_LD2410S_PARAMETERS)
112 LD2410SNumber *numbers_[PARAMETER_COUNT]{}; // null for parameters without a number
113#endif
114
115 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
121 uint8_t rx_buffer_[RX_BUFFER_SIZE];
122 uint8_t rx_len_{0};
123 uint8_t expected_len_{0};
124 uint8_t sequence_len_{0};
125 uint8_t step_{0};
126 uint8_t step_timeouts_{0};
127 uint8_t dropped_frames_{0}; // since the last accepted data frame, saturates at 255
129 bool awaiting_ack_{false};
130 bool rescanning_{false};
131 bool init_done_{false};
132#ifdef USE_LD2410S_PARAMETERS
133 bool parameters_known_{false}; // the module answered the parameter read at least once
134 bool parameters_dirty_{false};
135#endif
136};
137
138} // 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:383
bool handle_version_(const uint8_t *values, uint16_t len)
Definition ld2410s.cpp:480
bool handle_frame_()
Returns false when the footer does not match, so the bytes can be scanned again.
Definition ld2410s.cpp:311
void start_sequence_(const uint16_t *sequence, uint8_t length, uint32_t now)
Definition ld2410s.cpp:164
uint8_t parameters_[PARAMETER_COUNT]
Definition ld2410s.h:119
void publish_target_(bool target, uint16_t distance)
Definition ld2410s.cpp:497
void send_command_(uint16_t command)
Definition ld2410s.cpp:196
LD2410SNumber * numbers_[PARAMETER_COUNT]
Definition ld2410s.h:112
uint8_t rx_buffer_[RX_BUFFER_SIZE]
Definition ld2410s.h:121
bool handle_parameters_(const uint8_t *values, uint16_t len)
Definition ld2410s.cpp:442
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:358
void receive_byte_(uint8_t byte)
Definition ld2410s.cpp:241
void run_sequence_(uint32_t now)
Definition ld2410s.cpp:175
Edits one common parameter; scale converts the module's units to the number's.
Parameter
The module's common parameters, in the order the parameter read answers them.
Definition ld2410s.h:28
const void size_t len
Definition hal.h:68
static void uint32_t