18static const char *
const TAG =
"ld2450";
92template<
size_t N> uint8_t
find_uint8(
const StringToUint8 (&arr)[N],
const std::string &str) {
93 for (
const auto &entry : arr) {
100template<
size_t N>
const char *
find_str(
const Uint8ToString (&arr)[N], uint8_t value) {
101 for (
const auto &entry : arr) {
102 if (value == entry.value)
109static constexpr uint8_t CMD_ENABLE_CONF = 0xFF;
110static constexpr uint8_t CMD_DISABLE_CONF = 0xFE;
111static constexpr uint8_t CMD_QUERY_VERSION = 0xA0;
112static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5;
113static constexpr uint8_t CMD_RESET = 0xA2;
114static constexpr uint8_t CMD_RESTART = 0xA3;
115static constexpr uint8_t CMD_BLUETOOTH = 0xA4;
116static constexpr uint8_t CMD_SINGLE_TARGET_MODE = 0x80;
117static constexpr uint8_t CMD_MULTI_TARGET_MODE = 0x90;
118static constexpr uint8_t CMD_QUERY_TARGET_MODE = 0x91;
119static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1;
120static constexpr uint8_t CMD_QUERY_ZONE = 0xC1;
121static constexpr uint8_t CMD_SET_ZONE = 0xC2;
123static constexpr uint8_t HEADER_FOOTER_SIZE = 4;
125static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xFD, 0xFC, 0xFB, 0xFA};
126static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0x04, 0x03, 0x02, 0x01};
128static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xAA, 0xFF, 0x03, 0x00};
129static constexpr uint8_t DATA_FRAME_FOOTER[2] = {0x55, 0xCC};
131static constexpr uint8_t NO_MAC[] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01};
133static inline uint16_t convert_seconds_to_ms(uint16_t value) {
return value * 1000; };
135static inline void convert_int_values_to_hex(
const int *values, uint8_t *bytes) {
136 for (uint8_t i = 0; i < 4; i++) {
137 uint16_t
val = values[i] & 0xFFFF;
139 bytes[i * 2 + 1] = (
val >> 8) & 0xFF;
143static inline int16_t decode_coordinate(uint8_t low_byte, uint8_t high_byte) {
144 int16_t coordinate = (high_byte & 0x7F) << 8 | low_byte;
145 if ((high_byte & 0x80) == 0) {
146 coordinate = -coordinate;
151static inline int16_t decode_speed(uint8_t low_byte, uint8_t high_byte) {
152 int16_t
speed = (high_byte & 0x7F) << 8 | low_byte;
153 if ((high_byte & 0x80) == 0) {
159static inline int16_t hex_to_signed_int(
const uint8_t *buffer, uint8_t offset) {
160 uint16_t hex_val = (buffer[offset + 1] << 8) | buffer[offset];
161 int16_t dec_val =
static_cast<int16_t
>(hex_val);
162 if (dec_val & 0x8000) {
168static inline float calculate_angle(
float base,
float hypotenuse) {
169 if (base < 0.0f || hypotenuse <= 0.0f) {
172 float angle_radians = acosf(base / hypotenuse);
173 float angle_degrees = angle_radians * (180.0f / std::numbers::pi_v<float>);
174 return angle_degrees;
177static inline bool validate_header_footer(
const uint8_t *header_footer,
const uint8_t *buffer) {
178 return std::memcmp(header_footer, buffer, HEADER_FOOTER_SIZE) == 0;
183 if (this->presence_timeout_number_ !=
nullptr) {
185 this->set_presence_timeout();
188 this->restart_and_read_all_info();
191void LD2450Component::dump_config() {
198 " Firmware version: %s\n"
201#ifdef USE_BINARY_SENSOR
202 ESP_LOGCONFIG(TAG,
"Binary Sensors:");
203 LOG_BINARY_SENSOR(
" ",
"MovingTarget", this->moving_target_binary_sensor_);
204 LOG_BINARY_SENSOR(
" ",
"StillTarget", this->still_target_binary_sensor_);
205 LOG_BINARY_SENSOR(
" ",
"Target", this->target_binary_sensor_);
208 ESP_LOGCONFIG(TAG,
"Sensors:");
209 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"MovingTargetCount", this->moving_target_count_sensor_);
210 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"StillTargetCount", this->still_target_count_sensor_);
211 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"TargetCount", this->target_count_sensor_);
213 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"TargetX", s);
216 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"TargetY", s);
219 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"TargetAngle", s);
222 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"TargetDistance", s);
225 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"TargetResolution", s);
228 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"TargetSpeed", s);
231 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"ZoneTargetCount", s);
234 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"ZoneMovingTargetCount", s);
237 LOG_SENSOR_WITH_DEDUP_SAFE(
" ",
"ZoneStillTargetCount", s);
240#ifdef USE_TEXT_SENSOR
241 ESP_LOGCONFIG(TAG,
"Text Sensors:");
242 LOG_TEXT_SENSOR(
" ",
"Version", this->version_text_sensor_);
243 LOG_TEXT_SENSOR(
" ",
"MAC address", this->mac_text_sensor_);
245 LOG_TEXT_SENSOR(
" ",
"Direction", s);
249 ESP_LOGCONFIG(TAG,
"Numbers:");
250 LOG_NUMBER(
" ",
"PresenceTimeout", this->presence_timeout_number_);
252 LOG_NUMBER(
" ",
"ZoneX1", n.x1);
253 LOG_NUMBER(
" ",
"ZoneY1", n.y1);
254 LOG_NUMBER(
" ",
"ZoneX2", n.x2);
255 LOG_NUMBER(
" ",
"ZoneY2", n.y2);
259 ESP_LOGCONFIG(TAG,
"Selects:");
260 LOG_SELECT(
" ",
"BaudRate", this->baud_rate_select_);
261 LOG_SELECT(
" ",
"ZoneType", this->zone_type_select_);
264 ESP_LOGCONFIG(TAG,
"Switches:");
265 LOG_SWITCH(
" ",
"Bluetooth", this->bluetooth_switch_);
266 LOG_SWITCH(
" ",
"MultiTarget", this->multi_target_switch_);
269 ESP_LOGCONFIG(TAG,
"Buttons:");
270 LOG_BUTTON(
" ",
"FactoryReset", this->factory_reset_button_);
271 LOG_BUTTON(
" ",
"Restart", this->restart_button_);
275void LD2450Component::loop() {
285 if (index.x > zone.
x1 && index.x < zone.
x2 && index.y > zone.
y1 && index.y < zone.
y2 &&
286 index.is_moving == is_moving) {
294void LD2450Component::reset_radar_zone() {
305void LD2450Component::set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2,
306 int32_t zone1_y2, int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2,
307 int32_t zone2_y2, int32_t zone3_x1, int32_t zone3_y1, int32_t zone3_x2,
310 int zone_parameters[12] = {zone1_x1, zone1_y1, zone1_x2, zone1_y2, zone2_x1, zone2_y1,
311 zone2_x2, zone2_y2, zone3_x1, zone3_y1, zone3_x2, zone3_y2};
312 for (uint8_t i = 0; i < MAX_ZONES; i++) {
323 uint8_t cmd_value[26] = {};
324 uint8_t zone_type_bytes[2] = {
static_cast<uint8_t
>(this->
zone_type_), 0x00};
325 uint8_t area_config[24] = {};
326 for (uint8_t i = 0; i < MAX_ZONES; i++) {
329 ld2450::convert_int_values_to_hex(values, area_config + (i * 8));
331 std::memcpy(cmd_value, zone_type_bytes,
sizeof(zone_type_bytes));
332 std::memcpy(cmd_value + 2, area_config,
sizeof(area_config));
334 this->
send_command_(CMD_SET_ZONE, cmd_value,
sizeof(cmd_value));
340 if (check_millis == 0) {
344 this->
timeout_ = ld2450::convert_seconds_to_ms(DEFAULT_PRESENCE_TIMEOUT);
351 uint8_t index, start;
352 for (index = 0; index < MAX_ZONES; index++) {
353 start = 12 + index * 8;
371void LD2450Component::read_all_info() {
380 if (this->baud_rate_select_ !=
nullptr && strcmp(this->baud_rate_select_->current_option(), baud_rate.c_str()) != 0) {
381 this->baud_rate_select_->publish_state(baud_rate);
383 this->publish_zone_type();
388void LD2450Component::query_zone_info() {
395void LD2450Component::restart_and_read_all_info() {
398 this->
set_timeout(1500, [
this]() { this->read_all_info(); });
403 ESP_LOGV(TAG,
"Sending COMMAND %02X", command);
405 this->
write_array(CMD_FRAME_HEADER,
sizeof(CMD_FRAME_HEADER));
408 if (command_value !=
nullptr) {
409 len += command_value_len;
412 uint8_t len_cmd[] = {
len, 0x00, command, 0x00};
415 if (command_value !=
nullptr) {
416 this->
write_array(command_value, command_value_len);
419 this->
write_array(CMD_FRAME_FOOTER,
sizeof(CMD_FRAME_FOOTER));
421 if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
431 ESP_LOGE(TAG,
"Invalid length");
434 if (!ld2450::validate_header_footer(DATA_FRAME_HEADER, this->
buffer_data_) ||
437 ESP_LOGE(TAG,
"Invalid header/footer");
441 int16_t target_count = 0;
442 int16_t still_target_count = 0;
443 int16_t moving_target_count = 0;
453 bool is_moving =
false;
455#if defined(USE_BINARY_SENSOR) || defined(USE_SENSOR) || defined(USE_TEXT_SENSOR)
457 for (index = 0; index < MAX_TARGETS; index++) {
464 SAFE_PUBLISH_SENSOR(this->move_x_sensors_[index], tx);
468 SAFE_PUBLISH_SENSOR(this->move_y_sensors_[index], ty);
472 SAFE_PUBLISH_SENSOR(this->move_resolution_sensors_[index], res);
479 moving_target_count++;
482 SAFE_PUBLISH_SENSOR(this->move_speed_sensors_[index], ts);
486 int32_t x_squared = (int32_t) tx * tx;
487 int32_t y_squared = (int32_t) ty * ty;
488 td = (uint16_t) sqrtf(x_squared + y_squared);
493 SAFE_PUBLISH_SENSOR(this->move_distance_sensors_[index], td);
495 angle = ld2450::calculate_angle(
static_cast<float>(ty),
static_cast<float>(td));
499 SAFE_PUBLISH_SENSOR(this->move_angle_sensors_[index], angle);
501#ifdef USE_TEXT_SENSOR
526 still_target_count = target_count - moving_target_count;
531 uint8_t zone_still_targets = 0;
532 uint8_t zone_moving_targets = 0;
533 uint8_t zone_all_targets = 0;
534 for (index = 0; index < MAX_ZONES; index++) {
537 zone_all_targets = zone_still_targets + zone_moving_targets;
540 SAFE_PUBLISH_SENSOR(this->zone_still_target_count_sensors_[index], zone_still_targets);
542 SAFE_PUBLISH_SENSOR(this->zone_moving_target_count_sensors_[index], zone_moving_targets);
544 SAFE_PUBLISH_SENSOR(this->zone_target_count_sensors_[index], zone_all_targets);
548 SAFE_PUBLISH_SENSOR(this->target_count_sensor_, target_count);
550 SAFE_PUBLISH_SENSOR(this->still_target_count_sensor_, still_target_count);
552 SAFE_PUBLISH_SENSOR(this->moving_target_count_sensor_, moving_target_count);
555#ifdef USE_BINARY_SENSOR
557 if (this->target_binary_sensor_ !=
nullptr) {
558 if (target_count > 0) {
559 this->target_binary_sensor_->publish_state(
true);
562 this->target_binary_sensor_->publish_state(
false);
564 ESP_LOGV(TAG,
"Clear presence waiting timeout: %d", this->
timeout_);
569 if (this->moving_target_binary_sensor_ !=
nullptr) {
570 if (moving_target_count > 0) {
571 this->moving_target_binary_sensor_->publish_state(
true);
574 this->moving_target_binary_sensor_->publish_state(
false);
579 if (this->still_target_binary_sensor_ !=
nullptr) {
580 if (still_target_count > 0) {
581 this->still_target_binary_sensor_->publish_state(
true);
584 this->still_target_binary_sensor_->publish_state(
false);
591 if (target_count > 0) {
594 if (moving_target_count > 0) {
597 if (still_target_count > 0) {
606 ESP_LOGE(TAG,
"Invalid length");
609 if (!ld2450::validate_header_footer(CMD_FRAME_HEADER, this->
buffer_data_)) {
614 ESP_LOGE(TAG,
"Invalid status");
623 case CMD_ENABLE_CONF:
624 ESP_LOGV(TAG,
"Enable conf");
627 case CMD_DISABLE_CONF:
628 ESP_LOGV(TAG,
"Disabled conf");
631 case CMD_SET_BAUD_RATE:
632 ESP_LOGV(TAG,
"Baud rate change");
634 if (this->baud_rate_select_ !=
nullptr) {
635 ESP_LOGE(TAG,
"Change baud rate to %s and reinstall", this->baud_rate_select_->current_option());
640 case CMD_QUERY_VERSION: {
644 ESP_LOGV(TAG,
"Firmware version: %s", version_s);
645#ifdef USE_TEXT_SENSOR
646 if (this->version_text_sensor_ !=
nullptr) {
647 this->version_text_sensor_->publish_state(version_s);
653 case CMD_QUERY_MAC_ADDRESS: {
665 ESP_LOGV(TAG,
"MAC address: %s", mac_str);
666#ifdef USE_TEXT_SENSOR
667 if (this->mac_text_sensor_ !=
nullptr) {
668 this->mac_text_sensor_->publish_state(mac_str);
672 if (this->bluetooth_switch_ !=
nullptr) {
680 ESP_LOGV(TAG,
"Bluetooth");
683 case CMD_SINGLE_TARGET_MODE:
684 ESP_LOGV(TAG,
"Single target conf");
686 if (this->multi_target_switch_ !=
nullptr) {
687 this->multi_target_switch_->publish_state(
false);
692 case CMD_MULTI_TARGET_MODE:
693 ESP_LOGV(TAG,
"Multi target conf");
695 if (this->multi_target_switch_ !=
nullptr) {
696 this->multi_target_switch_->publish_state(
true);
701 case CMD_QUERY_TARGET_MODE:
702 ESP_LOGV(TAG,
"Query target tracking mode");
704 if (this->multi_target_switch_ !=
nullptr) {
705 this->multi_target_switch_->publish_state(this->
buffer_data_[10] == 0x02);
711 ESP_LOGV(TAG,
"Query zone conf");
713 this->publish_zone_type();
715 if (this->zone_type_select_ !=
nullptr) {
716 ESP_LOGV(TAG,
"Change zone type to: %s", this->zone_type_select_->current_option());
720 ESP_LOGV(TAG,
"Zone: Disabled");
723 ESP_LOGV(TAG,
"Zone: Area detection");
726 ESP_LOGV(TAG,
"Zone: Area filter");
732 ESP_LOGV(TAG,
"Set zone conf");
733 this->query_zone_info();
753 ESP_LOGW(TAG,
"Max command length exceeded; ignoring");
769 ESP_LOGV(TAG,
"Ack Data incomplete");
776 const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
777 const uint8_t cmd_value[2] = {0x01, 0x00};
778 this->
send_command_(cmd, enable ? cmd_value :
nullptr,
sizeof(cmd_value));
782void LD2450Component::set_bluetooth(
bool enable) {
784 const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00};
785 this->
send_command_(CMD_BLUETOOTH, cmd_value,
sizeof(cmd_value));
786 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
790void LD2450Component::set_baud_rate(
const char *
state) {
793 this->
send_command_(CMD_SET_BAUD_RATE, cmd_value,
sizeof(cmd_value));
798void LD2450Component::set_zone_type(
const char *
state) {
799 ESP_LOGV(TAG,
"Set zone type: %s",
state);
806void LD2450Component::publish_zone_type() {
809 if (this->zone_type_select_ !=
nullptr) {
810 this->zone_type_select_->publish_state(zone_type);
816void LD2450Component::set_multi_target(
bool enable) {
818 uint8_t cmd = enable ? CMD_MULTI_TARGET_MODE : CMD_SINGLE_TARGET_MODE;
824void LD2450Component::factory_reset() {
827 this->
set_timeout(200, [
this]() { this->restart_and_read_all_info(); });
838 uint8_t cmd_value[2] = {0x01, 0x00};
850void LD2450Component::set_move_x_sensor(uint8_t target,
sensor::Sensor *s) {
853void LD2450Component::set_move_y_sensor(uint8_t target,
sensor::Sensor *s) {
854 this->move_y_sensors_[target] =
new SensorWithDedup<int16_t>(s);
856void LD2450Component::set_move_speed_sensor(uint8_t target, sensor::Sensor *s) {
857 this->move_speed_sensors_[target] =
new SensorWithDedup<int16_t>(s);
859void LD2450Component::set_move_angle_sensor(uint8_t target, sensor::Sensor *s) {
860 this->move_angle_sensors_[target] =
new SensorWithDedup<float>(s);
862void LD2450Component::set_move_distance_sensor(uint8_t target, sensor::Sensor *s) {
863 this->move_distance_sensors_[target] =
new SensorWithDedup<uint16_t>(s);
865void LD2450Component::set_move_resolution_sensor(uint8_t target, sensor::Sensor *s) {
866 this->move_resolution_sensors_[target] =
new SensorWithDedup<uint16_t>(s);
868void LD2450Component::set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
869 this->zone_target_count_sensors_[zone] =
new SensorWithDedup<uint8_t>(s);
871void LD2450Component::set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
872 this->zone_still_target_count_sensors_[zone] =
new SensorWithDedup<uint8_t>(s);
874void LD2450Component::set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
875 this->zone_moving_target_count_sensors_[zone] =
new SensorWithDedup<uint8_t>(s);
878#ifdef USE_TEXT_SENSOR
879void LD2450Component::set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s) {
880 this->direction_text_sensors_[target] = s;
886void LD2450Component::set_zone_coordinate(uint8_t zone) {
891 if (!x1sens->has_state() || !y1sens->has_state() || !x2sens->has_state() || !y2sens->has_state()) {
901void LD2450Component::set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2,
902 number::Number *y2) {
903 if (zone < MAX_ZONES) {
914void LD2450Component::set_presence_timeout() {
915 if (this->presence_timeout_number_ !=
nullptr) {
916 if (this->presence_timeout_number_->state == 0) {
918 this->presence_timeout_number_->publish_state(timeout);
919 this->
timeout_ = ld2450::convert_seconds_to_ms(timeout);
921 if (this->presence_timeout_number_->has_state()) {
923 this->
timeout_ = ld2450::convert_seconds_to_ms(this->presence_timeout_number_->state);
935 value = DEFAULT_PRESENCE_TIMEOUT;
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
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.
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
void save_to_flash_(float value)
uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving)
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_speed_sensors_
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_x_sensors_
uint32_t still_presence_millis_
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len)
void query_target_tracking_mode_()
void send_set_zone_command_()
std::array< text_sensor::TextSensor *, 3 > direction_text_sensors_
std::array< SensorWithDedup< float > *, MAX_TARGETS > move_angle_sensors_
void set_config_mode_(bool enable)
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_still_target_count_sensors_
uint32_t moving_presence_millis_
std::array< SensorWithDedup< uint16_t > *, MAX_TARGETS > move_resolution_sensors_
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_target_count_sensors_
void readline_(int readch)
uint8_t buffer_data_[MAX_LINE_LENGTH]
std::array< SensorWithDedup< uint8_t > *, MAX_ZONES > zone_moving_target_count_sensors_
Target target_info_[MAX_TARGETS]
uint32_t presence_millis_
Zone zone_config_[MAX_ZONES]
std::array< SensorWithDedup< uint16_t > *, MAX_TARGETS > move_distance_sensors_
ESPPreferenceObject pref_
std::array< SensorWithDedup< int16_t > *, MAX_TARGETS > move_y_sensors_
float restore_from_flash_()
ZoneOfNumbers zone_numbers_[MAX_ZONES]
void handle_periodic_data_()
bool get_timeout_status_(uint32_t check_millis)
void publish_state(float state)
Base-class for all sensors.
const std::string & get_state() const
Getter-syntax for .state.
void publish_state(const std::string &state)
uint32_t get_baud_rate() const
void write_array(const uint8_t *data, size_t len)
constexpr Uint8ToString DIRECTION_BY_UINT[]
constexpr StringToUint8 ZONE_TYPE_BY_STR[]
constexpr StringToUint8 BAUD_RATES_BY_STR[]
constexpr Uint8ToString ZONE_TYPE_BY_UINT[]
uint8_t find_uint8(const StringToUint8(&arr)[N], const std::string &str)
const char * find_str(const Uint8ToString(&arr)[N], uint8_t value)
void format_version_str(const uint8_t *version, std::span< char, 20 > buffer)
const char * format_mac_str(const uint8_t *mac_address, std::span< char, 18 > buffer)
std::vector< uint8_t > bytes
ESPPreferences * global_preferences
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.
void IRAM_ATTR HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.