mirror of
https://github.com/esphome/esphome.git
synced 2026-02-26 14:03:14 -07:00
Merge branch 'web_server_stack_format' into integration
This commit is contained in:
@@ -436,7 +436,10 @@ static void set_json_icon_state_value(JsonObject &root, EntityBase *obj, const c
|
||||
root[ESPHOME_F("state")] = state;
|
||||
}
|
||||
|
||||
// Macros for stack-based value formatting (avoid heap allocation)
|
||||
// Macros for stack-based value formatting (avoid heap allocation).
|
||||
// Usage: Declare VALUE_BUF once per scope, then use VALUE_OR_NA/VALUE_UOM_OR_NA.
|
||||
// Safe because ArduinoJson copies the string immediately on assignment.
|
||||
// Note: Do NOT use multiple macros in the same expression - use separate statements.
|
||||
#define VALUE_BUF char _vbuf_[VALUE_ACCURACY_MAX_LEN]
|
||||
#define VALUE_OR_NA(value, decimals) \
|
||||
(std::isnan(value) ? "NA" : (value_accuracy_to_buf(_vbuf_, value, decimals), _vbuf_))
|
||||
|
||||
@@ -392,8 +392,12 @@ std::string value_accuracy_to_string(float value, int8_t accuracy_decimals) {
|
||||
|
||||
size_t value_accuracy_to_buf(std::span<char, VALUE_ACCURACY_MAX_LEN> buf, float value, int8_t accuracy_decimals) {
|
||||
normalize_accuracy_decimals(value, accuracy_decimals);
|
||||
// snprintf returns chars that would be written (excluding null), or negative on error
|
||||
int len = snprintf(buf.data(), buf.size(), "%.*f", accuracy_decimals, value);
|
||||
return len > 0 ? std::min(static_cast<size_t>(len), buf.size() - 1) : 0;
|
||||
if (len < 0)
|
||||
return 0; // encoding error
|
||||
// On truncation, snprintf returns would-be length; actual written is buf.size() - 1
|
||||
return static_cast<size_t>(len) >= buf.size() ? buf.size() - 1 : static_cast<size_t>(len);
|
||||
}
|
||||
|
||||
size_t value_accuracy_with_uom_to_buf(std::span<char, VALUE_ACCURACY_MAX_LEN> buf, float value,
|
||||
@@ -402,8 +406,12 @@ size_t value_accuracy_with_uom_to_buf(std::span<char, VALUE_ACCURACY_MAX_LEN> bu
|
||||
return value_accuracy_to_buf(buf, value, accuracy_decimals);
|
||||
}
|
||||
normalize_accuracy_decimals(value, accuracy_decimals);
|
||||
// snprintf returns chars that would be written (excluding null), or negative on error
|
||||
int len = snprintf(buf.data(), buf.size(), "%.*f %s", accuracy_decimals, value, unit_of_measurement.c_str());
|
||||
return len > 0 ? std::min(static_cast<size_t>(len), buf.size() - 1) : 0;
|
||||
if (len < 0)
|
||||
return 0; // encoding error
|
||||
// On truncation, snprintf returns would-be length; actual written is buf.size() - 1
|
||||
return static_cast<size_t>(len) >= buf.size() ? buf.size() - 1 : static_cast<size_t>(len);
|
||||
}
|
||||
|
||||
int8_t step_to_accuracy_decimals(float step) {
|
||||
|
||||
Reference in New Issue
Block a user