mirror of
https://github.com/esphome/esphome.git
synced 2026-10-07 03:16:37 +00:00
[pylontech] Batch UART reads to reduce loop overhead (#13824)
This commit is contained in:
@@ -56,17 +56,23 @@ void PylontechComponent::setup() {
|
|||||||
void PylontechComponent::update() { this->write_str("pwr\n"); }
|
void PylontechComponent::update() { this->write_str("pwr\n"); }
|
||||||
|
|
||||||
void PylontechComponent::loop() {
|
void PylontechComponent::loop() {
|
||||||
if (this->available() > 0) {
|
int avail = this->available();
|
||||||
|
if (avail > 0) {
|
||||||
// pylontech sends a lot of data very suddenly
|
// pylontech sends a lot of data very suddenly
|
||||||
// we need to quickly put it all into our own buffer, otherwise the uart's buffer will overflow
|
// we need to quickly put it all into our own buffer, otherwise the uart's buffer will overflow
|
||||||
uint8_t data;
|
|
||||||
int recv = 0;
|
int recv = 0;
|
||||||
while (this->available() > 0) {
|
uint8_t buf[64];
|
||||||
if (this->read_byte(&data)) {
|
while (avail > 0) {
|
||||||
buffer_[buffer_index_write_] += (char) data;
|
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
|
||||||
recv++;
|
if (!this->read_array(buf, to_read)) {
|
||||||
if (buffer_[buffer_index_write_].back() == static_cast<char>(ASCII_LF) ||
|
break;
|
||||||
buffer_[buffer_index_write_].length() >= MAX_DATA_LENGTH_BYTES) {
|
}
|
||||||
|
avail -= to_read;
|
||||||
|
recv += to_read;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
|
buffer_[buffer_index_write_] += (char) buf[i];
|
||||||
|
if (buf[i] == ASCII_LF || buffer_[buffer_index_write_].length() >= MAX_DATA_LENGTH_BYTES) {
|
||||||
// complete line received
|
// complete line received
|
||||||
buffer_index_write_ = (buffer_index_write_ + 1) % NUM_BUFFERS;
|
buffer_index_write_ = (buffer_index_write_ + 1) % NUM_BUFFERS;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user