ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ld6004.cpp
Go to the documentation of this file.
1#include "ld6004.h"
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
6
7namespace esphome::ld6004 {
8
9ESPHOME_LOG_TAG(TAG, "ld6004");
10
11static constexpr uint16_t TYPE_SET_DWELL_LIFETIME = 0x0206;
12static constexpr uint16_t TYPE_SET_OUTPUT_INTERVAL = 0x0207;
13static constexpr uint16_t TYPE_REPORT_TARGET = 0x0A04;
14static constexpr uint16_t TYPE_REPORT_AREA_PRESENCE = 0x0A0A;
15static constexpr uint16_t TYPE_REPORT_WORK_MODE = 0x0A12;
16static constexpr uint16_t TYPE_REPORT_WORK_STATUS = 0x0A14;
17static constexpr uint16_t TYPE_REPORT_P20_MODE = 0x0A15;
18static constexpr uint16_t TYPE_REPORT_DWELL_LIFETIME = 0x0A17;
19static constexpr uint16_t TYPE_REPORT_OUTPUT_INTERVAL = 0x0A18;
20
21static constexpr uint32_t CMD_WORK_LOW_POWER = 0x16;
22static constexpr uint32_t CMD_WORK_NORMAL = 0x17;
23static constexpr uint32_t CMD_GET_WORK_MODE = 0x18;
24static constexpr uint32_t CMD_WORK_RADAR_OFF_P20_HIGH = 0x1B;
25static constexpr uint32_t CMD_WORK_RADAR_OFF_P20_LOW = 0x1C;
26static constexpr uint32_t CMD_P20_PRESENCE_HIGH = 0x1D;
27static constexpr uint32_t CMD_P20_PRESENCE_LOW = 0x1E;
28static constexpr uint32_t CMD_GET_P20_MODE = 0x1F;
29static constexpr uint32_t CMD_P20_CONSTANT_LOW = 0x20;
30static constexpr uint32_t CMD_P20_CONSTANT_HIGH = 0x21;
31static constexpr uint32_t CMD_P20_PULSE_LOW = 0x22;
32static constexpr uint32_t CMD_P20_PULSE_HIGH = 0x23;
33static constexpr uint32_t CMD_WORK_HIGH_REFLECTIVITY = 0x24;
34static constexpr uint32_t CMD_CLEAR_DWELL = 0x25;
35static constexpr uint32_t CMD_GET_DWELL_LIFETIME = 0x26;
36static constexpr uint32_t CMD_GET_OUTPUT_INTERVAL = 0x27;
37
38// The work_mode select options, the 0x0A12 report values and the tables below share one order.
39static constexpr uint8_t WORK_MODE_COUNT = 5;
40static constexpr uint8_t WORK_MODE_RADAR_OFF_P20_HIGH = 2;
41static constexpr uint8_t WORK_MODE_RADAR_OFF_P20_LOW = 3;
42static constexpr uint8_t P20_MODE_COUNT = 6;
43#ifdef USE_SELECT
44// Every control code fits in a byte, so the tables are bytes in flash.
45static constexpr uint8_t WORK_MODE_COMMANDS[WORK_MODE_COUNT] PROGMEM = {
46 CMD_WORK_NORMAL, CMD_WORK_LOW_POWER, CMD_WORK_RADAR_OFF_P20_HIGH, CMD_WORK_RADAR_OFF_P20_LOW,
47 CMD_WORK_HIGH_REFLECTIVITY};
48static constexpr uint8_t P20_MODE_COMMANDS[P20_MODE_COUNT] PROGMEM = {CMD_P20_PRESENCE_HIGH, CMD_P20_PRESENCE_LOW,
49 CMD_P20_CONSTANT_LOW, CMD_P20_CONSTANT_HIGH,
50 CMD_P20_PULSE_LOW, CMD_P20_PULSE_HIGH};
51#endif
52#ifdef USE_TEXT_SENSOR
53PROGMEM_STRING_TABLE(WorkModeStrings, "normal", "low_power", "radar_off_p20_high", "radar_off_p20_low",
54 "high_reflectivity");
55static_assert(WorkModeStrings::COUNT == WORK_MODE_COUNT);
56#endif
57
58// The mode reports carry one byte or a little-endian uint32. Any other length returns UINT32_MAX, which the
59// callers' range checks reject.
60static uint32_t read_mode(const uint8_t *data, uint16_t len) {
61 if (len == 1)
62 return data[0];
63 if (len == 4)
64 return encode_value_le<uint32_t>(data);
65 return UINT32_MAX;
66}
67
68bool LD6004Component::handle_model_report(uint16_t type, const uint8_t *data, uint16_t len) {
69 switch (type) {
70 case TYPE_REPORT_WORK_MODE: {
71 const uint32_t mode = read_mode(data, len);
72 if (mode >= WORK_MODE_COUNT)
73 break;
74 this->radar_off_ = mode == WORK_MODE_RADAR_OFF_P20_HIGH || mode == WORK_MODE_RADAR_OFF_P20_LOW;
75 if (this->radar_off_) {
76 // Detection has stopped, so no later report clears what the last ones showed.
78 this->clear_target_state_();
79 }
80#ifdef USE_SELECT
81 PUBLISH_STATE(this->work_mode_select_, mode);
82#endif
83#ifdef USE_TEXT_SENSOR
84 PUBLISH_STATE(this->work_mode_text_sensor_, WorkModeStrings::get_progmem_str(static_cast<uint8_t>(mode), 0));
85#endif
86 return true;
87 }
88 case TYPE_REPORT_TARGET:
89 case TYPE_REPORT_AREA_PRESENCE:
90 // Claimed while the radar is off: frames still in flight must not restore the cleared presence.
91 return this->radar_off_;
92 case TYPE_REPORT_WORK_STATUS:
93 // 0x0A14 marks the unattended low-power transition; a zero means nobody is in any area.
94 if (len >= 1 && data[0] == 0 && !this->target_presence_any_)
96 return true;
97 case TYPE_REPORT_P20_MODE: {
98 const uint32_t mode = read_mode(data, len);
99 if (mode >= P20_MODE_COUNT)
100 break;
101#ifdef USE_SELECT
102 PUBLISH_STATE(this->p20_mode_select_, mode);
103#endif
104 return true;
105 }
106 case TYPE_REPORT_DWELL_LIFETIME:
107 if (len != 4)
108 break;
109#ifdef USE_NUMBER
110 this->publish_number_clamped_(this->dwell_lifetime_number_, encode_value_le<uint32_t>(data));
111#endif
112 return true;
113 case TYPE_REPORT_OUTPUT_INTERVAL:
114 if (len != 4)
115 break;
116#ifdef USE_NUMBER
117 this->publish_number_clamped_(this->output_interval_number_, encode_value_le<uint32_t>(data));
118#endif
119 return true;
120 default:
121 return false;
122 }
123 // Still claimed, so the base does not treat it as an unknown frame.
124 ESP_LOGD(TAG, "Unexpected report 0x%04X: %u bytes, first byte 0x%02X", type, len, len > 0 ? data[0] : 0);
125 return true;
126}
127
128#ifdef USE_NUMBER
129bool LD6004Component::set_number_value(number::Number *entity, float value) {
130 if (entity != this->dwell_lifetime_number_ && entity != this->output_interval_number_)
131 return this->LD600XComponent::set_number_value(entity, value);
132 return this->queue_u32_setting_(
133 entity == this->dwell_lifetime_number_ ? TYPE_SET_DWELL_LIFETIME : TYPE_SET_OUTPUT_INTERVAL, value);
134}
135#endif // USE_NUMBER
136
137#ifdef USE_SELECT
139 if (entity == this->work_mode_select_)
140 return index < WORK_MODE_COUNT && this->send_control_command_(progmem_read_byte(&WORK_MODE_COMMANDS[index]));
141 if (entity == this->p20_mode_select_)
142 return index < P20_MODE_COUNT && this->send_control_command_(progmem_read_byte(&P20_MODE_COMMANDS[index]));
143 return this->LD600XComponent::set_select_value(entity, index);
144}
145#endif // USE_SELECT
146
147#ifdef USE_BUTTON
149 if (kind == BUTTON_CLEAR_DWELL) {
150 if (this->send_control_command_(CMD_CLEAR_DWELL)) {
151 this->LD600XComponent::press_button(ld600x::BUTTON_GET_AREAS);
152 }
153 } else {
154 this->LD600XComponent::press_button(kind);
155 }
156}
157#endif // USE_BUTTON
158
160 bool want_work_mode = false;
161#ifdef USE_SELECT
162 want_work_mode = this->work_mode_select_ != nullptr;
163 if (this->p20_mode_select_ != nullptr)
164 this->send_control_command_(CMD_GET_P20_MODE);
165#endif
166#ifdef USE_TEXT_SENSOR
167 // The text sensor follows the 0x0A12 report too, so it needs the same boot query as the select
168 // to show a mode before the module next changes it.
169 want_work_mode = want_work_mode || this->work_mode_text_sensor_ != nullptr;
170#endif
171 if (want_work_mode)
172 this->send_control_command_(CMD_GET_WORK_MODE);
173#ifdef USE_NUMBER
174 if (this->dwell_lifetime_number_ != nullptr)
175 this->send_control_command_(CMD_GET_DWELL_LIFETIME);
176 if (this->output_interval_number_ != nullptr)
177 this->send_control_command_(CMD_GET_OUTPUT_INTERVAL);
178#endif
179}
180
182#ifdef USE_NUMBER
183 LOG_NUMBER(" ", "Dwell Lifetime", this->dwell_lifetime_number_);
184 LOG_NUMBER(" ", "Output Interval", this->output_interval_number_);
185#endif
186#ifdef USE_SELECT
187 LOG_SELECT(" ", "Work Mode", this->work_mode_select_);
188 LOG_SELECT(" ", "P20 Mode", this->p20_mode_select_);
189#endif
190}
191
192} // namespace esphome::ld6004
BedjetMode mode
BedJet operating mode.
SUB_NUMBER(dwell_lifetime) SUB_NUMBER(output_interval) SUB_SELECT(work_mode) SUB_SELECT(p20_mode) bool set_number_value(number bool set_select_value(select::Select *entity, size_t index) override
Definition ld6004.cpp:138
bool handle_model_report(uint16_t type, const uint8_t *data, uint16_t len) override
Definition ld6004.cpp:68
void dump_model_config() override
Definition ld6004.cpp:181
void press_button(uint8_t kind) override
Definition ld6004.cpp:148
bool queue_u32_setting_(uint16_t type, float value)
Definition ld600x.cpp:967
void publish_number_clamped_(number::Number *number, float value)
Definition ld600x.cpp:752
bool send_control_command_(uint32_t command)
Definition ld600x.cpp:943
Base-class for all numbers.
Definition number.h:29
Base-class for all selects.
Definition select.h:28
uint8_t type
@ BUTTON_CLEAR_DWELL
Definition ld6004.h:8
ESPHOME_LOG_TAG(TAG, "ld6004")
PROGMEM_STRING_TABLE(WorkModeStrings, "normal", "low_power", "radar_off_p20_high", "radar_off_p20_low", "high_reflectivity")
uint32_t value
Definition hal.cpp:41
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:1084
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t