15static const char *
const TAG =
"ld2410";
16static const char *
const UNKNOWN_MAC =
"unknown";
17static const char *
const VERSION_FMT =
"%u.%02X.%02X%02X%02X%02X";
124template<
size_t N> uint8_t
find_uint8(
const StringToUint8 (&arr)[N],
const std::string &str) {
125 for (
const auto &entry : arr) {
126 if (str == entry.str)
132template<
size_t N>
const char *
find_str(
const Uint8ToString (&arr)[N], uint8_t value) {
133 for (
const auto &entry : arr) {
134 if (value == entry.value)
141static constexpr uint8_t CMD_ENABLE_CONF = 0xFF;
142static constexpr uint8_t CMD_DISABLE_CONF = 0xFE;
143static constexpr uint8_t CMD_ENABLE_ENG = 0x62;
144static constexpr uint8_t CMD_DISABLE_ENG = 0x63;
145static constexpr uint8_t CMD_MAXDIST_DURATION = 0x60;
146static constexpr uint8_t CMD_QUERY = 0x61;
147static constexpr uint8_t CMD_GATE_SENS = 0x64;
148static constexpr uint8_t CMD_QUERY_VERSION = 0xA0;
149static constexpr uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0xAB;
150static constexpr uint8_t CMD_SET_DISTANCE_RESOLUTION = 0xAA;
151static constexpr uint8_t CMD_QUERY_LIGHT_CONTROL = 0xAE;
152static constexpr uint8_t CMD_SET_LIGHT_CONTROL = 0xAD;
153static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1;
154static constexpr uint8_t CMD_BT_PASSWORD = 0xA9;
155static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5;
156static constexpr uint8_t CMD_RESET = 0xA2;
157static constexpr uint8_t CMD_RESTART = 0xA3;
158static constexpr uint8_t CMD_BLUETOOTH = 0xA4;
160static constexpr uint8_t CMD_MAX_MOVE_VALUE = 0x00;
161static constexpr uint8_t CMD_MAX_STILL_VALUE = 0x01;
162static constexpr uint8_t CMD_DURATION_VALUE = 0x02;
164static constexpr uint8_t MOVE_BITMASK = 0x01;
165static constexpr uint8_t STILL_BITMASK = 0x02;
167static constexpr uint8_t HEADER_FOOTER_SIZE = 4;
169static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xFD, 0xFC, 0xFB, 0xFA};
170static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0x04, 0x03, 0x02, 0x01};
172static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xF4, 0xF3, 0xF2, 0xF1};
173static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0xF8, 0xF7, 0xF6, 0xF5};
175static constexpr uint8_t NO_MAC[] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01};
177static inline int two_byte_to_int(
char firstbyte,
char secondbyte) {
return (int16_t) (secondbyte << 8) + firstbyte; }
179static inline bool validate_header_footer(
const uint8_t *header_footer,
const uint8_t *buffer) {
180 return std::memcmp(header_footer, buffer, HEADER_FOOTER_SIZE) == 0;
183void LD2410Component::dump_config() {
184 std::string mac_str =
190 " Firmware version: %s\n"
192 version.c_str(), mac_str.c_str());
193#ifdef USE_BINARY_SENSOR
194 ESP_LOGCONFIG(TAG,
"Binary Sensors:");
195 LOG_BINARY_SENSOR(
" ",
"Target", this->target_binary_sensor_);
196 LOG_BINARY_SENSOR(
" ",
"MovingTarget", this->moving_target_binary_sensor_);
197 LOG_BINARY_SENSOR(
" ",
"StillTarget", this->still_target_binary_sensor_);
198 LOG_BINARY_SENSOR(
" ",
"OutPinPresenceStatus", this->out_pin_presence_status_binary_sensor_);
201 ESP_LOGCONFIG(TAG,
"Sensors:");
202 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"Light", this->light_sensor_);
203 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"DetectionDistance", this->detection_distance_sensor_);
204 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"MovingTargetDistance", this->moving_target_distance_sensor_);
205 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"MovingTargetEnergy", this->moving_target_energy_sensor_);
206 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"StillTargetDistance", this->still_target_distance_sensor_);
207 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"StillTargetEnergy", this->still_target_energy_sensor_);
209 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"GateMove", s);
212 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"GateStill", s);
215#ifdef USE_TEXT_SENSOR
216 ESP_LOGCONFIG(TAG,
"Text Sensors:");
217 LOG_TEXT_SENSOR(
" ",
"Mac", this->mac_text_sensor_);
218 LOG_TEXT_SENSOR(
" ",
"Version", this->version_text_sensor_);
221 ESP_LOGCONFIG(TAG,
"Numbers:");
222 LOG_NUMBER(
" ",
"LightThreshold", this->light_threshold_number_);
223 LOG_NUMBER(
" ",
"MaxMoveDistanceGate", this->max_move_distance_gate_number_);
224 LOG_NUMBER(
" ",
"MaxStillDistanceGate", this->max_still_distance_gate_number_);
225 LOG_NUMBER(
" ",
"Timeout", this->timeout_number_);
227 LOG_NUMBER(
" ",
"MoveThreshold", n);
230 LOG_NUMBER(
" ",
"StillThreshold", n);
234 ESP_LOGCONFIG(TAG,
"Selects:");
235 LOG_SELECT(
" ",
"BaudRate", this->baud_rate_select_);
236 LOG_SELECT(
" ",
"DistanceResolution", this->distance_resolution_select_);
237 LOG_SELECT(
" ",
"LightFunction", this->light_function_select_);
238 LOG_SELECT(
" ",
"OutPinLevel", this->out_pin_level_select_);
241 ESP_LOGCONFIG(TAG,
"Switches:");
242 LOG_SWITCH(
" ",
"Bluetooth", this->bluetooth_switch_);
243 LOG_SWITCH(
" ",
"EngineeringMode", this->engineering_mode_switch_);
246 ESP_LOGCONFIG(TAG,
"Buttons:");
247 LOG_BUTTON(
" ",
"FactoryReset", this->factory_reset_button_);
248 LOG_BUTTON(
" ",
"Query", this->query_button_);
249 LOG_BUTTON(
" ",
"Restart", this->restart_button_);
255void LD2410Component::read_all_info() {
265 if (this->baud_rate_select_ !=
nullptr) {
266 this->baud_rate_select_->publish_state(baud_rate);
271void LD2410Component::restart_and_read_all_info() {
274 this->
set_timeout(1000, [
this]() { this->read_all_info(); });
277void LD2410Component::loop() {
284 ESP_LOGV(TAG,
"Sending COMMAND %02X", command);
286 this->
write_array(CMD_FRAME_HEADER,
sizeof(CMD_FRAME_HEADER));
289 if (command_value !=
nullptr) {
290 len += command_value_len;
293 uint8_t len_cmd[] = {
len, 0x00, command, 0x00};
296 if (command_value !=
nullptr) {
297 this->
write_array(command_value, command_value_len);
300 this->
write_array(CMD_FRAME_FOOTER,
sizeof(CMD_FRAME_FOOTER));
302 if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
322 if (this->engineering_mode_switch_ !=
nullptr) {
323 this->engineering_mode_switch_->publish_state(engineering_mode);
326#ifdef USE_BINARY_SENSOR
335 if (this->target_binary_sensor_ !=
nullptr) {
336 this->target_binary_sensor_->publish_state(target_state != 0x00);
338 if (this->moving_target_binary_sensor_ !=
nullptr) {
339 this->moving_target_binary_sensor_->publish_state(target_state & MOVE_BITMASK);
341 if (this->still_target_binary_sensor_ !=
nullptr) {
342 this->still_target_binary_sensor_->publish_state(target_state & STILL_BITMASK);
354 this->moving_target_distance_sensor_,
358 this->still_target_distance_sensor_,
362 this->detection_distance_sensor_,
365 if (engineering_mode) {
371 for (uint8_t i = 0; i < TOTAL_GATES; i++) {
377 for (uint8_t i = 0; i < TOTAL_GATES; i++) {
385 for (
auto &gate_move_sensor : this->gate_move_sensors_) {
386 SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor)
388 for (
auto &gate_still_sensor : this->gate_still_sensors_) {
389 SAFE_PUBLISH_SENSOR_UNKNOWN(gate_still_sensor)
391 SAFE_PUBLISH_SENSOR_UNKNOWN(this->light_sensor_)
394#ifdef USE_BINARY_SENSOR
395 if (this->out_pin_presence_status_binary_sensor_ !=
nullptr) {
396 this->out_pin_presence_status_binary_sensor_->publish_state(
415 ESP_LOGE(TAG,
"Invalid length");
418 if (!ld2410::validate_header_footer(CMD_FRAME_HEADER, this->
buffer_data_)) {
423 ESP_LOGE(TAG,
"Invalid status");
432 case CMD_ENABLE_CONF:
433 ESP_LOGV(TAG,
"Enable conf");
436 case CMD_DISABLE_CONF:
437 ESP_LOGV(TAG,
"Disabled conf");
440 case CMD_SET_BAUD_RATE:
441 ESP_LOGV(TAG,
"Baud rate change");
443 if (this->baud_rate_select_ !=
nullptr) {
444 ESP_LOGE(TAG,
"Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
449 case CMD_QUERY_VERSION: {
453 ESP_LOGV(TAG,
"Firmware version: %s", version.c_str());
454#ifdef USE_TEXT_SENSOR
455 if (this->version_text_sensor_ !=
nullptr) {
456 this->version_text_sensor_->publish_state(version);
462 case CMD_QUERY_DISTANCE_RESOLUTION: {
464 ESP_LOGV(TAG,
"Distance resolution: %s", distance_resolution);
466 if (this->distance_resolution_select_ !=
nullptr) {
467 this->distance_resolution_select_->publish_state(distance_resolution);
473 case CMD_QUERY_LIGHT_CONTROL: {
480 "Light function: %s\n"
481 "Light threshold: %u\n"
485 if (this->light_function_select_ !=
nullptr) {
486 this->light_function_select_->publish_state(light_function_str);
488 if (this->out_pin_level_select_ !=
nullptr) {
489 this->out_pin_level_select_->publish_state(out_pin_level_str);
493 if (this->light_threshold_number_ !=
nullptr) {
494 this->light_threshold_number_->publish_state(
static_cast<float>(this->
light_threshold_));
499 case CMD_QUERY_MAC_ADDRESS: {
509 std::string mac_str =
511 ESP_LOGV(TAG,
"MAC address: %s", mac_str.c_str());
512#ifdef USE_TEXT_SENSOR
513 if (this->mac_text_sensor_ !=
nullptr) {
514 this->mac_text_sensor_->publish_state(mac_str);
518 if (this->bluetooth_switch_ !=
nullptr) {
526 ESP_LOGV(TAG,
"Sensitivity");
530 ESP_LOGV(TAG,
"Bluetooth");
533 case CMD_SET_DISTANCE_RESOLUTION:
534 ESP_LOGV(TAG,
"Set distance resolution");
537 case CMD_SET_LIGHT_CONTROL:
538 ESP_LOGV(TAG,
"Set light control");
541 case CMD_BT_PASSWORD:
542 ESP_LOGV(TAG,
"Set bluetooth password");
553 std::vector<std::function<void(
void)>> updates;
559 for (std::vector<number::Number *>::size_type i = 0; i != this->gate_move_threshold_numbers_.size(); i++) {
573 for (
auto &update : updates) {
596 ESP_LOGW(TAG,
"Max command length exceeded; ignoring");
611 ESP_LOGV(TAG,
"Ack Data incomplete");
617 const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
618 const uint8_t cmd_value[2] = {0x01, 0x00};
619 this->
send_command_(cmd, enable ? cmd_value :
nullptr,
sizeof(cmd_value));
622void LD2410Component::set_bluetooth(
bool enable) {
624 const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00};
625 this->
send_command_(CMD_BLUETOOTH, cmd_value,
sizeof(cmd_value));
626 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
629void LD2410Component::set_distance_resolution(
const std::string &
state) {
632 this->
send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value,
sizeof(cmd_value));
633 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
636void LD2410Component::set_baud_rate(
const std::string &
state) {
639 this->
send_command_(CMD_SET_BAUD_RATE, cmd_value,
sizeof(cmd_value));
643void LD2410Component::set_bluetooth_password(
const std::string &password) {
644 if (password.length() != 6) {
645 ESP_LOGE(TAG,
"Password must be exactly 6 chars");
649 uint8_t cmd_value[6];
650 std::copy(password.begin(), password.end(), std::begin(cmd_value));
651 this->
send_command_(CMD_BT_PASSWORD, cmd_value,
sizeof(cmd_value));
655void LD2410Component::set_engineering_mode(
bool enable) {
656 const uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG;
662void LD2410Component::factory_reset() {
665 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
675 const uint8_t cmd_value[2] = {0x01, 0x00};
676 this->
send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value,
sizeof(cmd_value));
684void LD2410Component::set_max_distances_timeout() {
685 if (!this->max_move_distance_gate_number_->has_state() || !this->max_still_distance_gate_number_->has_state() ||
686 !this->timeout_number_->has_state()) {
689 int max_moving_distance_gate_range =
static_cast<int>(this->max_move_distance_gate_number_->state);
690 int max_still_distance_gate_range =
static_cast<int>(this->max_still_distance_gate_number_->state);
691 int timeout =
static_cast<int>(this->timeout_number_->state);
692 uint8_t value[18] = {0x00,
694 lowbyte(max_moving_distance_gate_range),
695 highbyte(max_moving_distance_gate_range),
700 lowbyte(max_still_distance_gate_range),
701 highbyte(max_still_distance_gate_range),
711 this->
send_command_(CMD_MAXDIST_DURATION, value,
sizeof(value));
713 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
717void LD2410Component::set_gate_threshold(uint8_t gate) {
718 number::Number *motionsens = this->gate_move_threshold_numbers_[gate];
721 if (!motionsens->has_state() || !stillsens->has_state()) {
724 int motion =
static_cast<int>(motionsens->state);
725 int still =
static_cast<int>(stillsens->state);
737 uint8_t value[18] = {0x00, 0x00, lowbyte(gate), highbyte(gate), 0x00, 0x00,
738 0x01, 0x00, lowbyte(motion), highbyte(motion), 0x00, 0x00,
739 0x02, 0x00, lowbyte(still), highbyte(still), 0x00, 0x00};
745void LD2410Component::set_gate_still_threshold_number(uint8_t gate, number::Number *n) {
749void LD2410Component::set_gate_move_threshold_number(uint8_t gate, number::Number *n) {
750 this->gate_move_threshold_numbers_[gate] = n;
754void LD2410Component::set_light_out_control() {
756 if (this->light_threshold_number_ !=
nullptr && this->light_threshold_number_->has_state()) {
757 this->
light_threshold_ =
static_cast<uint8_t
>(this->light_threshold_number_->state);
761 if (this->light_function_select_ !=
nullptr && this->light_function_select_->has_state()) {
764 if (this->out_pin_level_select_ !=
nullptr && this->out_pin_level_select_->has_state()) {
770 this->
send_command_(CMD_SET_LIGHT_CONTROL, value,
sizeof(value));
772 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
778void LD2410Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) {
779 this->gate_move_sensors_[gate] =
new SensorWithDedup<uint8_t>(s);
782void LD2410Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) {
783 this->gate_still_sensors_[gate] =
new SensorWithDedup<uint8_t>(s);
virtual void setup()
Where the component's initialization should happen.
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
std::array< number::Number *, TOTAL_GATES > gate_still_threshold_numbers_
void get_distance_resolution_()
void set_config_mode_(bool enable)
std::array< number::Number *, TOTAL_GATES > gate_move_threshold_numbers_
void readline_(int readch)
std::array< SensorWithDedup< uint8_t > *, TOTAL_GATES > gate_still_sensors_
std::array< SensorWithDedup< uint8_t > *, TOTAL_GATES > gate_move_sensors_
void handle_periodic_data_()
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
uint8_t buffer_data_[MAX_LINE_LENGTH]
void query_light_control_()
Base-class for all numbers.
void publish_state(float state)
uint32_t get_baud_rate() const
void write_array(const uint8_t *data, size_t len)
constexpr Uint8ToString DISTANCE_RESOLUTIONS_BY_UINT[]
constexpr StringToUint8 DISTANCE_RESOLUTIONS_BY_STR[]
constexpr StringToUint8 BAUD_RATES_BY_STR[]
constexpr StringToUint8 OUT_PIN_LEVELS_BY_STR[]
const char * find_str(const Uint8ToString(&arr)[N], uint8_t value)
std::function< void(void)> set_number_value(number::Number *n, float value)
@ DISTANCE_RESOLUTION_0_2
@ DISTANCE_RESOLUTION_0_75
constexpr Uint8ToString OUT_PIN_LEVELS_BY_UINT[]
constexpr StringToUint8 LIGHT_FUNCTIONS_BY_STR[]
uint8_t find_uint8(const StringToUint8(&arr)[N], const std::string &str)
constexpr Uint8ToString LIGHT_FUNCTIONS_BY_UINT[]
Providing packet encoding functions for exchanging data with a remote host.
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
std::string format_mac_address_pretty(const uint8_t *mac)
std::string str_sprintf(const char *fmt,...)
void IRAM_ATTR HOT delay(uint32_t ms)