Skip to content

Commit

Permalink
Use uint32_t for timestamps, Fix possible lock-up while reading UART
Browse files Browse the repository at this point in the history
  • Loading branch information
TillFleisch committed Feb 14, 2024
1 parent 304f28c commit f7007e4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
31 changes: 18 additions & 13 deletions components/LD2450/LD2450.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -411,7 +416,7 @@ namespace esphome::ld2450

// Write message length
write(static_cast<uint8_t>(len));
write(static_cast<uint8_t>(len << 8));
write(static_cast<uint8_t>(len >> 8));

// Write message content
write_array(msg, len);
Expand Down
4 changes: 2 additions & 2 deletions components/LD2450/LD2450.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions components/LD2450/target.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion components/LD2450/zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Target *, long> tracked_targets_{};
std::map<Target *, uint32_t> tracked_targets_{};
};
} // namespace esphome::ld2450

0 comments on commit f7007e4

Please sign in to comment.