ESPHome 2025.12.0-dev
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 {
9namespace uart {
10
11template<typename... Ts> class UARTWriteAction : public Action<Ts...>, public Parented<UARTComponent> {
12 public:
13 void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
14 // Stateless lambdas (generated by ESPHome) implicitly convert to function pointers
15 this->code_.func = func;
16 this->len_ = -1; // Sentinel value indicates template mode
17 }
18
19 // Store pointer to static data in flash (no RAM copy)
20 void set_data_static(const uint8_t *data, size_t len) {
21 this->code_.data = data;
22 this->len_ = len; // Length >= 0 indicates static mode
23 }
24
25 void play(const Ts &...x) override {
26 if (this->len_ >= 0) {
27 // Static mode: use pointer and length
28 this->parent_->write_array(this->code_.data, static_cast<size_t>(this->len_));
29 } else {
30 // Template mode: call function
31 auto val = this->code_.func(x...);
32 this->parent_->write_array(val);
33 }
34 }
35
36 protected:
37 ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
38 union Code {
39 std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
40 const uint8_t *data; // Pointer to static data in flash
42};
43
44} // namespace uart
45} // namespace esphome
Helper class to easily give an object a parent of type T.
Definition helpers.h:918
void play(const Ts &...x) override
Definition automation.h:25
union esphome::uart::UARTWriteAction::Code code_
void set_data_template(std::vector< uint8_t >(*func)(Ts...))
Definition automation.h:13
void set_data_static(const uint8_t *data, size_t len)
Definition automation.h:20
__int64 ssize_t
Definition httplib.h:178
mopeka_std_values val[4]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:500
uint16_t x
Definition tt21100.cpp:5
std::vector< uint8_t >(* func)(Ts...)
Definition automation.h:39