ESPHome  2024.9.2
Data Structures | Namespaces
helpers.h File Reference

Go to the source code of this file.

Data Structures

struct  esphome::is_trivially_copyable< T >
 
struct  esphome::is_invocable< T, Args >
 
class  esphome::CallbackManager< X >
 
class  esphome::CallbackManager< void(Ts...)>
 Helper class to allow having multiple subscribers to a callback. More...
 
class  esphome::Deduplicator< T >
 Helper class to deduplicate items in a series of values. More...
 
class  esphome::Parented< T >
 Helper class to easily give an object a parent of type T. More...
 
class  esphome::Mutex
 Mutex implementation, with API based on the unavailable std::mutex. More...
 
class  esphome::LockGuard
 Helper class that wraps a mutex with a RAII-style API. More...
 
class  esphome::InterruptLock
 Helper class to disable interrupts. More...
 
class  esphome::HighFrequencyLoopRequester
 Helper class to request loop() to be called as fast as possible. More...
 
class  esphome::ExternalRAMAllocator< T >
 An STL allocator that uses SPI RAM. More...
 

Namespaces

 esphome
 Implementation of SPI Controller mode.
 

Functions

Mathematics
float esphome::lerp (float completion, float start, float end)
 Linearly interpolate between start and end by completion (between 0 and 1). More...
 
template<typename T , typename U >
esphome::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). More...
 
uint8_t esphome::crc8 (const uint8_t *data, uint8_t len)
 Calculate a CRC-8 checksum of data with size len. More...
 
uint16_t esphome::crc16 (const uint8_t *data, uint16_t len, uint16_t crc=0xffff, uint16_t reverse_poly=0xa001, bool refin=false, bool refout=false)
 Calculate a CRC-16 checksum of data with size len. More...
 
uint16_t esphome::crc16be (const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
 
uint32_t esphome::fnv1_hash (const std::string &str)
 Calculate a FNV-1 hash of str. More...
 
uint32_t esphome::random_uint32 ()
 Return a random 32-bit unsigned integer. More...
 
float esphome::random_float ()
 Return a random float between 0 and 1. More...
 
bool esphome::random_bytes (uint8_t *data, size_t len)
 Generate len number of random bytes. More...
 
Bit manipulation
constexpr uint16_t esphome::encode_uint16 (uint8_t msb, uint8_t lsb)
 Encode a 16-bit value given the most and least significant byte. More...
 
constexpr uint32_t esphome::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. More...
 
constexpr uint32_t esphome::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. More...
 
template<typename T , enable_if_t< std::is_unsigned< T >::value, int > = 0>
constexpr14 T esphome::encode_value (const uint8_t *bytes)
 Encode a value from its constituent bytes (from most to least significant) in an array with length sizeof(T). More...
 
template<typename T , enable_if_t< std::is_unsigned< T >::value, int > = 0>
constexpr14 T esphome::encode_value (const std::array< uint8_t, sizeof(T)> bytes)
 Encode a value from its constituent bytes (from most to least significant) in an std::array with length sizeof(T). More...
 
template<typename T , enable_if_t< std::is_unsigned< T >::value, int > = 0>
constexpr14 std::array< uint8_t, sizeof(T)> esphome::decode_value (T val)
 Decode a value into its constituent bytes (from most to least significant). More...
 
uint8_t esphome::reverse_bits (uint8_t x)
 Reverse the order of 8 bits. More...
 
uint16_t esphome::reverse_bits (uint16_t x)
 Reverse the order of 16 bits. More...
 
uint32_t esphome::reverse_bits (uint32_t x)
 Reverse the order of 32 bits. More...
 
template<typename T >
constexpr14 T esphome::convert_big_endian (T val)
 Convert a value between host byte order and big endian (most significant byte first) order. More...
 
template<typename T >
constexpr14 T esphome::convert_little_endian (T val)
 Convert a value between host byte order and little endian (least significant byte first) order. More...
 
Colors
float esphome::gamma_correct (float value, float gamma)
 Applies gamma correction of gamma to value. More...
 
float esphome::gamma_uncorrect (float value, float gamma)
 Reverts gamma correction of gamma to value. More...
 
void esphome::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). More...
 
void esphome::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). More...
 
Units
constexpr float esphome::celsius_to_fahrenheit (float value)
 Convert degrees Celsius to degrees Fahrenheit. More...
 
constexpr float esphome::fahrenheit_to_celsius (float value)
 Convert degrees Fahrenheit to degrees Celsius. More...
 
System APIs
void esphome::get_mac_address_raw (uint8_t *mac)
 Get the device MAC address as raw bytes, written into the provided byte array (6 bytes). More...
 
