|
|
@@ -55,7 +55,28 @@ uint32_t random(uint32_t howbig); |
|
|
|
int32_t random(int32_t howsmall, int32_t howbig); |
|
|
|
void randomSeed(uint32_t newseed); |
|
|
|
void srandom(uint32_t newseed); |
|
|
|
long map(long, long, long, long, long); |
|
|
|
|
|
|
|
#include <type_traits> |
|
|
|
// when the input number is an integer type, do all math as 32 bit signed long |
|
|
|
template <class T, class A, class B, class C, class D> |
|
|
|
T map(T _x, A _in_min, B _in_max, C _out_min, D _out_max, typename std::enable_if<std::is_integral<T>::value >::type* = 0) |
|
|
|
{ |
|
|
|
long x = _x, in_min = _in_min, in_max = _in_max, out_min = _out_min, out_max = _out_max; |
|
|
|
// Arduino's traditional algorithm |
|
|
|
//return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
|
|
|
// st42's suggestion: https://github.com/arduino/Arduino/issues/2466#issuecomment-69873889 |
|
|
|
if ((in_max - in_min) > (out_max - out_min)) { |
|
|
|
return (x - in_min) * (out_max - out_min+1) / (in_max - in_min+1) + out_min; |
|
|
|
} else { |
|
|
|
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
|
|
|
} |
|
|
|
} |
|
|
|
// when the input is a float or double, do all math using the input's type |
|
|
|
template <class T, class A, class B, class C, class D> |
|
|
|
T map(T x, A in_min, B in_max, C out_min, D out_max, typename std::enable_if<std::is_floating_point<T>::value >::type* = 0) |
|
|
|
{ |
|
|
|
return (x - (T)in_min) * ((T)out_max - (T)out_min) / ((T)in_max - (T)in_min) + (T)out_min; |
|
|
|
} |
|
|
|
|
|
|
|
#include "pins_arduino.h" |
|
|
|
|