diff --git a/components/LD2450/LD2450.cpp b/components/LD2450/LD2450.cpp index c70c2b9..38a6105 100644 --- a/components/LD2450/LD2450.cpp +++ b/components/LD2450/LD2450.cpp @@ -115,8 +115,8 @@ namespace esphome::ld2450 else { command_queue_.erase(command_queue_.begin()); - command_send_retries_ = 0; } + command_send_retries_ = 0; ESP_LOGW(TAG, "Sending command timed out! Is the sensor connected?"); } else @@ -132,36 +132,41 @@ namespace esphome::ld2450 { // Inject leave config command after clearing the queue command_queue_.push_back({COMMAND_LEAVE_CONFIG, 0x00}); + command_send_retries_ = 0; } // Skip stream until start of message and parse header - while (!peek_status_ && available() >= 4) + if (!peek_status_ && available() >= 4) { // Try to read the header and abort on mismatch const uint8_t *header; bool skip = false; - uint8_t target; - if (peek() == update_header[0]) + uint8_t message_type; + uint8_t first_byte = read(); + if (first_byte == update_header[0]) { header = update_header; - target = 1; + message_type = 1; } - else + else if (first_byte == config_header[0]) { header = config_header; - target = 2; + message_type = 2; + } + else + { + skip = true; } - for (int i = 0; i < 4 && !skip; i++) + for (int i = 1; i < 4 && !skip; i++) { if (read() != header[i]) skip = true; } - if (skip) - continue; - // Flag successful header reading - peek_status_ = target; + if (!skip) + // Flag successful header reading + peek_status_ = message_type; } if (peek_status_ == 1 && available() >= 28) @@ -411,7 +416,7 @@ namespace esphome::ld2450 // Write message length write(static_cast(len)); - write(static_cast(len << 8)); + write(static_cast(len >> 8)); // Write message content write_array(msg, len); diff --git a/components/LD2450/LD2450.h b/components/LD2450/LD2450.h index 9d96cf9..56fe397 100644 --- a/components/LD2450/LD2450.h +++ b/components/LD2450/LD2450.h @@ -23,7 +23,7 @@ #define SENSOR_UNAVAILABLE_TIMEOUT 1000 -#define COMMAND_MAX_RETRIES 5 +#define COMMAND_MAX_RETRIES 10 #define COMMAND_RETRY_DELAY 100 #define COMMAND_ENTER_CONFIG 0xFF @@ -333,7 +333,7 @@ namespace esphome::ld2450 int configuration_message_length_ = 0; /// @brief timestamp of the last message which was sent to the sensor - long command_last_sent_ = 0; + uint32_t command_last_sent_ = 0; /// @brief timestamp of the last received message uint32_t last_message_received_ = 0; diff --git a/components/LD2450/target.h b/components/LD2450/target.h index d44adcc..d6e8ba1 100644 --- a/components/LD2450/target.h +++ b/components/LD2450/target.h @@ -125,7 +125,7 @@ namespace esphome::ld2450 * @brief Time since last last change in values. * @return timestamp in milliseconds since start */ - long get_last_change() + uint32_t get_last_change() { return last_change_; } @@ -206,10 +206,10 @@ namespace esphome::ld2450 bool fast_off_detection_ = false; /// @brief time stamp of the last debug message which was sent. - long last_debug_message_ = 0; + uint32_t last_debug_message_ = 0; /// @brief time of the last value change - long last_change_ = 0; + uint32_t last_change_ = 0; /// @brief sensor reference of the x position sensor PollingSensor *x_position_sensor_ = nullptr; diff --git a/components/LD2450/zone.h b/components/LD2450/zone.h index fba2827..cff0d72 100644 --- a/components/LD2450/zone.h +++ b/components/LD2450/zone.h @@ -143,6 +143,6 @@ namespace esphome::ld2450 int target_timeout_ = 5000; /// @brief Map of targets which are currently tracked inside of this polygon with their last seen timestamp - std::map tracked_targets_{}; + std::map tracked_targets_{}; }; } // namespace esphome::ld2450