diff --git a/esphome/components/ble_client/automation.h b/esphome/components/ble_client/automation.h index 46915be9ed..9c5646b3d1 100644 --- a/esphome/components/ble_client/automation.h +++ b/esphome/components/ble_client/automation.h @@ -107,15 +107,14 @@ template class BLEClientWriteAction : public Action, publ void set_char_uuid128(uint8_t *uuid) { this->char_uuid_ = espbt::ESPBTUUID::from_raw(uuid); } void set_value_template(std::vector (*func)(Ts...)) { - this->value_.template_func = func; - this->has_simple_value_ = false; + this->value_.func = func; + this->len_ = -1; // Sentinel value indicates template mode } // Store pointer to static data in flash (no RAM copy) void set_value_simple(const uint8_t *data, size_t len) { - this->value_.simple_data.ptr = data; - this->value_.simple_data.len = len; - this->has_simple_value_ = true; + this->value_.data = data; + this->len_ = len; // Length >= 0 indicates static mode } void play(const Ts &...x) override {} @@ -124,10 +123,12 @@ template class BLEClientWriteAction : public Action, publ this->num_running_++; this->var_ = std::make_tuple(x...); std::vector value; - if (this->has_simple_value_) { - value.assign(this->value_.simple_data.ptr, this->value_.simple_data.ptr + this->value_.simple_data.len); + if (this->len_ >= 0) { + // Static mode: copy from flash to vector + value.assign(this->value_.data, this->value_.data + this->len_); } else { - value = this->value_.template_func(x...); + // Template mode: call function + value = this->value_.func(x...); } // on write failure, continue the automation chain rather than stopping so that e.g. disconnect can work. if (!write(value)) @@ -202,13 +203,10 @@ template class BLEClientWriteAction : public Action, publ private: BLEClient *ble_client_; - bool has_simple_value_{true}; + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length union Value { - std::vector (*template_func)(Ts...); - struct { - const uint8_t *ptr; - size_t len; - } simple_data; + std::vector (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash } value_; espbt::ESPBTUUID service_uuid_; espbt::ESPBTUUID char_uuid_; diff --git a/esphome/components/canbus/canbus.h b/esphome/components/canbus/canbus.h index 122ccfe39e..f7b84111bd 100644 --- a/esphome/components/canbus/canbus.h +++ b/esphome/components/canbus/canbus.h @@ -115,14 +115,13 @@ template class CanbusSendAction : public Action, public P void set_data_template(std::vector (*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 class CanbusSendAction : public Action, public P auto use_extended_id = this->use_extended_id_.has_value() ? *this->use_extended_id_ : this->parent_->use_extended_id_; std::vector 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 class CanbusSendAction : public Action, public P optional can_id_{}; optional 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 (*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 (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash + } data_; }; class CanbusTrigger : public Trigger, uint32_t, bool>, public Component { diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index 9b671e611f..941b6aab42 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -42,17 +42,20 @@ class RawTrigger : public Trigger, public Component, public RemoteRe template class RawAction : public RemoteTransmitterActionBase { public: - void set_code_template(std::function func) { this->code_func_ = func; } + void set_code_template(RawTimings (*func)(Ts...)) { + this->code_.func = func; + this->len_ = -1; // Sentinel value indicates template mode + } void set_code_static(const int32_t *code, size_t len) { - this->code_static_ = code; - this->code_static_len_ = len; + this->code_.data = code; + this->len_ = len; // Length >= 0 indicates static mode } TEMPLATABLE_VALUE(uint32_t, carrier_frequency); void encode(RemoteTransmitData *dst, Ts... x) override { - if (this->code_static_ != nullptr) { - for (size_t i = 0; i < this->code_static_len_; i++) { - auto val = this->code_static_[i]; + if (this->len_ >= 0) { + for (size_t i = 0; i < static_cast(this->len_); i++) { + auto val = this->code_.data[i]; if (val < 0) { dst->space(static_cast(-val)); } else { @@ -60,15 +63,17 @@ template class RawAction : public RemoteTransmitterActionBaseset_data(this->code_func_(x...)); + dst->set_data(this->code_.func(x...)); } dst->set_carrier_frequency(this->carrier_frequency_.value(x...)); } protected: - std::function code_func_{nullptr}; - const int32_t *code_static_{nullptr}; - int32_t code_static_len_{0}; + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length + union Code { + RawTimings (*func)(Ts...); + const int32_t *data; + } code_; }; class RawDumper : public RemoteReceiverDumperBase { diff --git a/esphome/components/sx126x/automation.h b/esphome/components/sx126x/automation.h index 41f0a888e5..2282c583cb 100644 --- a/esphome/components/sx126x/automation.h +++ b/esphome/components/sx126x/automation.h @@ -16,33 +16,31 @@ template class SendPacketAction : public Action, public P public: void set_data_template(std::vector (*func)(Ts...)) { this->data_.func = func; - this->static_ = false; + this->len_ = -1; // Sentinel value indicates template mode } 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 play(const Ts &...x) override { std::vector 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_->transmit_packet(data); } protected: - bool static_{true}; + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length union Data { - std::vector (*func)(Ts...); - struct { - const uint8_t *ptr; - size_t len; - } static_data; + std::vector (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash } data_; }; diff --git a/esphome/components/sx127x/automation.h b/esphome/components/sx127x/automation.h index 52dbf37e09..fb0367fcca 100644 --- a/esphome/components/sx127x/automation.h +++ b/esphome/components/sx127x/automation.h @@ -16,33 +16,31 @@ template class SendPacketAction : public Action, public P public: void set_data_template(std::vector (*func)(Ts...)) { this->data_.func = func; - this->static_ = false; + this->len_ = -1; // Sentinel value indicates template mode } 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 play(const Ts &...x) override { std::vector 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_->transmit_packet(data); } protected: - bool static_{true}; + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length union Data { - std::vector (*func)(Ts...); - struct { - const uint8_t *ptr; - size_t len; - } static_data; + std::vector (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash } data_; }; diff --git a/esphome/components/uart/automation.h b/esphome/components/uart/automation.h index 7a3344c2f1..c2eb308eb8 100644 --- a/esphome/components/uart/automation.h +++ b/esphome/components/uart/automation.h @@ -12,36 +12,33 @@ template class UARTWriteAction : public Action, public Pa public: void set_data_template(std::vector (*func)(Ts...)) { // Stateless lambdas (generated by ESPHome) implicitly convert to function pointers - this->data_.func = func; - this->static_ = false; + this->code_.func = func; + 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) { - // Simply set pointer and length - no construction needed for POD types - this->data_.static_data.ptr = data; - this->data_.static_data.len = len; - this->static_ = true; + this->code_.data = data; + this->len_ = len; // Length >= 0 indicates static mode } void play(const Ts &...x) override { - if (this->static_) { - this->parent_->write_array(this->data_.static_data.ptr, this->data_.static_data.len); + if (this->len_ >= 0) { + // Static mode: use pointer and length + this->parent_->write_array(this->code_.data, static_cast(this->len_)); } else { - auto val = this->data_.func(x...); + // Template mode: call function + auto val = this->code_.func(x...); this->parent_->write_array(val); } } protected: - bool static_{true}; // Default to static mode (most common case) - union Data { + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length + union Code { std::vector (*func)(Ts...); // Function pointer (stateless lambdas) - struct { - const uint8_t *ptr; - size_t len; - } static_data; - } data_; + const uint8_t *data; // Pointer to static data in flash + } code_; }; } // namespace uart diff --git a/esphome/components/udp/automation.h b/esphome/components/udp/automation.h index a3b76fb4ea..b66c2a9892 100644 --- a/esphome/components/udp/automation.h +++ b/esphome/components/udp/automation.h @@ -13,32 +13,30 @@ template class UDPWriteAction : public Action, public Par public: void set_data_template(std::vector (*func)(Ts...)) { this->data_.func = func; - this->static_ = false; + this->len_ = -1; // Sentinel value indicates template mode } 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 play(const Ts &...x) override { - if (this->static_) { - this->parent_->send_packet(this->data_.static_data.ptr, this->data_.static_data.len); + if (this->len_ >= 0) { + // Static mode: pass pointer directly to send_packet(const uint8_t *, size_t) + this->parent_->send_packet(this->data_.data, static_cast(this->len_)); } else { + // Template mode: call function and pass vector to send_packet(const std::vector &) auto val = this->data_.func(x...); this->parent_->send_packet(val); } } protected: - bool static_{true}; + ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length union Data { - std::vector (*func)(Ts...); - struct { - const uint8_t *ptr; - size_t len; - } static_data; + std::vector (*func)(Ts...); // Function pointer (stateless lambdas) + const uint8_t *data; // Pointer to static data in flash } data_; }; diff --git a/tests/components/remote_transmitter/common-buttons.yaml b/tests/components/remote_transmitter/common-buttons.yaml index 101d60a893..d48d36bd54 100644 --- a/tests/components/remote_transmitter/common-buttons.yaml +++ b/tests/components/remote_transmitter/common-buttons.yaml @@ -136,10 +136,16 @@ button: address: 0x00 command: 0x0B - platform: template - name: RC5 Raw + name: RC5 Raw static on_press: remote_transmitter.transmit_raw: code: [1000, -1000] + - platform: template + name: RC5 Raw lambda + on_press: + remote_transmitter.transmit_raw: + code: !lambda |- + return {(int32_t)id(test_number).state * 100, -1000}; - platform: template name: AEHA id: eaha_hitachi_climate_power_on