ESPHome 2025.12.0
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
3#include "uart.h"
5
6#include <vector>
7
8namespace esphome::uart {
9
10template<typename... Ts> class UARTWriteAction : public Action<Ts...>, public Parented<UARTComponent> {
11 public:
12 void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
13 // Stateless lambdas (generated by ESPHome) implicitly convert to function pointers
14 this->code_.func = func;
15 this->len_ = -1; // Sentinel value indicates template mode
16 }
17
18 // Store pointer to static data in flash (no RAM copy)
19 void set_data_static(const uint8_t *data, size_t len) {
20 this->code_.data = data;
21 this->len_ = len; // Length >= 0 indicates static mode
22 }
23
24 void play(const Ts &...x) override {
25 if (this->len_ >= 0) {
26 // Static mode: use pointer and length
27 this->parent_->write_array(this->code_.data, static_cast<size_t>(this->len_));
28 } else {
29 // Template mode: call function
30 auto val = this->code_.func(x...);
31 this->parent_->write_array(val);
32 }
33 }
34
35 protected:
36 ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
37 union Code {
38 std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
39 const uint8_t *data; // Pointer to static data in flash
41};
42
43} // namespace esphome::uart
Helper class to easily give an object a parent of type T.
Definition helpers.h:932
void play(const Ts &...x) override
Definition automation.h:24
union esphome::uart::UARTWriteAction::Code code_
void set_data_template(std::vector< uint8_t >(*func)(Ts...))
Definition automation.h:12
void set_data_static(const uint8_t *data, size_t len)
Definition automation.h:19
__int64 ssize_t
Definition httplib.h:178
mopeka_std_values val[4]
std::string size_t len
Definition helpers.h:503
uint16_t x
Definition tt21100.cpp:5
std::vector< uint8_t >(* func)(Ts...)
Definition automation.h:38