mirror of
https://github.com/esphome/esphome.git
synced 2025-11-19 16:25:50 +00:00
[uart] Store static data in flash and use function pointers for lambdas (#11784)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,32 +10,35 @@ 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->static_ = false;
|
this->code_.func = func;
|
||||||
|
this->len_ = -1; // Sentinel value indicates template mode
|
||||||
}
|
}
|
||||||
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) {
|
||||||
}
|
this->code_.data = data;
|
||||||
void set_data_static(std::initializer_list<uint8_t> data) {
|
this->len_ = len; // Length >= 0 indicates static mode
|
||||||
this->data_static_ = std::vector<uint8_t>(data);
|
|
||||||
this->static_ = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void play(const Ts &...x) override {
|
void play(const Ts &...x) override {
|
||||||
if (this->static_) {
|
if (this->len_ >= 0) {
|
||||||
this->parent_->write_array(this->data_static_);
|
// Static mode: use pointer and length
|
||||||
|
this->parent_->write_array(this->code_.data, static_cast<size_t>(this->len_));
|
||||||
} else {
|
} else {
|
||||||
auto val = this->data_func_(x...);
|
// Template mode: call function
|
||||||
|
auto val = this->code_.func(x...);
|
||||||
this->parent_->write_array(val);
|
this->parent_->write_array(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 Code {
|
||||||
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
|
||||||
|
} code_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace uart
|
} // namespace uart
|
||||||
|
|||||||
Reference in New Issue
Block a user