ESPHome 2025.6.2
Loading...
Searching...
No Matches
ld2410.cpp
Go to the documentation of this file.
1#include "ld2410.h"
2
3#include <utility>
4#ifdef USE_NUMBER
6#endif
7#ifdef USE_SENSOR
9#endif
10
11#define highbyte(val) (uint8_t)((val) >> 8)
12#define lowbyte(val) (uint8_t)((val) &0xff)
13
14namespace esphome {
15namespace ld2410 {
16
17static const char *const TAG = "ld2410";
18
19LD2410Component::LD2410Component() {}
20
21void LD2410Component::dump_config() {
22 ESP_LOGCONFIG(TAG, "LD2410:");
23#ifdef USE_BINARY_SENSOR
24 LOG_BINARY_SENSOR(" ", "TargetBinarySensor", this->target_binary_sensor_);
25 LOG_BINARY_SENSOR(" ", "MovingTargetBinarySensor", this->moving_target_binary_sensor_);
26 LOG_BINARY_SENSOR(" ", "StillTargetBinarySensor", this->still_target_binary_sensor_);
27 LOG_BINARY_SENSOR(" ", "OutPinPresenceStatusBinarySensor", this->out_pin_presence_status_binary_sensor_);
28#endif
29#ifdef USE_SWITCH
30 LOG_SWITCH(" ", "EngineeringModeSwitch", this->engineering_mode_switch_);
31 LOG_SWITCH(" ", "BluetoothSwitch", this->bluetooth_switch_);
32#endif
33#ifdef USE_BUTTON
34 LOG_BUTTON(" ", "ResetButton", this->reset_button_);
35 LOG_BUTTON(" ", "RestartButton", this->restart_button_);
36 LOG_BUTTON(" ", "QueryButton", this->query_button_);
37#endif
38#ifdef USE_SENSOR
39 LOG_SENSOR(" ", "LightSensor", this->light_sensor_);
40 LOG_SENSOR(" ", "MovingTargetDistanceSensor", this->moving_target_distance_sensor_);
41 LOG_SENSOR(" ", "StillTargetDistanceSensor", this->still_target_distance_sensor_);
42 LOG_SENSOR(" ", "MovingTargetEnergySensor", this->moving_target_energy_sensor_);
43 LOG_SENSOR(" ", "StillTargetEnergySensor", this->still_target_energy_sensor_);
44 LOG_SENSOR(" ", "DetectionDistanceSensor", this->detection_distance_sensor_);
45 for (sensor::Sensor *s : this->gate_still_sensors_) {
46 LOG_SENSOR(" ", "NthGateStillSesnsor", s);
47 }
48 for (sensor::Sensor *s : this->gate_move_sensors_) {
49 LOG_SENSOR(" ", "NthGateMoveSesnsor", s);
50 }
51#endif
52#ifdef USE_TEXT_SENSOR
53 LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_);
54 LOG_TEXT_SENSOR(" ", "MacTextSensor", this->mac_text_sensor_);
55#endif
56#ifdef USE_SELECT
57 LOG_SELECT(" ", "LightFunctionSelect", this->light_function_select_);
58 LOG_SELECT(" ", "OutPinLevelSelect", this->out_pin_level_select_);
59 LOG_SELECT(" ", "DistanceResolutionSelect", this->distance_resolution_select_);
60 LOG_SELECT(" ", "BaudRateSelect", this->baud_rate_select_);
61#endif
62#ifdef USE_NUMBER
63 LOG_NUMBER(" ", "LightThresholdNumber", this->light_threshold_number_);
64 LOG_NUMBER(" ", "MaxStillDistanceGateNumber", this->max_still_distance_gate_number_);
65 LOG_NUMBER(" ", "MaxMoveDistanceGateNumber", this->max_move_distance_gate_number_);
66 LOG_NUMBER(" ", "TimeoutNumber", this->timeout_number_);
67 for (number::Number *n : this->gate_still_threshold_numbers_) {
68 LOG_NUMBER(" ", "Still Thresholds Number", n);
69 }
70 for (number::Number *n : this->gate_move_threshold_numbers_) {
71 LOG_NUMBER(" ", "Move Thresholds Number", n);
72 }
73#endif
74 this->read_all_info();
75 ESP_LOGCONFIG(TAG,
76 " Throttle_ : %ums\n"
77 " MAC Address : %s\n"
78 " Firmware Version : %s",
79 this->throttle_, const_cast<char *>(this->mac_.c_str()), const_cast<char *>(this->version_.c_str()));
80}
81
82void LD2410Component::setup() {
83 ESP_LOGCONFIG(TAG, "Running setup");
84 this->read_all_info();
85}
86
87void LD2410Component::read_all_info() {
88 this->set_config_mode_(true);
89 this->get_version_();
90 this->get_mac_();
92 this->get_light_control_();
93 this->query_parameters_();
94 this->set_config_mode_(false);
95#ifdef USE_SELECT
96 const auto baud_rate = std::to_string(this->parent_->get_baud_rate());
97 if (this->baud_rate_select_ != nullptr && this->baud_rate_select_->state != baud_rate) {
98 this->baud_rate_select_->publish_state(baud_rate);
99 }
100#endif
101}
102
103void LD2410Component::restart_and_read_all_info() {
104 this->set_config_mode_(true);
105 this->restart_();
106 this->set_timeout(1000, [this]() { this->read_all_info(); });
107}
108
109void LD2410Component::loop() {
110 const int max_line_length = 80;
111 static uint8_t buffer[max_line_length];
112
113 while (available()) {
114 this->readline_(read(), buffer, max_line_length);
115 }
116}
117
118void LD2410Component::send_command_(uint8_t command, const uint8_t *command_value, int command_value_len) {
119 ESP_LOGV(TAG, "Sending COMMAND %02X", command);
120 // frame start bytes
121 this->write_array(CMD_FRAME_HEADER, 4);
122 // length bytes
123 int len = 2;
124 if (command_value != nullptr)
125 len += command_value_len;
126 this->write_byte(lowbyte(len));
127 this->write_byte(highbyte(len));
128
129 // command
130 this->write_byte(lowbyte(command));
131 this->write_byte(highbyte(command));
132
133 // command value bytes
134 if (command_value != nullptr) {
135 for (int i = 0; i < command_value_len; i++) {
136 this->write_byte(command_value[i]);
137 }
138 }
139 // frame end bytes
140 this->write_array(CMD_FRAME_END, 4);
141 // FIXME to remove
142 delay(50); // NOLINT
143}
144
145void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
146 if (len < 12)
147 return; // 4 frame start bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame end bytes
148 if (buffer[0] != 0xF4 || buffer[1] != 0xF3 || buffer[2] != 0xF2 || buffer[3] != 0xF1) // check 4 frame start bytes
149 return;
150 if (buffer[7] != HEAD || buffer[len - 6] != END || buffer[len - 5] != CHECK) // Check constant values
151 return; // data head=0xAA, data end=0x55, crc=0x00
152
153 /*
154 Reduce data update rate to prevent home assistant database size grow fast
155 */
156 int32_t current_millis = millis();
157 if (current_millis - last_periodic_millis_ < this->throttle_)
158 return;
159 last_periodic_millis_ = current_millis;
160
161 /*
162 Data Type: 7th
163 0x01: Engineering mode
164 0x02: Normal mode
165 */
166 bool engineering_mode = buffer[DATA_TYPES] == 0x01;
167#ifdef USE_SWITCH
168 if (this->engineering_mode_switch_ != nullptr &&
169 current_millis - last_engineering_mode_change_millis_ > this->throttle_) {
170 this->engineering_mode_switch_->publish_state(engineering_mode);
171 }
172#endif
173#ifdef USE_BINARY_SENSOR
174 /*
175 Target states: 9th
176 0x00 = No target
177 0x01 = Moving targets
178 0x02 = Still targets
179 0x03 = Moving+Still targets
180 */
181 char target_state = buffer[TARGET_STATES];
182 if (this->target_binary_sensor_ != nullptr) {
183 this->target_binary_sensor_->publish_state(target_state != 0x00);
184 }
185 if (this->moving_target_binary_sensor_ != nullptr) {
186 this->moving_target_binary_sensor_->publish_state(CHECK_BIT(target_state, 0));
187 }
188 if (this->still_target_binary_sensor_ != nullptr) {
189 this->still_target_binary_sensor_->publish_state(CHECK_BIT(target_state, 1));
190 }
191#endif
192 /*
193 Moving target distance: 10~11th bytes
194 Moving target energy: 12th byte
195 Still target distance: 13~14th bytes
196 Still target energy: 15th byte
197 Detect distance: 16~17th bytes
198 */
199#ifdef USE_SENSOR
200 if (this->moving_target_distance_sensor_ != nullptr) {
201 int new_moving_target_distance = this->two_byte_to_int_(buffer[MOVING_TARGET_LOW], buffer[MOVING_TARGET_HIGH]);
202 if (this->moving_target_distance_sensor_->get_state() != new_moving_target_distance)
203 this->moving_target_distance_sensor_->publish_state(new_moving_target_distance);
204 }
205 if (this->moving_target_energy_sensor_ != nullptr) {
206 int new_moving_target_energy = buffer[MOVING_ENERGY];
207 if (this->moving_target_energy_sensor_->get_state() != new_moving_target_energy)
208 this->moving_target_energy_sensor_->publish_state(new_moving_target_energy);
209 }
210 if (this->still_target_distance_sensor_ != nullptr) {
211 int new_still_target_distance = this->two_byte_to_int_(buffer[STILL_TARGET_LOW], buffer[STILL_TARGET_HIGH]);
212 if (this->still_target_distance_sensor_->get_state() != new_still_target_distance)
213 this->still_target_distance_sensor_->publish_state(new_still_target_distance);
214 }
215 if (this->still_target_energy_sensor_ != nullptr) {
216 int new_still_target_energy = buffer[STILL_ENERGY];
217 if (this->still_target_energy_sensor_->get_state() != new_still_target_energy)
218 this->still_target_energy_sensor_->publish_state(new_still_target_energy);
219 }
220 if (this->detection_distance_sensor_ != nullptr) {
221 int new_detect_distance = this->two_byte_to_int_(buffer[DETECT_DISTANCE_LOW], buffer[DETECT_DISTANCE_HIGH]);
222 if (this->detection_distance_sensor_->get_state() != new_detect_distance)
223 this->detection_distance_sensor_->publish_state(new_detect_distance);
224 }
225 if (engineering_mode) {
226 /*
227 Moving distance range: 18th byte
228 Still distance range: 19th byte
229 Moving enery: 20~28th bytes
230 */
231 for (std::vector<sensor::Sensor *>::size_type i = 0; i != this->gate_move_sensors_.size(); i++) {
232 sensor::Sensor *s = this->gate_move_sensors_[i];
233 if (s != nullptr) {
234 s->publish_state(buffer[MOVING_SENSOR_START + i]);
235 }
236 }
237 /*
238 Still energy: 29~37th bytes
239 */
240 for (std::vector<sensor::Sensor *>::size_type i = 0; i != this->gate_still_sensors_.size(); i++) {
241 sensor::Sensor *s = this->gate_still_sensors_[i];
242 if (s != nullptr) {
243 s->publish_state(buffer[STILL_SENSOR_START + i]);
244 }
245 }
246 /*
247 Light sensor: 38th bytes
248 */
249 if (this->light_sensor_ != nullptr) {
250 int new_light_sensor = buffer[LIGHT_SENSOR];
251 if (this->light_sensor_->get_state() != new_light_sensor)
252 this->light_sensor_->publish_state(new_light_sensor);
253 }
254 } else {
255 for (auto *s : this->gate_move_sensors_) {
256 if (s != nullptr && !std::isnan(s->get_state())) {
257 s->publish_state(NAN);
258 }
259 }
260 for (auto *s : this->gate_still_sensors_) {
261 if (s != nullptr && !std::isnan(s->get_state())) {
262 s->publish_state(NAN);
263 }
264 }
265 if (this->light_sensor_ != nullptr && !std::isnan(this->light_sensor_->get_state())) {
266 this->light_sensor_->publish_state(NAN);
267 }
268 }
269#endif
270#ifdef USE_BINARY_SENSOR
271 if (engineering_mode) {
272 if (this->out_pin_presence_status_binary_sensor_ != nullptr) {
273 this->out_pin_presence_status_binary_sensor_->publish_state(buffer[OUT_PIN_SENSOR] == 0x01);
274 }
275 } else {
276 if (this->out_pin_presence_status_binary_sensor_ != nullptr) {
277 this->out_pin_presence_status_binary_sensor_->publish_state(false);
278 }
279 }
280#endif
281}
282
283const char VERSION_FMT[] = "%u.%02X.%02X%02X%02X%02X";
284
285std::string format_version(uint8_t *buffer) {
286 std::string::size_type version_size = 256;
287 std::string version;
288 do {
289 version.resize(version_size + 1);
290 version_size = std::snprintf(&version[0], version.size(), VERSION_FMT, buffer[13], buffer[12], buffer[17],
291 buffer[16], buffer[15], buffer[14]);
292 } while (version_size + 1 > version.size());
293 version.resize(version_size);
294 return version;
295}
296
297const char MAC_FMT[] = "%02X:%02X:%02X:%02X:%02X:%02X";
298
299const std::string UNKNOWN_MAC("unknown");
300const std::string NO_MAC("08:05:04:03:02:01");
301
302std::string format_mac(uint8_t *buffer) {
303 std::string::size_type mac_size = 256;
304 std::string mac;
305 do {
306 mac.resize(mac_size + 1);
307 mac_size = std::snprintf(&mac[0], mac.size(), MAC_FMT, buffer[10], buffer[11], buffer[12], buffer[13], buffer[14],
308 buffer[15]);
309 } while (mac_size + 1 > mac.size());
310 mac.resize(mac_size);
311 if (mac == NO_MAC) {
312 return UNKNOWN_MAC;
313 }
314 return mac;
315}
316
317#ifdef USE_NUMBER
318std::function<void(void)> set_number_value(number::Number *n, float value) {
319 float normalized_value = value * 1.0;
320 if (n != nullptr && (!n->has_state() || n->state != normalized_value)) {
321 n->state = normalized_value;
322 return [n, normalized_value]() { n->publish_state(normalized_value); };
323 }
324 return []() {};
325}
326#endif
327
328bool LD2410Component::handle_ack_data_(uint8_t *buffer, int len) {
329 ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", buffer[COMMAND]);
330 if (len < 10) {
331 ESP_LOGE(TAG, "Error with last command : incorrect length");
332 return true;
333 }
334 if (buffer[0] != 0xFD || buffer[1] != 0xFC || buffer[2] != 0xFB || buffer[3] != 0xFA) { // check 4 frame start bytes
335 ESP_LOGE(TAG, "Error with last command : incorrect Header");
336 return true;
337 }
338 if (buffer[COMMAND_STATUS] != 0x01) {
339 ESP_LOGE(TAG, "Error with last command : status != 0x01");
340 return true;
341 }
342 if (this->two_byte_to_int_(buffer[8], buffer[9]) != 0x00) {
343 ESP_LOGE(TAG, "Error with last command , last buffer was: %u , %u", buffer[8], buffer[9]);
344 return true;
345 }
346
347 switch (buffer[COMMAND]) {
348 case lowbyte(CMD_ENABLE_CONF):
349 ESP_LOGV(TAG, "Handled Enable conf command");
350 break;
351 case lowbyte(CMD_DISABLE_CONF):
352 ESP_LOGV(TAG, "Handled Disabled conf command");
353 break;
354 case lowbyte(CMD_SET_BAUD_RATE):
355 ESP_LOGV(TAG, "Handled baud rate change command");
356#ifdef USE_SELECT
357 if (this->baud_rate_select_ != nullptr) {
358 ESP_LOGE(TAG, "Change baud rate component config to %s and reinstall", this->baud_rate_select_->state.c_str());
359 }
360#endif
361 break;
362 case lowbyte(CMD_VERSION):
363 this->version_ = format_version(buffer);
364 ESP_LOGV(TAG, "FW Version is: %s", const_cast<char *>(this->version_.c_str()));
365#ifdef USE_TEXT_SENSOR
366 if (this->version_text_sensor_ != nullptr) {
367 this->version_text_sensor_->publish_state(this->version_);
368 }
369#endif
370 break;
371 case lowbyte(CMD_QUERY_DISTANCE_RESOLUTION): {
372 std::string distance_resolution =
373 DISTANCE_RESOLUTION_INT_TO_ENUM.at(this->two_byte_to_int_(buffer[10], buffer[11]));
374 ESP_LOGV(TAG, "Distance resolution is: %s", const_cast<char *>(distance_resolution.c_str()));
375#ifdef USE_SELECT
376 if (this->distance_resolution_select_ != nullptr &&
377 this->distance_resolution_select_->state != distance_resolution) {
378 this->distance_resolution_select_->publish_state(distance_resolution);
379 }
380#endif
381 } break;
382 case lowbyte(CMD_QUERY_LIGHT_CONTROL): {
383 this->light_function_ = LIGHT_FUNCTION_INT_TO_ENUM.at(buffer[10]);
384 this->light_threshold_ = buffer[11] * 1.0;
385 this->out_pin_level_ = OUT_PIN_LEVEL_INT_TO_ENUM.at(buffer[12]);
386 ESP_LOGV(TAG, "Light function is: %s", const_cast<char *>(this->light_function_.c_str()));
387 ESP_LOGV(TAG, "Light threshold is: %f", this->light_threshold_);
388 ESP_LOGV(TAG, "Out pin level is: %s", const_cast<char *>(this->out_pin_level_.c_str()));
389#ifdef USE_SELECT
390 if (this->light_function_select_ != nullptr && this->light_function_select_->state != this->light_function_) {
391 this->light_function_select_->publish_state(this->light_function_);
392 }
393 if (this->out_pin_level_select_ != nullptr && this->out_pin_level_select_->state != this->out_pin_level_) {
394 this->out_pin_level_select_->publish_state(this->out_pin_level_);
395 }
396#endif
397#ifdef USE_NUMBER
398 if (this->light_threshold_number_ != nullptr &&
399 (!this->light_threshold_number_->has_state() ||
400 this->light_threshold_number_->state != this->light_threshold_)) {
401 this->light_threshold_number_->publish_state(this->light_threshold_);
402 }
403#endif
404 } break;
405 case lowbyte(CMD_MAC):
406 if (len < 20) {
407 return false;
408 }
409 this->mac_ = format_mac(buffer);
410 ESP_LOGV(TAG, "MAC Address is: %s", const_cast<char *>(this->mac_.c_str()));
411#ifdef USE_TEXT_SENSOR
412 if (this->mac_text_sensor_ != nullptr) {
413 this->mac_text_sensor_->publish_state(this->mac_);
414 }
415#endif
416#ifdef USE_SWITCH
417 if (this->bluetooth_switch_ != nullptr) {
418 this->bluetooth_switch_->publish_state(this->mac_ != UNKNOWN_MAC);
419 }
420#endif
421 break;
422 case lowbyte(CMD_GATE_SENS):
423 ESP_LOGV(TAG, "Handled sensitivity command");
424 break;
425 case lowbyte(CMD_BLUETOOTH):
426 ESP_LOGV(TAG, "Handled bluetooth command");
427 break;
428 case lowbyte(CMD_SET_DISTANCE_RESOLUTION):
429 ESP_LOGV(TAG, "Handled set distance resolution command");
430 break;
431 case lowbyte(CMD_SET_LIGHT_CONTROL):
432 ESP_LOGV(TAG, "Handled set light control command");
433 break;
434 case lowbyte(CMD_BT_PASSWORD):
435 ESP_LOGV(TAG, "Handled set bluetooth password command");
436 break;
437 case lowbyte(CMD_QUERY): // Query parameters response
438 {
439 if (buffer[10] != 0xAA)
440 return true; // value head=0xAA
441#ifdef USE_NUMBER
442 /*
443 Moving distance range: 13th byte
444 Still distance range: 14th byte
445 */
446 std::vector<std::function<void(void)>> updates;
447 updates.push_back(set_number_value(this->max_move_distance_gate_number_, buffer[12]));
448 updates.push_back(set_number_value(this->max_still_distance_gate_number_, buffer[13]));
449 /*
450 Moving Sensitivities: 15~23th bytes
451 */
452 for (std::vector<number::Number *>::size_type i = 0; i != this->gate_move_threshold_numbers_.size(); i++) {
453 updates.push_back(set_number_value(this->gate_move_threshold_numbers_[i], buffer[14 + i]));
454 }
455 /*
456 Still Sensitivities: 24~32th bytes
457 */
458 for (std::vector<number::Number *>::size_type i = 0; i != this->gate_still_threshold_numbers_.size(); i++) {
459 updates.push_back(set_number_value(this->gate_still_threshold_numbers_[i], buffer[23 + i]));
460 }
461 /*
462 None Duration: 33~34th bytes
463 */
464 updates.push_back(set_number_value(this->timeout_number_, this->two_byte_to_int_(buffer[32], buffer[33])));
465 for (auto &update : updates) {
466 update();
467 }
468#endif
469 } break;
470 default:
471 break;
472 }
473
474 return true;
475}
476
477void LD2410Component::readline_(int readch, uint8_t *buffer, int len) {
478 static int pos = 0;
479
480 if (readch >= 0) {
481 if (pos < len - 1) {
482 buffer[pos++] = readch;
483 buffer[pos] = 0;
484 } else {
485 pos = 0;
486 }
487 if (pos >= 4) {
488 if (buffer[pos - 4] == 0xF8 && buffer[pos - 3] == 0xF7 && buffer[pos - 2] == 0xF6 && buffer[pos - 1] == 0xF5) {
489 ESP_LOGV(TAG, "Will handle Periodic Data");
490 this->handle_periodic_data_(buffer, pos);
491 pos = 0; // Reset position index ready for next time
492 } else if (buffer[pos - 4] == 0x04 && buffer[pos - 3] == 0x03 && buffer[pos - 2] == 0x02 &&
493 buffer[pos - 1] == 0x01) {
494 ESP_LOGV(TAG, "Will handle ACK Data");
495 if (this->handle_ack_data_(buffer, pos)) {
496 pos = 0; // Reset position index ready for next time
497 } else {
498 ESP_LOGV(TAG, "ACK Data incomplete");
499 }
500 }
501 }
502 }
503}
504
506 uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
507 uint8_t cmd_value[2] = {0x01, 0x00};
508 this->send_command_(cmd, enable ? cmd_value : nullptr, 2);
509}
510
511void LD2410Component::set_bluetooth(bool enable) {
512 this->set_config_mode_(true);
513 uint8_t enable_cmd_value[2] = {0x01, 0x00};
514 uint8_t disable_cmd_value[2] = {0x00, 0x00};
515 this->send_command_(CMD_BLUETOOTH, enable ? enable_cmd_value : disable_cmd_value, 2);
516 this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
517}
518
519void LD2410Component::set_distance_resolution(const std::string &state) {
520 this->set_config_mode_(true);
521 uint8_t cmd_value[2] = {DISTANCE_RESOLUTION_ENUM_TO_INT.at(state), 0x00};
522 this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, 2);
523 this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
524}
525
526void LD2410Component::set_baud_rate(const std::string &state) {
527 this->set_config_mode_(true);
528 uint8_t cmd_value[2] = {BAUD_RATE_ENUM_TO_INT.at(state), 0x00};
529 this->send_command_(CMD_SET_BAUD_RATE, cmd_value, 2);
530 this->set_timeout(200, [this]() { this->restart_(); });
531}
532
533void LD2410Component::set_bluetooth_password(const std::string &password) {
534 if (password.length() != 6) {
535 ESP_LOGE(TAG, "set_bluetooth_password(): invalid password length, must be exactly 6 chars '%s'", password.c_str());
536 return;
537 }
538 this->set_config_mode_(true);
539 uint8_t cmd_value[6];
540 std::copy(password.begin(), password.end(), std::begin(cmd_value));
541 this->send_command_(CMD_BT_PASSWORD, cmd_value, 6);
542 this->set_config_mode_(false);
543}
544
545void LD2410Component::set_engineering_mode(bool enable) {
546 this->set_config_mode_(true);
548 uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG;
549 this->send_command_(cmd, nullptr, 0);
550 this->set_config_mode_(false);
551}
552
553void LD2410Component::factory_reset() {
554 this->set_config_mode_(true);
555 this->send_command_(CMD_RESET, nullptr, 0);
556 this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
557}
558
559void LD2410Component::restart_() { this->send_command_(CMD_RESTART, nullptr, 0); }
560
561void LD2410Component::query_parameters_() { this->send_command_(CMD_QUERY, nullptr, 0); }
562void LD2410Component::get_version_() { this->send_command_(CMD_VERSION, nullptr, 0); }
564 uint8_t cmd_value[2] = {0x01, 0x00};
565 this->send_command_(CMD_MAC, cmd_value, 2);
566}
567void LD2410Component::get_distance_resolution_() { this->send_command_(CMD_QUERY_DISTANCE_RESOLUTION, nullptr, 0); }
568
569void LD2410Component::get_light_control_() { this->send_command_(CMD_QUERY_LIGHT_CONTROL, nullptr, 0); }
570
571#ifdef USE_NUMBER
572void LD2410Component::set_max_distances_timeout() {
573 if (!this->max_move_distance_gate_number_->has_state() || !this->max_still_distance_gate_number_->has_state() ||
574 !this->timeout_number_->has_state()) {
575 return;
576 }
577 int max_moving_distance_gate_range = static_cast<int>(this->max_move_distance_gate_number_->state);
578 int max_still_distance_gate_range = static_cast<int>(this->max_still_distance_gate_number_->state);
579 int timeout = static_cast<int>(this->timeout_number_->state);
580 uint8_t value[18] = {0x00,
581 0x00,
582 lowbyte(max_moving_distance_gate_range),
583 highbyte(max_moving_distance_gate_range),
584 0x00,
585 0x00,
586 0x01,
587 0x00,
588 lowbyte(max_still_distance_gate_range),
589 highbyte(max_still_distance_gate_range),
590 0x00,
591 0x00,
592 0x02,
593 0x00,
594 lowbyte(timeout),
595 highbyte(timeout),
596 0x00,
597 0x00};
598 this->set_config_mode_(true);
599 this->send_command_(CMD_MAXDIST_DURATION, value, 18);
600 delay(50); // NOLINT
601 this->query_parameters_();
602 this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
603 this->set_config_mode_(false);
604}
605
606void LD2410Component::set_gate_threshold(uint8_t gate) {
607 number::Number *motionsens = this->gate_move_threshold_numbers_[gate];
608 number::Number *stillsens = this->gate_still_threshold_numbers_[gate];
609
610 if (!motionsens->has_state() || !stillsens->has_state()) {
611 return;
612 }
613 int motion = static_cast<int>(motionsens->state);
614 int still = static_cast<int>(stillsens->state);
615
616 this->set_config_mode_(true);
617 // reference
618 // https://drive.google.com/drive/folders/1p4dhbEJA3YubyIjIIC7wwVsSo8x29Fq-?spm=a2g0o.detail.1000023.17.93465697yFwVxH
619 // Send data: configure the motion sensitivity of distance gate 3 to 40, and the static sensitivity of 40
620 // 00 00 (gate)
621 // 03 00 00 00 (gate number)
622 // 01 00 (motion sensitivity)
623 // 28 00 00 00 (value)
624 // 02 00 (still sensitivtiy)
625 // 28 00 00 00 (value)
626 uint8_t value[18] = {0x00, 0x00, lowbyte(gate), highbyte(gate), 0x00, 0x00,
627 0x01, 0x00, lowbyte(motion), highbyte(motion), 0x00, 0x00,
628 0x02, 0x00, lowbyte(still), highbyte(still), 0x00, 0x00};
629 this->send_command_(CMD_GATE_SENS, value, 18);
630 delay(50); // NOLINT
631 this->query_parameters_();
632 this->set_config_mode_(false);
633}
634
635void LD2410Component::set_gate_still_threshold_number(int gate, number::Number *n) {
636 this->gate_still_threshold_numbers_[gate] = n;
637}
638
639void LD2410Component::set_gate_move_threshold_number(int gate, number::Number *n) {
640 this->gate_move_threshold_numbers_[gate] = n;
641}
642#endif
643
644void LD2410Component::set_light_out_control() {
645#ifdef USE_NUMBER
646 if (this->light_threshold_number_ != nullptr && this->light_threshold_number_->has_state()) {
647 this->light_threshold_ = this->light_threshold_number_->state;
648 }
649#endif
650#ifdef USE_SELECT
651 if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) {
652 this->light_function_ = this->light_function_select_->state;
653 }
654 if (this->out_pin_level_select_ != nullptr && this->out_pin_level_select_->has_state()) {
655 this->out_pin_level_ = this->out_pin_level_select_->state;
656 }
657#endif
658 if (this->light_function_.empty() || this->out_pin_level_.empty() || this->light_threshold_ < 0) {
659 return;
660 }
661 this->set_config_mode_(true);
662 uint8_t light_function = LIGHT_FUNCTION_ENUM_TO_INT.at(this->light_function_);
663 uint8_t light_threshold = static_cast<uint8_t>(this->light_threshold_);
664 uint8_t out_pin_level = OUT_PIN_LEVEL_ENUM_TO_INT.at(this->out_pin_level_);
665 uint8_t value[4] = {light_function, light_threshold, out_pin_level, 0x00};
666 this->send_command_(CMD_SET_LIGHT_CONTROL, value, 4);
667 delay(50); // NOLINT
668 this->get_light_control_();
669 this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
670 this->set_config_mode_(false);
671}
672
673#ifdef USE_SENSOR
674void LD2410Component::set_gate_move_sensor(int gate, sensor::Sensor *s) { this->gate_move_sensors_[gate] = s; }
675void LD2410Component::set_gate_still_sensor(int gate, sensor::Sensor *s) { this->gate_still_sensors_[gate] = s; }
676#endif
677
678} // namespace ld2410
679} // namespace esphome
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.cpp:75
bool has_state() const
Definition entity_base.h:53
int two_byte_to_int_(char firstbyte, char secondbyte)
Definition ld2410.h:205
void set_config_mode_(bool enable)
Definition ld2410.cpp:505
void handle_periodic_data_(uint8_t *buffer, int len)
Definition ld2410.cpp:145
std::vector< number::Number * > gate_still_threshold_numbers_
Definition ld2410.h:227
void send_command_(uint8_t command_str, const uint8_t *command_value, int command_value_len)
Definition ld2410.cpp:118
std::vector< sensor::Sensor * > gate_move_sensors_
Definition ld2410.h:232
void readline_(int readch, uint8_t *buffer, int len)
Definition ld2410.cpp:477
int32_t last_engineering_mode_change_millis_
Definition ld2410.h:219
bool handle_ack_data_(uint8_t *buffer, int len)
Definition ld2410.cpp:328
std::vector< sensor::Sensor * > gate_still_sensors_
Definition ld2410.h:231
std::vector< number::Number * > gate_move_threshold_numbers_
Definition ld2410.h:228
Base-class for all numbers.
Definition number.h:39
void publish_state(float state)
Definition number.cpp:9
Base-class for all sensors.
Definition sensor.h:62
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:39
UARTComponent * parent_
Definition uart.h:68
void write_byte(uint8_t data)
Definition uart.h:19
void write_array(const uint8_t *data, size_t len)
Definition uart.h:21
bool state
Definition fan.h:0
@ DETECT_DISTANCE_LOW
Definition ld2410.h:125
@ DETECT_DISTANCE_HIGH
Definition ld2410.h:126
@ MOVING_SENSOR_START
Definition ld2410.h:127
std::string format_version(uint8_t *buffer)
Definition ld2410.cpp:285
const char VERSION_FMT[]
Definition ld2410.cpp:283
std::function< void(void)> set_number_value(number::Number *n, float value)
Definition ld2410.cpp:318
const std::string NO_MAC("08:05:04:03:02:01")
const std::string UNKNOWN_MAC("unknown")
const char MAC_FMT[]
Definition ld2410.cpp:297
std::string format_mac(uint8_t *buffer)
Definition ld2410.cpp:302
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:302
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28