mirror of
https://github.com/esphome/esphome.git
synced 2026-07-10 08:55:36 +00:00
optimize
This commit is contained in:
@@ -115,14 +115,13 @@ template<typename... Ts> class CanbusSendAction : public Action<Ts...>, public P
|
||||
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
|
||||
// Stateless lambdas (generated by ESPHome) implicitly convert to function pointers
|
||||
this->data_.func = func;
|
||||
this->static_ = false;
|
||||
this->len_ = -1; // Sentinel value indicates template mode
|
||||
}
|
||||
|
||||
// Store pointer to static data in flash (no RAM copy)
|
||||
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;
|
||||
this->data_.data = data;
|
||||
this->len_ = len; // Length >= 0 indicates static mode
|
||||
}
|
||||
|
||||
void set_can_id(uint32_t can_id) { this->can_id_ = can_id; }
|
||||
@@ -138,9 +137,11 @@ template<typename... Ts> class CanbusSendAction : public Action<Ts...>, public P
|
||||
auto use_extended_id =
|
||||
this->use_extended_id_.has_value() ? *this->use_extended_id_ : this->parent_->use_extended_id_;
|
||||
std::vector<uint8_t> data;
|
||||
if (this->static_) {
|
||||
data.assign(this->data_.static_data.ptr, this->data_.static_data.ptr + this->data_.static_data.len);
|
||||
if (this->len_ >= 0) {
|
||||
// Static mode: copy from flash to vector
|
||||
data.assign(this->data_.data, this->data_.data + this->len_);
|
||||
} else {
|
||||
// Template mode: call function
|
||||
data = this->data_.func(x...);
|
||||
}
|
||||
this->parent_->send_data(can_id, use_extended_id, this->remote_transmission_request_, data);
|
||||
@@ -150,14 +151,11 @@ template<typename... Ts> class CanbusSendAction : public Action<Ts...>, public P
|
||||
optional<uint32_t> can_id_{};
|
||||
optional<bool> use_extended_id_{};
|
||||
bool remote_transmission_request_{false};
|
||||
bool static_{true}; // Default to static mode (most common case)
|
||||
ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
|
||||
union Data {
|
||||
std::vector<uint8_t> (*func)(Ts...); // 4 bytes on 32-bit
|
||||
struct {
|
||||
const uint8_t *ptr; // 4 bytes on 32-bit
|
||||
size_t len; // 4 bytes on 32-bit
|
||||
} static_data; // 8 bytes total on 32-bit
|
||||
} data_; // Union size = 8 bytes (max of 4 and 8)
|
||||
std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
|
||||
const uint8_t *data; // Pointer to static data in flash
|
||||
} data_;
|
||||
};
|
||||
|
||||
class CanbusTrigger : public Trigger<std::vector<uint8_t>, uint32_t, bool>, public Component {
|
||||
|
||||
Reference in New Issue
Block a user