ESPHome
2026.1.0-dev
Loading...
Searching...
No Matches
esphome
components
uart
event
uart_event.cpp
Go to the documentation of this file.
1
#include "
uart_event.h
"
2
#include "
esphome/core/log.h
"
3
#include <algorithm>
4
5
namespace
esphome::uart
{
6
7
static
const
char
*
const
TAG
=
"uart.event"
;
8
9
void
UARTEvent::setup
() {}
10
11
void
UARTEvent::dump_config
() { LOG_EVENT(
""
,
"UART Event"
,
this
); }
12
13
void
UARTEvent::loop
() { this->
read_data_
(); }
14
15
void
UARTEvent::add_event_matcher
(
const
char
*event_name,
const
uint8_t *match_data,
size_t
match_data_len) {
16
this->
matchers_
.push_back({event_name, match_data, match_data_len});
17
if
(match_data_len > this->
max_matcher_len_
) {
18
this->
max_matcher_len_
= match_data_len;
19
}
20
}
21
22
void
UARTEvent::read_data_
() {
23
while
(this->
available
()) {
24
uint8_t data;
25
this->
read_byte
(&data);
26
this->
buffer_
.push_back(data);
27
28
bool
match_found =
false
;
29
for
(
const
auto
&matcher : this->
matchers_
) {
30
if
(this->
buffer_
.size() < matcher.data_len) {
31
continue
;
32
}
33
34
if
(std::equal(matcher.data, matcher.data + matcher.data_len, this->buffer_.end() - matcher.data_len)) {
35
this->
trigger
(matcher.event_name);
36
this->
buffer_
.clear();
37
match_found =
true
;
38
break
;
39
}
40
}
41
42
if
(!match_found && this->
max_matcher_len_
> 0 && this->
buffer_
.size() > this->max_matcher_len_) {
43
this->
buffer_
.erase(this->
buffer_
.begin());
44
}
45
}
46
}
47
48
}
// namespace esphome::uart
esphome::event::Event::trigger
void trigger(const std::string &event_type)
Definition
event.cpp:11
esphome::uart::UARTDevice::read_byte
bool read_byte(uint8_t *data)
Definition
uart.h:34
esphome::uart::UARTDevice::available
int available()
Definition
uart.h:46
esphome::uart::UARTEvent::loop
void loop() override
Definition
uart_event.cpp:13
esphome::uart::UARTEvent::dump_config
void dump_config() override
Definition
uart_event.cpp:11
esphome::uart::UARTEvent::buffer_
std::vector< uint8_t > buffer_
Definition
uart_event.h:27
esphome::uart::UARTEvent::setup
void setup() override
Definition
uart_event.cpp:9
esphome::uart::UARTEvent::add_event_matcher
void add_event_matcher(const char *event_name, const uint8_t *match_data, size_t match_data_len)
Definition
uart_event.cpp:15
esphome::uart::UARTEvent::max_matcher_len_
size_t max_matcher_len_
Definition
uart_event.h:28
esphome::uart::UARTEvent::read_data_
void read_data_()
Definition
uart_event.cpp:22
esphome::uart::UARTEvent::matchers_
std::vector< EventMatcher > matchers_
Definition
uart_event.h:26
log.h
esphome::spi::TAG
const char *const TAG
Definition
spi.cpp:7
esphome::uart
Definition
automation.h:8
uart_event.h
Generated by
1.12.0