ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
ld2460.h
Go to the documentation of this file.
1#pragma once
2
7
8#include <array>
9
10namespace esphome::ld2460 {
11
12static constexpr uint8_t MAX_FRAME_LENGTH = 31; // A 5-target data frame
13static constexpr uint8_t MAX_TARGETS = 5;
14// Header (4), command, whole-frame length (2, LE), payload, footer (4)
15static constexpr uint8_t MIN_FRAME_LENGTH = 11;
19
20enum class InstallationMode : uint8_t {
23};
24
26#ifdef USE_SENSOR
27 SUB_SENSOR_WITH_DEDUP(target_count, uint8_t)
28#endif
29
30 public:
31 void setup() override;
32 void dump_config() override;
33 void loop() override;
34
35#ifdef USE_SENSOR
36 void set_target_x_sensor(uint8_t target, sensor::Sensor *s) { this->target_sensors_[target].x.set_sensor(s); }
37 void set_target_y_sensor(uint8_t target, sensor::Sensor *s) { this->target_sensors_[target].y.set_sensor(s); }
38 void set_target_distance_sensor(uint8_t target, sensor::Sensor *s) {
39 this->target_sensors_[target].distance.set_sensor(s);
40 }
41 void set_target_angle_sensor(uint8_t target, sensor::Sensor *s) { this->target_sensors_[target].angle.set_sensor(s); }
42#endif
43
45 template<typename F> void add_on_data_callback(F &&callback) { this->data_callback_.add(std::forward<F>(callback)); }
46
47 protected:
48 void send_query_();
49 bool handle_periodic_data_(const uint8_t *data, uint8_t packet_len);
50 bool handle_ack_(const uint8_t *data, uint8_t len);
51 bool handle_frame_(const uint8_t *frame, uint8_t len);
52
55 // The angle formula depends on how the module is mounted, so it is read from the module at boot
57 // Queries sent so far; 0 once the module answered. Data frames resend up to MAX_QUERY_ATTEMPTS.
58 uint8_t query_attempts_{1};
59
60#ifdef USE_SENSOR
61 // INT16_MIN marks a slot with no published coordinates, so the next report always publishes
64 int16_t last_x{INT16_MIN};
65 int16_t last_y{INT16_MIN};
66 };
67 std::array<TargetSensors, MAX_TARGETS> target_sensors_{};
68#endif
69
71};
72
73} // namespace esphome::ld2460
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the...
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
Base-class for all sensors.
Definition sensor.h:48
A sensor pointer that may be null plus the last value it published, stored inline.
const void size_t len
Definition hal.h:68
static void uint32_t
One frame type for LengthFrameReader: a FrameHeader, a length rule from above, then any of the option...
offset: where the length field starts, counted from the first header byte.
sensor::SensorWithDedup< float > angle
Definition ld2460.h:63
sensor::SensorWithDedup< float > x
Definition ld2460.h:63
sensor::SensorWithDedup< float > y
Definition ld2460.h:63
sensor::SensorWithDedup< float > distance
Definition ld2460.h:63