1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-19 16:25:50 +00:00

Merge branch 'speaker_automation' into integration

This commit is contained in:
J. Nick Koston
2025-11-08 22:56:57 -06:00
3 changed files with 39 additions and 11 deletions

View File

@@ -3,7 +3,7 @@ import esphome.codegen as cg
from esphome.components import audio, audio_dac from esphome.components import audio, audio_dac
import esphome.config_validation as cv import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_ID, CONF_VOLUME from esphome.const import CONF_DATA, CONF_ID, CONF_VOLUME
from esphome.core import CORE from esphome.core import CORE, ID
from esphome.coroutine import CoroPriority, coroutine_with_priority from esphome.coroutine import CoroPriority, coroutine_with_priority
AUTO_LOAD = ["audio"] AUTO_LOAD = ["audio"]
@@ -90,7 +90,10 @@ async def speaker_play_action(config, action_id, template_arg, args):
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8)) templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
cg.add(var.set_data_template(templ)) cg.add(var.set_data_template(templ))
else: else:
cg.add(var.set_data_static(data)) # Generate static array in flash to avoid RAM copy
arr_id = ID(f"{action_id}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data))
cg.add(var.set_data_static(arr, len(data)))
return var return var

View File

@@ -10,28 +10,35 @@ namespace speaker {
template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<Speaker> { template<typename... Ts> class PlayAction : public Action<Ts...>, public Parented<Speaker> {
public: public:
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) { void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
this->data_func_ = func; this->data_.func = func;
this->static_ = false; this->static_ = false;
} }
void set_data_static(const std::vector<uint8_t> &data) {
this->data_static_ = data; void set_data_static(const uint8_t *data, size_t len) {
this->data_.static_data.ptr = data;
this->data_.static_data.len = len;
this->static_ = true; this->static_ = true;
} }
void play(const Ts &...x) override { void play(const Ts &...x) override {
if (this->static_) { if (this->static_) {
this->parent_->play(this->data_static_); this->parent_->play(this->data_.static_data.ptr, this->data_.static_data.len);
} else { } else {
auto val = this->data_func_(x...); auto val = this->data_.func(x...);
this->parent_->play(val); this->parent_->play(val);
} }
} }
protected: protected:
bool static_{false}; bool static_{true};
std::function<std::vector<uint8_t>(Ts...)> data_func_{}; union Data {
std::vector<uint8_t> data_static_{}; std::vector<uint8_t> (*func)(Ts...);
struct {
const uint8_t *ptr;
size_t len;
} static_data;
} data_;
}; };
template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> { template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> {

View File

@@ -1,3 +1,12 @@
number:
- platform: template
name: "Speaker Number"
id: my_number
optimistic: true
min_value: 0
max_value: 100
step: 1
esphome: esphome:
on_boot: on_boot:
then: then:
@@ -14,6 +23,15 @@ esphome:
- speaker.finish: - speaker.finish:
- speaker.stop: - speaker.stop:
button:
- platform: template
name: "Speaker Button"
on_press:
then:
- speaker.play: [0x10, 0x20, 0x30, 0x40]
- speaker.play: !lambda |-
return {0x01, 0x02, (uint8_t)id(my_number).state};
i2s_audio: i2s_audio:
i2s_lrclk_pin: ${i2s_bclk_pin} i2s_lrclk_pin: ${i2s_bclk_pin}
i2s_bclk_pin: ${i2s_lrclk_pin} i2s_bclk_pin: ${i2s_lrclk_pin}