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;
20static constexpr float RADIANS_TO_DEGREES = 180.0f / std::numbers::pi_v<float>;
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,
30 this->
write_array_progmem(QUERY_INSTALLATION_MODE_FRAME,
sizeof(QUERY_INSTALLATION_MODE_FRAME));
33void LD2460Component::dump_config() {
36 " Installation mode: %s",
39 : LOG_STR_LITERAL(
"side"));
41 LOG_SENSOR_WITH_DEDUP(
" ",
"TargetCount", this->target_count_sensor_);
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);
51void LD2460Component::loop() {
53 auto handler = [
this](
const uint8_t *frame,
size_t len) {
return this->
handle_frame_(frame,
len); };
58 if (data[4] != CMD_REPORT_DATA) {
59 ESP_LOGW(TAG,
"Invalid periodic data header");
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);
74 ESP_LOGW(TAG,
"No installation mode reply after %u queries; assuming side mount", MAX_QUERY_ATTEMPTS);
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++) {
82 if (i >= num_targets) {
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);
91 const uint8_t *p = &data[7 + i * 4];
94 if (raw_x == t.last_x && raw_y == t.last_y) {
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));
108 if (t.angle.has_sensor()) {
111 angle = std::atan2(
y,
x) * RADIANS_TO_DEGREES;
116 angle = std::atan2(
x,
y) * RADIANS_TO_DEGREES;
118 SAFE_PUBLISH_SENSOR(t.angle, angle);
121 SAFE_PUBLISH_SENSOR(this->target_count_sensor_, num_targets);
131 ESP_LOGW(TAG,
"Invalid footer");
132 ESP_LOGV_HEX(TAG, frame,
len, MAX_FRAME_LENGTH,
"Rejected: %s");
135 ESP_LOGV_HEX(TAG, frame,
len, MAX_FRAME_LENGTH,
"Frame: %s");
142 if (
len != ACK_FRAME_LENGTH) {
143 ESP_LOGW(TAG,
"Dropping malformed command frame");
146 if (data[4] != CMD_QUERY_INSTALLATION_MODE) {
147 ESP_LOGD(TAG,
"Ignoring reply to command 0x%02X", data[4]);
150 const uint8_t
mode = data[7];
153 ESP_LOGW(TAG,
"Unexpected installation mode %u",
mode);
158 ESP_LOGD(TAG,
"Installation mode: %s",
160 : LOG_STR_LITERAL(
"side"));
164 t.last_x = INT16_MIN;
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.
InstallationMode installation_mode_
LengthFrameReader< MAX_FRAME_LENGTH, DataFrame, CmdFrame > reader_
bool handle_frame_(const uint8_t *frame, uint8_t len)
bool handle_periodic_data_(const uint8_t *data, uint8_t packet_len)
bool handle_ack_(const uint8_t *data, uint8_t len)
std::array< TargetSensors, MAX_TARGETS > target_sensors_
LazyCallbackManager< void()> data_callback_
void write_array_progmem(const uint8_t *data, size_t len)
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,...
ESPHOME_LOG_TAG(TAG, "ld2460")
bool has_footer(const uint8_t *frame, size_t len)
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...
Application App
Global storage of Application pointer - only one Application can exist.