ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
uart_event.cpp
Go to the documentation of this file.
1#include "uart_event.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome::uart {
6
7ESPHOME_LOG_TAG(TAG, "uart.event");
8
10
11void UARTEvent::dump_config() { LOG_EVENT("", "UART Event", this); }
12
13void UARTEvent::loop() { this->read_data_(); }
14
16 this->read_available([this](uint8_t data) {
17 this->buffer_.push_back(data);
18
19 bool match_found = false;
20 for (uint16_t i = 0; i < this->matcher_count_; i++) {
21 const UARTEventMatcher &matcher = this->matchers_[i];
22 if (this->buffer_.size() < matcher.data_len) {
23 continue;
24 }
25
26 const uint8_t *tail = this->buffer_.data() + this->buffer_.size() - matcher.data_len;
27 size_t pos = 0;
28 // The pattern is in flash; ESP8266 only allows word loads there, so read it with progmem_read_byte
29 while (pos < matcher.data_len && progmem_read_byte(matcher.data + pos) == tail[pos])
30 pos++;
31 if (pos == matcher.data_len) {
32 this->trigger(matcher.event_name);
33 this->buffer_.clear();
34 match_found = true;
35 break;
36 }
37 }
38
39 if (!match_found && this->max_matcher_len_ > 0 && this->buffer_.size() > this->max_matcher_len_) {
40 this->buffer_.erase(this->buffer_.begin());
41 }
42 });
43}
44
45} // namespace esphome::uart
void trigger(const char *event_type)
Trigger an event; the type is matched against the configured types by string compare.
Definition event.cpp:10
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
const UARTEventMatcher * matchers_
Definition uart_event.h:44
void dump_config() override
std::vector< uint8_t > buffer_
Definition uart_event.h:45
void setup() override
Definition uart_event.cpp:9
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")
size_t size_t pos
Definition helpers.h:1333
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
An event type and the byte pattern that fires it; data may point to PROGMEM.
Definition uart_event.h:13