Compare commits

..

4 Commits

Author SHA1 Message Date
pre-commit-ci-lite[bot]
21507c570d [pre-commit.ci lite] apply automatic fixes 2026-01-15 02:29:46 +00:00
J. Nick Koston
5b6be2c8d9 Update esphome/components/modbus_controller/text_sensor/modbus_textsensor.cpp
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-14 16:28:26 -10:00
J. Nick Koston
66e80fe13b Update esphome/components/modbus_controller/modbus_controller.h
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-14 16:28:16 -10:00
J. Nick Koston
a50654ef4d [modbus_controller] Use stack buffers instead of str_sprintf/str_snprintf 2026-01-14 15:13:54 -10:00
5 changed files with 24 additions and 24 deletions

View File

@@ -413,7 +413,6 @@ class TextValidator(LValidator):
str_args = [str(x) for x in value[CONF_ARGS]]
arg_expr = cg.RawExpression(",".join(str_args))
format_str = cpp_string_escape(format_str)
# str_sprintf justified: user-defined format, can't optimize without permanent RAM cost
sprintf_str = f"str_sprintf({format_str}, {arg_expr}).c_str()"
if nanval := value.get(CONF_IF_NAN):
nanval = cpp_string_escape(nanval)

View File

@@ -65,10 +65,7 @@ std::string lv_event_code_name_for(uint8_t event_code) {
if (event_code < sizeof(EVENT_NAMES) / sizeof(EVENT_NAMES[0])) {
return EVENT_NAMES[event_code];
}
// max 4 bytes: "%u" with uint8_t (max 255, 3 digits) + null
char buf[4];
snprintf(buf, sizeof(buf), "%u", event_code);
return buf;
return str_sprintf("%2d", event_code);
}
static void rounder_cb(lv_disp_drv_t *disp_drv, lv_area_t *area) {

View File

@@ -285,8 +285,13 @@ class ServerRegister {
case SensorValueType::S_QWORD_R:
return std::to_string(value);
case SensorValueType::FP32_R:
case SensorValueType::FP32:
return str_sprintf("%.1f", bit_cast<float>(static_cast<uint32_t>(value)));
case SensorValueType::FP32: {
// max 48: float with %.1f can be up to 42 chars incl. null (3.4e38 → 38 integer digits + decimal point + 1
// decimal digit + optional sign)
char buf[48];
snprintf(buf, sizeof(buf), "%.1f", bit_cast<float>(static_cast<uint32_t>(value)));
return buf;
}
default:
return std::to_string(value);
}

View File

@@ -16,12 +16,20 @@ void ModbusTextSensor::parse_and_publish(const std::vector<uint8_t> &data) {
while ((items_left > 0) && index < data.size()) {
uint8_t b = data[index];
switch (this->encode_) {
case RawEncoding::HEXBYTES:
output_str += str_snprintf("%02x", 2, b);
case RawEncoding::HEXBYTES: {
// max 3: 2 hex digits + null
char hex_buf[3];
snprintf(hex_buf, sizeof(hex_buf), "%02x", b);
output_str += hex_buf;
break;
case RawEncoding::COMMA:
output_str += str_sprintf(index != this->offset ? ",%d" : "%d", b);
}
case RawEncoding::COMMA: {
// max 5: optional ','(1) + uint8(3) + null, for both ",%d" and "%d"
char dec_buf[5];
snprintf(dec_buf, sizeof(dec_buf), index != this->offset ? ",%d" : "%d", b);
output_str += dec_buf;
break;
}
case RawEncoding::ANSI:
if (b < 0x20)
break;

View File

@@ -114,23 +114,14 @@ void StatsdComponent::update() {
// This implies you can't explicitly set a gauge to a negative number without first setting it to zero.
if (val < 0) {
if (this->prefix_) {
out.append(this->prefix_);
out.append(".");
out.append(str_sprintf("%s.", this->prefix_));
}
out.append(s.name);
out.append(":0|g\n");
out.append(str_sprintf("%s:0|g\n", s.name));
}
if (this->prefix_) {
out.append(this->prefix_);
out.append(".");
out.append(str_sprintf("%s.", this->prefix_));
}
out.append(s.name);
// Buffer for ":" + value + "|g\n".
// %g uses max 13 chars for value (sign + 6 significant digits + e+xxx)
// Total: 1 + 13 + 4 = 18 chars + null, use 24 for safety
char val_buf[24];
snprintf(val_buf, sizeof(val_buf), ":%g|g\n", val);
out.append(val_buf);
out.append(str_sprintf("%s:%f|g\n", s.name, val));
if (out.length() > SEND_THRESHOLD) {
this->send_(&out);