ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ld2460.cpp
Go to the documentation of this file.
1#include "ld2460.h"
2
4#include "esphome/core/hal.h"
6
7#include <algorithm>
8#include <cmath>
9#include <numbers>
10
11namespace esphome::ld2460 {
12
13ESPHOME_LOG_TAG(TAG, "ld2460");
14
15static constexpr uint8_t CMD_REPORT_DATA = 0x04;
16static constexpr uint8_t CMD_QUERY_INSTALLATION_MODE = 0x0A;
17static constexpr uint8_t ACK_FRAME_LENGTH = 12;
18static constexpr uint8_t MAX_QUERY_ATTEMPTS = 3;
19
20static constexpr float RADIANS_TO_DEGREES = 180.0f / std::numbers::pi_v<float>;
21
22void LD2460Component::setup() { this->send_query_(); }
23
24// Header, command, length (12, little endian), payload 0x01, footer
25static constexpr uint8_t QUERY_INSTALLATION_MODE_FRAME[] PROGMEM = {
26 0xFD, 0xFC, 0xFB, 0xFA, CMD_QUERY_INSTALLATION_MODE, 0x0C, 0x00, 0x01, 0x04, 0x03, 0x02, 0x01,
27};
28
30 this->write_array_progmem(QUERY_INSTALLATION_MODE_FRAME, sizeof(QUERY_INSTALLATION_MODE_FRAME));
31}
32
33void LD2460Component::dump_config() {
34 ESP_LOGCONFIG(TAG,
35 "LD2460:\n"
36 " Installation mode: %s",
37 this->query_attempts_ != 0 ? LOG_STR_LITERAL("unknown")
38 : this->installation_mode_ == InstallationMode::INSTALLATION_MODE_TOP ? LOG_STR_LITERAL("top")
39 : LOG_STR_LITERAL("side"));
40#ifdef USE_SENSOR
41 LOG_SENSOR_WITH_DEDUP(" ", "TargetCount", this->target_count_sensor_);
42 for (const auto &t : this->target_sensors_) {
43 LOG_SENSOR_WITH_DEDUP(" ", "TargetX", t.x);
44 LOG_SENSOR_WITH_DEDUP(" ", "TargetY", t.y);
45 LOG_SENSOR_WITH_DEDUP(" ", "TargetDistance", t.distance);
46 LOG_SENSOR_WITH_DEDUP(" ", "TargetAngle", t.angle);
47 }
48#endif
49}
50
51void LD2460Component::loop() {
53 auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
54 this->read_frames<MAX_FRAME_LENGTH>(this->reader_, handler, now, this->last_rx_);
55}
56
57bool LD2460Component::handle_periodic_data_(const uint8_t *data, uint8_t packet_len) {
58 if (data[4] != CMD_REPORT_DATA) {
59 ESP_LOGW(TAG, "Invalid periodic data header");
60 return false;
61 }
62 if ((packet_len - MIN_FRAME_LENGTH) % 4 != 0) {
63 ESP_LOGW(TAG, "Periodic packet length %u is not a whole number of targets", packet_len);
64 return false;
65 }
66
67 if (this->query_attempts_ != 0) {
68 if (this->query_attempts_ < MAX_QUERY_ATTEMPTS) {
69 // The module may have booted after us and missed the earlier query
70 this->query_attempts_++;
71 this->send_query_();
72 } else if (this->query_attempts_ == MAX_QUERY_ATTEMPTS) {
73 this->query_attempts_++;
74 ESP_LOGW(TAG, "No installation mode reply after %u queries; assuming side mount", MAX_QUERY_ATTEMPTS);
75 }
76 }
77
78#ifdef USE_SENSOR
79 uint8_t num_targets = std::min<uint8_t>((packet_len - MIN_FRAME_LENGTH) / 4, MAX_TARGETS);
80 for (uint8_t i = 0; i < MAX_TARGETS; i++) {
81 TargetSensors &t = this->target_sensors_[i];
82 if (i >= num_targets) {
83 t.last_x = INT16_MIN;
84 t.last_y = INT16_MIN;
85 SAFE_PUBLISH_SENSOR_UNKNOWN(t.x);
86 SAFE_PUBLISH_SENSOR_UNKNOWN(t.y);
87 SAFE_PUBLISH_SENSOR_UNKNOWN(t.distance);
88 SAFE_PUBLISH_SENSOR_UNKNOWN(t.angle);
89 continue;
90 }
91 const uint8_t *p = &data[7 + i * 4];
92 auto raw_x = encode_value_le<int16_t>(p);
93 auto raw_y = encode_value_le<int16_t>(&p[2]);
94 if (raw_x == t.last_x && raw_y == t.last_y) {
95 continue;
96 }
97 t.last_x = raw_x;
98 t.last_y = raw_y;
99
100 // Coordinates are reported in 0.1 m steps
101 float x = raw_x * 0.1f;
102 float y = raw_y * 0.1f;
103 SAFE_PUBLISH_SENSOR(t.x, x);
104 SAFE_PUBLISH_SENSOR(t.y, y);
105 if (t.distance.has_sensor()) {
106 SAFE_PUBLISH_SENSOR(t.distance, std::sqrt(x * x + y * y));
107 }
108 if (t.angle.has_sensor()) {
109 float angle;
111 angle = std::atan2(y, x) * RADIANS_TO_DEGREES;
112 if (angle < 0.0f) {
113 angle += 360.0f;
114 }
115 } else {
116 angle = std::atan2(x, y) * RADIANS_TO_DEGREES;
117 }
118 SAFE_PUBLISH_SENSOR(t.angle, angle);
119 }
120 }
121 SAFE_PUBLISH_SENSOR(this->target_count_sensor_, num_targets);
122#endif
123
124 this->data_callback_.call();
125 return true;
126}
127
128// Handles the frame when its footer and structure are in place; returns false otherwise
129bool LD2460Component::handle_frame_(const uint8_t *frame, uint8_t len) {
130 if (!ld24xx::has_footer(frame, len)) {
131 ESP_LOGW(TAG, "Invalid footer");
132 ESP_LOGV_HEX(TAG, frame, len, MAX_FRAME_LENGTH, "Rejected: %s");
133 return false;
134 }
135 ESP_LOGV_HEX(TAG, frame, len, MAX_FRAME_LENGTH, "Frame: %s");
136 if (frame[0] == DataFrame::FIRST)
137 return this->handle_periodic_data_(frame, len);
138 return this->handle_ack_(frame, len);
139}
140
141bool LD2460Component::handle_ack_(const uint8_t *data, uint8_t len) {
142 if (len != ACK_FRAME_LENGTH) {
143 ESP_LOGW(TAG, "Dropping malformed command frame");
144 return false;
145 }
146 if (data[4] != CMD_QUERY_INSTALLATION_MODE) {
147 ESP_LOGD(TAG, "Ignoring reply to command 0x%02X", data[4]);
148 return true;
149 }
150 const uint8_t mode = data[7];
151 if (mode != static_cast<uint8_t>(InstallationMode::INSTALLATION_MODE_SIDE) &&
152 mode != static_cast<uint8_t>(InstallationMode::INSTALLATION_MODE_TOP)) {
153 ESP_LOGW(TAG, "Unexpected installation mode %u", mode);
154 return true;
155 }
156 this->installation_mode_ = static_cast<InstallationMode>(mode);
157 this->query_attempts_ = 0;
158 ESP_LOGD(TAG, "Installation mode: %s",
159 mode == static_cast<uint8_t>(InstallationMode::INSTALLATION_MODE_TOP) ? LOG_STR_LITERAL("top")
160 : LOG_STR_LITERAL("side"));
161#ifdef USE_SENSOR
162 // Angles depend on the mode, so republish every target on the next frame
163 for (auto &t : this->target_sensors_) {
164 t.last_x = INT16_MIN;
165 }
166#endif
167 return true;
168}
169
170} // namespace esphome::ld2460
BedjetMode mode
BedJet operating mode.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
InstallationMode installation_mode_
Definition ld2460.h:56
LengthFrameReader< MAX_FRAME_LENGTH, DataFrame, CmdFrame > reader_
Definition ld2460.h:54
bool handle_frame_(const uint8_t *frame, uint8_t len)
Definition ld2460.cpp:129
bool handle_periodic_data_(const uint8_t *data, uint8_t packet_len)
Definition ld2460.cpp:57
bool handle_ack_(const uint8_t *data, uint8_t len)
Definition ld2460.cpp:141
std::array< TargetSensors, MAX_TARGETS > target_sensors_
Definition ld2460.h:67
LazyCallbackManager< void()> data_callback_
Definition ld2460.h:70
void write_array_progmem(const uint8_t *data, size_t len)
Definition uart.h:31
bool read_frames(R &reader, F &&handle, uint32_t now, uint32_t &last_rx, uint32_t gap_ms=250)
Feed the waiting bytes to a LengthFrameReader; on a pass with none, gap_ms after last_rx,...
Definition uart.h:96
ESPHOME_LOG_TAG(TAG, "ld2460")
bool has_footer(const uint8_t *frame, size_t len)
Definition ld24xx.h:96
const void size_t len
Definition hal.h:68
constexpr T encode_value_le(const uint8_t *bytes)
Encode a value from its constituent bytes (from least to most significant) in an array with length si...
Definition helpers.h:1268
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
static constexpr uint8_t FIRST
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6