Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
5#ifndef UTILS_H
6#define UTILS_H
7
8#include <cassert>
9#include <cmath>
10#include <cstdint>
11#include <limits>
12
13#include "constants.h"
14
15namespace lampda {
16
17#define SQR(x) ((x) * (x))
18#define POW2(x) SQR(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)
24
25#ifndef Arduino_h
26
28template<typename N> static constexpr N min(const N a, const N b)
29{
30#ifdef LMBD_CPP17
31 assert(not std::isnan(a) && "invalid param a");
32 assert(not std::isnan(b) && "invalid param b");
33#endif
34 return a < b ? a : b;
35}
37template<typename N> static constexpr N max(const N a, const N b)
38{
39#ifdef LMBD_CPP17
40 assert(not std::isnan(a) && "invalid param a");
41 assert(not std::isnan(b) && "invalid param b");
42#endif
43 return a > b ? a : b;
44}
46template<typename N> static constexpr N abs(const N a) { return std::abs(N(a)); }
47
48#endif
49
57template<typename T> static constexpr T lmpd_constrain(const T& a, const T& mini, const T& maxi)
58{
59#ifdef LMBD_CPP17
60 assert(static_cast<float>(mini) <= static_cast<float>(maxi) && "invalid parameters");
61#endif
62 // prevent invalid values
63 return std::isnan(a) ? static_cast<T>(mini) :
64 // constrain the value
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) :
67 static_cast<T>(a);
68}
69
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)
81{
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");
89
90 if (std::isnan(x))
91 return out_min;
92 if (x > 0 && std::isinf(x))
93 return out_max;
94 if (std::isinf(x))
95 return out_min;
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();
101
102 return static_cast<T>(res);
103}
104
106static constexpr inline float to_radians(float degrees)
107{
108#ifdef LMBD_CPP17
109 assert(not(std::isnan(degrees) or std::isinf(degrees)) && "invalid value");
110#endif
111 return degrees * M_PI / 180.f;
112}
113
115static constexpr inline float wrap_angle(const float angle_rad)
116{
117#ifdef LMBD_CPP17
118 if (angle_rad >= 0 and angle_rad < c_TWO_PI)
119 return angle_rad;
120#endif
121 return angle_rad - c_TWO_PI * floor(angle_rad / c_TWO_PI);
122}
123
127union COLOR
128{
129 uint32_t color;
130
131 struct
132 {
133 uint8_t blue;
134 uint8_t green;
135 uint8_t red;
136 uint8_t white;
137 };
138};
139
140namespace utils {
141
149uint32_t get_random_complementary_color(const uint32_t color, const float tolerance);
150
157uint32_t get_gradient(const uint32_t colorStart, const uint32_t colorEnd, const float level);
158
159COLOR color_fade(COLOR c1, uint8_t amount, bool video = false);
160COLOR color_add(COLOR c1, COLOR c2, bool fast = false);
161
162uint32_t hue_to_rgb_sinus(const uint16_t angle);
163
174template<typename T> static constexpr uint32_t hash(const T s, const uint16_t maxSize = 14, const uint16_t off = 0)
175{
176#ifdef LMBD_CPP17
177 uint32_t hashAcc = 5381;
178 for (uint16_t I = 0; I < maxSize; ++I)
179 {
180 if (s[I + off] == '\0')
181 {
182 return hashAcc;
183 }
184
185 hashAcc = ((hashAcc << 5) + hashAcc) ^ s[I + off];
186 }
187 return hashAcc;
188#else
189 return !s[off] ? 5381 : (hash(s, maxSize, off + 1) * 33) ^ s[off];
190#endif
191}
192
194template<int16_t N> static constexpr uint32_t hash(const char (&s)[N])
195{
196 static_assert((N - 1 <= 14) && "Use hash(s, maxSize) to hash strings longer than 14 bytes!");
197 return hash(s, 14);
198}
199
200// Convert a read on an analog pin to a voltage value
201constexpr double analogReadToVoltage(const uint16_t analogVal)
202{
203 return analogVal * internalReferenceVoltage / (float)ADC_MAX_VALUE;
204}
205constexpr uint16_t voltageToAnalogRead(const float voltage)
206{
207 return lmpd_constrain<uint16_t>(voltage, 0, internalReferenceVoltage) * ADC_MAX_VALUE / internalReferenceVoltage;
208}
209
210} // namespace utils
211} // namespace lampda
212
213#endif
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