From 572376091e8b0f338fde57a056dff991817ed991 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 6 Feb 2026 23:07:02 +0100 Subject: [PATCH] loop --- esphome/components/cse7766/cse7766.cpp | 35 ++++++++++++++------------ 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/esphome/components/cse7766/cse7766.cpp b/esphome/components/cse7766/cse7766.cpp index 5cbb790e73..2868af5152 100644 --- a/esphome/components/cse7766/cse7766.cpp +++ b/esphome/components/cse7766/cse7766.cpp @@ -22,26 +22,29 @@ void CSE7766Component::loop() { this->last_transmission_ = now; - // Read all available bytes at once to reduce UART call overhead. + // Read all available bytes in batches to reduce UART call overhead. // At 4800 baud (~480 bytes/sec) with ~122 Hz loop rate, typically ~4 bytes per call. uint8_t buf[CSE7766_RAW_DATA_SIZE]; - size_t to_read = std::min(static_cast(avail), sizeof(buf)); - this->read_array(buf, to_read); + while (avail > 0) { + size_t to_read = std::min(static_cast(avail), sizeof(buf)); + this->read_array(buf, to_read); + avail -= to_read; - for (size_t i = 0; i < to_read; i++) { - this->raw_data_[this->raw_data_index_] = buf[i]; - if (!this->check_byte_()) { - this->raw_data_index_ = 0; - this->status_set_warning(); - continue; + for (size_t i = 0; i < to_read; i++) { + this->raw_data_[this->raw_data_index_] = buf[i]; + if (!this->check_byte_()) { + this->raw_data_index_ = 0; + this->status_set_warning(); + continue; + } + + if (this->raw_data_index_ == CSE7766_RAW_DATA_SIZE - 1) { + this->parse_data_(); + this->status_clear_warning(); + } + + this->raw_data_index_ = (this->raw_data_index_ + 1) % CSE7766_RAW_DATA_SIZE; } - - if (this->raw_data_index_ == CSE7766_RAW_DATA_SIZE - 1) { - this->parse_data_(); - this->status_clear_warning(); - } - - this->raw_data_index_ = (this->raw_data_index_ + 1) % CSE7766_RAW_DATA_SIZE; } }