70PROGMEM_STRING_TABLE(BaudRateStrings,
"9600",
"19200",
"38400",
"57600",
"115200",
"230400",
"256000",
"460800");
76 "LightFunction is the select option index");
80static constexpr uint32_t BAUD_RATES[] PROGMEM = {9600, 19200, 38400, 57600, 115200, 230400, 256000, 460800};
83static constexpr uint8_t CMD_ENABLE_CONF = 0xFF;
84static constexpr uint8_t CMD_DISABLE_CONF = 0xFE;
85static constexpr uint8_t CMD_ENABLE_ENG = 0x62;
86static constexpr uint8_t CMD_DISABLE_ENG = 0x63;
87static constexpr uint8_t CMD_QUERY_BASIC_CONF = 0x12;
88static constexpr uint8_t CMD_BASIC_CONF = 0x02;
89static constexpr uint8_t CMD_QUERY_VERSION = 0xA0;
90static constexpr uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0x11;
91static constexpr uint8_t CMD_SET_DISTANCE_RESOLUTION = 0x01;
92static constexpr uint8_t CMD_QUERY_LIGHT_CONTROL = 0x1C;
93static constexpr uint8_t CMD_SET_LIGHT_CONTROL = 0x0C;
94static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1;
95static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5;
96static constexpr uint8_t CMD_FACTORY_RESET = 0xA2;
97static constexpr uint8_t CMD_RESTART = 0xA3;
98static constexpr uint8_t CMD_BLUETOOTH = 0xA4;
99static constexpr uint8_t CMD_DYNAMIC_BACKGROUND_CORRECTION = 0x0B;
100static constexpr uint8_t CMD_QUERY_DYNAMIC_BACKGROUND_CORRECTION = 0x1B;
101static constexpr uint8_t CMD_MOTION_GATE_SENS = 0x03;
102static constexpr uint8_t CMD_QUERY_MOTION_GATE_SENS = 0x13;
103static constexpr uint8_t CMD_STATIC_GATE_SENS = 0x04;
104static constexpr uint8_t CMD_QUERY_STATIC_GATE_SENS = 0x14;
105static constexpr uint8_t CMD_NONE = 0x00;
107static constexpr uint8_t CMD_MAX_MOVE_VALUE = 0x00;
108static constexpr uint8_t CMD_MAX_STILL_VALUE = 0x01;
109static constexpr uint8_t CMD_DURATION_VALUE = 0x02;
111static constexpr uint8_t MOVE_BITMASK = 0x01;
112static constexpr uint8_t STILL_BITMASK = 0x02;
113using ld24xx::CMD_FRAME_FOOTER;
114using ld24xx::CMD_FRAME_HEADER;
115using ld24xx::HEADER_FOOTER_SIZE;
118static constexpr uint8_t PERIODIC_FRAME_MIN =
STILL_ENERGY + 3 + HEADER_FOOTER_SIZE;
119static constexpr uint8_t ENGINEERING_FRAME_MIN =
LIGHT_SENSOR + 3 + HEADER_FOOTER_SIZE;
121static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xF4, 0xF3, 0xF2, 0xF1};
122static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0xF8, 0xF7, 0xF6, 0xF5};
123static constexpr uint8_t GATE_SENS_ACK_SIZE =
ACK_PAYLOAD + TOTAL_GATES + HEADER_FOOTER_SIZE;
125void LD2412Component::dump_config() {
126 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
132 " Firmware version: %s\n"
135#ifdef USE_BINARY_SENSOR
136 ESP_LOGCONFIG(TAG,
"Binary Sensors:");
137 LOG_BINARY_SENSOR(
" ",
"DynamicBackgroundCorrectionStatus",
138 this->dynamic_background_correction_status_binary_sensor_);
139 LOG_BINARY_SENSOR(
" ",
"MovingTarget", this->moving_target_binary_sensor_);
140 LOG_BINARY_SENSOR(
" ",
"StillTarget", this->still_target_binary_sensor_);
141 LOG_BINARY_SENSOR(
" ",
"Target", this->target_binary_sensor_);
144 ESP_LOGCONFIG(TAG,
"Sensors:");
145 LOG_SENSOR_WITH_DEDUP(
" ",
"Light", this->light_sensor_);
146 LOG_SENSOR_WITH_DEDUP(
" ",
"DetectionDistance", this->detection_distance_sensor_);
147 LOG_SENSOR_WITH_DEDUP(
" ",
"MovingTargetDistance", this->moving_target_distance_sensor_);
148 LOG_SENSOR_WITH_DEDUP(
" ",
"MovingTargetEnergy", this->moving_target_energy_sensor_);
149 LOG_SENSOR_WITH_DEDUP(
" ",
"StillTargetDistance", this->still_target_distance_sensor_);
150 LOG_SENSOR_WITH_DEDUP(
" ",
"StillTargetEnergy", this->still_target_energy_sensor_);
152 LOG_SENSOR_WITH_DEDUP(
" ",
"GateStill", s);
155 LOG_SENSOR_WITH_DEDUP(
" ",
"GateMove", s);
158#ifdef USE_TEXT_SENSOR
159 ESP_LOGCONFIG(TAG,
"Text Sensors:");
160 LOG_TEXT_SENSOR(
" ",
"MAC address", this->mac_text_sensor_);
161 LOG_TEXT_SENSOR(
" ",
"Version", this->version_text_sensor_);
164 ESP_LOGCONFIG(TAG,
"Numbers:");
165 LOG_NUMBER(
" ",
"LightThreshold", this->light_threshold_number_);
166 LOG_NUMBER(
" ",
"MaxDistanceGate", this->max_distance_gate_number_);
167 LOG_NUMBER(
" ",
"MinDistanceGate", this->min_distance_gate_number_);
168 LOG_NUMBER(
" ",
"Timeout", this->timeout_number_);
170 LOG_NUMBER(
" ",
"Move Thresholds", n);
173 LOG_NUMBER(
" ",
"Still Thresholds", n);
177 ESP_LOGCONFIG(TAG,
"Selects:");
178 LOG_SELECT(
" ",
"BaudRate", this->baud_rate_select_);
179 LOG_SELECT(
" ",
"DistanceResolution", this->distance_resolution_select_);
180 LOG_SELECT(
" ",
"LightFunction", this->light_function_select_);
181 LOG_SELECT(
" ",
"OutPinLevel", this->out_pin_level_select_);
184 ESP_LOGCONFIG(TAG,
"Switches:");
185 LOG_SWITCH(
" ",
"Bluetooth", this->bluetooth_switch_);
186 LOG_SWITCH(
" ",
"EngineeringMode", this->engineering_mode_switch_);
189 ESP_LOGCONFIG(TAG,
"Buttons:");
190 LOG_BUTTON(
" ",
"FactoryReset", this->factory_reset_button_);
191 LOG_BUTTON(
" ",
"Query", this->query_button_);
192 LOG_BUTTON(
" ",
"Restart", this->restart_button_);
193 LOG_BUTTON(
" ",
"StartDynamicBackgroundCorrection", this->start_dynamic_background_correction_button_);
198 ESP_LOGCONFIG(TAG,
"Running setup");
199 this->read_all_info();
202void LD2412Component::read_all_info() {
217 this->get_gate_threshold();
222 if (this->baud_rate_select_ !=
nullptr) {
224 this->baud_rate_select_->publish_state(*index);
230void LD2412Component::restart_and_read_all_info() {
233 this->
set_timeout(1000, [
this]() { this->read_all_info(); });
236void LD2412Component::loop() {
241 ESP_LOGV(TAG,
"Sending COMMAND %02X", command);
243 this->
write_array(CMD_FRAME_HEADER, HEADER_FOOTER_SIZE);
246 if (command_value !=
nullptr) {
247 len += command_value_len;
250 uint8_t len_cmd[] = {
len, 0x00, command, 0x00};
254 if (command_value !=
nullptr) {
255 this->
write_array(command_value, command_value_len);
258 this->
write_array(CMD_FRAME_FOOTER, HEADER_FOOTER_SIZE);
260 if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
283 PUBLISH_STATE(this->engineering_mode_switch_, engineering_mode);
286#ifdef USE_BINARY_SENSOR
295 PUBLISH_STATE(this->target_binary_sensor_, target_state != 0x00);
296 PUBLISH_STATE(this->moving_target_binary_sensor_, target_state & MOVE_BITMASK);
297 PUBLISH_STATE(this->still_target_binary_sensor_, target_state & STILL_BITMASK);
307 SAFE_PUBLISH_SENSOR(this->moving_target_distance_sensor_,
310 SAFE_PUBLISH_SENSOR(this->still_target_distance_sensor_,
313 if (this->detection_distance_sensor_.has_sensor()) {
314 int new_detect_distance = 0;
315 if (target_state != 0x00 && (target_state & MOVE_BITMASK)) {
316 new_detect_distance =
318 }
else if (target_state != 0x00) {
321 this->detection_distance_sensor_.publish_state_if_not_dup(new_detect_distance);
323 if (engineering_mode) {
325 ESP_LOGW(TAG,
"Engineering mode packet too short: %u", this->
buffer_pos_);
332 for (uint8_t i = 0; i < TOTAL_GATES; i++) {
338 for (uint8_t i = 0; i < TOTAL_GATES; i++) {
347 for (
auto &gate_move_sensor : this->gate_move_sensors_) {
348 SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor);
350 for (
auto &gate_still_sensor : this->gate_still_sensors_) {
351 SAFE_PUBLISH_SENSOR_UNKNOWN(gate_still_sensor);
353 SAFE_PUBLISH_SENSOR_UNKNOWN(this->light_sensor_);
375static constexpr uint32_t GATE_QUERY_TIMEOUT_ID = 0;
378static constexpr uint32_t BASIC_CONF_TIMEOUT_ID = 2;
379static constexpr uint32_t QUERY_TIMEOUT_MS = 2000;
384 ESP_LOGW(TAG,
"Invalid status");
393 case CMD_ENABLE_CONF:
394 ESP_LOGV(TAG,
"Enable conf");
397 case CMD_DISABLE_CONF:
398 ESP_LOGV(TAG,
"Disabled conf");
401 case CMD_SET_BAUD_RATE:
402 ESP_LOGV(TAG,
"Baud rate change");
404 if (this->baud_rate_select_ !=
nullptr) {
405 auto baud = this->baud_rate_select_->current_option();
406 ESP_LOGW(TAG,
"Change baud rate to %.*s and reinstall", (
int) baud.size(), baud.c_str());
411 case CMD_QUERY_VERSION: {
418 ESP_LOGV(TAG,
"Firmware version: %s", version_s);
419#ifdef USE_TEXT_SENSOR
420 PUBLISH_STATE(this->version_text_sensor_, version_s);
424 case CMD_QUERY_DISTANCE_RESOLUTION: {
430 ESP_LOGW(TAG,
"Unknown distance resolution %u", this->
buffer_data_[10]);
433 ESP_LOGV(TAG,
"Distance resolution: %s",
434 LOG_STR_ARG(DistanceResolutionStrings::get_log_str(*index, DistanceResolutionStrings::LAST_INDEX)));
436 PUBLISH_STATE(this->distance_resolution_select_, *index);
441 case CMD_QUERY_LIGHT_CONTROL: {
449 PUBLISH_STATE(this->light_function_select_,
static_cast<size_t>(this->
light_function_));
452 PUBLISH_STATE(this->light_threshold_number_,
static_cast<float>(this->
light_threshold_));
457 case CMD_QUERY_MAC_ADDRESS: {
464 char mac_s[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
466 ESP_LOGV(TAG,
"MAC address: %s", mac_str);
467#ifdef USE_TEXT_SENSOR
468 PUBLISH_STATE(this->mac_text_sensor_, mac_str);
476 case CMD_SET_DISTANCE_RESOLUTION:
477 ESP_LOGV(TAG,
"Handled set distance resolution command");
480 case CMD_QUERY_DYNAMIC_BACKGROUND_CORRECTION: {
484 ESP_LOGV(TAG,
"Handled query dynamic background correction");
485 bool dynamic_background_correction_active = (this->
buffer_data_[10] != 0x00);
486#ifdef USE_BINARY_SENSOR
487 PUBLISH_STATE(this->dynamic_background_correction_status_binary_sensor_, dynamic_background_correction_active);
494 ESP_LOGV(TAG,
"Handled bluetooth command");
497 case CMD_SET_LIGHT_CONTROL:
498 ESP_LOGV(TAG,
"Handled set light control command");
501 case CMD_QUERY_MOTION_GATE_SENS:
502 case CMD_QUERY_STATIC_GATE_SENS: {
506 ESP_LOGW(TAG,
"Dropping gate threshold answer of %u bytes", this->
buffer_pos_);
512 const bool pending = this->
cancel_timeout(GATE_QUERY_TIMEOUT_ID + motion);
513 for (
size_t i = 0; i < TOTAL_GATES; i++) {
516 if (!pending || !has_value(group.
numbers[i])) {
530 case CMD_QUERY_BASIC_CONF: {
542 ESP_LOGV(TAG,
"min_distance_gate_number_: %u, max_distance_gate_number_ %u", this->
min_gate_, this->
max_gate_);
543 ESP_LOGV(TAG,
"timeout_number_: %u", this->
timeout_);
546 if (!pending || !has_value(this->min_distance_gate_number_)) {
549 if (!pending || !has_value(this->max_distance_gate_number_)) {
552 if (!pending || !has_value(this->timeout_number_)) {
557 if (this->out_pin_level_select_ !=
nullptr && !(pending && this->out_pin_level_select_->has_state())) {
559 this->out_pin_level_select_->publish_state(*index);
564 this->set_basic_config();
580 [
this](uint8_t frame_len) {
return this->
handle_frame_(frame_len); }, []() { ESP_LOGW(TAG,
"Invalid length"); });
586 const uint8_t *tail = &this->
buffer_data_[frame_len - HEADER_FOOTER_SIZE];
588 if (!ld2412::validate_header_footer(DATA_FRAME_FOOTER, tail)) {
595 if (!ld2412::validate_header_footer(CMD_FRAME_FOOTER, tail)) {
604 const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
605 const uint8_t cmd_value[2] = {0x01, 0x00};
606 this->
send_command_(cmd, enable ? cmd_value :
nullptr,
sizeof(cmd_value));
609void LD2412Component::set_bluetooth(
bool enable) {
611 const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00};
612 this->
send_command_(CMD_BLUETOOTH, cmd_value,
sizeof(cmd_value));
613 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
616void LD2412Component::set_distance_resolution(
const char *
state) {
625 const uint8_t cmd_value[6] = {
resolution, 0x00, 0x00, 0x00, 0x00, 0x00};
626 this->
send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value,
sizeof(cmd_value));
627 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
630void LD2412Component::set_baud_rate(
const char *
state) {
633 this->set_baud_rate(
static_cast<BaudRate>(index + 1));
637void LD2412Component::set_baud_rate(
BaudRate rate) {
639 const uint8_t cmd_value[2] = {rate, 0x00};
640 this->
send_command_(CMD_SET_BAUD_RATE, cmd_value,
sizeof(cmd_value));
645 this->
send_command_(CMD_QUERY_DYNAMIC_BACKGROUND_CORRECTION,
nullptr, 0);
648void LD2412Component::start_dynamic_background_correction() {
652#ifdef USE_BINARY_SENSOR
653 PUBLISH_STATE(this->dynamic_background_correction_status_binary_sensor_,
true);
657 this->
send_command_(CMD_DYNAMIC_BACKGROUND_CORRECTION,
nullptr, 0);
661void LD2412Component::set_engineering_mode(
bool enable) {
662 const uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG;
668void LD2412Component::factory_reset() {
677 this->
set_timeout(2000, [
this]() { this->restart_and_read_all_info(); });
687 const uint8_t cmd_value[2] = {0x01, 0x00};
688 this->
send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value,
sizeof(cmd_value));
695void LD2412Component::set_basic_config() {
701 bool from_module =
false;
704 from_module = this->min_distance_gate_number_ ==
nullptr || this->max_distance_gate_number_ ==
nullptr ||
705 this->timeout_number_ ==
nullptr;
714 if (this->min_distance_gate_number_ !=
nullptr) {
715 if (!this->min_distance_gate_number_->has_state())
717 min_gate =
static_cast<int>(this->min_distance_gate_number_->state);
719 if (this->max_distance_gate_number_ !=
nullptr) {
720 if (!this->max_distance_gate_number_->has_state())
722 max_gate =
static_cast<int>(this->max_distance_gate_number_->state) + 1;
724 if (this->timeout_number_ !=
nullptr) {
725 if (!this->timeout_number_->has_state())
727 timeout =
static_cast<int>(this->timeout_number_->state);
731 if (this->out_pin_level_select_ !=
nullptr) {
732 const auto index = this->out_pin_level_select_->active_index();
742 ESP_LOGW(TAG,
"Command %02X held back until the module reports its settings", CMD_BASIC_CONF);
751 lowbyte(min_gate), lowbyte(max_gate), lowbyte(timeout), highbyte(timeout), out_pin_level,
759 ESP_LOGE(TAG,
"Command %02X not sent, the module did not report its settings", CMD_BASIC_CONF);
762 for (
number::Number *n : {this->min_distance_gate_number_, this->max_distance_gate_number_, this->timeout_number_}) {
763 PUBLISH_STATE(n, NAN);
768 if (this->out_pin_level_select_ !=
nullptr) {
770 this->out_pin_level_select_->publish_state(*index);
779 const uint8_t command = motion ? CMD_MOTION_GATE_SENS : CMD_STATIC_GATE_SENS;
780 uint8_t
value[TOTAL_GATES];
781 uint8_t configured = 0;
782 for (uint8_t i = 0; i < TOTAL_GATES; i++) {
791 if (configured < TOTAL_GATES && !group.
read) {
793 ESP_LOGW(TAG,
"Command %02X held back until the module reports its settings", command);
794 this->
send_command_(motion ? CMD_QUERY_MOTION_GATE_SENS : CMD_QUERY_STATIC_GATE_SENS,
nullptr, 0);
795 this->
set_timeout(GATE_QUERY_TIMEOUT_ID + motion, QUERY_TIMEOUT_MS,
803 ESP_LOGE(TAG,
"Command %02X not sent, the module did not report its settings",
804 motion ? CMD_MOTION_GATE_SENS : CMD_STATIC_GATE_SENS);
807 PUBLISH_STATE(n, NAN);
814 const bool motion = std::find(motion_numbers.begin(), motion_numbers.end(), n) != motion_numbers.end();
820void LD2412Component::get_gate_threshold() {
827void LD2412Component::set_light_out_control() {
829 if (this->light_threshold_number_ !=
nullptr && this->light_threshold_number_->has_state()) {
830 this->
light_threshold_ =
static_cast<uint8_t
>(this->light_threshold_number_->state);
834 if (this->light_function_select_ !=
nullptr) {
835 if (
const auto index = this->light_function_select_->active_index()) {
844 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
848void LD2412Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) {
849 this->gate_move_sensors_[gate].set_sensor(s);
851void LD2412Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) {
852 this->gate_still_sensors_[gate].set_sensor(s);