mirror of
https://github.com/esphome/esphome.git
synced 2025-11-16 06:45:48 +00:00
[speaker] Optimize speaker.play action memory usage - store static data in flash (#11796)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,28 +10,33 @@ 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->len_ = -1; // Sentinel value indicates template mode
|
||||||
}
|
}
|
||||||
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->static_ = true;
|
this->data_.data = data;
|
||||||
|
this->len_ = len; // Length >= 0 indicates static mode
|
||||||
}
|
}
|
||||||
|
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
if (this->static_) {
|
if (this->len_ >= 0) {
|
||||||
this->parent_->play(this->data_static_);
|
// Static mode: pass pointer directly to play(const uint8_t *, size_t)
|
||||||
|
this->parent_->play(this->data_.data, static_cast<size_t>(this->len_));
|
||||||
} else {
|
} else {
|
||||||
auto val = this->data_func_(x...);
|
// Template mode: call function and pass vector to play(const std::vector<uint8_t> &)
|
||||||
|
auto val = this->data_.func(x...);
|
||||||
this->parent_->play(val);
|
this->parent_->play(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool static_{false};
|
ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
|
||||||
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
|
union Data {
|
||||||
std::vector<uint8_t> data_static_{};
|
std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
|
||||||
|
const uint8_t *data; // Pointer to static data in flash
|
||||||
|
} data_;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> {
|
template<typename... Ts> class VolumeSetAction : public Action<Ts...>, public Parented<Speaker> {
|
||||||
|
|||||||
Reference in New Issue
Block a user