mirror of
https://github.com/esphome/esphome.git
synced 2026-02-12 12:37:36 -07:00
Compare commits
84 Commits
dev
...
integratio
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86c1803538 | ||
|
|
eb8bb260e5 | ||
|
|
ef079f9113 | ||
|
|
647f39504a | ||
|
|
b6c98f0586 | ||
|
|
77b46ba90f | ||
|
|
8cc6915558 | ||
|
|
10e71255a0 | ||
|
|
30521f7de6 | ||
|
|
c97ae656e4 | ||
|
|
f09e72766e | ||
|
|
a320e87a17 | ||
|
|
45932fabea | ||
|
|
f62ea6bdc2 | ||
|
|
675625cf25 | ||
|
|
6baeaf5b7b | ||
|
|
ca8617cf10 | ||
|
|
a0bc6a9922 | ||
|
|
d5295a894b | ||
|
|
fb6b96ff58 | ||
|
|
e07144ef74 | ||
|
|
1c4cf1a3e8 | ||
|
|
a198df34ee | ||
|
|
15904ab583 | ||
|
|
9c9e8ac388 | ||
|
|
04f4636d36 | ||
|
|
3cbadfe42a | ||
|
|
277a11f0ea | ||
|
|
08cca414e7 | ||
|
|
4db5835b6f | ||
|
|
527003e16b | ||
|
|
e9a0d06880 | ||
|
|
b2879f7f99 | ||
|
|
44e9346e9c | ||
|
|
6670c2b6c4 | ||
|
|
6013b473ca | ||
|
|
cc1f83ac35 | ||
|
|
1f1405364d | ||
|
|
5d3ae8cbec | ||
|
|
59a2f6f538 | ||
|
|
a9c37cae26 | ||
|
|
c8a93f31e9 | ||
|
|
f79448a09a | ||
|
|
5e096826c3 | ||
|
|
457d68256d | ||
|
|
a9029fb67a | ||
|
|
cd891d4b16 | ||
|
|
2784059a64 | ||
|
|
4827f53156 | ||
|
|
8dff0ee449 | ||
|
|
a7f04a6cf9 | ||
|
|
53bde863f5 | ||
|
|
dfb0c8670d | ||
|
|
7490efedd7 | ||
|
|
a0f736b7aa | ||
|
|
21f270677b | ||
|
|
d6e692e302 | ||
|
|
991ce396a9 | ||
|
|
68dfb844bd | ||
|
|
9742880bf7 | ||
|
|
13f9726534 | ||
|
|
dd07e25a8f | ||
|
|
a875a2fb9b | ||
|
|
836bfc625d | ||
|
|
2a17592d57 | ||
|
|
04697ac223 | ||
|
|
3f3cf83aab | ||
|
|
39013388dd | ||
|
|
cfbeea9983 | ||
|
|
8f6e1abbce | ||
|
|
c77d70c093 | ||
|
|
25762c62f8 | ||
|
|
441ec35d9f | ||
|
|
33c831dbb8 | ||
|
|
38aeb9be37 | ||
|
|
6b7c52799d | ||
|
|
f19bb2cd0a | ||
|
|
26c98a1e25 | ||
|
|
b544cf2ffe | ||
|
|
6d1281301f | ||
|
|
901192cca1 | ||
|
|
67e7ba4812 | ||
|
|
572376091e | ||
|
|
e7c9808b87 |
@@ -7,7 +7,6 @@ namespace esphome {
|
|||||||
namespace cse7766 {
|
namespace cse7766 {
|
||||||
|
|
||||||
static const char *const TAG = "cse7766";
|
static const char *const TAG = "cse7766";
|
||||||
static constexpr size_t CSE7766_RAW_DATA_SIZE = 24;
|
|
||||||
|
|
||||||
void CSE7766Component::loop() {
|
void CSE7766Component::loop() {
|
||||||
const uint32_t now = App.get_loop_component_start_time();
|
const uint32_t now = App.get_loop_component_start_time();
|
||||||
@@ -16,25 +15,39 @@ void CSE7766Component::loop() {
|
|||||||
this->raw_data_index_ = 0;
|
this->raw_data_index_ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->available() == 0) {
|
// Early return prevents updating last_transmission_ when no data is available.
|
||||||
|
int avail = this->available();
|
||||||
|
if (avail <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this->last_transmission_ = now;
|
this->last_transmission_ = now;
|
||||||
while (this->available() != 0) {
|
|
||||||
this->read_byte(&this->raw_data_[this->raw_data_index_]);
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
// At 4800 baud (~480 bytes/sec) with ~122 Hz loop rate, typically ~4 bytes per call.
|
||||||
|
uint8_t buf[CSE7766_RAW_DATA_SIZE];
|
||||||
|
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->raw_data_[this->raw_data_index_] = buf[i];
|
||||||
if (!this->check_byte_()) {
|
if (!this->check_byte_()) {
|
||||||
this->raw_data_index_ = 0;
|
this->raw_data_index_ = 0;
|
||||||
this->status_set_warning();
|
this->status_set_warning();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this->raw_data_index_ == 23) {
|
if (this->raw_data_index_ == CSE7766_RAW_DATA_SIZE - 1) {
|
||||||
this->parse_data_();
|
this->parse_data_();
|
||||||
this->status_clear_warning();
|
this->status_clear_warning();
|
||||||
}
|
}
|
||||||
|
|
||||||
this->raw_data_index_ = (this->raw_data_index_ + 1) % 24;
|
this->raw_data_index_ = (this->raw_data_index_ + 1) % CSE7766_RAW_DATA_SIZE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,14 +66,15 @@ bool CSE7766Component::check_byte_() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (index == 23) {
|
if (index == CSE7766_RAW_DATA_SIZE - 1) {
|
||||||
uint8_t checksum = 0;
|
uint8_t checksum = 0;
|
||||||
for (uint8_t i = 2; i < 23; i++) {
|
for (uint8_t i = 2; i < CSE7766_RAW_DATA_SIZE - 1; i++) {
|
||||||
checksum += this->raw_data_[i];
|
checksum += this->raw_data_[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (checksum != this->raw_data_[23]) {
|
if (checksum != this->raw_data_[CSE7766_RAW_DATA_SIZE - 1]) {
|
||||||
ESP_LOGW(TAG, "Invalid checksum from CSE7766: 0x%02X != 0x%02X", checksum, this->raw_data_[23]);
|
ESP_LOGW(TAG, "Invalid checksum from CSE7766: 0x%02X != 0x%02X", checksum,
|
||||||
|
this->raw_data_[CSE7766_RAW_DATA_SIZE - 1]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
namespace esphome {
|
namespace esphome {
|
||||||
namespace cse7766 {
|
namespace cse7766 {
|
||||||
|
|
||||||
|
static constexpr size_t CSE7766_RAW_DATA_SIZE = 24;
|
||||||
|
|
||||||
class CSE7766Component : public Component, public uart::UARTDevice {
|
class CSE7766Component : public Component, public uart::UARTDevice {
|
||||||
public:
|
public:
|
||||||
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
|
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
|
||||||
@@ -33,7 +35,7 @@ class CSE7766Component : public Component, public uart::UARTDevice {
|
|||||||
this->raw_data_[start_index + 2]);
|
this->raw_data_[start_index + 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t raw_data_[24];
|
uint8_t raw_data_[CSE7766_RAW_DATA_SIZE];
|
||||||
uint8_t raw_data_index_{0};
|
uint8_t raw_data_index_{0};
|
||||||
uint32_t last_transmission_{0};
|
uint32_t last_transmission_{0};
|
||||||
sensor::Sensor *voltage_sensor_{nullptr};
|
sensor::Sensor *voltage_sensor_{nullptr};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "dfplayer.h"
|
#include "dfplayer.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
namespace esphome {
|
namespace esphome {
|
||||||
@@ -131,10 +132,17 @@ void DFPlayer::send_cmd_(uint8_t cmd, uint16_t argument) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DFPlayer::loop() {
|
void DFPlayer::loop() {
|
||||||
// Read message
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
while (this->available()) {
|
int avail = this->available();
|
||||||
uint8_t byte;
|
uint8_t buf[64];
|
||||||
this->read_byte(&byte);
|
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 bi = 0; bi < to_read; bi++) {
|
||||||
|
uint8_t byte = buf[bi];
|
||||||
|
|
||||||
if (this->read_pos_ == DFPLAYER_READ_BUFFER_LENGTH)
|
if (this->read_pos_ == DFPLAYER_READ_BUFFER_LENGTH)
|
||||||
this->read_pos_ = 0;
|
this->read_pos_ = 0;
|
||||||
@@ -229,7 +237,8 @@ void DFPlayer::loop() {
|
|||||||
this->is_playing_ = false;
|
this->is_playing_ = false;
|
||||||
break;
|
break;
|
||||||
case 0x07:
|
case 0x07:
|
||||||
ESP_LOGE(TAG, "Insertion error (an inserting operation only can be done when a track is being played)");
|
ESP_LOGE(TAG,
|
||||||
|
"Insertion error (an inserting operation only can be done when a track is being played)");
|
||||||
break;
|
break;
|
||||||
case 0x08:
|
case 0x08:
|
||||||
ESP_LOGE(TAG, "SD card reading failed (SD card pulled out or damaged)");
|
ESP_LOGE(TAG, "SD card reading failed (SD card pulled out or damaged)");
|
||||||
@@ -267,6 +276,7 @@ void DFPlayer::loop() {
|
|||||||
this->read_pos_++;
|
this->read_pos_++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
void DFPlayer::dump_config() {
|
void DFPlayer::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "DFPlayer:");
|
ESP_LOGCONFIG(TAG, "DFPlayer:");
|
||||||
this->check_uart_settings(9600);
|
this->check_uart_settings(9600);
|
||||||
|
|||||||
@@ -28,16 +28,29 @@ void DlmsMeterComponent::dump_config() {
|
|||||||
|
|
||||||
void DlmsMeterComponent::loop() {
|
void DlmsMeterComponent::loop() {
|
||||||
// Read while data is available, netznoe uses two frames so allow 2x max frame length
|
// Read while data is available, netznoe uses two frames so allow 2x max frame length
|
||||||
while (this->available()) {
|
int avail = this->available();
|
||||||
if (this->receive_buffer_.size() >= MBUS_MAX_FRAME_LENGTH * 2) {
|
if (avail > 0) {
|
||||||
|
size_t remaining = MBUS_MAX_FRAME_LENGTH * 2 - this->receive_buffer_.size();
|
||||||
|
if (remaining == 0) {
|
||||||
ESP_LOGW(TAG, "Receive buffer full, dropping remaining bytes");
|
ESP_LOGW(TAG, "Receive buffer full, dropping remaining bytes");
|
||||||
|
} else {
|
||||||
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
// Cap reads to remaining buffer capacity.
|
||||||
|
if (static_cast<size_t>(avail) > remaining) {
|
||||||
|
avail = remaining;
|
||||||
|
}
|
||||||
|
uint8_t buf[64];
|
||||||
|
while (avail > 0) {
|
||||||
|
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
|
||||||
|
if (!this->read_array(buf, to_read)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
uint8_t c;
|
avail -= to_read;
|
||||||
this->read_byte(&c);
|
this->receive_buffer_.insert(this->receive_buffer_.end(), buf, buf + to_read);
|
||||||
this->receive_buffer_.push_back(c);
|
|
||||||
this->last_read_ = millis();
|
this->last_read_ = millis();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!this->receive_buffer_.empty() && millis() - this->last_read_ > this->read_timeout_) {
|
if (!this->receive_buffer_.empty() && millis() - this->last_read_ > this->read_timeout_) {
|
||||||
this->mbus_payload_.clear();
|
this->mbus_payload_.clear();
|
||||||
|
|||||||
@@ -40,9 +40,7 @@ bool Dsmr::ready_to_request_data_() {
|
|||||||
this->start_requesting_data_();
|
this->start_requesting_data_();
|
||||||
}
|
}
|
||||||
if (!this->requesting_data_) {
|
if (!this->requesting_data_) {
|
||||||
while (this->available()) {
|
this->drain_rx_buffer_();
|
||||||
this->read();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return this->requesting_data_;
|
return this->requesting_data_;
|
||||||
@@ -115,13 +113,21 @@ void Dsmr::stop_requesting_data_() {
|
|||||||
} else {
|
} else {
|
||||||
ESP_LOGV(TAG, "Stop reading data from P1 port");
|
ESP_LOGV(TAG, "Stop reading data from P1 port");
|
||||||
}
|
}
|
||||||
while (this->available()) {
|
this->drain_rx_buffer_();
|
||||||
this->read();
|
|
||||||
}
|
|
||||||
this->requesting_data_ = false;
|
this->requesting_data_ = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Dsmr::drain_rx_buffer_() {
|
||||||
|
uint8_t buf[64];
|
||||||
|
int avail;
|
||||||
|
while ((avail = this->available()) > 0) {
|
||||||
|
if (!this->read_array(buf, std::min(static_cast<size_t>(avail), sizeof(buf)))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Dsmr::reset_telegram_() {
|
void Dsmr::reset_telegram_() {
|
||||||
this->header_found_ = false;
|
this->header_found_ = false;
|
||||||
this->footer_found_ = false;
|
this->footer_found_ = false;
|
||||||
@@ -133,7 +139,17 @@ void Dsmr::reset_telegram_() {
|
|||||||
|
|
||||||
void Dsmr::receive_telegram_() {
|
void Dsmr::receive_telegram_() {
|
||||||
while (this->available_within_timeout_()) {
|
while (this->available_within_timeout_()) {
|
||||||
const char c = this->read();
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
uint8_t buf[64];
|
||||||
|
int avail = this->available();
|
||||||
|
while (avail > 0) {
|
||||||
|
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
|
||||||
|
if (!this->read_array(buf, to_read))
|
||||||
|
return;
|
||||||
|
avail -= to_read;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
|
const char c = static_cast<char>(buf[i]);
|
||||||
|
|
||||||
// Find a new telegram header, i.e. forward slash.
|
// Find a new telegram header, i.e. forward slash.
|
||||||
if (c == '/') {
|
if (c == '/') {
|
||||||
@@ -184,10 +200,22 @@ void Dsmr::receive_telegram_() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Dsmr::receive_encrypted_telegram_() {
|
void Dsmr::receive_encrypted_telegram_() {
|
||||||
while (this->available_within_timeout_()) {
|
while (this->available_within_timeout_()) {
|
||||||
const char c = this->read();
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
uint8_t buf[64];
|
||||||
|
int avail = this->available();
|
||||||
|
while (avail > 0) {
|
||||||
|
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
|
||||||
|
if (!this->read_array(buf, to_read))
|
||||||
|
return;
|
||||||
|
avail -= to_read;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
|
const char c = static_cast<char>(buf[i]);
|
||||||
|
|
||||||
// Find a new telegram start byte.
|
// Find a new telegram start byte.
|
||||||
if (!this->header_found_) {
|
if (!this->header_found_) {
|
||||||
@@ -249,6 +277,8 @@ void Dsmr::receive_encrypted_telegram_() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool Dsmr::parse_telegram() {
|
bool Dsmr::parse_telegram() {
|
||||||
MyData data;
|
MyData data;
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ class Dsmr : public Component, public uart::UARTDevice {
|
|||||||
void receive_telegram_();
|
void receive_telegram_();
|
||||||
void receive_encrypted_telegram_();
|
void receive_encrypted_telegram_();
|
||||||
void reset_telegram_();
|
void reset_telegram_();
|
||||||
|
void drain_rx_buffer_();
|
||||||
|
|
||||||
/// Wait for UART data to become available within the read timeout.
|
/// Wait for UART data to become available within the read timeout.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -275,8 +275,19 @@ void LD2410Component::restart_and_read_all_info() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LD2410Component::loop() {
|
void LD2410Component::loop() {
|
||||||
while (this->available()) {
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
this->readline_(this->read());
|
int avail = this->available();
|
||||||
|
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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -310,8 +310,19 @@ void LD2412Component::restart_and_read_all_info() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LD2412Component::loop() {
|
void LD2412Component::loop() {
|
||||||
while (this->available()) {
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
this->readline_(this->read());
|
int avail = this->available();
|
||||||
|
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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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,23 @@ void LD2420Component::handle_simple_mode_(const uint8_t *inbuf, int len) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LD2420Component::read_batch_(std::span<uint8_t, MAX_LINE_LENGTH> buffer) {
|
||||||
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
int avail = this->available();
|
||||||
|
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];
|
||||||
|
|||||||
@@ -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_; };
|
||||||
|
|
||||||
|
|||||||
@@ -276,8 +276,19 @@ void LD2450Component::dump_config() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LD2450Component::loop() {
|
void LD2450Component::loop() {
|
||||||
while (this->available()) {
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
this->readline_(this->read());
|
int avail = this->available();
|
||||||
|
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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,10 +19,18 @@ void Modbus::setup() {
|
|||||||
void Modbus::loop() {
|
void Modbus::loop() {
|
||||||
const uint32_t now = App.get_loop_component_start_time();
|
const uint32_t now = App.get_loop_component_start_time();
|
||||||
|
|
||||||
while (this->available()) {
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
uint8_t byte;
|
int avail = this->available();
|
||||||
this->read_byte(&byte);
|
uint8_t buf[64];
|
||||||
if (this->parse_modbus_byte_(byte)) {
|
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++) {
|
||||||
|
if (this->parse_modbus_byte_(buf[i])) {
|
||||||
this->last_modbus_byte_ = now;
|
this->last_modbus_byte_ = now;
|
||||||
} else {
|
} else {
|
||||||
size_t at = this->rx_buffer_.size();
|
size_t at = this->rx_buffer_.size();
|
||||||
@@ -32,6 +40,7 @@ void Modbus::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (now - this->last_modbus_byte_ > 50) {
|
if (now - this->last_modbus_byte_ > 50) {
|
||||||
size_t at = this->rx_buffer_.size();
|
size_t at = this->rx_buffer_.size();
|
||||||
|
|||||||
@@ -397,11 +397,17 @@ bool Nextion::remove_from_q_(bool report_empty) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Nextion::process_serial_() {
|
void Nextion::process_serial_() {
|
||||||
uint8_t d;
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
int avail = this->available();
|
||||||
|
uint8_t buf[64];
|
||||||
|
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;
|
||||||
|
|
||||||
while (this->available()) {
|
this->command_data_.append(reinterpret_cast<const char *>(buf), to_read);
|
||||||
read_byte(&d);
|
|
||||||
this->command_data_ += d;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// nextion.tech/instruction-set/
|
// nextion.tech/instruction-set/
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include "rd03d.h"
|
#include "rd03d.h"
|
||||||
|
#include "esphome/core/helpers.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
@@ -80,8 +81,17 @@ void RD03DComponent::dump_config() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void RD03DComponent::loop() {
|
void RD03DComponent::loop() {
|
||||||
while (this->available()) {
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
uint8_t byte = this->read();
|
int avail = this->available();
|
||||||
|
uint8_t buf[64];
|
||||||
|
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++) {
|
||||||
|
uint8_t byte = buf[i];
|
||||||
ESP_LOGVV(TAG, "Received byte: 0x%02X, buffer_pos: %d", byte, this->buffer_pos_);
|
ESP_LOGVV(TAG, "Received byte: 0x%02X, buffer_pos: %d", byte, this->buffer_pos_);
|
||||||
|
|
||||||
// Check if we're looking for frame header
|
// Check if we're looking for frame header
|
||||||
@@ -114,6 +124,7 @@ void RD03DComponent::loop() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RD03DComponent::process_frame_() {
|
void RD03DComponent::process_frame_() {
|
||||||
// Apply throttle if configured
|
// Apply throttle if configured
|
||||||
|
|||||||
@@ -136,17 +136,24 @@ void RFBridgeComponent::loop() {
|
|||||||
this->last_bridge_byte_ = now;
|
this->last_bridge_byte_ = now;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (this->available()) {
|
int avail = this->available();
|
||||||
uint8_t byte;
|
while (avail > 0) {
|
||||||
this->read_byte(&byte);
|
uint8_t buf[64];
|
||||||
if (this->parse_bridge_byte_(byte)) {
|
size_t to_read = std::min(static_cast<size_t>(avail), sizeof(buf));
|
||||||
ESP_LOGVV(TAG, "Parsed: 0x%02X", byte);
|
if (!this->read_array(buf, to_read)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
avail -= to_read;
|
||||||
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
|
if (this->parse_bridge_byte_(buf[i])) {
|
||||||
|
ESP_LOGVV(TAG, "Parsed: 0x%02X", buf[i]);
|
||||||
this->last_bridge_byte_ = now;
|
this->last_bridge_byte_ = now;
|
||||||
} else {
|
} else {
|
||||||
this->rx_buffer_.clear();
|
this->rx_buffer_.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void RFBridgeComponent::send_code(RFBridgeData data) {
|
void RFBridgeComponent::send_code(RFBridgeData data) {
|
||||||
ESP_LOGD(TAG, "Sending code: sync=0x%04" PRIX16 " low=0x%04" PRIX16 " high=0x%04" PRIX16 " code=0x%06" PRIX32,
|
ESP_LOGD(TAG, "Sending code: sync=0x%04" PRIX16 " low=0x%04" PRIX16 " high=0x%04" PRIX16 " code=0x%06" PRIX32,
|
||||||
|
|||||||
@@ -106,12 +106,19 @@ void MR24HPC1Component::update_() {
|
|||||||
|
|
||||||
// main loop
|
// main loop
|
||||||
void MR24HPC1Component::loop() {
|
void MR24HPC1Component::loop() {
|
||||||
uint8_t byte;
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
int avail = this->available();
|
||||||
|
uint8_t buf[64];
|
||||||
|
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;
|
||||||
|
|
||||||
// Is there data on the serial port
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
while (this->available()) {
|
this->r24_split_data_frame_(buf[i]); // split data frame
|
||||||
this->read_byte(&byte);
|
}
|
||||||
this->r24_split_data_frame_(byte); // split data frame
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((this->s_output_info_switch_flag_ == OUTPUT_SWTICH_OFF) &&
|
if ((this->s_output_info_switch_flag_ == OUTPUT_SWTICH_OFF) &&
|
||||||
|
|||||||
@@ -30,17 +30,24 @@ void MR60BHA2Component::dump_config() {
|
|||||||
|
|
||||||
// main loop
|
// main loop
|
||||||
void MR60BHA2Component::loop() {
|
void MR60BHA2Component::loop() {
|
||||||
uint8_t byte;
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
int avail = this->available();
|
||||||
|
uint8_t buf[64];
|
||||||
|
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;
|
||||||
|
|
||||||
// Is there data on the serial port
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
while (this->available()) {
|
this->rx_message_.push_back(buf[i]);
|
||||||
this->read_byte(&byte);
|
|
||||||
this->rx_message_.push_back(byte);
|
|
||||||
if (!this->validate_message_()) {
|
if (!this->validate_message_()) {
|
||||||
this->rx_message_.clear();
|
this->rx_message_.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Calculate the checksum for a byte array.
|
* @brief Calculate the checksum for a byte array.
|
||||||
|
|||||||
@@ -49,12 +49,19 @@ void MR60FDA2Component::setup() {
|
|||||||
|
|
||||||
// main loop
|
// main loop
|
||||||
void MR60FDA2Component::loop() {
|
void MR60FDA2Component::loop() {
|
||||||
uint8_t byte;
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
|
int avail = this->available();
|
||||||
|
uint8_t buf[64];
|
||||||
|
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;
|
||||||
|
|
||||||
// Is there data on the serial port
|
for (size_t i = 0; i < to_read; i++) {
|
||||||
while (this->available()) {
|
this->split_frame_(buf[i]); // split data frame
|
||||||
this->read_byte(&byte);
|
}
|
||||||
this->split_frame_(byte); // split data frame
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,10 +31,19 @@ void Tuya::setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Tuya::loop() {
|
void Tuya::loop() {
|
||||||
while (this->available()) {
|
// Read all available bytes in batches to reduce UART call overhead.
|
||||||
uint8_t c;
|
int avail = this->available();
|
||||||
this->read_byte(&c);
|
uint8_t buf[64];
|
||||||
this->handle_char_(c);
|
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->handle_char_(buf[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
process_command_queue_();
|
process_command_queue_();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user