2026-11-05 12:27:54 +01:00
|
|
|
#include "uart_event.h"
|
2026-11-19 00:31:45 +01:00
|
|
|
#include "esphome/core/hal.h"
|
2026-11-05 12:27:54 +01:00
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
|
|
|
|
|
namespace esphome::uart {
|
|
|
|
|
|
2026-11-19 00:31:45 +01:00
|
|
|
ESPHOME_LOG_TAG(TAG, "uart.event");
|
2026-11-05 12:27:54 +01:00
|
|
|
|
|
|
|
|
void UARTEvent::setup() {}
|
|
|
|
|
|
|
|
|
|
void UARTEvent::dump_config() { LOG_EVENT("", "UART Event", this); }
|
|
|
|
|
|
|
|
|
|
void UARTEvent::loop() { this->read_data_(); }
|
|
|
|
|
|
|
|
|
|
void UARTEvent::read_data_() {
|
2026-11-19 00:31:45 +01:00
|
|
|
this->read_available([this](uint8_t data) {
|
2026-11-05 12:27:54 +01:00
|
|
|
this->buffer_.push_back(data);
|
|
|
|
|
|
|
|
|
|
bool match_found = false;
|
2026-11-19 00:31:45 +01:00
|
|
|
for (uint16_t i = 0; i < this->matcher_count_; i++) {
|
|
|
|
|
const UARTEventMatcher &matcher = this->matchers_[i];
|
2026-11-05 12:27:54 +01:00
|
|
|
if (this->buffer_.size() < matcher.data_len) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 00:31:45 +01:00
|
|
|
const uint8_t *tail = this->buffer_.data() + this->buffer_.size() - matcher.data_len;
|
|
|
|
|
size_t pos = 0;
|
|
|
|
|
// The pattern is in flash; ESP8266 only allows word loads there, so read it with progmem_read_byte
|
|
|
|
|
while (pos < matcher.data_len && progmem_read_byte(matcher.data + pos) == tail[pos])
|
|
|
|
|
pos++;
|
|
|
|
|
if (pos == matcher.data_len) {
|
2026-11-05 12:27:54 +01:00
|
|
|
this->trigger(matcher.event_name);
|
|
|
|
|
this->buffer_.clear();
|
|
|
|
|
match_found = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!match_found && this->max_matcher_len_ > 0 && this->buffer_.size() > this->max_matcher_len_) {
|
|
|
|
|
this->buffer_.erase(this->buffer_.begin());
|
|
|
|
|
}
|
2026-11-19 00:31:45 +01:00
|
|
|
});
|
2026-11-05 12:27:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace esphome::uart
|