mirror of
https://github.com/esphome/esphome.git
synced 2025-11-16 23:05:46 +00:00
[udp] Optimize udp.write action memory usage - store static data in flash
This commit is contained in:
@@ -12,7 +12,7 @@ from esphome.components.packet_transport import (
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_DATA, CONF_ID, CONF_PORT, CONF_TRIGGER_ID
|
||||
from esphome.core import Lambda
|
||||
from esphome.core import ID, Lambda
|
||||
from esphome.cpp_generator import ExpressionStatement, MockObj
|
||||
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
@@ -158,5 +158,8 @@ async def udp_write_to_code(config, action_id, template_arg, args):
|
||||
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
|
||||
cg.add(var.set_data_template(templ))
|
||||
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
|
||||
|
||||
@@ -11,28 +11,35 @@ namespace udp {
|
||||
|
||||
template<typename... Ts> class UDPWriteAction : public Action<Ts...>, public Parented<UDPComponent> {
|
||||
public:
|
||||
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
|
||||
this->data_func_ = func;
|
||||
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
|
||||
this->data_.func = func;
|
||||
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;
|
||||
}
|
||||
|
||||
void play(const Ts &...x) override {
|
||||
if (this->static_) {
|
||||
this->parent_->send_packet(this->data_static_);
|
||||
this->parent_->send_packet(this->data_.static_data.ptr, this->data_.static_data.len);
|
||||
} else {
|
||||
auto val = this->data_func_(x...);
|
||||
auto val = this->data_.func(x...);
|
||||
this->parent_->send_packet(val);
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
bool static_{false};
|
||||
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
|
||||
std::vector<uint8_t> data_static_{};
|
||||
bool static_{true};
|
||||
union Data {
|
||||
std::vector<uint8_t> (*func)(Ts...);
|
||||
struct {
|
||||
const uint8_t *ptr;
|
||||
size_t len;
|
||||
} static_data;
|
||||
} data_;
|
||||
};
|
||||
|
||||
} // namespace udp
|
||||
|
||||
@@ -17,3 +17,22 @@ udp:
|
||||
id: my_udp
|
||||
data: !lambda |-
|
||||
return std::vector<uint8_t>{1,3,4,5,6};
|
||||
|
||||
number:
|
||||
- platform: template
|
||||
name: "UDP Number"
|
||||
id: my_number
|
||||
optimistic: true
|
||||
min_value: 0
|
||||
max_value: 100
|
||||
step: 1
|
||||
|
||||
button:
|
||||
- platform: template
|
||||
name: "UDP Button"
|
||||
on_press:
|
||||
then:
|
||||
- udp.write:
|
||||
data: [0x01, 0x02, 0x03]
|
||||
- udp.write: !lambda |-
|
||||
return {0x10, 0x20, (uint8_t)id(my_number).state};
|
||||
|
||||
Reference in New Issue
Block a user