Add dedicated 0-1024 mapping function (Super speeeed!)

This commit is contained in:
Josh Stewart 2015-02-25 13:13:35 +11:00
parent a0458b4bb6
commit c3fe8cb36b
1 changed files with 10 additions and 2 deletions

12
math.h
View File

@ -2,9 +2,17 @@
#define MATH_H
//Replace the standard arduino map() function to use the div function instead
int fastMap(int x, int in_min, int in_max, int out_min, int out_max)
int fastMap(unsigned long x, int in_min, int in_max, int out_min, int out_max)
{
return div( ((x - in_min) * (out_max - out_min)) , (in_max - in_min) ).quot + out_min;
return ldiv( ((x - in_min) * (out_max - out_min)) , (in_max - in_min) ).quot + out_min;
//return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
//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)
int fastMap1023toX(unsigned long x, int in_min, int in_max, int out_min, int out_max)
{
return (x * out_max) >> 10;
}
/*