|
1 | 1 | #pragma once
|
| 2 | +#include "ArduinoDefines.h" |
2 | 3 | #include <math.h>
|
3 | 4 |
|
4 |
| -#define constrain(x,l,h) ((x)<(l)?(l):((x)>(h)?(h):(x))) |
5 |
| -#define map(x,inMin,inMax,outMin,outMax) (((x)-(inMin))*((outMax)-(outMin))/((inMax)-(inMin))+outMin) |
| 5 | +#ifdef __cplusplus |
6 | 6 |
|
7 |
| -#define sq(x) ((x)*(x)) |
| 7 | +template <class Amt, class Low, class High> |
| 8 | +auto constrain(const Amt &amt, const Low &low, const High &high) |
| 9 | + -> decltype(amt < low ? low : (amt > high ? high : amt)) { |
| 10 | + return (amt < low ? low : (amt > high ? high : amt)); |
| 11 | +} |
8 | 12 |
|
9 |
| -#define radians(deg) ((deg)*DEG_TO_RAD) |
10 |
| -#define degrees(rad) ((rad)*RAD_TO_DEG) |
| 13 | +template <class X, class InMin, class InMax, class OutMin, class OutMax> |
| 14 | +auto map(const X &x, const InMin &inMin, const InMax &inMax, |
| 15 | + const OutMin &outMin, const OutMax &outMax) |
| 16 | + -> decltype((x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin) { |
| 17 | + return (x - inMin) * (outMax - outMin) / (inMax - inMin) + outMin; |
| 18 | +} |
| 19 | + |
| 20 | +template <class T> auto radians(const T °) -> decltype(deg * DEG_TO_RAD) { |
| 21 | + return deg * DEG_TO_RAD; |
| 22 | +} |
| 23 | + |
| 24 | +template <class T> auto degrees(const T &rad) -> decltype(rad * RAD_TO_DEG) { |
| 25 | + return rad * RAD_TO_DEG; |
| 26 | +} |
| 27 | + |
| 28 | +template <class T> auto sq(const T &x) -> decltype(x * x) { return x * x; } |
| 29 | + |
| 30 | +template <class T> auto abs(const T &x) -> decltype(x > 0 ? x : -x) { |
| 31 | + return x > 0 ? x : -x; |
| 32 | +} |
| 33 | + |
| 34 | +template <class T, class L> |
| 35 | +auto min(const T &a, const L &b) -> decltype((b < a) ? b : a) { |
| 36 | + return (b < a) ? b : a; |
| 37 | +} |
| 38 | + |
| 39 | +template <class T, class L> |
| 40 | +auto max(const T &a, const L &b) -> decltype((b < a) ? b : a) { |
| 41 | + return (a < b) ? b : a; |
| 42 | +} |
| 43 | + |
| 44 | +#else // __cplusplus |
| 45 | + |
| 46 | +#ifdef constrain |
| 47 | +#undef constrain |
| 48 | +#endif |
| 49 | +#define constrain(amt, low, high) \ |
| 50 | + ({ \ |
| 51 | + __typeof__(amt) _amt = (amt); \ |
| 52 | + __typeof__(low) _low = (low); \ |
| 53 | + __typeof__(high) _high = (high); \ |
| 54 | + (amt < low ? low : (amt > high ? high : amt)); \ |
| 55 | + }) |
| 56 | + |
| 57 | +#ifdef map |
| 58 | +#undef map |
| 59 | +#endif |
| 60 | +#define map(x, inMin, inMax, outMin, outMax) \ |
| 61 | + ({ \ |
| 62 | + __typeof__(x) _x = (x); \ |
| 63 | + __typeof__(inMin) _inMin = (inMin); \ |
| 64 | + __typeof__(inMax) _inMax = (inMax); \ |
| 65 | + __typeof__(outMin) _outMin = (outMin); \ |
| 66 | + __typeof__(outMax) _outMax = (outMax); \ |
| 67 | + (_x - _inMin) * (_outMax - _outMin) / (_inMax - _inMin) + _outMin; \ |
| 68 | + }) |
| 69 | + |
| 70 | +#ifdef radians |
| 71 | +#undef radians |
| 72 | +#endif |
| 73 | +#define radians(deg) \ |
| 74 | + ({ \ |
| 75 | + __typeof__(deg) _deg = (deg); \ |
| 76 | + _deg *DEG_TO_RAD; \ |
| 77 | + }) |
| 78 | + |
| 79 | +#ifdef degrees |
| 80 | +#undef degrees |
| 81 | +#endif |
| 82 | +#define degrees(rad) \ |
| 83 | + ({ \ |
| 84 | + __typeof__(rad) _rad = (rad); \ |
| 85 | + _rad *RAD_TO_DEG; \ |
| 86 | + }) |
| 87 | + |
| 88 | +#ifdef sq |
| 89 | +#undef sq |
| 90 | +#endif |
| 91 | +#define sq(x) \ |
| 92 | + ({ \ |
| 93 | + __typeof__(x) _x = (x); \ |
| 94 | + _x *_x; \ |
| 95 | + }) |
11 | 96 |
|
12 | 97 | #ifdef abs
|
13 | 98 | #undef abs
|
14 | 99 | #endif
|
15 |
| -#define abs(x) ((x)>0?(x):-(x)) |
| 100 | +#define abs(x) \ |
| 101 | + ({ \ |
| 102 | + __typeof__(x) _x = (x); \ |
| 103 | + _x > 0 ? _x : -_x; \ |
| 104 | + }) |
| 105 | + |
| 106 | +#ifdef min |
| 107 | +#undef min |
| 108 | +#endif |
| 109 | +#define min(a, b) \ |
| 110 | + ({ \ |
| 111 | + __typeof__(a) _a = (a); \ |
| 112 | + __typeof__(b) _b = (b); \ |
| 113 | + _a < _b ? _a : _b; \ |
| 114 | + }) |
16 | 115 |
|
17 | 116 | #ifdef max
|
18 | 117 | #undef max
|
19 | 118 | #endif
|
20 |
| -#define max(a,b) ((a)>(b)?(a):(b)) |
| 119 | +#define max(a, b) \ |
| 120 | + ({ \ |
| 121 | + __typeof__(a) _a = (a); \ |
| 122 | + __typeof__(b) _b = (b); \ |
| 123 | + _a > _b ? _a : _b; \ |
| 124 | + }) |
21 | 125 |
|
22 |
| -#ifdef min |
23 |
| -#undef min |
24 | 126 | #endif
|
25 |
| -#define min(a,b) ((a)<(b)?(a):(b)) |
26 |
| - |
|
0 commit comments