mirror of
https://github.com/esphome/esphome.git
synced 2026-01-09 19:50:49 -07:00
[pvvx_mithermometer] Reduce heap allocations with stack-based string formatting (#12994)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include "pvvx_display.h"
|
#include "pvvx_display.h"
|
||||||
|
#include "esphome/components/esp32_ble/ble_uuid.h"
|
||||||
#include "esphome/core/log.h"
|
#include "esphome/core/log.h"
|
||||||
|
|
||||||
#ifdef USE_ESP32
|
#ifdef USE_ESP32
|
||||||
@@ -8,14 +9,16 @@ namespace pvvx_mithermometer {
|
|||||||
static const char *const TAG = "display.pvvx_mithermometer";
|
static const char *const TAG = "display.pvvx_mithermometer";
|
||||||
|
|
||||||
void PVVXDisplay::dump_config() {
|
void PVVXDisplay::dump_config() {
|
||||||
|
char service_buf[esp32_ble::UUID_STR_LEN];
|
||||||
|
char char_buf[esp32_ble::UUID_STR_LEN];
|
||||||
ESP_LOGCONFIG(TAG,
|
ESP_LOGCONFIG(TAG,
|
||||||
"PVVX MiThermometer display:\n"
|
"PVVX MiThermometer display:\n"
|
||||||
" MAC address : %s\n"
|
" MAC address : %s\n"
|
||||||
" Service UUID : %s\n"
|
" Service UUID : %s\n"
|
||||||
" Characteristic UUID : %s\n"
|
" Characteristic UUID : %s\n"
|
||||||
" Auto clear : %s",
|
" Auto clear : %s",
|
||||||
this->parent_->address_str(), this->service_uuid_.to_string().c_str(),
|
this->parent_->address_str(), this->service_uuid_.to_str(service_buf),
|
||||||
this->char_uuid_.to_string().c_str(), YESNO(this->auto_clear_enabled_));
|
this->char_uuid_.to_str(char_buf), YESNO(this->auto_clear_enabled_));
|
||||||
#ifdef USE_TIME
|
#ifdef USE_TIME
|
||||||
ESP_LOGCONFIG(TAG, " Set time on connection: %s", YESNO(this->time_ != nullptr));
|
ESP_LOGCONFIG(TAG, " Set time on connection: %s", YESNO(this->time_ != nullptr));
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ bool PVVXMiThermometer::parse_device(const esp32_ble_tracker::ESPBTDevice &devic
|
|||||||
ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
|
ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", device.address_str().c_str());
|
char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
|
||||||
|
const char *addr_str = device.address_str_to(addr_buf);
|
||||||
|
ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_str);
|
||||||
|
|
||||||
bool success = false;
|
bool success = false;
|
||||||
for (auto &service_data : device.get_service_datas()) {
|
for (auto &service_data : device.get_service_datas()) {
|
||||||
@@ -32,7 +34,7 @@ bool PVVXMiThermometer::parse_device(const esp32_ble_tracker::ESPBTDevice &devic
|
|||||||
if (!(parse_message_(service_data.data, *res))) {
|
if (!(parse_message_(service_data.data, *res))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!(report_results_(res, device.address_str()))) {
|
if (!(report_results_(res, addr_str))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (res->temperature.has_value() && this->temperature_ != nullptr)
|
if (res->temperature.has_value() && this->temperature_ != nullptr)
|
||||||
@@ -111,13 +113,13 @@ bool PVVXMiThermometer::parse_message_(const std::vector<uint8_t> &message, Pars
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PVVXMiThermometer::report_results_(const optional<ParseResult> &result, const std::string &address) {
|
bool PVVXMiThermometer::report_results_(const optional<ParseResult> &result, const char *address) {
|
||||||
if (!result.has_value()) {
|
if (!result.has_value()) {
|
||||||
ESP_LOGVV(TAG, "report_results(): no results available.");
|
ESP_LOGVV(TAG, "report_results(): no results available.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ESP_LOGD(TAG, "Got PVVX MiThermometer (%s):", address.c_str());
|
ESP_LOGD(TAG, "Got PVVX MiThermometer (%s):", address);
|
||||||
|
|
||||||
if (result->temperature.has_value()) {
|
if (result->temperature.has_value()) {
|
||||||
ESP_LOGD(TAG, " Temperature: %.2f °C", *result->temperature);
|
ESP_LOGD(TAG, " Temperature: %.2f °C", *result->temperature);
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ class PVVXMiThermometer : public Component, public esp32_ble_tracker::ESPBTDevic
|
|||||||
|
|
||||||
optional<ParseResult> parse_header_(const esp32_ble_tracker::ServiceData &service_data);
|
optional<ParseResult> parse_header_(const esp32_ble_tracker::ServiceData &service_data);
|
||||||
bool parse_message_(const std::vector<uint8_t> &message, ParseResult &result);
|
bool parse_message_(const std::vector<uint8_t> &message, ParseResult &result);
|
||||||
bool report_results_(const optional<ParseResult> &result, const std::string &address);
|
bool report_results_(const optional<ParseResult> &result, const char *address);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace pvvx_mithermometer
|
} // namespace pvvx_mithermometer
|
||||||
|
|||||||
Reference in New Issue
Block a user