mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[ld2412] Batch UART reads to reduce loop overhead
Read all available bytes in batches via read_array() instead of byte-at-a-time read() calls. Each read() internally chains through read_byte -> read_array(1) -> check_read_timeout_ -> available(), resulting in 3 UART calls per byte. Batching reduces this significantly.
This commit is contained in:
@@ -310,8 +310,23 @@ void LD2412Component::restart_and_read_all_info() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LD2412Component::loop() {
|
void LD2412Component::loop() {
|
||||||
while (this->available()) {
|
int avail = this->available();
|
||||||
this->readline_(this->read());
|
if (avail == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
uint8_t buf[MAX_LINE_LENGTH];
|
||||||
|
while (avail > 0) {
|
||||||
|
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
|
||||||
|
if (!this->read_array(buf, to_read)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
avail -= to_read;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
|
this->readline_(buf[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user