mirror of
https://github.com/esphome/esphome.git
synced 2026-01-10 12:10:48 -07:00
[midea] Reduce heap allocations with stack-based string formatting (#13000)
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user