mirror of
https://github.com/esphome/esphome.git
synced 2025-11-20 00:35:44 +00:00
[sx126x] Optimize send_packet action memory usage - store static data in flash
This commit is contained in:
@@ -3,7 +3,7 @@ import esphome.codegen as cg
|
|||||||
from esphome.components import spi
|
from esphome.components import spi
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
from esphome.const import CONF_BUSY_PIN, CONF_DATA, CONF_FREQUENCY, CONF_ID
|
from esphome.const import CONF_BUSY_PIN, CONF_DATA, CONF_FREQUENCY, CONF_ID
|
||||||
from esphome.core import TimePeriod
|
from esphome.core import ID, TimePeriod
|
||||||
|
|
||||||
MULTI_CONF = True
|
MULTI_CONF = True
|
||||||
CODEOWNERS = ["@swoboda1337"]
|
CODEOWNERS = ["@swoboda1337"]
|
||||||
@@ -329,5 +329,8 @@ async def send_packet_action_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(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
|
||||||
|
|||||||
@@ -14,28 +14,36 @@ template<typename... Ts> class RunImageCalAction : public Action<Ts...>, public
|
|||||||
|
|
||||||
template<typename... Ts> class SendPacketAction : public Action<Ts...>, public Parented<SX126x> {
|
template<typename... Ts> class SendPacketAction : public Action<Ts...>, public Parented<SX126x> {
|
||||||
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) {
|
void set_data_static(const uint8_t *data, size_t len) {
|
||||||
this->data_static_ = data;
|
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 {
|
||||||
|
std::vector<uint8_t> data;
|
||||||
if (this->static_) {
|
if (this->static_) {
|
||||||
this->parent_->transmit_packet(this->data_static_);
|
data.assign(this->data_.static_data.ptr, this->data_.static_data.ptr + this->data_.static_data.len);
|
||||||
} else {
|
} else {
|
||||||
this->parent_->transmit_packet(this->data_func_(x...));
|
data = this->data_.func(x...);
|
||||||
}
|
}
|
||||||
|
this->parent_->transmit_packet(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
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 SetModeTxAction : public Action<Ts...>, public Parented<SX126x> {
|
template<typename... Ts> class SetModeTxAction : public Action<Ts...>, public Parented<SX126x> {
|
||||||
|
|||||||
@@ -26,6 +26,15 @@ sx126x:
|
|||||||
- lambda: |-
|
- lambda: |-
|
||||||
ESP_LOGD("lambda", "packet %.2f %.2f %s", rssi, snr, format_hex(x).c_str());
|
ESP_LOGD("lambda", "packet %.2f %.2f %s", rssi, snr, format_hex(x).c_str());
|
||||||
|
|
||||||
|
number:
|
||||||
|
- platform: template
|
||||||
|
name: "SX126x Number"
|
||||||
|
id: my_number
|
||||||
|
optimistic: true
|
||||||
|
min_value: 0
|
||||||
|
max_value: 100
|
||||||
|
step: 1
|
||||||
|
|
||||||
button:
|
button:
|
||||||
- platform: template
|
- platform: template
|
||||||
name: "SX126x Button"
|
name: "SX126x Button"
|
||||||
@@ -37,3 +46,5 @@ button:
|
|||||||
- sx126x.set_mode_rx
|
- sx126x.set_mode_rx
|
||||||
- sx126x.send_packet:
|
- sx126x.send_packet:
|
||||||
data: [0xC5, 0x51, 0x78, 0x82, 0xB7, 0xF9, 0x9C, 0x5C]
|
data: [0xC5, 0x51, 0x78, 0x82, 0xB7, 0xF9, 0x9C, 0x5C]
|
||||||
|
- sx126x.send_packet: !lambda |-
|
||||||
|
return {0x01, 0x02, (uint8_t)id(my_number).state};
|
||||||
|
|||||||
Reference in New Issue
Block a user