[remote_base] Optimize raw transmit action memory usage - use function pointers

This commit is contained in:
J. Nick Koston
2025-11-08 23:07:43 -06:00
parent b61027607f
commit 5b8827d47a
2 changed files with 34 additions and 11 deletions
+19 -10
View File
@@ -42,17 +42,21 @@ class RawTrigger : public Trigger<RawTimings>, public Component, public RemoteRe
template<typename... Ts> class RawAction : public RemoteTransmitterActionBase<Ts...> {
public:
void set_code_template(std::function<RawTimings(Ts...)> 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<uint32_t>(-val));
} else {
@@ -60,15 +64,20 @@ template<typename... Ts> class RawAction : public RemoteTransmitterActionBase<Ts
}
}
} else {
dst->set_data(this->code_func_(x...));
dst->set_data(this->code_.func(x...));
}
dst->set_carrier_frequency(this->carrier_frequency_.value(x...));
}
protected:
std::function<RawTimings(Ts...)> 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 {
@@ -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