From 4f42c289b2f0a92eff42ab2da7ae07344cef8c9e Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Mon, 19 Jun 2017 22:02:40 +1000 Subject: [PATCH] MISRA compliant maths.ino --- speeduino/maths.h | 2 +- speeduino/maths.ino | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/speeduino/maths.h b/speeduino/maths.h index 7daa0e7b..55e154b9 100644 --- a/speeduino/maths.h +++ b/speeduino/maths.h @@ -1,7 +1,7 @@ #ifndef MATH_H #define MATH_H -int fastMap1023toX(unsigned long, int); +int fastMap1023toX(int, int); unsigned long percentage(byte, unsigned long); #endif diff --git a/speeduino/maths.ino b/speeduino/maths.ino index a8d662b4..bb42960a 100644 --- a/speeduino/maths.ino +++ b/speeduino/maths.ino @@ -5,7 +5,12 @@ //Replace the standard arduino map() function to use the div function instead int fastMap(unsigned long x, int in_min, int in_max, int out_min, int out_max) { - return ldiv( ((x - in_min) * (out_max - out_min)) , (in_max - in_min) ).quot + out_min; + unsigned long a = (x - (unsigned long)in_min); + int b = (out_max - out_min); + int c = (in_max - in_min); + int d = (ldiv( (a * (long)b) , (long)c ).quot); + return d + 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; } @@ -13,7 +18,7 @@ int fastMap(unsigned long x, int in_min, int in_max, int out_min, int out_max) //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) //removed ununsed variables, in_min and out_min is aways 0, in_max is aways 1023 -int fastMap1023toX(unsigned long x, int out_max) +int fastMap1023toX(int x, int out_max) { return (x * out_max) >> 10; } @@ -40,7 +45,7 @@ unsigned int divu10(unsigned int n) int divs10(long n) { long q, r, p; - p = n + (n>>31 & 9); + p = n + ( (n>>31) & 9); q = (p >> 1) + (p >> 2); q = q + (q >> 4); q = q + (q >> 8); @@ -94,7 +99,6 @@ inline long powint(int factor, unsigned int exponent) { long product = 1; unsigned int counter = exponent; - while ( (counter--) > 0) - product *= factor; + while ( (counter--) > 0) { product *= factor; } return product; }