From 729304af01898a319f643417b257ad049409d1d1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 8 Nov 2025 23:28:23 -0600 Subject: [PATCH] optimize --- esphome/components/ble_client/automation.h | 26 ++++++++++------------ 1 file changed, 12 insertions(+), 14 deletions(-) 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_;