ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
3#include "udp_component.h"
4#ifdef USE_NETWORK
6
7#include <vector>
8
9namespace esphome {
10namespace udp {
11
12template<typename... Ts> class UDPWriteAction : public Action<Ts...>, public Parented<UDPComponent> {
13 public:
14 void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
15 this->data_.func = func;
16 this->len_ = -1; // Sentinel value indicates template mode
17 }
18
19 void set_data_static(const uint8_t *data, size_t len) {
20 this->data_.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: pass pointer directly to send_packet(const uint8_t *, size_t)
27 this->parent_->send_packet(this->data_.data, static_cast<size_t>(this->len_));
28 } else {
29 // Template mode: call function and pass vector to send_packet(const std::vector<uint8_t> &)
30 auto val = this->data_.func(x...);
31 this->parent_->send_packet(val);
32 }
33 }
34
35 protected:
36 ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
37 union Data {
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 udp
44} // namespace esphome
45#endif
Helper class to easily give an object a parent of type T.
Definition helpers.h:918
void set_data_static(const uint8_t *data, size_t len)
Definition automation.h:19
void play(const Ts &...x) override
Definition automation.h:24
union esphome::udp::UDPWriteAction::Data data_
void set_data_template(std::vector< uint8_t >(*func)(Ts...))
Definition automation.h:14
__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:38