| void randomSeed(uint32_t newseed); | void randomSeed(uint32_t newseed); | ||||
| void srandom(uint32_t newseed); | void srandom(uint32_t newseed); | ||||
| #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" | #include "pins_arduino.h" | ||||
| #endif // __cplusplus | #endif // __cplusplus |
| #include "core_id.h" | #include "core_id.h" | ||||
| #include "core_pins.h" | #include "core_pins.h" | ||||
| // type_traits interferes with min() and other defines | |||||
| // include it early, so we can define these later | |||||
| // for Arduino compatibility | |||||
| #ifdef __cplusplus | |||||
| #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; | |||||
| } | |||||
| #endif | |||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||
| extern "C"{ | extern "C"{ | ||||
| #endif | #endif |