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:
@@ -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,32 +97,47 @@ 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));
|
||||||
// make sure data and null terminator fit in buffer
|
if (!this->read_array(buf, to_read)) {
|
||||||
if (this->read_pos_ >= PIPSOLAR_READ_BUFFER_LENGTH - 1) {
|
|
||||||
this->read_pos_ = 0;
|
|
||||||
this->empty_uart_buffer_();
|
|
||||||
ESP_LOGW(TAG, "response data too long, discarding.");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
this->read_buffer_[this->read_pos_] = byte;
|
avail -= to_read;
|
||||||
this->read_pos_++;
|
bool done = false;
|
||||||
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
|
uint8_t byte = buf[i];
|
||||||
|
|
||||||
// end of answer
|
// make sure data and null terminator fit in buffer
|
||||||
if (byte == 0x0D) {
|
if (this->read_pos_ >= PIPSOLAR_READ_BUFFER_LENGTH - 1) {
|
||||||
this->read_buffer_[this->read_pos_] = 0;
|
this->read_pos_ = 0;
|
||||||
this->empty_uart_buffer_();
|
this->empty_uart_buffer_();
|
||||||
if (this->state_ == STATE_POLL) {
|
ESP_LOGW(TAG, "response data too long, discarding.");
|
||||||
this->state_ = STATE_POLL_COMPLETE;
|
done = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (this->state_ == STATE_COMMAND) {
|
this->read_buffer_[this->read_pos_] = byte;
|
||||||
this->state_ = STATE_COMMAND_COMPLETE;
|
this->read_pos_++;
|
||||||
|
|
||||||
|
// end of answer
|
||||||
|
if (byte == 0x0D) {
|
||||||
|
this->read_buffer_[this->read_pos_] = 0;
|
||||||
|
this->empty_uart_buffer_();
|
||||||
|
if (this->state_ == STATE_POLL) {
|
||||||
|
this->state_ = STATE_POLL_COMPLETE;
|
||||||
|
}
|
||||||
|
if (this->state_ == STATE_COMMAND) {
|
||||||
|
this->state_ = STATE_COMMAND_COMPLETE;
|
||||||
|
}
|
||||||
|
done = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // available
|
if (done) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user