17#define SQR(x) ((x) * (x))
19#define POW3(x) ((x) * (x) * (x))
20#define POW4(x) (POW2(x) * POW2(x))
21#define POW7(x) (POW3(x) * POW3(x) * (x))
22#define FADE16(x) scale16(x, x)
23#define FADE8(x) scale8(x, x)
28template<
typename N>
static constexpr N
min(
const N a,
const N b)
31 assert(not std::isnan(a) &&
"invalid param a");
32 assert(not std::isnan(b) &&
"invalid param b");
37template<
typename N>
static constexpr N
max(
const N a,
const N b)
40 assert(not std::isnan(a) &&
"invalid param a");
41 assert(not std::isnan(b) &&
"invalid param b");
46template<
typename N>
static constexpr N
abs(
const N a) {
return std::abs(N(a)); }
57template<
typename T>
static constexpr T
lmpd_constrain(
const T& a,
const T& mini,
const T& maxi)
60 assert(
static_cast<float>(mini) <=
static_cast<float>(maxi) &&
"invalid parameters");
63 return std::isnan(a) ?
static_cast<T
>(mini) :
65 (
static_cast<float>(a) <=
static_cast<float>(mini)) ?
static_cast<T
>(mini) :
66 (
static_cast<float>(a) >=
static_cast<float>(maxi)) ?
static_cast<T
>(maxi) :
79template<
typename T =
float>
80static inline T
lmpd_map(
float x,
const float in_min,
const float in_max,
const float out_min,
const float out_max)
82 using numeric_limits_T = std::numeric_limits<T>;
83 assert(not(std::isnan(in_min) or std::isinf(in_min)) &&
"in_min invalid");
84 assert(not(std::isnan(in_max) or std::isinf(in_max)) &&
"in_max invalid");
85 assert(out_min >= numeric_limits_T::lowest() && out_min <= numeric_limits_T::max() &&
"out_min invalid");
86 assert(not(std::isnan(out_min) or std::isinf(out_min)) &&
"out_min invalid");
87 assert(out_max >= numeric_limits_T::lowest() && out_max <= numeric_limits_T::max() &&
"out_max invalid");
88 assert(not(std::isnan(out_max) or std::isinf(out_max)) &&
"out_max invalid");
92 if (x > 0 && std::isinf(x))
96 const float res = (out_max - out_min) * (x - in_min) / (in_max - in_min) + out_min;
97 if (res > numeric_limits_T::max())
98 return numeric_limits_T::max();
99 if (res < numeric_limits_T::lowest())
100 return numeric_limits_T::lowest();
102 return static_cast<T
>(res);
109 assert(not(std::isnan(degrees) or std::isinf(degrees)) &&
"invalid value");
111 return degrees * M_PI / 180.f;
115static constexpr inline float wrap_angle(
const float angle_rad)
118 if (angle_rad >= 0 and angle_rad <
c_TWO_PI)
157uint32_t
get_gradient(
const uint32_t colorStart,
const uint32_t colorEnd,
const float level);
159COLOR color_fade(
COLOR c1, uint8_t amount,
bool video =
false);
162uint32_t hue_to_rgb_sinus(
const uint16_t angle);
174template<
typename T>
static constexpr uint32_t
hash(
const T s,
const uint16_t maxSize = 14,
const uint16_t off = 0)
177 uint32_t hashAcc = 5381;
178 for (uint16_t I = 0; I < maxSize; ++I)
180 if (s[I + off] ==
'\0')
185 hashAcc = ((hashAcc << 5) + hashAcc) ^ s[I + off];
189 return !s[off] ? 5381 : (
hash(s, maxSize, off + 1) * 33) ^ s[off];
194template<
int16_t N>
static constexpr uint32_t
hash(
const char (&s)[N])
196 static_assert((N - 1 <= 14) &&
"Use hash(s, maxSize) to hash strings longer than 14 bytes!");
201constexpr double analogReadToVoltage(
const uint16_t analogVal)
205constexpr uint16_t voltageToAnalogRead(
const float voltage)
uint32_t get_random_complementary_color(const uint32_t color, const float tolerance)
Compute the complementary color of the given color, with a random variation.
Definition: utils.cpp:47
static constexpr uint32_t hash(const T s, const uint16_t maxSize=14, const uint16_t off=0)
Hash input string into a 32-bit unsigned integer.
Definition: utils.h:174
uint32_t get_gradient(const uint32_t colorStart, const uint32_t colorEnd, const float level)
Return the color gradient between colorStart to colorEnd.
Definition: utils.cpp:60
Program scope.
Definition: control_fixed_modes.hpp:12
static constexpr float to_radians(float degrees)
Convert an angle in degrees to an angle in radians.
Definition: utils.h:106
static constexpr float wrap_angle(const float angle_rad)
Wrap an angle in radians between 0 and 2*PI.
Definition: utils.h:115
static const uint32_t ADC_MAX_VALUE
corresponding ADC max value
Definition: constants.h:50
static constexpr float internalReferenceVoltage
internal voltage reference
Definition: constants.h:51
static constexpr N abs(const N a)
absolute of a number
Definition: utils.h:46
static constexpr float c_TWO_PI
2*PI constant
Definition: constants.h:21
static constexpr T lmpd_constrain(const T &a, const T &mini, const T &maxi)
Constrain a number between two numbers.
Definition: utils.h:57
static T lmpd_map(float x, const float in_min, const float in_max, const float out_min, const float out_max)
Linearly interpolate a number between two numbers.
Definition: utils.h:80
static constexpr N max(const N a, const N b)
Maximum of two numbers.
Definition: utils.h:37
static constexpr N min(const N a, const N b)
Minimum of two numbers.
Definition: utils.h:28
Use this to convert color to bytes.
Definition: utils.h:128