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