From d260265cf54735e5570863e2376b3805af04a5a4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 26 Oct 2026 03:40:08 -1000 Subject: [PATCH] [ld6002b] Use StaticRingBuffer for the command queue (#21479) --- esphome/components/ld6002b/ld6002b.cpp | 14 ++++++-------- esphome/components/ld6002b/ld6002b.h | 5 +---- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/esphome/components/ld6002b/ld6002b.cpp b/esphome/components/ld6002b/ld6002b.cpp index 2b8ad310ad8..149ab0e0801 100644 --- a/esphome/components/ld6002b/ld6002b.cpp +++ b/esphome/components/ld6002b/ld6002b.cpp @@ -1066,20 +1066,19 @@ bool LD6002BComponent::queue_command_(uint16_t type, const uint8_t *data, uint8_ ESP_LOGW(TAG, "Command data too large: %u", len); return false; } - if (this->cmd_count_ >= CMD_QUEUE_SIZE) { + if (this->cmd_queue_.size() >= CMD_QUEUE_SIZE) { ESP_LOGW(TAG, "Command queue full, dropping command 0x%04X", type); return false; } - PendingCommand &cmd = this->cmd_queue_[this->cmd_tail_]; + PendingCommand cmd; cmd.type = type; cmd.len = len; if (len > 0 && data != nullptr) { std::memcpy(cmd.data.data(), data, len); } + this->cmd_queue_.push(cmd); - this->cmd_tail_ = (this->cmd_tail_ + 1) % CMD_QUEUE_SIZE; - this->cmd_count_++; this->process_command_queue_(); return true; } @@ -1146,12 +1145,11 @@ void LD6002BComponent::process_command_queue_() { return; } - if (this->cmd_count_ == 0) + if (this->cmd_queue_.empty()) return; - this->active_command_ = this->cmd_queue_[this->cmd_head_]; - this->cmd_head_ = (this->cmd_head_ + 1) % CMD_QUEUE_SIZE; - this->cmd_count_--; + this->active_command_ = this->cmd_queue_.front(); + this->cmd_queue_.pop(); this->send_generation_++; this->retries_left_ = CMD_MAX_RETRIES; diff --git a/esphome/components/ld6002b/ld6002b.h b/esphome/components/ld6002b/ld6002b.h index 663c73363ef..2b3e35d4b2d 100644 --- a/esphome/components/ld6002b/ld6002b.h +++ b/esphome/components/ld6002b/ld6002b.h @@ -419,10 +419,7 @@ class LD6002BComponent : public Component, public uart::UARTDevice { // A reply cannot trail the frame that earned it for longer than this; the field worst case is ~726ms. static constexpr uint32_t STALE_ACK_MAX_AGE_MS = 1000; - std::array cmd_queue_{}; - uint8_t cmd_head_{0}; - uint8_t cmd_tail_{0}; - uint8_t cmd_count_{0}; + StaticRingBuffer cmd_queue_; bool command_active_{false}; bool command_sent_{false}; PendingCommand active_command_{};