ESPHome 2025.8.0b1
Loading...
Searching...
No Matches
helpers.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cmath>
5#include <cstdint>
6#include <cstring>
7#include <functional>
8#include <iterator>
9#include <limits>
10#include <memory>
11#include <string>
12#include <type_traits>
13#include <vector>
14
16
17#ifdef USE_ESP8266
18#include <Esp.h>
19#endif
20
21#ifdef USE_RP2040
22#include <Arduino.h>
23#endif
24
25#ifdef USE_ESP32
26#include <esp_heap_caps.h>
27#endif
28
29#if defined(USE_ESP32)
30#include <freertos/FreeRTOS.h>
31#include <freertos/semphr.h>
32#elif defined(USE_LIBRETINY)
33#include <FreeRTOS.h>
34#include <semphr.h>
35#endif
36
37#ifdef USE_HOST
38#include <mutex>
39#endif
40
41#define HOT __attribute__((hot))
42#define ESPDEPRECATED(msg, when) __attribute__((deprecated(msg)))
43#define ESPHOME_ALWAYS_INLINE __attribute__((always_inline))
44#define PACKED __attribute__((packed))
45
46namespace esphome {
47
50
51// Keep "using" even after the removal of our backports, to avoid breaking existing code.
52using std::to_string;
53using std::is_trivially_copyable;
54using std::make_unique;
55using std::enable_if_t;
56using std::clamp;
57using std::is_invocable;
58#if __cpp_lib_bit_cast >= 201806
59using std::bit_cast;
60#else
62template<
63 typename To, typename From,
64 enable_if_t<sizeof(To) == sizeof(From) && is_trivially_copyable<From>::value && is_trivially_copyable<To>::value,
65 int> = 0>
66To bit_cast(const From &src) {
67 To dst;
68 memcpy(&dst, &src, sizeof(To));
69 return dst;
70}
71#endif
72
73// clang-format off
74inline float lerp(float completion, float start, float end) = delete; // Please use std::lerp. Notice that it has different order on arguments!
75// clang-format on
76
77// std::byteswap from C++23
78template<typename T> constexpr T byteswap(T n) {
79 T m;
80 for (size_t i = 0; i < sizeof(T); i++)
81 reinterpret_cast<uint8_t *>(&m)[i] = reinterpret_cast<uint8_t *>(&n)[sizeof(T) - 1 - i];
82 return m;
83}
84template<> constexpr uint8_t byteswap(uint8_t n) { return n; }
85template<> constexpr uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
86template<> constexpr uint32_t byteswap(uint32_t n) { return __builtin_bswap32(n); }
87template<> constexpr uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); }
88template<> constexpr int8_t byteswap(int8_t n) { return n; }
89template<> constexpr int16_t byteswap(int16_t n) { return __builtin_bswap16(n); }
90template<> constexpr int32_t byteswap(int32_t n) { return __builtin_bswap32(n); }
91template<> constexpr int64_t byteswap(int64_t n) { return __builtin_bswap64(n); }
92
94
97
99template<typename T, size_t N> class StaticVector {
100 public:
101 using value_type = T;
102 using iterator = typename std::array<T, N>::iterator;
103 using const_iterator = typename std::array<T, N>::const_iterator;
104 using reverse_iterator = std::reverse_iterator<iterator>;
105 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
106
107 private:
108 std::array<T, N> data_{};
109 size_t count_{0};
110
111 public:
112 // Minimal vector-compatible interface - only what we actually use
113 void push_back(const T &value) {
114 if (count_ < N) {
115 data_[count_++] = value;
116 }
117 }
118
119 size_t size() const { return count_; }
120 bool empty() const { return count_ == 0; }
121
122 T &operator[](size_t i) { return data_[i]; }
123 const T &operator[](size_t i) const { return data_[i]; }
124
125 // For range-based for loops
126 iterator begin() { return data_.begin(); }
127 iterator end() { return data_.begin() + count_; }
128 const_iterator begin() const { return data_.begin(); }
129 const_iterator end() const { return data_.begin() + count_; }
130
131 // Reverse iterators
136};
137
139
142
144template<typename T, typename U> T remap(U value, U min, U max, T min_out, T max_out) {
145 return (value - min) * (max_out - min_out) / (max - min) + min_out;
146}
147
149uint8_t crc8(const uint8_t *data, uint8_t len);
150
152uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc = 0xffff, uint16_t reverse_poly = 0xa001,
153 bool refin = false, bool refout = false);
154uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc = 0, uint16_t poly = 0x1021, bool refin = false,
155 bool refout = false);
156
158uint32_t fnv1_hash(const std::string &str);
159
161uint32_t random_uint32();
163float random_float();
165bool random_bytes(uint8_t *data, size_t len);
166
168
171
173constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb) {
174 return (static_cast<uint16_t>(msb) << 8) | (static_cast<uint16_t>(lsb));
175}
177constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3) {
178 return (static_cast<uint32_t>(byte1) << 16) | (static_cast<uint32_t>(byte2) << 8) | (static_cast<uint32_t>(byte3));
179}
181constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4) {
182 return (static_cast<uint32_t>(byte1) << 24) | (static_cast<uint32_t>(byte2) << 16) |
183 (static_cast<uint32_t>(byte3) << 8) | (static_cast<uint32_t>(byte4));
184}
185
187template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr T encode_value(const uint8_t *bytes) {
188 T val = 0;
189 for (size_t i = 0; i < sizeof(T); i++) {
190 val <<= 8;
191 val |= bytes[i];
192 }
193 return val;
194}
196template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
197constexpr T encode_value(const std::array<uint8_t, sizeof(T)> bytes) {
198 return encode_value<T>(bytes.data());
199}
201template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
202constexpr std::array<uint8_t, sizeof(T)> decode_value(T val) {
203 std::array<uint8_t, sizeof(T)> ret{};
204 for (size_t i = sizeof(T); i > 0; i--) {
205 ret[i - 1] = val & 0xFF;
206 val >>= 8;
207 }
208 return ret;
209}
210
212inline uint8_t reverse_bits(uint8_t x) {
213 x = ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
214 x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
215 x = ((x & 0xF0) >> 4) | ((x & 0x0F) << 4);
216 return x;
217}
219inline uint16_t reverse_bits(uint16_t x) {
220 return (reverse_bits(static_cast<uint8_t>(x & 0xFF)) << 8) | reverse_bits(static_cast<uint8_t>((x >> 8) & 0xFF));
221}
223inline uint32_t reverse_bits(uint32_t x) {
224 return (reverse_bits(static_cast<uint16_t>(x & 0xFFFF)) << 16) |
225 reverse_bits(static_cast<uint16_t>((x >> 16) & 0xFFFF));
226}
227
229template<typename T> constexpr T convert_big_endian(T val) {
230#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
231 return byteswap(val);
232#else
233 return val;
234#endif
235}
236
238template<typename T> constexpr T convert_little_endian(T val) {
239#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
240 return val;
241#else
242 return byteswap(val);
243#endif
244}
245
247
250
252bool str_equals_case_insensitive(const std::string &a, const std::string &b);
253
255bool str_startswith(const std::string &str, const std::string &start);
257bool str_endswith(const std::string &str, const std::string &end);
258
260std::string str_truncate(const std::string &str, size_t length);
261
264std::string str_until(const char *str, char ch);
266std::string str_until(const std::string &str, char ch);
267
269std::string str_lower_case(const std::string &str);
271std::string str_upper_case(const std::string &str);
273std::string str_snake_case(const std::string &str);
274
276std::string str_sanitize(const std::string &str);
277
279std::string __attribute__((format(printf, 1, 3))) str_snprintf(const char *fmt, size_t len, ...);
280
282std::string __attribute__((format(printf, 1, 2))) str_sprintf(const char *fmt, ...);
283
285
288
290template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
291optional<T> parse_number(const char *str) {
292 char *end = nullptr;
293 unsigned long value = ::strtoul(str, &end, 10); // NOLINT(google-runtime-int)
294 if (end == str || *end != '\0' || value > std::numeric_limits<T>::max())
295 return {};
296 return value;
297}
299template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
300optional<T> parse_number(const std::string &str) {
301 return parse_number<T>(str.c_str());
302}
304template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
305optional<T> parse_number(const char *str) {
306 char *end = nullptr;
307 signed long value = ::strtol(str, &end, 10); // NOLINT(google-runtime-int)
308 if (end == str || *end != '\0' || value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max())
309 return {};
310 return value;
311}
313template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
314optional<T> parse_number(const std::string &str) {
315 return parse_number<T>(str.c_str());
316}
318template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0> optional<T> parse_number(const char *str) {
319 char *end = nullptr;
320 float value = ::strtof(str, &end);
321 if (end == str || *end != '\0' || value == HUGE_VALF)
322 return {};
323 return value;
324}
326template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0>
327optional<T> parse_number(const std::string &str) {
328 return parse_number<T>(str.c_str());
329}
330
342size_t parse_hex(const char *str, size_t len, uint8_t *data, size_t count);
344inline bool parse_hex(const char *str, uint8_t *data, size_t count) {
345 return parse_hex(str, strlen(str), data, count) == 2 * count;
346}
348inline bool parse_hex(const std::string &str, uint8_t *data, size_t count) {
349 return parse_hex(str.c_str(), str.length(), data, count) == 2 * count;
350}
352inline bool parse_hex(const char *str, std::vector<uint8_t> &data, size_t count) {
353 data.resize(count);
354 return parse_hex(str, strlen(str), data.data(), count) == 2 * count;
355}
357inline bool parse_hex(const std::string &str, std::vector<uint8_t> &data, size_t count) {
358 data.resize(count);
359 return parse_hex(str.c_str(), str.length(), data.data(), count) == 2 * count;
360}
366template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
367optional<T> parse_hex(const char *str, size_t len) {
368 T val = 0;
369 if (len > 2 * sizeof(T) || parse_hex(str, len, reinterpret_cast<uint8_t *>(&val), sizeof(T)) == 0)
370 return {};
371 return convert_big_endian(val);
372}
374template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> optional<T> parse_hex(const char *str) {
375 return parse_hex<T>(str, strlen(str));
376}
378template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> optional<T> parse_hex(const std::string &str) {
379 return parse_hex<T>(str.c_str(), str.length());
380}
381
383std::string format_mac_address_pretty(const uint8_t mac[6]);
385std::string format_hex(const uint8_t *data, size_t length);
387std::string format_hex(const std::vector<uint8_t> &data);
389template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex(T val) {
391 return format_hex(reinterpret_cast<uint8_t *>(&val), sizeof(T));
392}
393template<std::size_t N> std::string format_hex(const std::array<uint8_t, N> &data) {
394 return format_hex(data.data(), data.size());
395}
396
422std::string format_hex_pretty(const uint8_t *data, size_t length, char separator = '.', bool show_length = true);
423
444std::string format_hex_pretty(const uint16_t *data, size_t length, char separator = '.', bool show_length = true);
445
467std::string format_hex_pretty(const std::vector<uint8_t> &data, char separator = '.', bool show_length = true);
468
489std::string format_hex_pretty(const std::vector<uint16_t> &data, char separator = '.', bool show_length = true);
490
511std::string format_hex_pretty(const std::string &data, char separator = '.', bool show_length = true);
512
536template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
537std::string format_hex_pretty(T val, char separator = '.', bool show_length = true) {
539 return format_hex_pretty(reinterpret_cast<uint8_t *>(&val), sizeof(T), separator, show_length);
540}
541
543std::string format_bin(const uint8_t *data, size_t length);
545template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_bin(T val) {
547 return format_bin(reinterpret_cast<uint8_t *>(&val), sizeof(T));
548}
549
558ParseOnOffState parse_on_off(const char *str, const char *on = nullptr, const char *off = nullptr);
559
561std::string value_accuracy_to_string(float value, int8_t accuracy_decimals);
562
564int8_t step_to_accuracy_decimals(float step);
565
566std::string base64_encode(const uint8_t *buf, size_t buf_len);
567std::string base64_encode(const std::vector<uint8_t> &buf);
568
569std::vector<uint8_t> base64_decode(const std::string &encoded_string);
570size_t base64_decode(std::string const &encoded_string, uint8_t *buf, size_t buf_len);
571
573
576
578float gamma_correct(float value, float gamma);
580float gamma_uncorrect(float value, float gamma);
581
583void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value);
585void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green, float &blue);
586
588
591
593constexpr float celsius_to_fahrenheit(float value) { return value * 1.8f + 32.0f; }
595constexpr float fahrenheit_to_celsius(float value) { return (value - 32.0f) / 1.8f; }
596
598
601
602template<typename... X> class CallbackManager;
603
608template<typename... Ts> class CallbackManager<void(Ts...)> {
609 public:
611 void add(std::function<void(Ts...)> &&callback) { this->callbacks_.push_back(std::move(callback)); }
612
614 void call(Ts... args) {
615 for (auto &cb : this->callbacks_)
616 cb(args...);
617 }
618 size_t size() const { return this->callbacks_.size(); }
619
621 void operator()(Ts... args) { call(args...); }
622
623 protected:
624 std::vector<std::function<void(Ts...)>> callbacks_;
625};
626
628template<typename T> class Deduplicator {
629 public:
631 bool next(T value) {
632 if (this->has_value_ && !this->value_unknown_ && this->last_value_ == value) {
633 return false;
634 }
635 this->has_value_ = true;
636 this->value_unknown_ = false;
637 this->last_value_ = value;
638 return true;
639 }
642 bool ret = !this->value_unknown_;
643 this->value_unknown_ = true;
644 return ret;
645 }
647 bool has_value() const { return this->has_value_; }
648
649 protected:
650 bool has_value_{false};
651 bool value_unknown_{false};
653};
654
656template<typename T> class Parented {
657 public:
659 Parented(T *parent) : parent_(parent) {}
660
662 T *get_parent() const { return parent_; }
664 void set_parent(T *parent) { parent_ = parent; }
665
666 protected:
667 T *parent_{nullptr};
668};
669
671
674
679class Mutex {
680 public:
681 Mutex();
682 Mutex(const Mutex &) = delete;
683 ~Mutex();
684 void lock();
685 bool try_lock();
686 void unlock();
687
688 Mutex &operator=(const Mutex &) = delete;
689
690 private:
691#if defined(USE_ESP32) || defined(USE_LIBRETINY)
692 SemaphoreHandle_t handle_;
693#else
694 // d-pointer to store private data on new platforms
695 void *handle_; // NOLINT(clang-diagnostic-unused-private-field)
696#endif
697};
698
704 public:
705 LockGuard(Mutex &mutex) : mutex_(mutex) { mutex_.lock(); }
706 ~LockGuard() { mutex_.unlock(); }
707
708 private:
709 Mutex &mutex_;
710};
711
733 public:
736
737 protected:
738#if defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_ZEPHYR)
739 uint32_t state_;
740#endif
741};
742
750class LwIPLock {
751 public:
752 LwIPLock();
753 ~LwIPLock();
754
755 // Delete copy constructor and copy assignment operator to prevent accidental copying
756 LwIPLock(const LwIPLock &) = delete;
757 LwIPLock &operator=(const LwIPLock &) = delete;
758};
759
766 public:
768 void start();
770 void stop();
771
773 static bool is_high_frequency();
774
775 protected:
776 bool started_{false};
777 static uint8_t num_requests; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
778};
779
781void get_mac_address_raw(uint8_t *mac); // NOLINT(readability-non-const-parameter)
782
784std::string get_mac_address();
785
787std::string get_mac_address_pretty();
788
789#ifdef USE_ESP32
791void set_mac_address(uint8_t *mac);
792#endif
793
797
800bool mac_address_is_valid(const uint8_t *mac);
801
803void delay_microseconds_safe(uint32_t us);
804
806
809
818template<class T> class RAMAllocator {
819 public:
820 using value_type = T;
821
822 enum Flags {
823 NONE = 0, // Perform external allocation and fall back to internal memory
824 ALLOC_EXTERNAL = 1 << 0, // Perform external allocation only.
825 ALLOC_INTERNAL = 1 << 1, // Perform internal allocation only.
826 ALLOW_FAILURE = 1 << 2, // Does nothing. Kept for compatibility.
827 };
828
829 RAMAllocator() = default;
830 RAMAllocator(uint8_t flags) {
831 // default is both external and internal
833 if (flags != 0)
834 this->flags_ = flags;
835 }
836 template<class U> constexpr RAMAllocator(const RAMAllocator<U> &other) : flags_{other.flags_} {}
837
838 T *allocate(size_t n) { return this->allocate(n, sizeof(T)); }
839
840 T *allocate(size_t n, size_t manual_size) {
841 size_t size = n * manual_size;
842 T *ptr = nullptr;
843#ifdef USE_ESP32
844 if (this->flags_ & Flags::ALLOC_EXTERNAL) {
845 ptr = static_cast<T *>(heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
846 }
847 if (ptr == nullptr && this->flags_ & Flags::ALLOC_INTERNAL) {
848 ptr = static_cast<T *>(heap_caps_malloc(size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
849 }
850#else
851 // Ignore ALLOC_EXTERNAL/ALLOC_INTERNAL flags if external allocation is not supported
852 ptr = static_cast<T *>(malloc(size)); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
853#endif
854 return ptr;
855 }
856
857 T *reallocate(T *p, size_t n) { return this->reallocate(p, n, sizeof(T)); }
858
859 T *reallocate(T *p, size_t n, size_t manual_size) {
860 size_t size = n * manual_size;
861 T *ptr = nullptr;
862#ifdef USE_ESP32
863 if (this->flags_ & Flags::ALLOC_EXTERNAL) {
864 ptr = static_cast<T *>(heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
865 }
866 if (ptr == nullptr && this->flags_ & Flags::ALLOC_INTERNAL) {
867 ptr = static_cast<T *>(heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT));
868 }
869#else
870 // Ignore ALLOC_EXTERNAL/ALLOC_INTERNAL flags if external allocation is not supported
871 ptr = static_cast<T *>(realloc(p, size)); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
872#endif
873 return ptr;
874 }
875
876 void deallocate(T *p, size_t n) {
877 free(p); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
878 }
879
883 size_t get_free_heap_size() const {
884#ifdef USE_ESP8266
885 return ESP.getFreeHeap(); // NOLINT(readability-static-accessed-through-instance)
886#elif defined(USE_ESP32)
887 auto max_internal =
888 this->flags_ & ALLOC_INTERNAL ? heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL) : 0;
889 auto max_external =
890 this->flags_ & ALLOC_EXTERNAL ? heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM) : 0;
891 return max_internal + max_external;
892#elif defined(USE_RP2040)
893 return ::rp2040.getFreeHeap();
894#elif defined(USE_LIBRETINY)
895 return lt_heap_get_free();
896#else
897 return 100000;
898#endif
899 }
900
904 size_t get_max_free_block_size() const {
905#ifdef USE_ESP8266
906 return ESP.getMaxFreeBlockSize(); // NOLINT(readability-static-accessed-through-instance)
907#elif defined(USE_ESP32)
908 auto max_internal =
909 this->flags_ & ALLOC_INTERNAL ? heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL) : 0;
910 auto max_external =
911 this->flags_ & ALLOC_EXTERNAL ? heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM) : 0;
912 return std::max(max_internal, max_external);
913#else
914 return this->get_free_heap_size();
915#endif
916 }
917
918 private:
919 uint8_t flags_{ALLOC_INTERNAL | ALLOC_EXTERNAL};
920};
921
922template<class T> using ExternalRAMAllocator = RAMAllocator<T>;
923
925
928
933template<typename T, enable_if_t<!std::is_pointer<T>::value, int> = 0> T id(T value) { return value; }
938template<typename T, enable_if_t<std::is_pointer<T *>::value, int> = 0> T &id(T *value) { return *value; }
939
941
944
945ESPDEPRECATED("hexencode() is deprecated, use format_hex_pretty() instead.", "2022.1")
946inline std::string hexencode(const uint8_t *data, uint32_t len) { return format_hex_pretty(data, len); }
947
948template<typename T>
949ESPDEPRECATED("hexencode() is deprecated, use format_hex_pretty() instead.", "2022.1")
950std::string hexencode(const T &data) {
951 return hexencode(data.data(), data.size());
952}
953
955
956} // namespace esphome
uint8_t m
Definition bl0906.h:1
void operator()(Ts... args)
Call all callbacks in this manager.
Definition helpers.h:621
std::vector< std::function< void(Ts...)> > callbacks_
Definition helpers.h:624
void call(Ts... args)
Call all callbacks in this manager.
Definition helpers.h:614
void add(std::function< void(Ts...)> &&callback)
Add a callback to the list.
Definition helpers.h:611
Helper class to deduplicate items in a series of values.
Definition helpers.h:628
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
Definition helpers.h:631
bool has_value() const
Returns true if this deduplicator has processed any items.
Definition helpers.h:647
bool next_unknown()
Returns true if the deduplicator's value was previously known.
Definition helpers.h:641
Helper class to request loop() to be called as fast as possible.
Definition helpers.h:765
void stop()
Stop running the loop continuously.
Definition helpers.cpp:570
static bool is_high_frequency()
Check whether the loop is running continuously.
Definition helpers.cpp:576
void start()
Start running the loop continuously.
Definition helpers.cpp:564
Helper class to disable interrupts.
Definition helpers.h:732
Helper class that wraps a mutex with a RAII-style API.
Definition helpers.h:703
LockGuard(Mutex &mutex)
Definition helpers.h:705
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:750
LwIPLock(const LwIPLock &)=delete
LwIPLock & operator=(const LwIPLock &)=delete
Mutex implementation, with API based on the unavailable std::mutex.
Definition helpers.h:679
void unlock()
Definition helpers.cpp:27
bool try_lock()
Definition helpers.cpp:26
Mutex(const Mutex &)=delete
Mutex & operator=(const Mutex &)=delete
Helper class to easily give an object a parent of type T.
Definition helpers.h:656
T * get_parent() const
Get the parent of this object.
Definition helpers.h:662
Parented(T *parent)
Definition helpers.h:659
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:664
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:818
RAMAllocator(uint8_t flags)
Definition helpers.h:830
T * reallocate(T *p, size_t n, size_t manual_size)
Definition helpers.h:859
size_t get_free_heap_size() const
Return the total heap space available via this allocator.
Definition helpers.h:883
T * reallocate(T *p, size_t n)
Definition helpers.h:857
void deallocate(T *p, size_t n)
Definition helpers.h:876
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:904
T * allocate(size_t n)
Definition helpers.h:838
constexpr RAMAllocator(const RAMAllocator< U > &other)
Definition helpers.h:836
T * allocate(size_t n, size_t manual_size)
Definition helpers.h:840
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:99
const_reverse_iterator rend() const
Definition helpers.h:135
size_t size() const
Definition helpers.h:119
reverse_iterator rbegin()
Definition helpers.h:132
const T & operator[](size_t i) const
Definition helpers.h:123
reverse_iterator rend()
Definition helpers.h:133
void push_back(const T &value)
Definition helpers.h:113
bool empty() const
Definition helpers.h:120
const_reverse_iterator rbegin() const
Definition helpers.h:134
T & operator[](size_t i)
Definition helpers.h:122
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition helpers.h:105
typename std::array< T, N >::iterator iterator
Definition helpers.h:102
typename std::array< T, N >::const_iterator const_iterator
Definition helpers.h:103
std::reverse_iterator< iterator > reverse_iterator
Definition helpers.h:104
const_iterator end() const
Definition helpers.h:129
const_iterator begin() const
Definition helpers.h:128
struct @67::@68 __attribute__
mopeka_std_values val[4]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:134
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
Definition helpers.cpp:18
uint8_t crc8(const uint8_t *data, uint8_t len)
Calculate a CRC-8 checksum of data with size len using the CRC-8-Dallas/Maxim polynomial.
Definition helpers.cpp:44
ESPDEPRECATED("hexencode() is deprecated, use format_hex_pretty() instead.", "2022.1") inline std
Definition helpers.h:945
float random_float()
Return a random float between 0 and 1.
Definition helpers.cpp:143
float gamma_uncorrect(float value, float gamma)
Reverts gamma correction of gamma to value.
Definition helpers.cpp:490
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.
Definition helpers.cpp:60
std::string value_accuracy_to_string(float value, int8_t accuracy_decimals)
Create a string from a value and an accuracy in decimals.
Definition helpers.cpp:339
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
Definition helpers.h:229
float gamma_correct(float value, float gamma)
Applies gamma correction of gamma to value.
Definition helpers.cpp:482
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
Definition helpers.cpp:594
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).
Definition helpers.cpp:499
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition helpers.cpp:249
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition helpers.cpp:175
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
Definition helpers.cpp:324
std::string format_bin(const uint8_t *data, size_t length)
Format the byte array data of length len in binary.
Definition helpers.cpp:312
constexpr T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order.
Definition helpers.h:238
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores.
Definition helpers.cpp:184
std::string size_t len
Definition helpers.h:279
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:177
bool has_custom_mac_address()
Check if a custom MAC address is set (ESP32 & variants)
Definition helpers.cpp:93
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.
Definition helpers.cpp:226
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:291
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition helpers.cpp:584
std::string str_snprintf(const char *fmt, size_t len,...)
Definition helpers.cpp:194
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
Definition helpers.cpp:91
int8_t step_to_accuracy_decimals(float step)
Derive accuracy in decimals from an increment step.
Definition helpers.cpp:350
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:17
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.
Definition helpers.cpp:611
std::string str_upper_case(const std::string &str)
Convert the string to upper case.
Definition helpers.cpp:176
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.
Definition helpers.cpp:280
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:147
std::string str_until(const char *str, char ch)
Extract the part of the string until either the first occurrence of the specified character,...
Definition helpers.cpp:162
std::string format_mac_address_pretty(const uint8_t *mac)
Definition helpers.cpp:244
std::string base64_encode(const std::vector< uint8_t > &buf)
Definition helpers.cpp:382
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
Definition helpers.h:187
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).
Definition helpers.cpp:522
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
Definition helpers.cpp:100
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.
Definition helpers.h:181
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition helpers.h:593
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:208
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:173
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Definition helpers.cpp:73
bool str_startswith(const std::string &str, const std::string &start)
Check whether a string starts with a value.
Definition helpers.cpp:151
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
Definition helpers.h:202
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition helpers.cpp:578
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:66
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition helpers.h:595
uint8_t reverse_bits(uint8_t x)
Reverse the order of 8 bits.
Definition helpers.h:212
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
Definition helpers.cpp:177
float lerp(float completion, float start, float end)=delete
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).
Definition helpers.h:144
bool str_endswith(const std::string &str, const std::string &end)
Check whether a string ends with a value.
Definition helpers.cpp:152
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition helpers.h:933
size_t base64_decode(const std::string &encoded_string, uint8_t *buf, size_t buf_len)
Definition helpers.cpp:424
ParseOnOffState
Return values for parse_on_off().
Definition helpers.h:551
@ PARSE_ON
Definition helpers.h:553
@ PARSE_TOGGLE
Definition helpers.h:555
@ PARSE_OFF
Definition helpers.h:554
@ PARSE_NONE
Definition helpers.h:552
std::string str_truncate(const std::string &str, size_t length)
Truncate a string to a specific length.
Definition helpers.cpp:159
uint8_t end[39]
Definition sun_gtil2.cpp:17
void byteswap()
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5