std::string esphome::get_mac_address ()
 Get the device MAC address as a string, in lowercase hex notation. More...
 
std::string esphome::get_mac_address_pretty ()
 Get the device MAC address as a string, in colon-separated uppercase hex notation. More...
 
void esphome::set_mac_address (uint8_t *mac)
 Set the MAC address to use from the provided byte array (6 bytes). More...
 
void esphome::delay_microseconds_safe (uint32_t us)
 Delay for the given amount of microseconds, possibly yielding to other processes during the wait. More...
 
Internal functions
template<typename T , enable_if_t<!std::is_pointer< T >::value, int > = 0>
esphome::id (T value)
 Helper function to make id(var) known from lambdas work in custom components. More...
 
template<typename T , enable_if_t< std::is_pointer< T *>::value, int > = 0>
T & esphome::id (T *value)
 Helper function to make id(var) known from lambdas work in custom components. More...
 
Deprecated functions
 esphome::ESPDEPRECATED ("hexencode() is deprecated, use format_hex_pretty() instead.", "2022.1") inline std
 

STL backports

template<bool B, class T = void>
using esphome::enable_if_t = typename std::enable_if< B, T >::type
 
std::string esphome::to_string (int value)
 
std::string esphome::to_string (long value)
 
std::string esphome::to_string (long long value)
 
std::string esphome::to_string (unsigned value)
 
std::string esphome::to_string (unsigned long value)
 
std::string esphome::to_string (unsigned long long value)
 
std::string esphome::to_string (float value)
 
std::string esphome::to_string (double value)
 
std::string esphome::to_string (long double value)
 
template<typename T , typename... Args>
std::unique_ptr< T > esphome::make_unique (Args &&...args)
 
template<typename T , typename Compare >
constexpr const T & esphome::clamp (const T &v, const T &lo, const T &hi, Compare comp)
 
template<typename T >
constexpr const T & esphome::clamp (const T &v, const T &lo, const T &hi)
 
template<typename To , typename From , enable_if_t< sizeof(To)==sizeof(From) &&is_trivially_copyable< From >::value &&is_trivially_copyable< To >::value, int > = 0>
To esphome::bit_cast (const From &src)
 Convert data between types, without aliasing issues or undefined behaviour. More...
 
template<typename T >
constexpr14 T esphome::byteswap (T n)
 
template<>
constexpr14 uint8_t esphome::byteswap (uint8_t n)
 
template<>
constexpr14 uint16_t esphome::byteswap (uint16_t n)
 
template<>
constexpr14 uint32_t esphome::byteswap (uint32_t n)
 
template<>
constexpr14 uint64_t esphome::byteswap (uint64_t n)
 
template<>
constexpr14 int8_t esphome::byteswap (int8_t n)
 
template<>
constexpr14 int16_t esphome::byteswap (int16_t n)
 
template<>
constexpr14 int32_t esphome::byteswap (int32_t n)
 
template<>
constexpr14 int64_t esphome::byteswap (int64_t n)
 

Strings

std::string size_t esphome::len
 
bool esphome::str_equals_case_insensitive (const std::string &a, const std::string &b)
 Compare strings for equality in case-insensitive manner. More...
 
bool esphome::str_startswith (const std::string &str, const std::string &start)
 Check whether a string starts with a value. More...
 
bool esphome::str_endswith (const std::string &str, const std::string &end)
 Check whether a string ends with a value. More...
 
std::string esphome::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 stringifiable types). More...
 
std::string esphome::str_truncate (const std::string &str, size_t length)
 Truncate a string to a specific length. More...
 
std::string esphome::str_until (const char *str, char ch)
 Extract the part of the string until either the first occurrence of the specified character, or the end (requires str to be null-terminated). More...
 
std::string esphome::str_until (const std::string &str, char ch)
 Extract the part of the string until either the first occurrence of the specified character, or the end. More...
 
std::string esphome::str_lower_case (const std::string &str)
 Convert the string to lower case. More...
 
std::string esphome::str_upper_case (const std::string &str)
 Convert the string to upper case. More...
 
std::string esphome::str_snake_case (const std::string &str)
 Convert the string to snake case (lowercase with underscores). More...
 
std::string esphome::str_sanitize (const std::string &str)
 Sanitizes the input string by removing all characters but alphanumerics, dashes and underscores. More...
 
