ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ld2451.cpp
Go to the documentation of this file.
1#include "ld2451.h"
2
4#include "esphome/core/log.h"
5
6namespace esphome::ld2451 {
7
8ESPHOME_LOG_TAG(TAG, "ld2451");
9
10// Angles are sent offset by 0x80, so 0x80 is straight ahead
11static constexpr uint8_t ANGLE_OFFSET = 0x80;
12// Protocol V1.03 documents 0x00 as approaching, but radars watching traffic send 0x01, matching the alarm byte
13static constexpr uint8_t DIRECTION_APPROACHING = 0x01;
14
16 ESP_LOGW(TAG, "Skipped a data frame of %u bytes; more than %u targets are not supported", static_cast<unsigned>(len),
17 MAX_TARGETS);
18}
19
21 ESP_LOGCONFIG(TAG, "LD2451:");
22#ifdef USE_SENSOR
23 LOG_SENSOR_WITH_DEDUP(" ", "TargetCount", this->target_count_sensor_);
24 for (const auto &t : this->target_sensors_) {
25 LOG_SENSOR_WITH_DEDUP(" ", "TargetAngle", t.angle);
26 LOG_SENSOR_WITH_DEDUP(" ", "TargetDistance", t.distance);
27 LOG_SENSOR_WITH_DEDUP(" ", "TargetSpeed", t.speed);
28 LOG_SENSOR_WITH_DEDUP(" ", "TargetSnr", t.snr);
29 }
30#endif
31}
32
33void LD2451Component::loop() {
35 auto handler = [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); };
36 this->read_frames<MAX_FRAME_LENGTH>(this->reader_, handler, now, this->last_rx_);
37}
38
39// Handles the frame when its footer and structure are in place; returns false otherwise
40bool LD2451Component::handle_frame_(const uint8_t *frame, size_t len) {
41 if (!ld24xx::has_footer(frame, len)) {
42 ESP_LOGW(TAG, "Invalid footer");
43 ESP_LOGV_HEX(TAG, frame, len, MAX_FRAME_LENGTH, "Rejected: %s");
44 return false;
45 }
46 ESP_LOGV_HEX(TAG, frame, len, MAX_FRAME_LENGTH, "Frame: %s");
47 if (frame[0] != DataFrame::FIRST) {
48 return true; // A reply to a configuration command, none of which are sent yet
49 }
50 return this->handle_data_(&frame[PAYLOAD_OFFSET], len - PAYLOAD_OFFSET - ld24xx::HEADER_FOOTER_SIZE);
51}
52
53// Payload: target count, alarm, then TARGET_SIZE bytes per target
54bool LD2451Component::handle_data_(const uint8_t *payload, size_t len) {
55 // An idle radar may send a frame with no payload
56 const uint8_t num_targets = len == 0 ? 0 : payload[0];
57 if (len != 0 && len != 2 + num_targets * TARGET_SIZE) {
58 ESP_LOGW(TAG, "Payload of %u bytes does not match a target count of %u", static_cast<unsigned>(len), num_targets);
59 return false;
60 }
61 // MAX_FRAME_LENGTH bounds len, and so the count
62#ifdef USE_SENSOR
63 for (uint8_t i = 0; i < MAX_TARGETS; i++) {
64 TargetSensors &t = this->target_sensors_[i];
65 if (i >= num_targets) {
66 SAFE_PUBLISH_SENSOR_UNKNOWN(t.angle);
67 SAFE_PUBLISH_SENSOR_UNKNOWN(t.distance);
68 SAFE_PUBLISH_SENSOR_UNKNOWN(t.speed);
69 SAFE_PUBLISH_SENSOR_UNKNOWN(t.snr);
70 continue;
71 }
72 const uint8_t *p = &payload[2 + i * TARGET_SIZE];
73 const auto angle = static_cast<int8_t>(p[0] - ANGLE_OFFSET);
74 const int16_t speed = p[2] == DIRECTION_APPROACHING ? -p[3] : p[3];
75 ESP_LOGV(TAG, "Target %u: angle %d, distance %u m, speed %d km/h, SNR %u", i + 1, angle, p[1], speed, p[4]);
76 SAFE_PUBLISH_SENSOR(t.angle, angle);
77 SAFE_PUBLISH_SENSOR(t.distance, p[1]);
78 SAFE_PUBLISH_SENSOR(t.speed, speed);
79 SAFE_PUBLISH_SENSOR(t.snr, p[4]);
80 }
81 SAFE_PUBLISH_SENSOR(this->target_count_sensor_, num_targets);
82#endif
83 return true;
84}
85
86} // namespace esphome::ld2451
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 dump_config()
bool handle_data_(const uint8_t *payload, size_t len)
Definition ld2451.cpp:54
LengthFrameReader< MAX_FRAME_LENGTH, DataFrame, ld24xx::CmdFrameFormat > reader_
Definition ld2451.h:48
bool handle_frame_(const uint8_t *frame, size_t len)
Definition ld2451.cpp:40
std::array< TargetSensors, MAX_TARGETS > target_sensors_
Definition ld2451.h:57
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
int speed
Definition fan.h:3
void warn_too_many_targets(size_t len)
Warns about a data frame with more targets than MAX_TARGETS, which is skipped.
Definition ld2451.cpp:15
ESPHOME_LOG_TAG(TAG, "ld2451")
bool has_footer(const uint8_t *frame, size_t len)
Definition ld24xx.h:96
const void size_t len
Definition hal.h:68
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
static constexpr uint8_t FIRST