From 5b8827d47acec9732a191d5b2f900610ad7fcf92 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:07:43 -0600 Subject: [PATCH 1/4] [remote_base] Optimize raw transmit action memory usage - use function pointers --- esphome/components/remote_base/raw_protocol.h | 29 ++++++++++++------- .../remote_transmitter/common-buttons.yaml | 16 +++++++++- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index 9b671e611f4..4f50a859088 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -42,17 +42,21 @@ 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->static_ = false; + } void set_code_static(const int32_t *code, size_t len) { - this->code_static_ = code; - this->code_static_len_ = len; + this->code_.static_code.data = code; + this->code_.static_code.len = len; + this->static_ = true; } 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->static_) { + for (size_t i = 0; i < this->code_.static_code.len; i++) { + auto val = this->code_.static_code.data[i]; if (val < 0) { dst->space(static_cast(-val)); } else { @@ -60,15 +64,20 @@ 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}; + bool static_{true}; + union Code { + RawTimings (*func)(Ts...); + struct { + const int32_t *data; + size_t len; + } static_code; + } code_; }; class RawDumper : public RemoteReceiverDumperBase { diff --git a/tests/components/remote_transmitter/common-buttons.yaml b/tests/components/remote_transmitter/common-buttons.yaml index e9593cc97ce..cab28d813bb 100644 --- a/tests/components/remote_transmitter/common-buttons.yaml +++ b/tests/components/remote_transmitter/common-buttons.yaml @@ -1,3 +1,11 @@ +number: + - platform: template + id: test_number + optimistic: true + min_value: 0 + max_value: 255 + step: 1 + button: - platform: template name: Beo4 audio mute @@ -128,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 From 353ea5674dd2243f4e0bfae2958c2da14d551665 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:09:31 -0600 Subject: [PATCH 2/4] Add additional tests for remote_transmitter raw --- .../remote_transmitter/common-buttons.yaml | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/components/remote_transmitter/common-buttons.yaml b/tests/components/remote_transmitter/common-buttons.yaml index e9593cc97ce..cab28d813bb 100644 --- a/tests/components/remote_transmitter/common-buttons.yaml +++ b/tests/components/remote_transmitter/common-buttons.yaml @@ -1,3 +1,11 @@ +number: + - platform: template + id: test_number + optimistic: true + min_value: 0 + max_value: 255 + step: 1 + button: - platform: template name: Beo4 audio mute @@ -128,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 From 59485c1d2b9090dfe2e30ff41e8111ad0466a6e8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:14:57 -0600 Subject: [PATCH 3/4] save 4 bytes --- esphome/components/remote_base/raw_protocol.h | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index 4f50a859088..f59431c88ad 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -44,19 +44,18 @@ template class RawAction : public RemoteTransmitterActionBasecode_.func = func; - this->static_ = false; + this->len_ = -1; } void set_code_static(const int32_t *code, size_t len) { - this->code_.static_code.data = code; - this->code_.static_code.len = len; - this->static_ = true; + this->code_.data = code; + this->len_ = len; } TEMPLATABLE_VALUE(uint32_t, carrier_frequency); void encode(RemoteTransmitData *dst, Ts... x) override { - if (this->static_) { - for (size_t i = 0; i < this->code_.static_code.len; i++) { - auto val = this->code_.static_code.data[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 { @@ -70,13 +69,10 @@ template class RawAction : public RemoteTransmitterActionBase Date: Sat, 8 Nov 2025 23:21:57 -0600 Subject: [PATCH 4/4] tweak --- esphome/components/remote_base/raw_protocol.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/components/remote_base/raw_protocol.h b/esphome/components/remote_base/raw_protocol.h index f59431c88ad..941b6aab426 100644 --- a/esphome/components/remote_base/raw_protocol.h +++ b/esphome/components/remote_base/raw_protocol.h @@ -44,11 +44,11 @@ template class RawAction : public RemoteTransmitterActionBasecode_.func = func; - this->len_ = -1; + this->len_ = -1; // Sentinel value indicates template mode } void set_code_static(const int32_t *code, size_t len) { this->code_.data = code; - this->len_ = len; + this->len_ = len; // Length >= 0 indicates static mode } TEMPLATABLE_VALUE(uint32_t, carrier_frequency); @@ -69,7 +69,7 @@ template class RawAction : public RemoteTransmitterActionBase=0 = static mode with length union Code { RawTimings (*func)(Ts...); const int32_t *data;