[modbus_controller] Replace format_hex_pretty with stack-based format_hex_pretty_to

This commit is contained in:
J. Nick Koston
2025-12-31 16:38:58 -10:00
parent 3c9ed126a6
commit 73b19bc5d1
3 changed files with 26 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
#include <vector>
#include "modbus_number.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -7,6 +8,9 @@ namespace modbus_controller {
static const char *const TAG = "modbus.number";
// Maximum bytes to log in verbose hex output (64 bytes = 32 uint16_t registers)
static constexpr size_t MODBUS_NUMBER_MAX_LOG_BYTES = 64;
void ModbusNumber::parse_and_publish(const std::vector<uint8_t> &data) {
float result = payload_to_float(data, *this) / this->multiply_by_;
@@ -47,7 +51,11 @@ void ModbusNumber::control(float value) {
}
if (!data.empty()) {
ESP_LOGV(TAG, "Modbus Number write raw: %s", format_hex_pretty(data).c_str());
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char hex_buf[format_hex_pretty_size(MODBUS_NUMBER_MAX_LOG_BYTES)];
#endif
ESP_LOGV(TAG, "Modbus Number write raw: %s",
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
write_cmd = ModbusCommandItem::create_custom_command(
this->parent_, data,
[this, write_cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {

View File

@@ -7,6 +7,9 @@ namespace modbus_controller {
static const char *const TAG = "modbus_controller.output";
// Maximum bytes to log in verbose hex output
static constexpr size_t MODBUS_OUTPUT_MAX_LOG_BYTES = 64;
/** Write a value to the device
*
*/
@@ -80,7 +83,11 @@ void ModbusBinaryOutput::write_state(bool state) {
}
}
if (!data.empty()) {
ESP_LOGV(TAG, "Modbus binary output write raw: %s", format_hex_pretty(data).c_str());
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char hex_buf[format_hex_pretty_size(MODBUS_OUTPUT_MAX_LOG_BYTES)];
#endif
ESP_LOGV(TAG, "Modbus binary output write raw: %s",
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
cmd = ModbusCommandItem::create_custom_command(
this->parent_, data,
[this, cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {

View File

@@ -1,11 +1,15 @@
#include "modbus_switch.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
namespace modbus_controller {
static const char *const TAG = "modbus_controller.switch";
// Maximum bytes to log in verbose hex output
static constexpr size_t MODBUS_SWITCH_MAX_LOG_BYTES = 64;
void ModbusSwitch::setup() {
optional<bool> initial_state = Switch::get_initial_state_with_restore_mode();
if (initial_state.has_value()) {
@@ -71,7 +75,11 @@ void ModbusSwitch::write_state(bool state) {
}
}
if (!data.empty()) {
ESP_LOGV(TAG, "Modbus Switch write raw: %s", format_hex_pretty(data).c_str());
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
char hex_buf[format_hex_pretty_size(MODBUS_SWITCH_MAX_LOG_BYTES)];
#endif
ESP_LOGV(TAG, "Modbus Switch write raw: %s",
format_hex_pretty_to(hex_buf, sizeof(hex_buf), data.data(), data.size()));
cmd = ModbusCommandItem::create_custom_command(
this->parent_, data,
[this, cmd](ModbusRegisterType register_type, uint16_t start_address, const std::vector<uint8_t> &data) {