58PROGMEM_STRING_TABLE(BaudRateStrings,
"9600",
"19200",
"38400",
"57600",
"115200",
"230400",
"256000",
"460800");
64 "LightFunction is the select option index");
67static constexpr uint32_t BAUD_RATES[] PROGMEM = {9600, 19200, 38400, 57600, 115200, 230400, 256000, 460800};
70static constexpr uint8_t CMD_ENABLE_CONF = 0xFF;
71static constexpr uint8_t CMD_DISABLE_CONF = 0xFE;
72static constexpr uint8_t CMD_ENABLE_ENG = 0x62;
73static constexpr uint8_t CMD_DISABLE_ENG = 0x63;
74static constexpr uint8_t CMD_MAXDIST_DURATION = 0x60;
75static constexpr uint8_t CMD_QUERY = 0x61;
76static constexpr uint8_t CMD_GATE_SENS = 0x64;
77static constexpr uint8_t CMD_QUERY_VERSION = 0xA0;
78static constexpr uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0xAB;
79static constexpr uint8_t CMD_SET_DISTANCE_RESOLUTION = 0xAA;
80static constexpr uint8_t CMD_QUERY_LIGHT_CONTROL = 0xAE;
81static constexpr uint8_t CMD_SET_LIGHT_CONTROL = 0xAD;
82static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1;
83static constexpr uint8_t CMD_BT_PASSWORD = 0xA9;
84static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5;
85static constexpr uint8_t CMD_RESET = 0xA2;
86static constexpr uint8_t CMD_RESTART = 0xA3;
87static constexpr uint8_t CMD_BLUETOOTH = 0xA4;
88#ifdef USE_LD2410_BG_CORRECTION
90static constexpr uint8_t CMD_BG_CORRECTION_START = 0x0B;
91static constexpr uint8_t CMD_BG_CORRECTION_QUERY = 0x1B;
93static constexpr uint8_t BG_CORRECTION_MIN_MAJOR = 2;
94static constexpr uint8_t BG_CORRECTION_MIN_MINOR = 0x44;
95static constexpr uint8_t BG_CORRECTION_IDLE = 0x00;
96static constexpr uint8_t BG_CORRECTION_COMPLETED = 0x02;
97static constexpr uint32_t BG_CORRECTION_POLL_INTERVAL_MS = 1000;
99static constexpr uint32_t BG_CORRECTION_EXTRA_MS = 13000;
100static constexpr uint32_t BG_CORRECTION_POLL_ID = 0;
101static constexpr uint32_t BG_CORRECTION_DEADLINE_ID = 1;
104static constexpr uint8_t CMD_MAX_MOVE_VALUE = 0x00;
105static constexpr uint8_t CMD_MAX_STILL_VALUE = 0x01;
106static constexpr uint8_t CMD_DURATION_VALUE = 0x02;
108static constexpr uint8_t MOVE_BITMASK = 0x01;
109static constexpr uint8_t STILL_BITMASK = 0x02;
111static constexpr uint8_t TARGET_STATE_CALIBRATING_MIN = 0x04;
112#ifdef USE_LD2410_BG_CORRECTION
113static constexpr uint8_t TARGET_STATE_BG_SUCCESS = 0x05;
114static constexpr uint8_t TARGET_STATE_BG_FAILED = 0x06;
115PROGMEM_STRING_TABLE(BgCorrectionResultStrings,
"Success",
"Failed",
"Unconfirmed",
"Refused",
"Timed out",
"Running");
119using ld24xx::CMD_FRAME_FOOTER;
120using ld24xx::CMD_FRAME_HEADER;
121using ld24xx::HEADER_FOOTER_SIZE;
125static constexpr uint8_t ENGINEERING_FRAME_MIN =
OUT_PIN_SENSOR + 3 + HEADER_FOOTER_SIZE;
127static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xF4, 0xF3, 0xF2, 0xF1};
128static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0xF8, 0xF7, 0xF6, 0xF5};
130void LD2410Component::dump_config() {
131 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
137 " Firmware version: %s\n"
140#ifdef USE_BINARY_SENSOR
141 ESP_LOGCONFIG(TAG,
"Binary Sensors:");
142 LOG_BINARY_SENSOR(
" ",
"Target", this->target_binary_sensor_);
143 LOG_BINARY_SENSOR(
" ",
"MovingTarget", this->moving_target_binary_sensor_);
144 LOG_BINARY_SENSOR(
" ",
"StillTarget", this->still_target_binary_sensor_);
145 LOG_BINARY_SENSOR(
" ",
"OutPinPresenceStatus", this->out_pin_presence_status_binary_sensor_);
146 LOG_BINARY_SENSOR(
" ",
"Calibration", this->calibration_binary_sensor_);
149 ESP_LOGCONFIG(TAG,
"Sensors:");
150 LOG_SENSOR_WITH_DEDUP(
" ",
"Light", this->light_sensor_);
151 LOG_SENSOR_WITH_DEDUP(
" ",
"DetectionDistance", this->detection_distance_sensor_);
152 LOG_SENSOR_WITH_DEDUP(
" ",
"MovingTargetDistance", this->moving_target_distance_sensor_);
153 LOG_SENSOR_WITH_DEDUP(
" ",
"MovingTargetEnergy", this->moving_target_energy_sensor_);
154 LOG_SENSOR_WITH_DEDUP(
" ",
"StillTargetDistance", this->still_target_distance_sensor_);
155 LOG_SENSOR_WITH_DEDUP(
" ",
"StillTargetEnergy", this->still_target_energy_sensor_);
157 LOG_SENSOR_WITH_DEDUP(
" ",
"GateMove", s);
160 LOG_SENSOR_WITH_DEDUP(
" ",
"GateStill", s);
163#ifdef USE_TEXT_SENSOR
164 ESP_LOGCONFIG(TAG,
"Text Sensors:");
165 LOG_TEXT_SENSOR(
" ",
"Mac", this->mac_text_sensor_);
166 LOG_TEXT_SENSOR(
" ",
"Version", this->version_text_sensor_);
167 LOG_TEXT_SENSOR(
" ",
"CalibrationResult", this->calibration_result_text_sensor_);
170 ESP_LOGCONFIG(TAG,
"Numbers:");
171 LOG_NUMBER(
" ",
"LightThreshold", this->light_threshold_number_);
172 LOG_NUMBER(
" ",
"MaxMoveDistanceGate", this->max_move_distance_gate_number_);
173 LOG_NUMBER(
" ",
"MaxStillDistanceGate", this->max_still_distance_gate_number_);
174 LOG_NUMBER(
" ",
"Timeout", this->timeout_number_);
176 LOG_NUMBER(
" ",
"MoveThreshold", n);
179 LOG_NUMBER(
" ",
"StillThreshold", n);
183 ESP_LOGCONFIG(TAG,
"Selects:");
184 LOG_SELECT(
" ",
"BaudRate", this->baud_rate_select_);
185 LOG_SELECT(
" ",
"DistanceResolution", this->distance_resolution_select_);
186 LOG_SELECT(
" ",
"LightFunction", this->light_function_select_);
187 LOG_SELECT(
" ",
"OutPinLevel", this->out_pin_level_select_);
190 ESP_LOGCONFIG(TAG,
"Switches:");
191 LOG_SWITCH(
" ",
"Bluetooth", this->bluetooth_switch_);
192 LOG_SWITCH(
" ",
"EngineeringMode", this->engineering_mode_switch_);
195 ESP_LOGCONFIG(TAG,
"Buttons:");
196 LOG_BUTTON(
" ",
"FactoryReset", this->factory_reset_button_);
197 LOG_BUTTON(
" ",
"Query", this->query_button_);
198 LOG_BUTTON(
" ",
"Restart", this->restart_button_);
199 LOG_BUTTON(
" ",
"BackgroundCorrection", this->background_correction_button_);
204#ifdef USE_LD2410_BG_CORRECTION
207 this->read_all_info();
210void LD2410Component::read_all_info() {
219 if (this->baud_rate_select_ !=
nullptr) {
221 this->baud_rate_select_->publish_state(*index);
227void LD2410Component::restart_and_read_all_info() {
230 this->
set_timeout(1000, [
this]() { this->read_all_info(); });
233void LD2410Component::loop() {
238 ESP_LOGV(TAG,
"Sending COMMAND %02X", command);
240 this->
write_array(CMD_FRAME_HEADER,
sizeof(CMD_FRAME_HEADER));
243 if (command_value !=
nullptr) {
244 len += command_value_len;
247 uint8_t len_cmd[] = {
len, 0x00, command, 0x00};
250 if (command_value !=
nullptr) {
251 this->
write_array(command_value, command_value_len);
254 this->
write_array(CMD_FRAME_FOOTER,
sizeof(CMD_FRAME_FOOTER));
256 if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
275 if (this->
buffer_pos_ < (engineering_mode ? ENGINEERING_FRAME_MIN : PERIODIC_FRAME_MIN)) {
279 PUBLISH_STATE(this->engineering_mode_switch_, engineering_mode);
289#ifdef USE_LD2410_BG_CORRECTION
292 if (target_state == TARGET_STATE_BG_SUCCESS) {
294 }
else if (target_state == TARGET_STATE_BG_FAILED) {
299#ifdef USE_BINARY_SENSOR
300 if (target_state < TARGET_STATE_CALIBRATING_MIN) {
301 PUBLISH_STATE(this->target_binary_sensor_, target_state != 0x00);
302 PUBLISH_STATE(this->moving_target_binary_sensor_, target_state & MOVE_BITMASK);
303 PUBLISH_STATE(this->still_target_binary_sensor_, target_state & STILL_BITMASK);
314 SAFE_PUBLISH_SENSOR(this->moving_target_distance_sensor_,
317 SAFE_PUBLISH_SENSOR(this->still_target_distance_sensor_,
320 SAFE_PUBLISH_SENSOR(this->detection_distance_sensor_,
323 if (engineering_mode) {
329 for (uint8_t i = 0; i < TOTAL_GATES; i++) {
335 for (uint8_t i = 0; i < TOTAL_GATES; i++) {
343 for (
auto &gate_move_sensor : this->gate_move_sensors_) {
344 SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor);
346 for (
auto &gate_still_sensor : this->gate_still_sensors_) {
347 SAFE_PUBLISH_SENSOR_UNKNOWN(gate_still_sensor);
349 SAFE_PUBLISH_SENSOR_UNKNOWN(this->light_sensor_);
352#ifdef USE_BINARY_SENSOR
353 PUBLISH_STATE(this->out_pin_presence_status_binary_sensor_,
369 ESP_LOGE(TAG,
"Invalid status");
374#ifdef USE_LD2410_BG_CORRECTION
376 if (this->
buffer_data_[COMMAND] == CMD_BG_CORRECTION_START) {
384 case CMD_ENABLE_CONF:
385 ESP_LOGV(TAG,
"Enable conf");
388 case CMD_DISABLE_CONF:
389 ESP_LOGV(TAG,
"Disabled conf");
392 case CMD_SET_BAUD_RATE:
393 ESP_LOGV(TAG,
"Baud rate change");
395 if (this->baud_rate_select_ !=
nullptr) {
396 auto baud = this->baud_rate_select_->current_option();
397 ESP_LOGE(TAG,
"Change baud rate to %.*s and reinstall", (
int) baud.size(), baud.c_str());
402 case CMD_QUERY_VERSION: {
409 ESP_LOGV(TAG,
"Firmware version: %s", version_s);
410#ifdef USE_TEXT_SENSOR
411 PUBLISH_STATE(this->version_text_sensor_, version_s);
416 case CMD_QUERY_DISTANCE_RESOLUTION: {
422 ESP_LOGW(TAG,
"Unknown distance resolution %u", this->
buffer_data_[10]);
425 ESP_LOGV(TAG,
"Distance resolution: %s",
426 LOG_STR_ARG(DistanceResolutionStrings::get_log_str(*index, DistanceResolutionStrings::LAST_INDEX)));
428 PUBLISH_STATE(this->distance_resolution_select_, *index);
433 case CMD_QUERY_LIGHT_CONTROL: {
440 ESP_LOGV(TAG,
"Light function: %u, threshold: %u, out pin level: %u", this->
light_function_,
443 PUBLISH_STATE(this->light_function_select_,
static_cast<size_t>(this->
light_function_));
444 PUBLISH_STATE(this->out_pin_level_select_,
static_cast<size_t>(this->
out_pin_level_));
447 PUBLISH_STATE(this->light_threshold_number_,
static_cast<float>(this->
light_threshold_));
451 case CMD_QUERY_MAC_ADDRESS: {
458 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
460 ESP_LOGV(TAG,
"MAC address: %s", mac_str);
461#ifdef USE_TEXT_SENSOR
462 PUBLISH_STATE(this->mac_text_sensor_, mac_str);
471 ESP_LOGV(TAG,
"Sensitivity");
474#ifdef USE_LD2410_BG_CORRECTION
475 case CMD_BG_CORRECTION_START:
476 ESP_LOGV(TAG,
"Background correction started");
479 case CMD_BG_CORRECTION_QUERY: {
481 ESP_LOGV(TAG,
"Background correction status: %u",
status);
483 if (
status == BG_CORRECTION_COMPLETED ||
484 (
status == BG_CORRECTION_IDLE &&
493 ESP_LOGV(TAG,
"Bluetooth");
496 case CMD_SET_DISTANCE_RESOLUTION:
497 ESP_LOGV(TAG,
"Set distance resolution");
500 case CMD_SET_LIGHT_CONTROL:
501 ESP_LOGV(TAG,
"Set light control");
504 case CMD_BT_PASSWORD:
505 ESP_LOGV(TAG,
"Set bluetooth password");
524 for (
size_t i = 0; i < TOTAL_GATES; i++) {
530 for (
size_t i = 0; i < TOTAL_GATES; i++) {
552 [
this](uint8_t frame_len) {
return this->
handle_frame_(frame_len); }, []() { ESP_LOGW(TAG,
"Invalid length"); });
558 const uint8_t *tail = &this->
buffer_data_[frame_len - HEADER_FOOTER_SIZE];
560 if (!ld2410::validate_header_footer(DATA_FRAME_FOOTER, tail)) {
567 if (!ld2410::validate_header_footer(CMD_FRAME_FOOTER, tail)) {
576 const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
577 const uint8_t cmd_value[2] = {0x01, 0x00};
578 this->
send_command_(cmd, enable ? cmd_value :
nullptr,
sizeof(cmd_value));
581void LD2410Component::set_bluetooth(
bool enable) {
583 const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00};
584 this->
send_command_(CMD_BLUETOOTH, cmd_value,
sizeof(cmd_value));
585 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
588void LD2410Component::set_distance_resolution(
const char *
state) {
597 const uint8_t cmd_value[2] = {
resolution, 0x00};
598 this->
send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value,
sizeof(cmd_value));
599 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
602void LD2410Component::set_baud_rate(
const char *
state) {
605 this->set_baud_rate(
static_cast<BaudRate>(index + 1));
609void LD2410Component::set_baud_rate(
BaudRate rate) {
611 const uint8_t cmd_value[2] = {rate, 0x00};
612 this->
send_command_(CMD_SET_BAUD_RATE, cmd_value,
sizeof(cmd_value));
616void LD2410Component::set_bluetooth_password(
const std::string &password) {
617 if (password.length() != 6) {
618 ESP_LOGE(TAG,
"Password must be exactly 6 chars");
622 uint8_t cmd_value[6];
623 std::copy(password.begin(), password.end(), std::begin(cmd_value));
624 this->
send_command_(CMD_BT_PASSWORD, cmd_value,
sizeof(cmd_value));
628void LD2410Component::set_engineering_mode(
bool enable) {
629 const uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG;
635void LD2410Component::factory_reset() {
638 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
648 const uint8_t cmd_value[2] = {0x01, 0x00};
649 this->
send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value,
sizeof(cmd_value));
656#ifdef USE_LD2410_BG_CORRECTION
657void LD2410Component::start_background_correction() {
659 ESP_LOGW(TAG,
"Background correction already running");
662 const uint8_t major = this->
version_[1];
663 const uint8_t minor = this->
version_[0];
666 (major < BG_CORRECTION_MIN_MAJOR || (major == BG_CORRECTION_MIN_MAJOR && minor < BG_CORRECTION_MIN_MINOR))) {
667 ESP_LOGW(TAG,
"Background correction needs firmware V2.44 or later, module has V%u.%02X", major, minor);
680 this->
set_interval(BG_CORRECTION_POLL_ID, BG_CORRECTION_POLL_INTERVAL_MS, [
this]() {
697 ESP_LOGI(TAG,
"Background correction: %s",
698 LOG_STR_ARG(BgCorrectionResultStrings::get_log_str(
static_cast<uint8_t
>(result), UNCONFIRMED_INDEX)));
710#ifdef USE_BINARY_SENSOR
711 PUBLISH_STATE(this->calibration_binary_sensor_, running);
716#ifdef USE_TEXT_SENSOR
717 PUBLISH_STATE(this->calibration_result_text_sensor_,
718 BgCorrectionResultStrings::get_progmem_str(
static_cast<uint8_t
>(result), UNCONFIRMED_INDEX));
724void LD2410Component::set_max_distances_timeout() {
725 if (!this->max_move_distance_gate_number_->has_state() || !this->max_still_distance_gate_number_->has_state() ||
726 !this->timeout_number_->has_state()) {
729 int max_moving_distance_gate_range =
static_cast<int>(this->max_move_distance_gate_number_->state);
730 int max_still_distance_gate_range =
static_cast<int>(this->max_still_distance_gate_number_->state);
731 int timeout =
static_cast<int>(this->timeout_number_->state);
732 uint8_t
value[18] = {0x00,
734 lowbyte(max_moving_distance_gate_range),
735 highbyte(max_moving_distance_gate_range),
740 lowbyte(max_still_distance_gate_range),
741 highbyte(max_still_distance_gate_range),
753 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
757void LD2410Component::set_gate_threshold(number::Number *n) {
763 if (gate == TOTAL_GATES) {
764 ESP_LOGW(TAG,
"Threshold number is not a known gate");
767 number::Number *motionsens = this->gate_move_threshold_numbers_[gate];
770 if (!motionsens->has_state() || !stillsens->has_state()) {
773 int motion =
static_cast<int>(motionsens->state);
774 int still =
static_cast<int>(stillsens->state);
786 uint8_t
value[18] = {0x00, 0x00, lowbyte(gate), highbyte(gate), 0x00, 0x00,
787 0x01, 0x00, lowbyte(motion), highbyte(motion), 0x00, 0x00,
788 0x02, 0x00, lowbyte(still), highbyte(still), 0x00, 0x00};
794void LD2410Component::set_gate_still_threshold_number(uint8_t gate, number::Number *n) {
798void LD2410Component::set_gate_move_threshold_number(uint8_t gate, number::Number *n) {
799 this->gate_move_threshold_numbers_[gate] = n;
803void LD2410Component::set_light_out_control() {
805 if (this->light_threshold_number_ !=
nullptr && this->light_threshold_number_->has_state()) {
806 this->
light_threshold_ =
static_cast<uint8_t
>(this->light_threshold_number_->state);
810 if (this->light_function_select_ !=
nullptr) {
811 if (
const auto index = this->light_function_select_->active_index()) {
815 if (this->out_pin_level_select_ !=
nullptr) {
816 if (
const auto index = this->out_pin_level_select_->active_index()) {
825 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
830void LD2410Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) {
831 this->gate_move_sensors_[gate].set_sensor(s);
834void LD2410Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) {
835 this->gate_still_sensors_[gate].set_sensor(s);