[uart] Store static data in flash and use function pointers for lambdas

This commit is contained in:
J. Nick Koston
2025-11-08 21:12:00 -06:00
parent b61027607f
commit f6bf6bd8ee
2 changed files with 24 additions and 15 deletions

View File

@@ -31,7 +31,7 @@ from esphome.const import (
PLATFORM_HOST, PLATFORM_HOST,
PlatformFramework, PlatformFramework,
) )
from esphome.core import CORE from esphome.core import CORE, ID
import esphome.final_validate as fv import esphome.final_validate as fv
from esphome.yaml_util import make_data_base from esphome.yaml_util import make_data_base
@@ -446,7 +446,10 @@ async def uart_write_to_code(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(cg.ArrayInitializer(*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,32 +10,38 @@ namespace uart {
template<typename... Ts> class UARTWriteAction : public Action<Ts...>, public Parented<UARTComponent> { template<typename... Ts> class UARTWriteAction : public Action<Ts...>, public Parented<UARTComponent> {
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; // Stateless lambdas (generated by ESPHome) implicitly convert to function pointers
this->data_.func = func;
this->static_ = false; this->static_ = false;
} }
void set_data_static(std::vector<uint8_t> &&data) {
this->data_static_ = std::move(data); // Store pointer to static data in flash (no RAM copy)
this->static_ = true; void set_data_static(const uint8_t *data, size_t len) {
} // Simply set pointer and length - no construction needed for POD types
void set_data_static(std::initializer_list<uint8_t> data) { this->data_.static_data.ptr = data;
this->data_static_ = std::vector<uint8_t>(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_->write_array(this->data_static_); this->parent_->write_array(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_->write_array(val); this->parent_->write_array(val);
} }
} }
protected: protected:
bool static_{false}; bool static_{true}; // Default to static mode (most common case)
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)
struct {
const uint8_t *ptr;
size_t len;
} static_data;
} data_;
}; };
} // namespace uart } // namespace uart