ESPHome  2023.8.3
helpers.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cmath>
4 #include <cstring>
5 #include <functional>
6 #include <memory>
7 #include <string>
8 #include <type_traits>
9 #include <vector>
10 
11 #include "esphome/core/optional.h"
12 
13 #ifdef USE_ESP32
14 #include <esp_heap_caps.h>
15 #endif
16 
17 #if defined(USE_ESP32)
18 #include <freertos/FreeRTOS.h>
19 #include <freertos/semphr.h>
20 #endif
21 
22 #define HOT __attribute__((hot))
23 #define ESPDEPRECATED(msg, when) __attribute__((deprecated(msg)))
24 #define ALWAYS_INLINE __attribute__((always_inline))
25 #define PACKED __attribute__((packed))
26 
27 // Various functions can be constexpr in C++14, but not in C++11 (because their body isn't just a return statement).
28 // Define a substitute constexpr keyword for those functions, until we can drop C++11 support.
29 #if __cplusplus >= 201402L
30 #define constexpr14 constexpr
31 #else
32 #define constexpr14 inline // constexpr implies inline
33 #endif
34 
35 namespace esphome {
36 
39 
40 // Backports for various STL features we like to use. Pull in the STL implementation wherever available, to avoid
41 // ambiguity and to provide a uniform API.
42 
43 // std::to_string() from C++11, available from libstdc++/g++ 8
44 // See https://github.com/espressif/esp-idf/issues/1445
45 #if _GLIBCXX_RELEASE >= 8
46 using std::to_string;
47 #else
48 std::string to_string(int value); // NOLINT
49 std::string to_string(long value); // NOLINT
50 std::string to_string(long long value); // NOLINT
51 std::string to_string(unsigned value); // NOLINT
52 std::string to_string(unsigned long value); // NOLINT
53 std::string to_string(unsigned long long value); // NOLINT
54 std::string to_string(float value);
55 std::string to_string(double value);
56 std::string to_string(long double value);
57 #endif
58 
59 // std::is_trivially_copyable from C++11, implemented in libstdc++/g++ 5.1 (but minor releases can't be detected)
60 #if _GLIBCXX_RELEASE >= 6
61 using std::is_trivially_copyable;
62 #else
63 // Implementing this is impossible without compiler intrinsics, so don't bother. Invalid usage will be detected on
64 // other variants that use a newer compiler anyway.
65 // NOLINTNEXTLINE(readability-identifier-naming)
66 template<typename T> struct is_trivially_copyable : public std::integral_constant<bool, true> {};
67 #endif
68 
69 // std::make_unique() from C++14
70 #if __cpp_lib_make_unique >= 201304
71 using std::make_unique;
72 #else
73 template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args &&...args) {
74  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
75 }
76 #endif
77 
78 // std::enable_if_t from C++14
79 #if __cplusplus >= 201402L
80 using std::enable_if_t;
81 #else
82 template<bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
83 #endif
84 
85 // std::clamp from C++17
86 #if __cpp_lib_clamp >= 201603
87 using std::clamp;
88 #else
89 template<typename T, typename Compare> constexpr const T &clamp(const T &v, const T &lo, const T &hi, Compare comp) {
90  return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
91 }
92 template<typename T> constexpr const T &clamp(const T &v, const T &lo, const T &hi) {
93  return clamp(v, lo, hi, std::less<T>{});
94 }
95 #endif
96 
97 // std::is_invocable from C++17
98 #if __cpp_lib_is_invocable >= 201703
99 using std::is_invocable;
100 #else
101 // https://stackoverflow.com/a/37161919/8924614
102 template<class T, class... Args> struct is_invocable { // NOLINT(readability-identifier-naming)
103  template<class U> static auto test(U *p) -> decltype((*p)(std::declval<Args>()...), void(), std::true_type());
104  template<class U> static auto test(...) -> decltype(std::false_type());
105  static constexpr auto value = decltype(test<T>(nullptr))::value; // NOLINT
106 };
107 #endif
108 
109 // std::bit_cast from C++20
110 #if __cpp_lib_bit_cast >= 201806
111 using std::bit_cast;
112 #else
113 template<
115  typename To, typename From,
117  int> = 0>
118 To bit_cast(const From &src) {
119  To dst;
120  memcpy(&dst, &src, sizeof(To));
121  return dst;
122 }
123 #endif
124 
125 // std::byteswap from C++23
126 template<typename T> constexpr14 T byteswap(T n) {
127  T m;
128  for (size_t i = 0; i < sizeof(T); i++)
129  reinterpret_cast<uint8_t *>(&m)[i] = reinterpret_cast<uint8_t *>(&n)[sizeof(T) - 1 - i];
130  return m;
131 }
132 template<> constexpr14 uint8_t byteswap(uint8_t n) { return n; }
133 template<> constexpr14 uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
134 template<> constexpr14 uint32_t byteswap(uint32_t n) { return __builtin_bswap32(n); }
135 template<> constexpr14 uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); }
136 template<> constexpr14 int8_t byteswap(int8_t n) { return n; }
137 template<> constexpr14 int16_t byteswap(int16_t n) { return __builtin_bswap16(n); }
138 template<> constexpr14 int32_t byteswap(int32_t n) { return __builtin_bswap32(n); }
139 template<> constexpr14 int64_t byteswap(int64_t n) { return __builtin_bswap64(n); }
140 
142 
145 
147 float lerp(float completion, float start, float end);
148 
150 template<typename T, typename U> T remap(U value, U min, U max, T min_out, T max_out) {
151  return (value - min) * (max_out - min_out) / (max - min) + min_out;
152 }
153 
155 uint8_t crc8(uint8_t *data, uint8_t len);
156 
158 uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc = 0xffff, uint16_t reverse_poly = 0xa001,
159  bool refin = false, bool refout = false);
160 uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc = 0, uint16_t poly = 0x1021, bool refin = false,
161  bool refout = false);
162 
164 uint32_t fnv1_hash(const std::string &str);
165 
167 uint32_t random_uint32();
169 float random_float();
171 bool random_bytes(uint8_t *data, size_t len);
172 
174 
177 
179 constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb) {
180  return (static_cast<uint16_t>(msb) << 8) | (static_cast<uint16_t>(lsb));
181 }
183 constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4) {
184  return (static_cast<uint32_t>(byte1) << 24) | (static_cast<uint32_t>(byte2) << 16) |
185  (static_cast<uint32_t>(byte3) << 8) | (static_cast<uint32_t>(byte4));
186 }
188 constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3) {
189  return ((static_cast<uint32_t>(byte1) << 16) | (static_cast<uint32_t>(byte2) << 8) | (static_cast<uint32_t>(byte3)));
190 }
191 
193 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
194 constexpr14 T encode_value(const uint8_t *bytes) {
195  T val = 0;
196  for (size_t i = 0; i < sizeof(T); i++) {
197  val <<= 8;
198  val |= bytes[i];
199  }
200  return val;
201 }
203 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
204 constexpr14 T encode_value(const std::array<uint8_t, sizeof(T)> bytes) {
205  return encode_value<T>(bytes.data());
206 }
208 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
209 constexpr14 std::array<uint8_t, sizeof(T)> decode_value(T val) {
210  std::array<uint8_t, sizeof(T)> ret{};
211  for (size_t i = sizeof(T); i > 0; i--) {
212  ret[i - 1] = val & 0xFF;
213  val >>= 8;
214  }
215  return ret;
216 }
217 
219 inline uint8_t reverse_bits(uint8_t x) {
220  x = ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
221  x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
222  x = ((x & 0xF0) >> 4) | ((x & 0x0F) << 4);
223  return x;
224 }
226 inline uint16_t reverse_bits(uint16_t x) {
227  return (reverse_bits(static_cast<uint8_t>(x & 0xFF)) << 8) | reverse_bits(static_cast<uint8_t>((x >> 8) & 0xFF));
228 }
230 inline uint32_t reverse_bits(uint32_t x) {
231  return (reverse_bits(static_cast<uint16_t>(x & 0xFFFF)) << 16) |
232  reverse_bits(static_cast<uint16_t>((x >> 16) & 0xFFFF));
233 }
234 
236 template<typename T> constexpr14 T convert_big_endian(T val) {
237 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
238  return byteswap(val);
239 #else
240  return val;
241 #endif
242 }
243 
245 template<typename T> constexpr14 T convert_little_endian(T val) {
246 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
247  return val;
248 #else
249  return byteswap(val);
250 #endif
251 }
252 
254 
257 
259 bool str_equals_case_insensitive(const std::string &a, const std::string &b);
260 
262 bool str_startswith(const std::string &str, const std::string &start);
264 bool str_endswith(const std::string &str, const std::string &end);
265 
267 inline std::string to_string(const std::string &val) { return val; }
268 
270 std::string str_truncate(const std::string &str, size_t length);
271 
274 std::string str_until(const char *str, char ch);
276 std::string str_until(const std::string &str, char ch);
277 
279 std::string str_lower_case(const std::string &str);
281 std::string str_upper_case(const std::string &str);
283 std::string str_snake_case(const std::string &str);
284 
286 std::string str_sanitize(const std::string &str);
287 
289 std::string __attribute__((format(printf, 1, 3))) str_snprintf(const char *fmt, size_t len, ...);
290 
292 std::string __attribute__((format(printf, 1, 2))) str_sprintf(const char *fmt, ...);
293 
295 
298 
300 template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
301 optional<T> parse_number(const char *str) {
302  char *end = nullptr;
303  unsigned long value = ::strtoul(str, &end, 10); // NOLINT(google-runtime-int)
304  if (end == str || *end != '\0' || value > std::numeric_limits<T>::max())
305  return {};
306  return value;
307 }
309 template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
310 optional<T> parse_number(const std::string &str) {
311  return parse_number<T>(str.c_str());
312 }
314 template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
315 optional<T> parse_number(const char *str) {
316  char *end = nullptr;
317  signed long value = ::strtol(str, &end, 10); // NOLINT(google-runtime-int)
318  if (end == str || *end != '\0' || value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max())
319  return {};
320  return value;
321 }
323 template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
324 optional<T> parse_number(const std::string &str) {
325  return parse_number<T>(str.c_str());
326 }
328 template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0> optional<T> parse_number(const char *str) {
329  char *end = nullptr;
330  float value = ::strtof(str, &end);
331  if (end == str || *end != '\0' || value == HUGE_VALF)
332  return {};
333  return value;
334 }
336 template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0>
337 optional<T> parse_number(const std::string &str) {
338  return parse_number<T>(str.c_str());
339 }
340 
352 size_t parse_hex(const char *str, size_t len, uint8_t *data, size_t count);
354 inline bool parse_hex(const char *str, uint8_t *data, size_t count) {
355  return parse_hex(str, strlen(str), data, count) == 2 * count;
356 }
358 inline bool parse_hex(const std::string &str, uint8_t *data, size_t count) {
359  return parse_hex(str.c_str(), str.length(), data, count) == 2 * count;
360 }
362 inline bool parse_hex(const char *str, std::vector<uint8_t> &data, size_t count) {
363  data.resize(count);
364  return parse_hex(str, strlen(str), data.data(), count) == 2 * count;
365 }
367 inline bool parse_hex(const std::string &str, std::vector<uint8_t> &data, size_t count) {
368  data.resize(count);
369  return parse_hex(str.c_str(), str.length(), data.data(), count) == 2 * count;
370 }
376 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
377 optional<T> parse_hex(const char *str, size_t len) {
378  T val = 0;
379  if (len > 2 * sizeof(T) || parse_hex(str, len, reinterpret_cast<uint8_t *>(&val), sizeof(T)) == 0)
380  return {};
381  return convert_big_endian(val);
382 }
384 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> optional<T> parse_hex(const char *str) {
385  return parse_hex<T>(str, strlen(str));
386 }
388 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> optional<T> parse_hex(const std::string &str) {
389  return parse_hex<T>(str.c_str(), str.length());
390 }
391 
393 std::string format_hex(const uint8_t *data, size_t length);
395 std::string format_hex(const std::vector<uint8_t> &data);
397 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex(T val) {
398  val = convert_big_endian(val);
399  return format_hex(reinterpret_cast<uint8_t *>(&val), sizeof(T));
400 }
401 template<std::size_t N> std::string format_hex(const std::array<uint8_t, N> &data) {
402  return format_hex(data.data(), data.size());
403 }
404 
406 std::string format_hex_pretty(const uint8_t *data, size_t length);
408 std::string format_hex_pretty(const uint16_t *data, size_t length);
410 std::string format_hex_pretty(const std::vector<uint8_t> &data);
412 std::string format_hex_pretty(const std::vector<uint16_t> &data);
414 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex_pretty(T val) {
415  val = convert_big_endian(val);
416  return format_hex_pretty(reinterpret_cast<uint8_t *>(&val), sizeof(T));
417 }
418 
425 };
427 ParseOnOffState parse_on_off(const char *str, const char *on = nullptr, const char *off = nullptr);
428 
430 std::string value_accuracy_to_string(float value, int8_t accuracy_decimals);
431 
433 int8_t step_to_accuracy_decimals(float step);
434 
436 
439 
441 float gamma_correct(float value, float gamma);
443 float gamma_uncorrect(float value, float gamma);
444 
446 void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value);
448 void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green, float &blue);
449 
451 
454 
456 constexpr float celsius_to_fahrenheit(float value) { return value * 1.8f + 32.0f; }
458 constexpr float fahrenheit_to_celsius(float value) { return (value - 32.0f) / 1.8f; }
459 
461 
464 
465 template<typename... X> class CallbackManager;
466 
471 template<typename... Ts> class CallbackManager<void(Ts...)> {
472  public:
474  void add(std::function<void(Ts...)> &&callback) { this->callbacks_.push_back(std::move(callback)); }
475 
477  void call(Ts... args) {
478  for (auto &cb : this->callbacks_)
479  cb(args...);
480  }
481  size_t size() const { return this->callbacks_.size(); }
482 
484  void operator()(Ts... args) { call(args...); }
485 
486  protected:
487  std::vector<std::function<void(Ts...)>> callbacks_;
488 };
489 
491 template<typename T> class Deduplicator {
492  public:
494  bool next(T value) {
495  if (this->has_value_) {
496  if (this->last_value_ == value)
497  return false;
498  }
499  this->has_value_ = true;
500  this->last_value_ = value;
501  return true;
502  }
504  bool has_value() const { return this->has_value_; }
505 
506  protected:
507  bool has_value_{false};
508  T last_value_{};
509 };
510 
512 template<typename T> class Parented {
513  public:
514  Parented() {}
515  Parented(T *parent) : parent_(parent) {}
516 
518  T *get_parent() const { return parent_; }
520  void set_parent(T *parent) { parent_ = parent; }
521 
522  protected:
523  T *parent_{nullptr};
524 };
525 
527 
530 
535 class Mutex {
536  public:
537  Mutex();
538  Mutex(const Mutex &) = delete;
539  void lock();
540  bool try_lock();
541  void unlock();
542 
543  Mutex &operator=(const Mutex &) = delete;
544 
545  private:
546 #if defined(USE_ESP32)
547  SemaphoreHandle_t handle_;
548 #endif
549 };
550 
555 class LockGuard {
556  public:
557  LockGuard(Mutex &mutex) : mutex_(mutex) { mutex_.lock(); }
558  ~LockGuard() { mutex_.unlock(); }
559 
560  private:
561  Mutex &mutex_;
562 };
563 
585  public:
586  InterruptLock();
587  ~InterruptLock();
588 
589  protected:
590 #if defined(USE_ESP8266) || defined(USE_RP2040)
591  uint32_t state_;
592 #endif
593 };
594 
601  public:
603  void start();
605  void stop();
606 
608  static bool is_high_frequency();
609 
610  protected:
611  bool started_{false};
612  static uint8_t num_requests; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
613 };
614 
616 void get_mac_address_raw(uint8_t *mac); // NOLINT(readability-non-const-parameter)
617 
619 std::string get_mac_address();
620 
622 std::string get_mac_address_pretty();
623 
624 #ifdef USE_ESP32
625 void set_mac_address(uint8_t *mac);
627 #endif
628 
630 void delay_microseconds_safe(uint32_t us);
631 
633 
636 
642 template<class T> class ExternalRAMAllocator {
643  public:
644  using value_type = T;
645 
646  enum Flags {
647  NONE = 0,
648  REFUSE_INTERNAL = 1 << 0,
649  ALLOW_FAILURE = 1 << 1,
650  };
651 
652  ExternalRAMAllocator() = default;
653  ExternalRAMAllocator(Flags flags) : flags_{flags} {}
654  template<class U> constexpr ExternalRAMAllocator(const ExternalRAMAllocator<U> &other) : flags_{other.flags_} {}
655 
656  T *allocate(size_t n) {
657  size_t size = n * sizeof(T);
658  T *ptr = nullptr;
659 #ifdef USE_ESP32
660  ptr = static_cast<T *>(heap_caps_malloc(size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT));
661 #endif
662  if (ptr == nullptr && (this->flags_ & Flags::REFUSE_INTERNAL) == 0)
663  ptr = static_cast<T *>(malloc(size)); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
664  if (ptr == nullptr && (this->flags_ & Flags::ALLOW_FAILURE) == 0)
665  abort();
666  return ptr;
667  }
668 
669  void deallocate(T *p, size_t n) {
670  free(p); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
671  }
672 
673  private:
674  Flags flags_{Flags::NONE};
675 };
676 
678 
681 
686 template<typename T, enable_if_t<!std::is_pointer<T>::value, int> = 0> T id(T value) { return value; }
691 template<typename T, enable_if_t<std::is_pointer<T *>::value, int> = 0> T &id(T *value) { return *value; }
692 
694 
697 
698 ESPDEPRECATED("hexencode() is deprecated, use format_hex_pretty() instead.", "2022.1")
699 inline std::string hexencode(const uint8_t *data, uint32_t len) { return format_hex_pretty(data, len); }
700 
701 template<typename T>
702 ESPDEPRECATED("hexencode() is deprecated, use format_hex_pretty() instead.", "2022.1")
703 std::string hexencode(const T &data) {
704  return hexencode(data.data(), data.size());
705 }
706 
708 
709 } // namespace esphome
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:458
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
Definition: helpers.cpp:264
std::string str_truncate(const std::string &str, size_t length)
Truncate a string to a specific length.
Definition: helpers.cpp:246
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
Definition: helpers.cpp:138
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:392
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.
Definition: helpers.cpp:341
std::string str_upper_case(const std::string &str)
Convert the string to upper case.
Definition: helpers.cpp:263
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:329
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:310
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate...
Definition: helpers.h:494
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition: helpers.cpp:181
constexpr ExternalRAMAllocator(const ExternalRAMAllocator< U > &other)
Definition: helpers.h:654
uint16_t x
Definition: tt21100.cpp:17
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:249
uint8_t crc8(uint8_t *data, uint8_t len)
Calculate a CRC-8 checksum of data with size len.
Definition: helpers.cpp:84
Helper class to request loop() to be called as fast as possible.
Definition: helpers.h:600
typename std::enable_if< B, T >::type enable_if_t
Definition: helpers.h:82
An STL allocator that uses SPI RAM.
Definition: helpers.h:642
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:183
std::string to_string(const std::string &val)
Convert the value to a string (added as extra overload so that to_string() can be used on all stringi...
Definition: helpers.h:267
void deallocate(T *p, size_t n)
Definition: helpers.h:669
STL namespace.
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition: helpers.h:686
std::vector< std::function< void(Ts...)> > callbacks_
Definition: helpers.h:487
float lerp(float completion, float start, float end)
Linearly interpolate between start and end by completion (between 0 and 1).
Definition: helpers.cpp:83
mopeka_std_values val[4]
void set_parent(T *parent)
Set the parent of this object.
Definition: helpers.h:520
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait...
Definition: helpers.cpp:574
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
Definition: helpers.cpp:203
constexpr14 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:194
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:100
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:89
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:377
void call(Ts... args)
Call all callbacks in this manager.
Definition: helpers.h:477
uint8_t reverse_bits(uint8_t x)
Reverse the order of 8 bits.
Definition: helpers.h:219
uint8_t m
Definition: bl0939.h:20
ParseOnOffState
Return values for parse_on_off().
Definition: helpers.h:420
float gamma_correct(float value, float gamma)
Applies gamma correction of gamma to value.
Definition: helpers.cpp:418
bool str_startswith(const std::string &str, const std::string &start)
Check whether a string starts with a value.
Definition: helpers.cpp:242
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition: helpers.h:456
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21") extern const Color COLOR_BLACK
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition: helpers.h:301
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:435
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition: helpers.cpp:262
std::string str_sprintf(const char *fmt,...)
Definition: helpers.cpp:292
constexpr14 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:209
constexpr14 T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order...
Definition: helpers.h:236
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:560
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:188
uint8_t type
ExternalRAMAllocator(Flags flags)
Definition: helpers.h:653
bool has_value() const
Returns whether this deduplicator has processed any items so far.
Definition: helpers.h:504
bool str_endswith(const std::string &str, const std::string &end)
Check whether a string ends with a value.
Definition: helpers.cpp:243
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:179
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
Definition: helpers.cpp:571
int8_t step_to_accuracy_decimals(float step)
Derive accuracy in decimals from an increment step.
Definition: helpers.cpp:403
enum esphome::EntityCategory __attribute__
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:150
const uint32_t flags
Definition: stm32flash.h:85
Parented(T *parent)
Definition: helpers.h:515
LockGuard(Mutex &mutex)
Definition: helpers.h:557
T * get_parent() const
Get the parent of this object.
Definition: helpers.h:518
Helper class to disable interrupts.
Definition: helpers.h:584
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores...
Definition: helpers.cpp:271
std::string to_string(int value)
Definition: helpers.cpp:70
std::string size_t len
Definition: helpers.h:289
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition: helpers.cpp:172
constexpr14 T byteswap(T n)
Definition: helpers.h:126
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition: helpers.h:118
constexpr14 T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order...
Definition: helpers.h:245
uint16_t length
Definition: tt21100.cpp:12
Helper class to deduplicate items in a series of values.
Definition: helpers.h:491
void operator()(Ts... args)
Call all callbacks in this manager.
Definition: helpers.h:484
num_t cb(num_t x)
Definition: sun.cpp:31
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
std::unique_ptr< T > make_unique(Args &&...args)
Definition: helpers.h:73
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition: helpers.cpp:565
void add(std::function< void(Ts...)> &&callback)
Add a callback to the list.
Definition: helpers.h:474
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition: helpers.h:458
std::string str_snprintf(const char *fmt, size_t len,...)
Definition: helpers.cpp:278
float random_float()
Return a random float between 0 and 1.
Definition: helpers.cpp:202
Helper class to easily give an object a parent of type T.
Definition: helpers.h:512
Helper class that wraps a mutex with a RAII-style API.
Definition: helpers.h:555
Mutex implementation, with API based on the unavailable std::mutex.
Definition: helpers.h:535
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition: helpers.cpp:239
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:541
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition: helpers.h:92
float gamma_uncorrect(float value, float gamma)
Reverts gamma correction of gamma to value.
Definition: helpers.cpp:426