std::string esphome::__attribute__ ((format(printf, 1, 3))) str_snprintf(const char *fmt
 snprintf-like function returning std::string of maximum length len (excluding null terminator). More...
 
std::string size_t std::string esphome::__attribute__ ((format(printf, 1, 2))) str_sprintf(const char *fmt
 sprintf-like function returning std::string. More...
 

Parsing & formatting

enum  esphome::ParseOnOffState { esphome::PARSE_NONE = 0, esphome::PARSE_ON, esphome::PARSE_OFF, esphome::PARSE_TOGGLE }
 Return values for parse_on_off(). More...
 
template<typename T , enable_if_t<(std::is_integral< T >::value &&std::is_unsigned< T >::value), int > = 0>
optional< T > esphome::parse_number (const char *str)
 Parse an unsigned decimal number from a null-terminated string. More...
 
template<typename T , enable_if_t<(std::is_integral< T >::value &&std::is_unsigned< T >::value), int > = 0>
optional< T > esphome::parse_number (const std::string &str)
 Parse an unsigned decimal number. More...
 
size_t esphome::parse_hex (const char *str, size_t len, uint8_t *data, size_t count)
 Parse bytes from a hex-encoded string into a byte array. More...
 
bool esphome::parse_hex (const char *str, uint8_t *data, size_t count)
 Parse count bytes from the hex-encoded string str of at least 2*count characters into array data. More...
 
bool esphome::parse_hex (const std::string &str, uint8_t *data, size_t count)
 Parse count bytes from the hex-encoded string str of at least 2*count characters into array data. More...
 
bool esphome::parse_hex (const char *str, std::vector< uint8_t > &data, size_t count)
 Parse count bytes from the hex-encoded string str of at least 2*count characters into vector data. More...
 
bool esphome::parse_hex (const std::string &str, std::vector< uint8_t > &data, size_t count)
 Parse count bytes from the hex-encoded string str of at least 2*count characters into vector data. More...
 
template<typename T , enable_if_t< std::is_unsigned< T >::value, int > = 0>
optional< T > esphome::parse_hex (const char *str, size_t len)
 Parse a hex-encoded string into an unsigned integer. More...
 
template<typename T , enable_if_t< std::is_unsigned< T >::value, int > = 0>
optional< T > esphome::parse_hex (const char *str)
 Parse a hex-encoded null-terminated string (starting with the most significant byte) into an unsigned integer. More...
 
template<typename T , enable_if_t< std::is_unsigned< T >::value, int > = 0>
optional< T > esphome::parse_hex (const std::string &str)
 Parse a hex-encoded null-terminated string (starting with the most significant byte) into an unsigned integer. More...
 
std::string esphome::format_hex (const uint8_t *data, size_t length)
 Format the byte array data of length len in lowercased hex. More...
 
std::string esphome::format_hex (const std::vector< uint8_t > &data)
 Format the vector data in lowercased hex. More...
 
template<typename T , enable_if_t< std::is_unsigned< T >::value, int > = 0>
std::string esphome::format_hex (T val)
 Format an unsigned integer in lowercased hex, starting with the most significant byte. More...
 
template<std::size_t N>
std::string esphome::format_hex (const std::array< uint8_t, N > &data)
 
std::string esphome::format_hex_pretty (const uint8_t *data, size_t length)
 Format the byte array data of length len in pretty-printed, human-readable hex. More...
 
std::string esphome::format_hex_pretty (const uint16_t *data, size_t length)
 Format the word array data of length len in pretty-printed, human-readable hex. More...
 
std::string esphome::format_hex_pretty (const std::vector< uint8_t > &data)
 Format the vector data in pretty-printed, human-readable hex. More...
 
std::string esphome::format_hex_pretty (const std::vector< uint16_t > &data)
 Format the vector data in pretty-printed, human-readable hex. More...
 
template<typename T , enable_if_t< std::is_unsigned< T >::value, int > = 0>
std::string esphome::format_hex_pretty (T val)
 Format an unsigned integer in pretty-printed, human-readable hex, starting with the most significant byte. More...
 
ParseOnOffState esphome::parse_on_off (const char *str, const char *on=nullptr, const char *off=nullptr)
 Parse a string that contains either on, off or toggle. More...
 
std::string esphome::value_accuracy_to_string (float value, int8_t accuracy_decimals)
 Create a string from a value and an accuracy in decimals. More...
 
int8_t esphome::step_to_accuracy_decimals (float step)
 Derive accuracy in decimals from an increment step. More...
 
std::string esphome::base64_encode (const uint8_t *buf, size_t buf_len)
 
std::string esphome::base64_encode (const std::vector< uint8_t > &buf)
 
std::vector< uint8_t > esphome::base64_decode (const std::string &encoded_string)
 
size_t esphome::base64_decode (const std::string &encoded_string, uint8_t *buf, size_t buf_len)