2015-02-14 05:11:43 -08:00
# ifndef MATH_H
# define MATH_H
2013-09-23 05:23:34 -07:00
2017-06-19 05:02:40 -07:00
int fastMap1023toX ( int , int ) ;
2017-01-29 22:10:17 -08:00
unsigned long percentage ( byte , unsigned long ) ;
2018-07-19 00:35:35 -07:00
inline long powint ( int , unsigned int ) ;
int divs100 ( long ) ;
2018-07-19 06:26:31 -07:00
unsigned long divu100 ( unsigned long ) ;
2015-02-24 18:13:35 -08:00
2017-08-20 18:51:05 -07:00
# define DIV_ROUND_CLOSEST(n, d) ((((n) < 0) ^ ((d) < 0)) ? (((n) - (d) / 2) / (d)) : (((n) + (d) / 2) / (d)))
//This is a dedicated function that specifically handles the case of mapping 0-1023 values into a 0 to X range
//This is a common case because it means converting from a standard 10-bit analog input to a byte or 10-bit analog into 0-511 (Eg the temperature readings)
2017-09-02 09:26:41 -07:00
# if defined(_VARIANT_ARDUINO_STM32_) //libmaple
# define fastMap1023toX(x, out_max) ( ((unsigned long)x * out_max) >> 12)
//This is a new version that allows for out_min
# define fastMap10Bit(x, out_min, out_max) ( ( ((unsigned long)x * (out_max-out_min)) >> 12 ) + out_min)
# else
# define fastMap1023toX(x, out_max) ( ((unsigned long)x * out_max) >> 10)
//This is a new version that allows for out_min
# define fastMap10Bit(x, out_min, out_max) ( ( ((unsigned long)x * (out_max-out_min)) >> 10 ) + out_min)
# endif
2017-08-16 20:51:12 -07:00
2016-10-25 16:41:23 -07:00
# endif