2026-11-05 12:27:54 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "esphome/core/component.h"
|
2026-11-19 00:31:45 +01:00
|
|
|
#include "esphome/core/defines.h"
|
2026-11-05 12:27:54 +01:00
|
|
|
#include "esphome/components/event/event.h"
|
|
|
|
|
#include "esphome/components/uart/uart.h"
|
2026-11-19 00:31:45 +01:00
|
|
|
#include <type_traits>
|
2026-11-05 12:27:54 +01:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
namespace esphome::uart {
|
|
|
|
|
|
2026-11-19 00:31:45 +01:00
|
|
|
/// An event type and the byte pattern that fires it; `data` may point to PROGMEM.
|
|
|
|
|
struct UARTEventMatcher {
|
|
|
|
|
const char *event_name;
|
|
|
|
|
const uint8_t *data;
|
|
|
|
|
size_t data_len;
|
|
|
|
|
};
|
|
|
|
|
// Codegen keeps the matchers in a flash table read with plain loads, which ESP8266 only allows for whole words.
|
|
|
|
|
static_assert(std::is_same_v<decltype(UARTEventMatcher::event_name), const char *>,
|
|
|
|
|
"UARTEventMatcher fields must stay word sized");
|
|
|
|
|
static_assert(std::is_same_v<decltype(UARTEventMatcher::data), const uint8_t *>,
|
|
|
|
|
"UARTEventMatcher fields must stay word sized");
|
|
|
|
|
static_assert(std::is_same_v<decltype(UARTEventMatcher::data_len), size_t>,
|
|
|
|
|
"UARTEventMatcher fields must stay word sized");
|
|
|
|
|
#ifdef USE_ESP8266
|
|
|
|
|
static_assert(sizeof(UARTEventMatcher) == 3 * sizeof(uint32_t), "UARTEventMatcher is read from flash with word loads");
|
|
|
|
|
#endif
|
|
|
|
|
|
2026-11-05 12:27:54 +01:00
|
|
|
class UARTEvent final : public event::Event, public UARTDevice, public Component {
|
|
|
|
|
public:
|
|
|
|
|
void setup() override;
|
|
|
|
|
void loop() override;
|
|
|
|
|
void dump_config() override;
|
|
|
|
|
|
2026-11-19 00:31:45 +01:00
|
|
|
/// `matchers` is a codegen PROGMEM table that must outlive the component.
|
|
|
|
|
void set_matchers(const UARTEventMatcher *matchers, uint16_t count, uint16_t max_len) {
|
|
|
|
|
this->matchers_ = matchers;
|
|
|
|
|
this->matcher_count_ = count;
|
|
|
|
|
this->max_matcher_len_ = max_len;
|
|
|
|
|
}
|
2026-11-05 12:27:54 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void read_data_();
|
2026-11-19 00:31:45 +01:00
|
|
|
const UARTEventMatcher *matchers_{nullptr};
|
2026-11-05 12:27:54 +01:00
|
|
|
std::vector<uint8_t> buffer_;
|
2026-11-19 00:31:45 +01:00
|
|
|
uint16_t matcher_count_{0};
|
|
|
|
|
uint16_t max_matcher_len_{0};
|
2026-11-05 12:27:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace esphome::uart
|