[midea] Reduce heap allocations with stack-based string formatting (#13000)

This commit is contained in:
J. Nick Koston
2026-01-05 17:15:50 -10:00
committed by GitHub
parent 0290ed5d23
commit 2d4cd4ce7e
3 changed files with 13 additions and 2 deletions

View File

@@ -165,7 +165,8 @@ bool MideaIR::on_receive(remote_base::RemoteReceiveData data) {
}
bool MideaIR::on_midea_(const MideaData &data) {
ESP_LOGV(TAG, "Decoded Midea IR data: %s", data.to_string().c_str());
char buf[MideaData::TO_STR_BUFFER_SIZE];
ESP_LOGV(TAG, "Decoded Midea IR data: %s", data.to_str(buf));
if (data.type() == MideaData::MIDEA_TYPE_CONTROL) {
const ControlData status = data;
if (status.get_mode() != climate::CLIMATE_MODE_FAN_ONLY)

View File

@@ -70,7 +70,10 @@ optional<MideaData> MideaProtocol::decode(RemoteReceiveData src) {
return {};
}
void MideaProtocol::dump(const MideaData &data) { ESP_LOGI(TAG, "Received Midea: %s", data.to_string().c_str()); }
void MideaProtocol::dump(const MideaData &data) {
char buf[MideaData::TO_STR_BUFFER_SIZE];
ESP_LOGI(TAG, "Received Midea: %s", data.to_str(buf));
}
} // namespace remote_base
} // namespace esphome

View File

@@ -30,6 +30,13 @@ class MideaData {
void finalize() { this->data_[OFFSET_CS] = this->calc_cs_(); }
bool is_compliment(const MideaData &rhs) const;
std::string to_string() const { return format_hex_pretty(this->data_.data(), this->data_.size()); }
/// Buffer size for to_str(): 6 bytes = "AA.BB.CC.DD.EE.FF\0"
static constexpr size_t TO_STR_BUFFER_SIZE = format_hex_pretty_size(6);
/// Format to buffer, returns pointer to buffer
const char *to_str(char *buffer) const {
format_hex_pretty_to(buffer, TO_STR_BUFFER_SIZE, this->data_.data(), this->data_.size(), '.');
return buffer;
}
// compare only 40-bits
bool operator==(const MideaData &rhs) const {
return std::equal(this->data_.begin(), this->data_.begin() + OFFSET_CS, rhs.data_.begin());