mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[ld2420] Batch UART reads to reduce loop overhead
This commit is contained in:
@@ -335,9 +335,10 @@ void LD2420Component::revert_config_action() {
|
|||||||
|
|
||||||
void LD2420Component::loop() {
|
void LD2420Component::loop() {
|
||||||
// If there is a active send command do not process it here, the send command call will handle it.
|
// If there is a active send command do not process it here, the send command call will handle it.
|
||||||
while (!this->cmd_active_ && this->available()) {
|
if (this->cmd_active_) {
|
||||||
this->readline_(this->read(), this->buffer_data_, MAX_LINE_LENGTH);
|
return;
|
||||||
}
|
}
|
||||||
|
this->read_batch_(this->buffer_data_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LD2420Component::update_radar_data(uint16_t const *gate_energy, uint8_t sample_number) {
|
void LD2420Component::update_radar_data(uint16_t const *gate_energy, uint8_t sample_number) {
|
||||||
@@ -539,6 +540,27 @@ void LD2420Component::handle_simple_mode_(const uint8_t *inbuf, int len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LD2420Component::read_batch_(std::span<uint8_t, MAX_LINE_LENGTH> buffer) {
|
||||||
|
int avail = this->available();
|
||||||
|
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], buffer.data(), buffer.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void LD2420Component::handle_ack_data_(uint8_t *buffer, int len) {
|
void LD2420Component::handle_ack_data_(uint8_t *buffer, int len) {
|
||||||
this->cmd_reply_.command = buffer[CMD_FRAME_COMMAND];
|
this->cmd_reply_.command = buffer[CMD_FRAME_COMMAND];
|
||||||
this->cmd_reply_.length = buffer[CMD_FRAME_DATA_LENGTH];
|
this->cmd_reply_.length = buffer[CMD_FRAME_DATA_LENGTH];
|
||||||
@@ -645,9 +667,7 @@ int LD2420Component::send_cmd_from_array(CmdFrameT frame) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (!this->cmd_reply_.ack) {
|
while (!this->cmd_reply_.ack) {
|
||||||
while (this->available()) {
|
this->read_batch_(ack_buffer);
|
||||||
this->readline_(this->read(), ack_buffer, sizeof(ack_buffer));
|
|
||||||
}
|
|
||||||
delay_microseconds_safe(1450);
|
delay_microseconds_safe(1450);
|
||||||
// Wait on an Rx from the LD2420 for up to 3 1 second loops, otherwise it could trigger a WDT.
|
// Wait on an Rx from the LD2420 for up to 3 1 second loops, otherwise it could trigger a WDT.
|
||||||
if ((millis() - start_millis) > 1000) {
|
if ((millis() - start_millis) > 1000) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "esphome/components/uart/uart.h"
|
#include "esphome/components/uart/uart.h"
|
||||||
#include "esphome/core/automation.h"
|
#include "esphome/core/automation.h"
|
||||||
#include "esphome/core/helpers.h"
|
#include "esphome/core/helpers.h"
|
||||||
|
#include <span>
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
#include "esphome/components/text_sensor/text_sensor.h"
|
#include "esphome/components/text_sensor/text_sensor.h"
|
||||||
#endif
|
#endif
|
||||||
@@ -165,6 +166,7 @@ class LD2420Component : public Component, public uart::UARTDevice {
|
|||||||
void handle_energy_mode_(uint8_t *buffer, int len);
|
void handle_energy_mode_(uint8_t *buffer, int len);
|
||||||
void handle_ack_data_(uint8_t *buffer, int len);
|
void handle_ack_data_(uint8_t *buffer, int len);
|
||||||
void readline_(int rx_data, uint8_t *buffer, int len);
|
void readline_(int rx_data, uint8_t *buffer, int len);
|
||||||
|
void read_batch_(std::span<uint8_t, MAX_LINE_LENGTH> buffer);
|
||||||
void set_calibration_(bool state) { this->calibration_ = state; };
|
void set_calibration_(bool state) { this->calibration_ = state; };
|
||||||
bool get_calibration_() { return this->calibration_; };
|
bool get_calibration_() { return this->calibration_; };
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user