1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 00:31:58 +00:00

pipsolar: batch UART reads to reduce per-loop overhead

This commit is contained in:
J. Nick Koston
2026-02-07 00:26:33 +01:00
parent 86f91eed2f
commit 39013388dd

View File

@@ -13,9 +13,12 @@ void Pipsolar::setup() {
} }
void Pipsolar::empty_uart_buffer_() { void Pipsolar::empty_uart_buffer_() {
uint8_t byte; uint8_t buf[64];
while (this->available()) { int avail;
this->read_byte(&byte); while ((avail = this->available()) > 0) {
if (!this->read_array(buf, std::min(static_cast<size_t>(avail), sizeof(buf)))) {
break;
}
} }
} }
@@ -94,15 +97,24 @@ void Pipsolar::loop() {
} }
if (this->state_ == STATE_COMMAND || this->state_ == STATE_POLL) { if (this->state_ == STATE_COMMAND || this->state_ == STATE_POLL) {
while (this->available()) { int avail = this->available();
uint8_t byte; while (avail > 0) {
this->read_byte(&byte); uint8_t buf[64];
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
if (!this->read_array(buf, to_read)) {
break;
}
avail -= to_read;
bool done = false;
for (size_t i = 0; i < to_read; i++) {
uint8_t byte = buf[i];
// make sure data and null terminator fit in buffer // make sure data and null terminator fit in buffer
if (this->read_pos_ >= PIPSOLAR_READ_BUFFER_LENGTH - 1) { if (this->read_pos_ >= PIPSOLAR_READ_BUFFER_LENGTH - 1) {
this->read_pos_ = 0; this->read_pos_ = 0;
this->empty_uart_buffer_(); this->empty_uart_buffer_();
ESP_LOGW(TAG, "response data too long, discarding."); ESP_LOGW(TAG, "response data too long, discarding.");
done = true;
break; break;
} }
this->read_buffer_[this->read_pos_] = byte; this->read_buffer_[this->read_pos_] = byte;
@@ -118,8 +130,14 @@ void Pipsolar::loop() {
if (this->state_ == STATE_COMMAND) { if (this->state_ == STATE_COMMAND) {
this->state_ = STATE_COMMAND_COMPLETE; this->state_ = STATE_COMMAND_COMPLETE;
} }
done = true;
break;
}
}
if (done) {
break;
}
} }
} // available
} }
if (this->state_ == STATE_COMMAND) { if (this->state_ == STATE_COMMAND) {
if (millis() - this->command_start_millis_ > esphome::pipsolar::Pipsolar::COMMAND_TIMEOUT) { if (millis() - this->command_start_millis_ > esphome::pipsolar::Pipsolar::COMMAND_TIMEOUT) {