13 #if defined(USE_ESP8266) 15 #include <user_interface.h> 18 #elif defined(USE_ESP32_FRAMEWORK_ARDUINO) 20 #elif defined(USE_ESP_IDF) 21 #include "esp_system.h" 22 #include <freertos/FreeRTOS.h> 23 #include <freertos/portmacro.h> 24 #elif defined(USE_RP2040) 28 #include <hardware/structs/rosc.h> 29 #include <hardware/sync.h> 32 #ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC 33 #include "esp_efuse.h" 34 #include "esp_efuse_table.h" 41 #if _GLIBCXX_RELEASE < 7 55 float lerp(
float completion,
float start,
float end) {
return start + (end - start) * completion; }
56 uint8_t
crc8(uint8_t *data, uint8_t
len) {
59 while ((len--) != 0u) {
60 uint8_t inbyte = *data++;
61 for (uint8_t i = 8; i != 0u; i--) {
62 bool mix = (crc ^ inbyte) & 0x01;
71 uint16_t
crc16(
const uint8_t *data, uint8_t
len) {
72 uint16_t
crc = 0xFFFF;
75 for (uint8_t i = 0; i < 8; i++) {
76 if ((crc & 0x01) != 0) {
87 uint32_t hash = 2166136261UL;
98 #elif defined(USE_ESP8266) 100 #elif defined(USE_RP2040) 102 for (uint8_t i = 0; i < 32; i++) {
104 result |= rosc_hw->randombit;
108 #error "No random source available for this configuration." 114 esp_fill_random(data, len);
116 #elif defined(USE_ESP8266) 117 return os_get_random(data, len) == 0;
118 #elif defined(USE_RP2040) 121 for (uint8_t i = 0; i < 8; i++) {
123 result |= rosc_hw->randombit;
129 #error "No random source available for this configuration." 136 return strcasecmp(a.c_str(), b.c_str()) == 0;
138 bool str_startswith(
const std::string &str,
const std::string &start) {
return str.rfind(start, 0) == 0; }
140 return str.rfind(end) == (str.size() - end.size());
143 return str.length() > length ? str.substr(0, length) : str;
146 char *pos = strchr(str, ch);
147 return pos ==
nullptr ? std::string(str) : std::string(str, pos - str);
149 std::string
str_until(
const std::string &str,
char ch) {
return str.substr(0, str.find(ch)); }
154 result.resize(str.length());
155 std::transform(str.begin(), str.end(), result.begin(), [](
unsigned char ch) {
return fn(ch); });
158 std::string
str_lower_case(
const std::string &str) {
return str_ctype_transform<std::tolower>(str); }
159 std::string
str_upper_case(
const std::string &str) {
return str_ctype_transform<std::toupper>(str); }
162 result.resize(str.length());
163 std::transform(str.begin(), str.end(), result.begin(), ::tolower);
164 std::replace(result.begin(), result.end(),
' ',
'_');
169 std::copy_if(str.begin(), str.end(), std::back_inserter(out), [](
const char &c) {
170 return c ==
'-' || c ==
'_' || (c >=
'0' && c <= '9') || (c >=
'a' && c <= 'z') || (c >=
'A' && c <=
'Z');
180 size_t out_length = vsnprintf(&str[0], len + 1, fmt, args);
183 if (out_length < len)
184 str.resize(out_length);
193 size_t length = vsnprintf(
nullptr, 0, fmt, args);
198 vsnprintf(&str[0], length + 1, fmt, args);
206 size_t parse_hex(
const char *str,
size_t length, uint8_t *data,
size_t count) {
208 size_t chars = std::min(length, 2 * count);
209 for (
size_t i = 2 * count - chars; i < 2 * count; i++, str++) {
210 if (*str >=
'0' && *str <=
'9') {
212 }
else if (*str >=
'A' && *str <=
'F') {
213 val = 10 + (*str -
'A');
214 }
else if (*str >=
'a' && *str <=
'f') {
215 val = 10 + (*str -
'a');
219 data[i >> 1] = !(i & 1) ? val << 4 : data[i >> 1] | val;
224 static char format_hex_char(uint8_t v) {
return v >= 10 ?
'a' + (v - 10) :
'0' + v; }
227 ret.resize(length * 2);
228 for (
size_t i = 0; i < length; i++) {
229 ret[2 * i] = format_hex_char((data[i] & 0xF0) >> 4);
230 ret[2 * i + 1] = format_hex_char(data[i] & 0x0F);
236 static char format_hex_pretty_char(uint8_t v) {
return v >= 10 ?
'A' + (v - 10) :
'0' + v; }
241 ret.resize(3 * length - 1);
242 for (
size_t i = 0; i < length; i++) {
243 ret[3 * i] = format_hex_pretty_char((data[i] & 0xF0) >> 4);
244 ret[3 * i + 1] = format_hex_pretty_char(data[i] & 0x0F);
246 ret[3 * i + 2] =
'.';
249 return ret +
" (" +
to_string(length) +
")";
258 ret.resize(5 * length - 1);
259 for (
size_t i = 0; i < length; i++) {
260 ret[5 * i] = format_hex_pretty_char((data[i] & 0xF000) >> 12);
261 ret[5 * i + 1] = format_hex_pretty_char((data[i] & 0x0F00) >> 8);
262 ret[5 * i + 2] = format_hex_pretty_char((data[i] & 0x00F0) >> 4);
263 ret[5 * i + 3] = format_hex_pretty_char(data[i] & 0x000F);
265 ret[5 * i + 2] =
'.';
268 return ret +
" (" +
to_string(length) +
")";
274 if (on ==
nullptr && strcasecmp(str,
"on") == 0)
276 if (on !=
nullptr && strcasecmp(str, on) == 0)
278 if (off ==
nullptr && strcasecmp(str,
"off") == 0)
280 if (off !=
nullptr && strcasecmp(str, off) == 0)
282 if (strcasecmp(str,
"toggle") == 0)
289 if (accuracy_decimals < 0) {
290 auto multiplier = powf(10.0f, accuracy_decimals);
291 value = roundf(value * multiplier) / multiplier;
292 accuracy_decimals = 0;
295 snprintf(tmp,
sizeof(tmp),
"%.*f", accuracy_decimals, value);
296 return std::string(tmp);
302 sprintf(buf,
"%.5g", step);
304 std::string str{buf};
305 size_t dot_pos = str.find(
'.');
306 if (dot_pos == std::string::npos)
309 return str.length() - dot_pos - 1;
320 return powf(value, gamma);
328 return powf(value, 1 / gamma);
331 void rgb_to_hsv(
float red,
float green,
float blue,
int &hue,
float &saturation,
float &value) {
332 float max_color_value = std::max(std::max(red, green), blue);
333 float min_color_value = std::min(std::min(red, green), blue);
334 float delta = max_color_value - min_color_value;
338 }
else if (max_color_value == red) {
339 hue = int(fmod(((60 * ((green - blue) / delta)) + 360), 360));
340 }
else if (max_color_value == green) {
341 hue = int(fmod(((60 * ((blue - red) / delta)) + 120), 360));
342 }
else if (max_color_value == blue) {
343 hue = int(fmod(((60 * ((red - green) / delta)) + 240), 360));
346 if (max_color_value == 0) {
349 saturation = delta / max_color_value;
352 value = max_color_value;
354 void hsv_to_rgb(
int hue,
float saturation,
float value,
float &red,
float &green,
float &blue) {
355 float chroma = value * saturation;
356 float hue_prime = fmod(hue / 60.0, 6);
357 float intermediate = chroma * (1 - fabs(fmod(hue_prime, 2) - 1));
358 float delta = value - chroma;
360 if (0 <= hue_prime && hue_prime < 1) {
362 green = intermediate;
364 }
else if (1 <= hue_prime && hue_prime < 2) {
368 }
else if (2 <= hue_prime && hue_prime < 3) {
372 }
else if (3 <= hue_prime && hue_prime < 4) {
374 green = intermediate;
376 }
else if (4 <= hue_prime && hue_prime < 5) {
380 }
else if (5 <= hue_prime && hue_prime < 6) {
396 #if defined(USE_ESP8266) || defined(USE_RP2040) 402 #elif defined(USE_ESP32) 404 void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); }
405 bool Mutex::try_lock() {
return xSemaphoreTake(this->handle_, 0) == pdTRUE; }
409 #if defined(USE_ESP8266) 412 #elif defined(USE_ESP32) 417 #elif defined(USE_RP2040) 427 this->started_ =
true;
433 this->started_ =
false;
438 #if defined(USE_ESP32) 439 #if defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC) 444 esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48);
446 esp_efuse_mac_get_default(mac);
448 #elif defined(USE_ESP8266) 449 wifi_get_macaddr(STATION_IF, mac);
450 #elif defined(USE_RP2040) && defined(USE_WIFI) 451 WiFi.macAddress(mac);
457 return str_snprintf(
"%02x%02x%02x%02x%02x%02x", 12, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
462 return str_snprintf(
"%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
469 uint32_t start =
micros();
470 const uint32_t lag = 5000;
474 delay((us - lag) / 1000UL);
475 while (
micros() - start < us - lag)
478 while (
micros() - start < us)
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)...
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
std::string str_truncate(const std::string &str, size_t length)
Truncate a string to a specific length.
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
static bool is_high_frequency()
Check whether the loop is running continuously.
std::string str_upper_case(const std::string &str)
Convert the string to upper case.
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
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.
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
std::string str_until(const char *str, char ch)
Extract the part of the string until either the first occurrence of the specified character...
uint8_t crc8(uint8_t *data, uint8_t len)
Calculate a CRC-8 checksum of data with size len.
std::string str_ctype_transform(const std::string &str)
float lerp(float completion, float start, float end)
Linearly interpolate between start and end by completion (between 0 and 1).
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait...
uint32_t IRAM_ATTR HOT micros()
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
uint16_t crc16(const uint8_t *data, uint8_t len)
Calculate a CRC-16 checksum of data with size len.
ParseOnOffState
Return values for parse_on_off().
float gamma_correct(float value, float gamma)
Applies gamma correction of gamma to value.
bool str_startswith(const std::string &str, const std::string &start)
Check whether a string starts with a value.
void start()
Start running the loop continuously.
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 str_lower_case(const std::string &str)
Convert the string to lower case.
std::string str_sprintf(const char *fmt,...)
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
bool str_endswith(const std::string &str, const std::string &end)
Check whether a string ends with a value.
void stop()
Stop running the loop continuously.
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
int8_t step_to_accuracy_decimals(float step)
Derive accuracy in decimals from an increment step.
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores...
std::string to_string(int value)
uint32_t fnv1_hash(const std::string &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.
static uint8_t num_requests
std::string str_snprintf(const char *fmt, size_t len,...)
float random_float()
Return a random float between 0 and 1.
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
void IRAM_ATTR HOT delay(uint32_t ms)
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
float gamma_uncorrect(float value, float gamma)
Reverts gamma correction of gamma to value.