23static const char *
const TAG =
"helpers";
25static const uint16_t CRC16_A001_LE_LUT_L[] = {0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
26 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440};
27static const uint16_t CRC16_A001_LE_LUT_H[] = {0x0000, 0xcc01, 0xd801, 0x1400, 0xf001, 0x3c00, 0x2800, 0xe401,
28 0xa001, 0x6c00, 0x7800, 0xb401, 0x5000, 0x9c01, 0x8801, 0x4400};
31static const uint16_t CRC16_8408_LE_LUT_L[] = {0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
32 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7};
33static const uint16_t CRC16_8408_LE_LUT_H[] = {0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387,
34 0x8408, 0x9489, 0xa50a, 0xb58b, 0xc60c, 0xd68d, 0xe70e, 0xf78f};
37#if !defined(USE_ESP32) || defined(USE_ESP32_VARIANT_ESP32S2)
38static const uint16_t CRC16_1021_BE_LUT_L[] = {0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
39 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef};
40static const uint16_t CRC16_1021_BE_LUT_H[] = {0x0000, 0x1231, 0x2462, 0x3653, 0x48c4, 0x5af5, 0x6ca6, 0x7e97,
41 0x9188, 0x83b9, 0xb5ea, 0xa7db, 0xd94c, 0xcb7d, 0xfd2e, 0xef1f};
46uint8_t
crc8(
const uint8_t *data, uint8_t
len, uint8_t crc, uint8_t poly,
bool msb_first) {
47 while ((
len--) != 0u) {
48 uint8_t inbyte = *data++;
52 for (uint8_t i = 8; i != 0u; i--) {
54 crc = (crc << 1) ^ poly;
61 for (uint8_t i = 8; i != 0u; i--) {
62 bool mix = (crc ^ inbyte) & 0x01;
73uint16_t
crc16(
const uint8_t *data, uint16_t
len, uint16_t crc, uint16_t reverse_poly,
bool refin,
bool refout) {
75 if (reverse_poly == 0x8408) {
76 crc = crc16_le(refin ? crc : (crc ^ 0xffff), data,
len);
77 return refout ? crc : (crc ^ 0xffff);
84 if (reverse_poly == 0x8408) {
86 uint8_t combo = crc ^ (uint8_t) *data++;
87 crc = (crc >> 8) ^ CRC16_8408_LE_LUT_L[combo & 0x0F] ^ CRC16_8408_LE_LUT_H[combo >> 4];
92 if (reverse_poly == 0xa001) {
94 uint8_t combo = crc ^ (uint8_t) *data++;
95 crc = (crc >> 8) ^ CRC16_A001_LE_LUT_L[combo & 0x0F] ^ CRC16_A001_LE_LUT_H[combo >> 4];
100 for (uint8_t i = 0; i < 8; i++) {
102 crc = (crc >> 1) ^ reverse_poly;
110 return refout ? (crc ^ 0xffff) : crc;
113uint16_t
crc16be(
const uint8_t *data, uint16_t
len, uint16_t crc, uint16_t poly,
bool refin,
bool refout) {
114#if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32S2)
115 if (poly == 0x1021) {
116 crc = crc16_be(refin ? crc : (crc ^ 0xffff), data,
len);
117 return refout ? crc : (crc ^ 0xffff);
123#if !defined(USE_ESP32) || defined(USE_ESP32_VARIANT_ESP32S2)
124 if (poly == 0x1021) {
126 uint8_t combo = (crc >> 8) ^ *data++;
127 crc = (crc << 8) ^ CRC16_1021_BE_LUT_L[combo & 0x0F] ^ CRC16_1021_BE_LUT_H[combo >> 4];
132 crc ^= (((uint16_t) *data++) << 8);
133 for (uint8_t i = 0; i < 8; i++) {
135 crc = (crc << 1) ^ poly;
141#if !defined(USE_ESP32) || defined(USE_ESP32_VARIANT_ESP32S2)
144 return refout ? (crc ^ 0xffff) : crc;
164 return strcasecmp(a.c_str(), b.c_str()) == 0;
167 return a.
size() == b.size() && strncasecmp(a.
c_str(), b.c_str(), a.
size()) == 0;
169#if __cplusplus >= 202002L
170bool str_startswith(
const std::string &str,
const std::string &start) {
return str.starts_with(start); }
173bool str_startswith(
const std::string &str,
const std::string &start) {
return str.rfind(start, 0) == 0; }
175 return str.rfind(
end) == (str.size() -
end.size());
180 if (suffix_len > str_len)
182 return strncasecmp(str + str_len - suffix_len, suffix, suffix_len) == 0;
186 return str.length() >
length ? str.substr(0,
length) : str;
189 const char *
pos = strchr(str, ch);
190 return pos ==
nullptr ? std::string(str) : std::string(str,
pos - str);
192std::string
str_until(
const std::string &str,
char ch) {
return str.substr(0, str.find(ch)); }
197 result.resize(str.length());
198 std::transform(str.begin(), str.end(), result.begin(), [](
unsigned char ch) { return fn(ch); });
204 std::string result = str;
205 for (
char &c : result) {
211 if (buffer_size == 0) {
215 while (*str && i < buffer_size - 1) {
224 result.resize(str.size());
234 size_t out_length = vsnprintf(&str[0],
len + 1,
fmt, args);
237 if (out_length <
len)
238 str.resize(out_length);
247 size_t length = vsnprintf(
nullptr, 0,
fmt, args);
252 vsnprintf(&str[0],
length + 1,
fmt, args);
259static constexpr size_t MAX_NAME_WITH_SUFFIX_SIZE = 128;
262 const char *suffix_ptr,
size_t suffix_len) {
263 size_t total_len = name_len + 1 + suffix_len;
266 if (total_len >= buffer_size) {
270 name_len = buffer_size - suffix_len - 2;
271 total_len = name_len + 1 + suffix_len;
274 memcpy(buffer, name, name_len);
275 buffer[name_len] = sep;
276 memcpy(buffer + name_len + 1, suffix_ptr, suffix_len);
277 buffer[total_len] =
'\0';
283 char buffer[MAX_NAME_WITH_SUFFIX_SIZE];
285 return std::string(buffer,
len);
295 size_t chars = std::min(
length, 2 * count);
296 for (
size_t i = 2 * count - chars; i < 2 * count; i++, str++) {
298 if (
val == INVALID_HEX_CHAR)
300 data[i >> 1] = (i & 1) ? data[i >> 1] |
val :
val << 4;
308 return std::string(buf);
312static char *format_hex_internal(
char *buffer,
size_t buffer_size,
const uint8_t *data,
size_t length,
char separator,
320 uint8_t stride = separator ? 3 : 2;
321 size_t max_bytes = separator ? (buffer_size / stride) : ((buffer_size - 1) / stride);
322 if (max_bytes == 0) {
329 for (
size_t i = 0; i <
length; i++) {
330 size_t pos = i * stride;
333 if (separator && i <
length - 1) {
334 buffer[
pos + 2] = separator;
337 buffer[
length * stride - (separator ? 1 : 0)] =
'\0';
342 return format_hex_internal(buffer, buffer_size, data,
length, 0,
'a');
354 return format_hex_internal(buffer, buffer_size, data,
length, separator,
'A');
358 if (
length == 0 || buffer_size == 0) {
365 uint8_t stride = separator ? 5 : 4;
366 size_t max_values = separator ? (buffer_size / stride) : ((buffer_size - 1) / stride);
367 if (max_values == 0) {
371 if (
length > max_values) {
374 for (
size_t i = 0; i <
length; i++) {
375 size_t pos = i * stride;
380 if (separator && i <
length - 1) {
381 buffer[
pos + 4] = separator;
384 buffer[
length * stride - (separator ? 1 : 0)] =
'\0';
389static std::string format_hex_pretty_uint8(
const uint8_t *data,
size_t length,
char separator,
bool show_length) {
390 if (data ==
nullptr ||
length == 0)
393 size_t hex_len = separator ? (
length * 3 - 1) : (
length * 2);
396 if (show_length &&
length > 4)
397 return ret +
" (" + std::to_string(
length) +
")";
402 return format_hex_pretty_uint8(data,
length, separator, show_length);
404std::string
format_hex_pretty(
const std::vector<uint8_t> &data,
char separator,
bool show_length) {
409 if (data ==
nullptr ||
length == 0)
412 size_t hex_len = separator ? (
length * 5 - 1) : (
length * 4);
415 if (show_length &&
length > 4)
416 return ret +
" (" + std::to_string(
length) +
")";
419std::string
format_hex_pretty(
const std::vector<uint16_t> &data,
char separator,
bool show_length) {
423 return format_hex_pretty_uint8(
reinterpret_cast<const uint8_t *
>(data.data()), data.length(), separator, show_length);
427 if (buffer_size == 0) {
431 size_t max_bytes = (buffer_size - 1) / 8;
432 if (max_bytes == 0 ||
length == 0) {
436 size_t bytes_to_format = std::min(
length, max_bytes);
438 for (
size_t byte_idx = 0; byte_idx < bytes_to_format; byte_idx++) {
439 for (
size_t bit_idx = 0; bit_idx < 8; bit_idx++) {
440 buffer[byte_idx * 8 + bit_idx] = ((data[byte_idx] >> (7 - bit_idx)) & 1) +
'0';
443 buffer[bytes_to_format * 8] =
'\0';
449 result.resize(
length * 8);
455 if (on ==
nullptr && ESPHOME_strcasecmp_P(str, ESPHOME_PSTR(
"on")) == 0)
457 if (on !=
nullptr && strcasecmp(str, on) == 0)
459 if (off ==
nullptr && ESPHOME_strcasecmp_P(str, ESPHOME_PSTR(
"off")) == 0)
461 if (off !=
nullptr && strcasecmp(str, off) == 0)
463 if (ESPHOME_strcasecmp_P(str, ESPHOME_PSTR(
"toggle")) == 0)
469static inline void normalize_accuracy_decimals(
float &value, int8_t &accuracy_decimals) {
470 if (accuracy_decimals < 0) {
472 if (accuracy_decimals == -1) {
474 }
else if (accuracy_decimals == -2) {
479 value = roundf(value / divisor) * divisor;
480 accuracy_decimals = 0;
485 char buf[VALUE_ACCURACY_MAX_LEN];
487 return std::string(buf);
491 normalize_accuracy_decimals(value, accuracy_decimals);
493 int len = snprintf(buf.data(), buf.size(),
"%.*f", accuracy_decimals, value);
497 return static_cast<size_t>(
len) >= buf.size() ? buf.size() - 1 :
static_cast<size_t>(
len);
501 int8_t accuracy_decimals,
StringRef unit_of_measurement) {
502 if (unit_of_measurement.
empty()) {
505 normalize_accuracy_decimals(value, accuracy_decimals);
507 int len = snprintf(buf.data(), buf.size(),
"%.*f %s", accuracy_decimals, value, unit_of_measurement.
c_str());
511 return static_cast<size_t>(
len) >= buf.size() ? buf.size() - 1 :
static_cast<size_t>(
len);
517 snprintf(buf,
sizeof buf,
"%.5g", step);
519 std::string str{buf};
520 size_t dot_pos = str.find(
'.');
521 if (dot_pos == std::string::npos)
524 return str.length() - dot_pos - 1;
528static constexpr const char *BASE64_CHARS =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
529 "abcdefghijklmnopqrstuvwxyz"
540static inline uint8_t base64_find_char(
char c) {
546 const char *
pos = strchr(BASE64_CHARS, c);
547 return pos ? (
pos - BASE64_CHARS) : 0;
551static inline bool is_base64(
char c) {
return (isalnum(c) || (c ==
'+') || (c ==
'/') || (c ==
'-') || (c ==
'_')); }
556static inline void base64_encode_triple(
const char *char_array_3,
int count, std::string &ret) {
557 char char_array_4[4];
558 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
559 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
560 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
561 char_array_4[3] = char_array_3[2] & 0x3f;
563 for (
int j = 0; j < count; j++)
564 ret += BASE64_CHARS[
static_cast<uint8_t
>(char_array_4[j])];
570 char char_array_3[3];
573 char_array_3[i++] = *(buf++);
575 base64_encode_triple(char_array_3, 4, ret);
581 for (
int j = i; j < 3; j++)
582 char_array_3[j] =
'\0';
584 base64_encode_triple(char_array_3, i + 1, ret);
593size_t base64_decode(
const std::string &encoded_string, uint8_t *buf,
size_t buf_len) {
594 return base64_decode(
reinterpret_cast<const uint8_t *
>(encoded_string.data()), encoded_string.size(), buf, buf_len);
598static inline bool base64_decode_quad(uint8_t *char_array_4,
int count, uint8_t *buf,
size_t buf_len,
size_t &out) {
599 for (
int i = 0; i < 4; i++)
600 char_array_4[i] = base64_find_char(char_array_4[i]);
602 uint8_t char_array_3[3];
603 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
604 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
605 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
607 bool truncated =
false;
608 for (
int j = 0; j < count; j++) {
610 buf[out++] = char_array_3[j];
618size_t base64_decode(
const uint8_t *encoded_data,
size_t encoded_len, uint8_t *buf,
size_t buf_len) {
619 size_t in_len = encoded_len;
623 uint8_t char_array_4[4];
624 bool truncated =
false;
629 while (in_len-- && (encoded_data[in] !=
'=') && is_base64(encoded_data[in])) {
630 char_array_4[i++] = encoded_data[in];
633 truncated |= base64_decode_quad(char_array_4, 3, buf, buf_len, out);
639 for (
int j = i; j < 4; j++)
642 truncated |= base64_decode_quad(char_array_4, i - 1, buf, buf_len, out);
646 ESP_LOGW(TAG,
"Base64 decode: buffer too small, truncating");
654 size_t max_len = ((encoded_string.size() + 3) / 4) * 3;
655 std::vector<uint8_t> ret(max_len);
656 size_t actual_len =
base64_decode(encoded_string, ret.data(), max_len);
657 ret.resize(actual_len);
667 constexpr size_t chunk_bytes = 48;
668 constexpr size_t chunk_chars = 64;
669 uint8_t chunk[chunk_bytes];
673 const uint8_t *input =
reinterpret_cast<const uint8_t *
>(base64.data());
674 size_t remaining = base64.size();
677 while (remaining > 0) {
678 size_t chars_to_decode = std::min(remaining, chunk_chars);
679 size_t decoded_len =
base64_decode(input +
pos, chars_to_decode, chunk, chunk_bytes);
681 if (decoded_len == 0)
685 for (
size_t i = 0; i + 3 < decoded_len; i += 4) {
686 int32_t timing =
static_cast<int32_t
>(
encode_uint32(chunk[i + 3], chunk[i + 2], chunk[i + 1], chunk[i]));
687 out.push_back(timing);
691 if (remaining <= chunk_chars && (decoded_len % 4) != 0)
694 pos += chars_to_decode;
695 remaining -= chars_to_decode;
709 return powf(value,
gamma);
717 return powf(value, 1 /
gamma);
720void rgb_to_hsv(
float red,
float green,
float blue,
int &hue,
float &saturation,
float &value) {
721 float max_color_value = std::max(std::max(red, green), blue);
722 float min_color_value = std::min(std::min(red, green), blue);
723 float delta = max_color_value - min_color_value;
727 }
else if (max_color_value == red) {
728 hue = int(fmod(((60 * ((green - blue) / delta)) + 360), 360));
729 }
else if (max_color_value == green) {
730 hue = int(fmod(((60 * ((blue - red) / delta)) + 120), 360));
731 }
else if (max_color_value == blue) {
732 hue = int(fmod(((60 * ((red - green) / delta)) + 240), 360));
735 if (max_color_value == 0) {
738 saturation = delta / max_color_value;
741 value = max_color_value;
743void hsv_to_rgb(
int hue,
float saturation,
float value,
float &red,
float &green,
float &blue) {
744 float chroma = value * saturation;
745 float hue_prime = fmod(hue / 60.0, 6);
746 float intermediate = chroma * (1 - fabs(fmod(hue_prime, 2) - 1));
747 float delta = value - chroma;
749 if (0 <= hue_prime && hue_prime < 1) {
751 green = intermediate;
753 }
else if (1 <= hue_prime && hue_prime < 2) {
757 }
else if (2 <= hue_prime && hue_prime < 3) {
761 }
else if (3 <= hue_prime && hue_prime < 4) {
763 green = intermediate;
765 }
else if (4 <= hue_prime && hue_prime < 5) {
769 }
else if (5 <= hue_prime && hue_prime < 6) {
804 return std::string(buf);
808 char buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
830 bool is_all_zeros =
true;
831 bool is_all_ones =
true;
833 for (uint8_t i = 0; i < 6; i++) {
835 is_all_zeros =
false;
837 if (mac[i] != 0xFF) {
841 return !(is_all_zeros || is_all_ones);
846 uint32_t start =
micros();
848 constexpr uint32_t lag = 5000;
852 delay((us - lag) / 1000UL);
853 while (
micros() - start < us - lag)
856 while (
micros() - start < us)
void stop()
Stop running the loop continuously.
static bool is_high_frequency()
Check whether the loop is running continuously.
static uint8_t num_requests
void start()
Start running the loop continuously.
StringRef is a reference to a string owned by something else.
constexpr const char * c_str() const
constexpr bool empty() const
constexpr size_type size() const
Providing packet encoding functions for exchanging data with a remote host.
float random_float()
Return a random float between 0 and 1.
float gamma_uncorrect(float value, float gamma)
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
size_t make_name_with_suffix_to(char *buffer, size_t buffer_size, const char *name, size_t name_len, char sep, const char *suffix_ptr, size_t suffix_len)
Zero-allocation version: format name + separator + suffix directly into buffer.
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
char format_hex_pretty_char(uint8_t v)
Convert a nibble (0-15) to uppercase hex char (used for pretty printing)
float gamma_correct(float value, float gamma)
constexpr char to_sanitized_char(char c)
Sanitize a single char: keep alphanumerics, dashes, underscores; replace others with underscore.
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
void format_mac_addr_lower_no_sep(const uint8_t *mac, char *output)
Format MAC address as xxxxxxxxxxxxxx (lowercase, no separators)
constexpr uint32_t FNV1_OFFSET_BASIS
FNV-1 32-bit offset basis.
void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value)
Convert red, green and blue (all 0-1) values to hue (0-360), saturation (0-1) and value (0-1).
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
char format_hex_char(uint8_t v, char base)
Convert a nibble (0-15) to hex char with specified base ('a' for lowercase, 'A' for uppercase)
size_t value_accuracy_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals)
Format value with accuracy to buffer, returns chars written (excluding null)
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
std::string format_bin(const uint8_t *data, size_t length)
Format the byte array data of length len in binary.
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
bool base64_decode_int32_vector(const std::string &base64, std::vector< int32_t > &out)
Decode base64/base64url string directly into vector of little-endian int32 values.
bool has_custom_mac_address()
Check if a custom MAC address is set (ESP32 & variants)
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
std::string str_snprintf(const char *fmt, size_t len,...)
int8_t step_to_accuracy_decimals(float step)
Derive accuracy in decimals from an increment step.
uint32_t IRAM_ATTR HOT micros()
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
const char * get_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in colon-separated uppercase hex notation.
void IRAM_ATTR HOT delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
std::string str_upper_case(const std::string &str)
Convert the string to upper case.
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
std::string str_until(const char *str, char ch)
Extract the part of the string until either the first occurrence of the specified character,...
bool str_endswith_ignore_case(const char *str, size_t str_len, const char *suffix, size_t suffix_len)
Case-insensitive check if string ends with suffix (no heap allocation).
char * str_sanitize_to(char *buffer, size_t buffer_size, const char *str)
Sanitize a string to buffer, keeping only alphanumerics, dashes, and underscores.
std::string format_mac_address_pretty(const uint8_t *mac)
size_t value_accuracy_with_uom_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals, StringRef unit_of_measurement)
Format value with accuracy and UOM to buffer, returns chars written (excluding null)
std::string base64_encode(const std::vector< uint8_t > &buf)
constexpr uint32_t FNV1_PRIME
FNV-1 32-bit prime.
void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green, float &blue)
Convert hue (0-360), saturation (0-1) and value (0-1) to red, green and blue (all 0-1).
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
std::string str_sprintf(const char *fmt,...)
size_t size_t const char va_start(args, fmt)
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
size_t size_t const char * fmt
constexpr uint8_t parse_hex_char(char c)
bool str_startswith(const std::string &str, const std::string &start)
Check whether a string starts with a value.
void HOT delay(uint32_t ms)
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
constexpr char to_snake_case_char(char c)
Convert a single char to snake_case: lowercase and space to underscore.
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
bool str_endswith(const std::string &str, const std::string &end)
Check whether a string ends with a value.
size_t base64_decode(const std::string &encoded_string, uint8_t *buf, size_t buf_len)
ParseOnOffState
Return values for parse_on_off().
float pow10_int(int8_t exp)
Compute 10^exp using iterative multiplication/division.
std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr, size_t suffix_len)
Optimized string concatenation: name + separator + suffix (const char* overload) Uses a fixed stack b...
std::string str_ctype_transform(const std::string &str)
char * format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as binary string to buffer.
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
std::string str_truncate(const std::string &str, size_t length)
Truncate a string to a specific length.