mirror of
https://github.com/esphome/esphome.git
synced 2026-11-02 03:16:40 +00:00
[ld6002b] Use StaticRingBuffer for the command queue (#21479)
This commit is contained in:
1 parent
2388c3313d
commit
d260265cf5
2 files changed
+7
-12
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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<PendingCommand, CMD_QUEUE_SIZE> cmd_queue_{};
|
||||
uint8_t cmd_head_{0};
|
||||
uint8_t cmd_tail_{0};
|
||||
uint8_t cmd_count_{0};
|
||||
StaticRingBuffer<PendingCommand, CMD_QUEUE_SIZE> cmd_queue_;
|
||||
bool command_active_{false};
|
||||
bool command_sent_{false};
|
||||
PendingCommand active_command_{};
|
||||
|
||||
Reference in new issue
Block a user