diff --git a/esphome/components/am43/am43_base.cpp b/esphome/components/am43/am43_base.cpp index af474dcb79..d70e638382 100644 --- a/esphome/components/am43/am43_base.cpp +++ b/esphome/components/am43/am43_base.cpp @@ -1,21 +1,12 @@ #include "am43_base.h" +#include "esphome/core/helpers.h" #include -#include namespace esphome { namespace am43 { const uint8_t START_PACKET[5] = {0x00, 0xff, 0x00, 0x00, 0x9a}; -std::string pkt_to_hex(const uint8_t *data, uint16_t len) { - char buf[64]; - memset(buf, 0, 64); - for (int i = 0; i < len; i++) - sprintf(&buf[i * 2], "%02x", data[i]); - std::string ret = buf; - return ret; -} - Am43Packet *Am43Encoder::get_battery_level_request() { uint8_t data = 0x1; return this->encode_(0xA2, &data, 1); @@ -73,7 +64,9 @@ Am43Packet *Am43Encoder::encode_(uint8_t command, uint8_t *data, uint8_t length) memcpy(&this->packet_.data[7], data, length); this->packet_.length = length + 7; this->checksum_(); - ESP_LOGV("am43", "ENC(%d): 0x%s", packet_.length, pkt_to_hex(packet_.data, packet_.length).c_str()); + char hex_buf[format_hex_size(sizeof(this->packet_.data))]; + ESP_LOGV("am43", "ENC(%d): 0x%s", this->packet_.length, + format_hex_to(hex_buf, this->packet_.data, this->packet_.length)); return &this->packet_; } @@ -88,7 +81,8 @@ void Am43Decoder::decode(const uint8_t *data, uint16_t length) { this->has_set_state_response_ = false; this->has_position_ = false; this->has_pin_response_ = false; - ESP_LOGV("am43", "DEC(%d): 0x%s", length, pkt_to_hex(data, length).c_str()); + char hex_buf[format_hex_size(24)]; // Max expected packet size + ESP_LOGV("am43", "DEC(%d): 0x%s", length, format_hex_to(hex_buf, data, length)); if (length < 2 || data[0] != 0x9a) return; diff --git a/esphome/components/lightwaverf/lightwaverf.cpp b/esphome/components/lightwaverf/lightwaverf.cpp index 31ac1fc576..2b44195c97 100644 --- a/esphome/components/lightwaverf/lightwaverf.cpp +++ b/esphome/components/lightwaverf/lightwaverf.cpp @@ -1,3 +1,4 @@ +#include "esphome/core/helpers.h" #include "esphome/core/log.h" #ifdef USE_ESP8266 @@ -44,13 +45,16 @@ void LightWaveRF::send_rx(const std::vector &msg, uint8_t repeats, bool } void LightWaveRF::print_msg_(uint8_t *msg, uint8_t len) { - char buffer[65]; +#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG + char buffer[65]; // max 10 entries * 6 chars + null ESP_LOGD(TAG, " Received code (len:%i): ", len); + size_t pos = 0; for (int i = 0; i < len; i++) { - sprintf(&buffer[i * 6], "0x%02x, ", msg[i]); + pos = buf_append_printf(buffer, sizeof(buffer), pos, "0x%02x, ", msg[i]); } ESP_LOGD(TAG, "[%s]", buffer); +#endif } void LightWaveRF::dump_config() { diff --git a/esphome/components/rf_bridge/rf_bridge.cpp b/esphome/components/rf_bridge/rf_bridge.cpp index 52ce037dbe..8105767485 100644 --- a/esphome/components/rf_bridge/rf_bridge.cpp +++ b/esphome/components/rf_bridge/rf_bridge.cpp @@ -1,6 +1,7 @@ #include "rf_bridge.h" -#include "esphome/core/log.h" #include "esphome/core/application.h" +#include "esphome/core/helpers.h" +#include "esphome/core/log.h" #include #include @@ -72,9 +73,9 @@ bool RFBridgeComponent::parse_bridge_byte_(uint8_t byte) { data.length = raw[2]; data.protocol = raw[3]; - char next_byte[3]; + char next_byte[3]; // 2 hex chars + null for (uint8_t i = 0; i < data.length - 1; i++) { - sprintf(next_byte, "%02X", raw[4 + i]); + buf_append_printf(next_byte, sizeof(next_byte), 0, "%02X", raw[4 + i]); data.code += next_byte; } @@ -90,10 +91,10 @@ bool RFBridgeComponent::parse_bridge_byte_(uint8_t byte) { uint8_t buckets = raw[2] << 1; std::string str; - char next_byte[3]; + char next_byte[3]; // 2 hex chars + null for (uint32_t i = 0; i <= at; i++) { - sprintf(next_byte, "%02X", raw[i]); + buf_append_printf(next_byte, sizeof(next_byte), 0, "%02X", raw[i]); str += next_byte; if ((i > 3) && buckets) { buckets--; diff --git a/esphome/components/spi_led_strip/spi_led_strip.cpp b/esphome/components/spi_led_strip/spi_led_strip.cpp index afb51afe3a..ff8d2e6ee0 100644 --- a/esphome/components/spi_led_strip/spi_led_strip.cpp +++ b/esphome/components/spi_led_strip/spi_led_strip.cpp @@ -1,4 +1,5 @@ #include "spi_led_strip.h" +#include "esphome/core/helpers.h" namespace esphome { namespace spi_led_strip { @@ -47,15 +48,14 @@ void SpiLedStrip::dump_config() { void SpiLedStrip::write_state(light::LightState *state) { if (this->is_failed()) return; - if (ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE) { - char strbuf[49]; - size_t len = std::min(this->buffer_size_, (size_t) (sizeof(strbuf) - 1) / 3); - memset(strbuf, 0, sizeof(strbuf)); - for (size_t i = 0; i != len; i++) { - sprintf(strbuf + i * 3, "%02X ", this->buf_[i]); - } +#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE + { + char strbuf[49]; // format_hex_pretty_size(16) = 48, fits 16 bytes + size_t len = std::min(this->buffer_size_, (size_t) 16); + format_hex_pretty_to(strbuf, sizeof(strbuf), this->buf_, len, ' '); esph_log_v(TAG, "write_state: buf = %s", strbuf); } +#endif this->enable(); this->write_array(this->buf_, this->buffer_size_); this->disable();