ESPHome 2026.10.0
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
4#include "speaker.h"
5
6#include <vector>
7
8namespace esphome::speaker {
9
10template<typename... Ts> class PlayAction final : public Action<Ts...>, public Parented<Speaker> {
11 public:
12 void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
13 this->data_.func = func;
14 this->len_ = -1; // Sentinel value indicates template mode
15 }
16
17 void set_data_static(const uint8_t *data, size_t len) {
18 this->data_.data = data;
19 this->len_ = len; // Length >= 0 indicates static mode
20 }
21
22 void play(const Ts &...x) override {
23 if (this->len_ >= 0) {
24 // Static mode: pass pointer directly to play(const uint8_t *, size_t)
25 this->parent_->play(this->data_.data, static_cast<size_t>(this->len_));
26 } else {
27 // Template mode: call function and pass vector to play(const std::vector<uint8_t> &)
28 auto val = this->data_.func(x...);
29 this->parent_->play(val);
30 }
31 }
32
33 protected:
34 ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
35 union Data {
36 std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
37 const uint8_t *data; // Pointer to static data in flash
39};
40
41} // namespace esphome::speaker
Helper class to easily give an object a parent of type T.
Definition helpers.h:2004
void play(const Ts &...x) override
Definition automation.h:22
union esphome::speaker::PlayAction::Data data_
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:17
__int64 ssize_t
Definition httplib.h:178
mopeka_std_values val[3]
const void size_t len
Definition hal.h:64
uint16_t x
Definition tt21100.cpp:5
std::vector< uint8_t >(* func)(Ts...)
Definition automation.h:36