ESPHome  2022.6.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 #define HOT __attribute__((hot))
18 #define ESPDEPRECATED(msg, when) __attribute__((deprecated(msg)))
19 #define ALWAYS_INLINE __attribute__((always_inline))
20 #define PACKED __attribute__((packed))
21 
22 // Various functions can be constexpr in C++14, but not in C++11 (because their body isn't just a return statement).
23 // Define a substitute constexpr keyword for those functions, until we can drop C++11 support.
24 #if __cplusplus >= 201402L
25 #define constexpr14 constexpr
26 #else
27 #define constexpr14 inline // constexpr implies inline
28 #endif
29 
30 namespace esphome {
31 
34 
35 // Backports for various STL features we like to use. Pull in the STL implementation wherever available, to avoid
36 // ambiguity and to provide a uniform API.
37 
38 // std::to_string() from C++11, available from libstdc++/g++ 8
39 // See https://github.com/espressif/esp-idf/issues/1445
40 #if _GLIBCXX_RELEASE >= 8
41 using std::to_string;
42 #else
43 std::string to_string(int value); // NOLINT
44 std::string to_string(long value); // NOLINT
45 std::string to_string(long long value); // NOLINT
46 std::string to_string(unsigned value); // NOLINT
47 std::string to_string(unsigned long value); // NOLINT
48 std::string to_string(unsigned long long value); // NOLINT
49 std::string to_string(float value);
50 std::string to_string(double value);
51 std::string to_string(long double value);
52 #endif
53 
54 // std::is_trivially_copyable from C++11, implemented in libstdc++/g++ 5.1 (but minor releases can't be detected)
55 #if _GLIBCXX_RELEASE >= 6
56 using std::is_trivially_copyable;
57 #else
58 // Implementing this is impossible without compiler intrinsics, so don't bother. Invalid usage will be detected on
59 // other variants that use a newer compiler anyway.
60 // NOLINTNEXTLINE(readability-identifier-naming)
61 template<typename T> struct is_trivially_copyable : public std::integral_constant<bool, true> {};
62 #endif
63 
64 // std::make_unique() from C++14
65 #if __cpp_lib_make_unique >= 201304
66 using std::make_unique;
67 #else
68 template<typename T, typename... Args> std::unique_ptr<T> make_unique(Args &&...args) {
69  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
70 }
71 #endif
72 
73 // std::enable_if_t from C++14
74 #if __cplusplus >= 201402L
75 using std::enable_if_t;
76 #else
77 template<bool B, class T = void> using enable_if_t = typename std::enable_if<B, T>::type;
78 #endif
79 
80 // std::clamp from C++17
81 #if __cpp_lib_clamp >= 201603
82 using std::clamp;
83 #else
84 template<typename T, typename Compare> constexpr const T &clamp(const T &v, const T &lo, const T &hi, Compare comp) {
85  return comp(v, lo) ? lo : comp(hi, v) ? hi : v;
86 }
87 template<typename T> constexpr const T &clamp(const T &v, const T &lo, const T &hi) {
88  return clamp(v, lo, hi, std::less<T>{});
89 }
90 #endif
91 
92 // std::is_invocable from C++17
93 #if __cpp_lib_is_invocable >= 201703
94 using std::is_invocable;
95 #else
96 // https://stackoverflow.com/a/37161919/8924614
97 template<class T, class... Args> struct is_invocable { // NOLINT(readability-identifier-naming)
98  template<class U> static auto test(U *p) -> decltype((*p)(std::declval<Args>()...), void(), std::true_type());
99  template<class U> static auto test(...) -> decltype(std::false_type());
100  static constexpr auto value = decltype(test<T>(nullptr))::value; // NOLINT
101 };
102 #endif
103 
104 // std::bit_cast from C++20
105 #if __cpp_lib_bit_cast >= 201806
106 using std::bit_cast;
107 #else
108 template<
110  typename To, typename From,
112  int> = 0>
113 To bit_cast(const From &src) {
114  To dst;
115  memcpy(&dst, &src, sizeof(To));
116  return dst;
117 }
118 #endif
119 
120 // std::byteswap from C++23
121 template<typename T> constexpr14 T byteswap(T n) {
122  T m;
123  for (size_t i = 0; i < sizeof(T); i++)
124  reinterpret_cast<uint8_t *>(&m)[i] = reinterpret_cast<uint8_t *>(&n)[sizeof(T) - 1 - i];
125  return m;
126 }
127 template<> constexpr14 uint8_t byteswap(uint8_t n) { return n; }
128 template<> constexpr14 uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
129 template<> constexpr14 uint32_t byteswap(uint32_t n) { return __builtin_bswap32(n); }
130 template<> constexpr14 uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); }
131 template<> constexpr14 int8_t byteswap(int8_t n) { return n; }
132 template<> constexpr14 int16_t byteswap(int16_t n) { return __builtin_bswap16(n); }
133 template<> constexpr14 int32_t byteswap(int32_t n) { return __builtin_bswap32(n); }
134 template<> constexpr14 int64_t byteswap(int64_t n) { return __builtin_bswap64(n); }
135 
137 
140 
142 float lerp(float completion, float start, float end);
143 
145 template<typename T, typename U> T remap(U value, U min, U max, T min_out, T max_out) {
146  return (value - min) * (max_out - min_out) / (max - min) + min_out;
147 }
148 
150 uint8_t crc8(uint8_t *data, uint8_t len);
151 
153 uint32_t fnv1_hash(const std::string &str);
154 
156 uint32_t random_uint32();
158 float random_float();
160 bool random_bytes(uint8_t *data, size_t len);
161 
163 
166 
168 constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb) {
169  return (static_cast<uint16_t>(msb) << 8) | (static_cast<uint16_t>(lsb));
170 }
172 constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4) {
173  return (static_cast<uint32_t>(byte1) << 24) | (static_cast<uint32_t>(byte2) << 16) |
174  (static_cast<uint32_t>(byte3) << 8) | (static_cast<uint32_t>(byte4));
175 }
177 constexpr 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 }
180 
182 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
183 constexpr14 T encode_value(const uint8_t *bytes) {
184  T val = 0;
185  for (size_t i = 0; i < sizeof(T); i++) {
186  val <<= 8;
187  val |= bytes[i];
188  }
189  return val;
190 }
192 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
193 constexpr14 T encode_value(const std::array<uint8_t, sizeof(T)> bytes) {
194  return encode_value<T>(bytes.data());
195 }
197 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
198 constexpr14 std::array<uint8_t, sizeof(T)> decode_value(T val) {
199  std::array<uint8_t, sizeof(T)> ret{};
200  for (size_t i = sizeof(T); i > 0; i--) {
201  ret[i - 1] = val & 0xFF;
202  val >>= 8;
203  }
204  return ret;
205 }
206 
208 inline uint8_t reverse_bits(uint8_t x) {
209  x = ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
210  x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
211  x = ((x & 0xF0) >> 4) | ((x & 0x0F) << 4);
212  return x;
213 }
215 inline uint16_t reverse_bits(uint16_t x) {
216  return (reverse_bits(static_cast<uint8_t>(x & 0xFF)) << 8) | reverse_bits(static_cast<uint8_t>((x >> 8) & 0xFF));
217 }
219 inline uint32_t reverse_bits(uint32_t x) {
220  return (reverse_bits(static_cast<uint16_t>(x & 0xFFFF)) << 16) |
221  reverse_bits(static_cast<uint16_t>((x >> 16) & 0xFFFF));
222 }
223 
225 template<typename T> constexpr14 T convert_big_endian(T val) {
226 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
227  return byteswap(val);
228 #else
229  return val;
230 #endif
231 }
232 
234 template<typename T> constexpr14 T convert_little_endian(T val) {
235 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
236  return val;
237 #else
238  return byteswap(val);
239 #endif
240 }
241 
243 
246 
248 bool str_equals_case_insensitive(const std::string &a, const std::string &b);
249 
251 bool str_startswith(const std::string &str, const std::string &start);
253 bool str_endswith(const std::string &str, const std::string &end);
254 
256 inline std::string to_string(const std::string &val) { return val; }
257 
259 std::string str_truncate(const std::string &str, size_t length);
260 
263 std::string str_until(const char *str, char ch);
265 std::string str_until(const std::string &str, char ch);
266 
268 std::string str_lower_case(const std::string &str);
270 std::string str_upper_case(const std::string &str);
272 std::string str_snake_case(const std::string &str);
273 
275 std::string str_sanitize(const std::string &str);
276 
278 std::string __attribute__((format(printf, 1, 3))) str_snprintf(const char *fmt, size_t len, ...);
279 
281 std::string __attribute__((format(printf, 1, 2))) str_sprintf(const char *fmt, ...);
282 
284 
287 
289 template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
290 optional<T> parse_number(const char *str) {
291  char *end = nullptr;
292  unsigned long value = ::strtoul(str, &end, 10); // NOLINT(google-runtime-int)
293  if (end == str || *end != '\0' || value > std::numeric_limits<T>::max())
294  return {};
295  return value;
296 }
298 template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
299 optional<T> parse_number(const std::string &str) {
300  return parse_number<T>(str.c_str());
301 }
303 template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
304 optional<T> parse_number(const char *str) {
305  char *end = nullptr;
306  signed long value = ::strtol(str, &end, 10); // NOLINT(google-runtime-int)
307  if (end == str || *end != '\0' || value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max())
308  return {};
309  return value;
310 }
312 template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
313 optional<T> parse_number(const std::string &str) {
314  return parse_number<T>(str.c_str());
315 }
317 template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0> optional<T> parse_number(const char *str) {
318  char *end = nullptr;
319  float value = ::strtof(str, &end);
320  if (end == str || *end != '\0' || value == HUGE_VALF)
321  return {};
322  return value;
323 }
325 template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0>
326 optional<T> parse_number(const std::string &str) {
327  return parse_number<T>(str.c_str());
328 }
329 
341 size_t parse_hex(const char *str, size_t len, uint8_t *data, size_t count);
343 inline bool parse_hex(const char *str, uint8_t *data, size_t count) {
344  return parse_hex(str, strlen(str), data, count) == 2 * count;
345 }
347 inline bool parse_hex(const std::string &str, uint8_t *data, size_t count) {
348  return parse_hex(str.c_str(), str.length(), data, count) == 2 * count;
349 }
351 inline bool parse_hex(const char *str, std::vector<uint8_t> &data, size_t count) {
352  data.resize(count);
353  return parse_hex(str, strlen(str), data.data(), count) == 2 * count;
354 }
356 inline bool parse_hex(const std::string &str, std::vector<uint8_t> &data, size_t count) {
357  data.resize(count);
358  return parse_hex(str.c_str(), str.length(), data.data(), count) == 2 * count;
359 }
365 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
366 optional<T> parse_hex(const char *str, size_t len) {
367  T val = 0;
368  if (len > 2 * sizeof(T) || parse_hex(str, len, reinterpret_cast<uint8_t *>(&val), sizeof(T)) == 0)
369  return {};
370  return convert_big_endian(val);
371 }
373 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> optional<T> parse_hex(const char *str) {
374  return parse_hex<T>(str, strlen(str));
375 }
377 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> optional<T> parse_hex(const std::string &str) {
378  return parse_hex<T>(str.c_str(), str.length());
379 }
380 
382 std::string format_hex(const uint8_t *data, size_t length);
384 std::string format_hex(const std::vector<uint8_t> &data);
386 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex(T val) {
387  val = convert_big_endian(val);
388  return format_hex(reinterpret_cast<uint8_t *>(&val), sizeof(T));
389 }
390 
392 std::string format_hex_pretty(const uint8_t *data, size_t length);
394 std::string format_hex_pretty(const uint16_t *data, size_t length);
396 std::string format_hex_pretty(const std::vector<uint8_t> &data);
398 std::string format_hex_pretty(const std::vector<uint16_t> &data);
400 template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex_pretty(T val) {
401  val = convert_big_endian(val);
402  return format_hex_pretty(reinterpret_cast<uint8_t *>(&val), sizeof(T));
403 }
404 
411 };
413 ParseOnOffState parse_on_off(const char *str, const char *on = nullptr, const char *off = nullptr);
414 
416 std::string value_accuracy_to_string(float value, int8_t accuracy_decimals);
417 
419 
422 
424 float gamma_correct(float value, float gamma);
426 float gamma_uncorrect(float value, float gamma);
427 
429 void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value);
431 void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green, float &blue);
432 
434 
437 
439 constexpr float celsius_to_fahrenheit(float value) { return value * 1.8f + 32.0f; }
441 constexpr float fahrenheit_to_celsius(float value) { return (value - 32.0f) / 1.8f; }
442 
444 
447 
448 template<typename... X> class CallbackManager;
449 
454 template<typename... Ts> class CallbackManager<void(Ts...)> {
455  public:
457  void add(std::function<void(Ts...)> &&callback) { this->callbacks_.push_back(std::move(callback)); }
458 
460  void call(Ts... args) {
461  for (auto &cb : this->callbacks_)
462  cb(args...);
463  }
464 
466  void operator()(Ts... args) { call(args...); }
467 
468  protected:
469  std::vector<std::function<void(Ts...)>> callbacks_;
470 };
471 
473 template<typename T> class Deduplicator {
474  public:
476  bool next(T value) {
477  if (this->has_value_) {
478  if (this->last_value_ == value)
479  return false;
480  }
481  this->has_value_ = true;
482  this->last_value_ = value;
483  return true;
484  }
486  bool has_value() const { return this->has_value_; }
487 
488  protected:
489  bool has_value_{false};
490  T last_value_{};
491 };
492 
494 template<typename T> class Parented {
495  public:
496  Parented() {}
497  Parented(T *parent) : parent_(parent) {}
498 
500  T *get_parent() const { return parent_; }
502  void set_parent(T *parent) { parent_ = parent; }
503 
504  protected:
505  T *parent_{nullptr};
506 };
507 
509 
512 
534  public:
535  InterruptLock();
536  ~InterruptLock();
537 
538  protected:
539 #ifdef USE_ESP8266
540  uint32_t xt_state_;
541 #endif
542 };
543 
550  public:
552  void start();
554  void stop();
555 
557  static bool is_high_frequency();
558 
559  protected:
560  bool started_{false};
561  static uint8_t num_requests; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
562 };
563 
565 void get_mac_address_raw(uint8_t *mac);
566 
568 std::string get_mac_address();
569 
571 std::string get_mac_address_pretty();
572 
573 #ifdef USE_ESP32
574 void set_mac_address(uint8_t *mac);
576 #endif
577 
579 void delay_microseconds_safe(uint32_t us);
580 
582 
585 
591 template<class T> class ExternalRAMAllocator {
592  public:
593  using value_type = T;
594 
595  enum Flags {
596  NONE = 0,
597  REFUSE_INTERNAL = 1 << 0,
598  ALLOW_FAILURE = 1 << 1,
599  };
600 
601  ExternalRAMAllocator() = default;
602  ExternalRAMAllocator(Flags flags) : flags_{flags} {}
603  template<class U> constexpr ExternalRAMAllocator(const ExternalRAMAllocator<U> &other) : flags_{other.flags_} {}
604 
605  T *allocate(size_t n) {
606  size_t size = n * sizeof(T);
607  T *ptr = nullptr;
608 #ifdef USE_ESP32
609  ptr = static_cast<T *>(heap_caps_malloc(size, MALLOC_CAP_SPIRAM));
610 #endif
611  if (ptr == nullptr && (this->flags_ & Flags::REFUSE_INTERNAL) == 0)
612  ptr = static_cast<T *>(malloc(size)); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
613  if (ptr == nullptr && (this->flags_ & Flags::ALLOW_FAILURE) == 0)
614  abort();
615  return ptr;
616  }
617 
618  void deallocate(T *p, size_t n) {
619  free(p); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
620  }
621 
622  private:
623  Flags flags_{Flags::NONE};
624 };
625 
627 
630 
635 template<typename T, enable_if_t<!std::is_pointer<T>::value, int> = 0> T id(T value) { return value; }
640 template<typename T, enable_if_t<std::is_pointer<T *>::value, int> = 0> T &id(T *value) { return *value; }
641 
643 
646 
647 ESPDEPRECATED("hexencode() is deprecated, use format_hex_pretty() instead.", "2022.1")
648 inline std::string hexencode(const uint8_t *data, uint32_t len) { return format_hex_pretty(data, len); }
649 
650 template<typename T>
651 ESPDEPRECATED("hexencode() is deprecated, use format_hex_pretty() instead.", "2022.1")
652 std::string hexencode(const T &data) {
653  return hexencode(data.data(), data.size());
654 }
655 
657 
658 } // 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:303
std::string str_snake_case(const std::string &str)
Convert the string to snake case (lowercase with underscores).
Definition: helpers.cpp:122
std::string str_truncate(const std::string &str, size_t length)
Truncate a string to a specific length.
Definition: helpers.cpp:104
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:250
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:199
std::string str_upper_case(const std::string &str)
Convert the string to upper case.
Definition: helpers.cpp:121
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:187
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:168
bool next(T value)
Feeds the next item in the series to the deduplicator and returns whether this is a duplicate...
Definition: helpers.h:476
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition: helpers.cpp:74
constexpr ExternalRAMAllocator(const ExternalRAMAllocator< U > &other)
Definition: helpers.h:603
std::string str_until(const char *str, char ch)
Extract the part of the string until either the first occurence of the specified character, or the end (requires str to be null-terminated).
Definition: helpers.cpp:107
uint8_t crc8(uint8_t *data, uint8_t len)
Calculate a CRC-8 checksum of data with size len.
Definition: helpers.cpp:50
Helper class to request loop() to be called as fast as possible.
Definition: helpers.h:549
typename std::enable_if< B, T >::type enable_if_t
Definition: helpers.h:77
An STL allocator that uses SPI RAM.
Definition: helpers.h:591
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:172
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:256
void deallocate(T *p, size_t n)
Definition: helpers.h:618
STL namespace.
T id(T value)
Helper function to make id(var) known from lambdas work in custom components.
Definition: helpers.h:635
std::vector< std::function< void(Ts...)> > callbacks_
Definition: helpers.h:469
float lerp(float completion, float start, float end)
Linearly interpolate between start and end by completion (between 0 and 1).
Definition: helpers.cpp:49
void set_parent(T *parent)
Set the parent of this object.
Definition: helpers.h:502
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:400
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
Definition: helpers.cpp:84
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:183
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: helpers.h:84
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:235
void call(Ts... args)
Call all callbacks in this manager.
Definition: helpers.h:460
uint8_t reverse_bits(uint8_t x)
Reverse the order of 8 bits.
Definition: helpers.h:208
uint8_t m
Definition: bl0939.h:20
ParseOnOffState
Return values for parse_on_off().
Definition: helpers.h:406
float gamma_correct(float value, float gamma)
Applies gamma correction of gamma to value.
Definition: helpers.cpp:263
bool str_startswith(const std::string &str, const std::string &start)
Check whether a string starts with a value.
Definition: helpers.cpp:100
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition: helpers.h:439
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:290
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:280
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
Definition: helpers.cpp:120
std::string str_sprintf(const char *fmt,...)
Definition: helpers.cpp:150
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:198
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:225
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Definition: helpers.cpp:386
uint8_t flags
Definition: bedjet_base.h:134
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
uint8_t type
ExternalRAMAllocator(Flags flags)
Definition: helpers.h:602
bool has_value() const
Returns whether this deduplicator has processed any items so far.
Definition: helpers.h:486
bool str_endswith(const std::string &str, const std::string &end)
Check whether a string ends with a value.
Definition: helpers.cpp:101
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:168
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
Definition: helpers.cpp:397
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:145
Parented(T *parent)
Definition: helpers.h:497
T * get_parent() const
Get the parent of this object.
Definition: helpers.h:500
Helper class to disable interrupts.
Definition: helpers.h:533
std::string str_sanitize(const std::string &str)
Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores...
Definition: helpers.cpp:129
std::string to_string(int value)
Definition: helpers.cpp:36
std::string size_t len
Definition: helpers.h:278
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition: helpers.cpp:65
constexpr14 T byteswap(T n)
Definition: helpers.h:121
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition: helpers.h:113
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:234
Helper class to deduplicate items in a series of values.
Definition: helpers.h:473
Definition: a4988.cpp:4
void operator()(Ts... args)
Call all callbacks in this manager.
Definition: helpers.h:466
num_t cb(num_t x)
Definition: sun.cpp:31
std::vector< uint8_t > bytes
Definition: sml_parser.h:12
uint32_t val
Definition: datatypes.h:85
std::unique_ptr< T > make_unique(Args &&...args)
Definition: helpers.h:68
std::string get_mac_address_pretty()
Get the device MAC address as a string, in colon-separated uppercase hex notation.
Definition: helpers.cpp:391
void add(std::function< void(Ts...)> &&callback)
Add a callback to the list.
Definition: helpers.h:457
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition: helpers.h:441
std::string str_snprintf(const char *fmt, size_t len,...)
Definition: helpers.cpp:136
float random_float()
Return a random float between 0 and 1.
Definition: helpers.cpp:83
Helper class to easily give an object a parent of type T.
Definition: helpers.h:494
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition: helpers.cpp:97
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:371
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition: helpers.h:87
float gamma_uncorrect(float value, float gamma)
Reverts gamma correction of gamma to value.
Definition: helpers.cpp:271