diff --git a/libraries/FastMap/FastMap.cpp b/libraries/FastMap/FastMap.cpp index 49507176..0c4529f7 100644 --- a/libraries/FastMap/FastMap.cpp +++ b/libraries/FastMap/FastMap.cpp @@ -1,17 +1,18 @@ // // FILE: FastMap.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.06 -// PURPOSE: class implementation of map function - library for Arduino +// VERSION: 0.1.7 +// PURPOSE: class with fast map function - library for Arduino // URL: http://forum.arduino.cc/index.php?topic=276194 // // HISTORY: -// 0.1.06 2015-03-08 replaced double by double (support ARM) +// 0.1.7 2017-04-28 cleaned up, get examples working again +// 0.1.06 2015-03-08 replaced float by double (support ARM) // 0.1.05 2014-11-02 stripped of bit mask experimental code // 0.1.04 add back() - the inverse map // tested with bit mask for constrain code (Perfomance was killed) // 0.1.03 proper name -// 0.1.02 squized the code (first public version) +// 0.1.02 sqeezed the code (first public version) // 0.1.01 refactor // 0.1.00 initial version // @@ -22,6 +23,11 @@ // // PUBLIC // +FastMap::FastMap() +{ + init(0, 1, 0, 1); +} + void FastMap::init(double in_min, double in_max, double out_min, double out_max) { _in_min = in_min; diff --git a/libraries/FastMap/FastMap.h b/libraries/FastMap/FastMap.h index 07374079..75a6f17f 100644 --- a/libraries/FastMap/FastMap.h +++ b/libraries/FastMap/FastMap.h @@ -1,7 +1,7 @@ // // FILE: FastMap.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.06 +// VERSION: 0.1.7 // PURPOSE: class with fast map function - library for Arduino // URL: http://forum.arduino.cc/index.php?topic=276194 // @@ -18,19 +18,21 @@ #include #endif -#define FASTMAP_LIB_VERSION (F("0.1.06")) +#define FASTMAP_LIB_VERSION (F("0.1.7")) class FastMap { public: - void init(double in_min, double in_max, double out_min, double out_max); + FastMap(); - double inline map(double value) { return _base + value * _factor; } - double inline back(double value) { return _backbase + value * _backfactor; } + void init(const double in_min, const double in_max, const double out_min, const double out_max); - double constrainedMap(double value); - double lowerConstrainedMap(double value); - double upperConstrainedMap(double value); + double inline map (const double value) { return _base + value * _factor; } + double inline back (const double value) { return _backbase + value * _backfactor; } + + double constrainedMap(const double value); + double lowerConstrainedMap(const double value); + double upperConstrainedMap(const double value); private: double _in_min, _in_max, _out_min, _out_max; diff --git a/libraries/FastMap/examples/fastMapDemo3/fastMapDemo3.ino b/libraries/FastMap/examples/fastMapDemo3/fastMapDemo3.ino index 4f08cd6e..d4a11d10 100644 --- a/libraries/FastMap/examples/fastMapDemo3/fastMapDemo3.ino +++ b/libraries/FastMap/examples/fastMapDemo3/fastMapDemo3.ino @@ -1,6 +1,6 @@ // FILE: fastMapDemo3.ino // AUTHOR: Rob Tillaart -// VERSION: 0.1.00 +// VERSION: 0.1.1 // PURPOSE: demo of FastMap class ==> a faster map function // DATE: 2014-11-02 // URL: http://forum.arduino.cc/index.php?topic=276194 @@ -31,13 +31,13 @@ void setup() Serial.print(char(176)); Serial.println('C'); - f = CtoF.map(163, CONSTRAIN_UPPER); + f = CtoF.upperConstrainedMap(163); Serial.print(f); Serial.print(char(176)); Serial.println('F'); - c = CtoF.back(f, CONSTRAIN_BOTH); - Serial.print(c); + f = CtoF.lowerConstrainedMap(163); + Serial.print(f); Serial.print(char(176)); Serial.println('C'); @@ -49,4 +49,3 @@ void loop() // // END OF FILE // - diff --git a/libraries/FastMap/examples/fastMapDemo4/fastMapDemo4.ino b/libraries/FastMap/examples/fastMapDemo4/fastMapDemo4.ino new file mode 100644 index 00000000..e83f0d01 --- /dev/null +++ b/libraries/FastMap/examples/fastMapDemo4/fastMapDemo4.ino @@ -0,0 +1,135 @@ +// +// FILE: fastMapDemo4.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.00 +// PURPOSE: demo of FastMap class ==> a faster map function +// DATE: 2014-11-02 +// URL: +// +// Released to the public domain +// + +// works only with 0.1.04 + +#include "FastMap.h" + +uint32_t start; +uint32_t stop; +uint32_t reference; + +volatile long zz = 3000, yy = 20000; +volatile float x; + +FastMap mapper; + +void setup() +{ + Serial.begin(115200); + Serial.print("Start fastMapDemo\nlib version: "); + Serial.println(FASTMAP_LIB_VERSION); + Serial.println(); + + // Get a non optimizable value; + int z = analogRead(A0); + + // REFERENCE + start = micros(); + for (int i = 0; i < 10000; i++) + { + x = map(z, 0, 1023, yy, zz); + } + stop = micros(); + reference = stop-start; + Serial.print("map():\t"); + Serial.println(reference); + Serial.print(z); + Serial.print(" -> "); + Serial.println(x); + Serial.println(); + + // FASTMAP + mapper.init(0, 1023, yy, zz); + start = micros(); + for (int i = 0; i < 10000; i++) + { + x = mapper.map(z); + } + stop = micros(); + Serial.print("fastmap.map():\t"); + Serial.println(stop-start); + Serial.print(z); + Serial.print(" -> "); + Serial.println(x); + + // GAIN + Serial.print("Performance factor: "); + Serial.println((float)reference/(stop-start)); + Serial.println(); + + // constrainedMap + mapper.init(0, 1023, yy, zz); + start = micros(); + for (int i = 0; i < 10000; i++) + { + x = mapper.constrainedMap(z); + } + stop = micros(); + Serial.print("fastmap.constrainedMap():\t"); + Serial.println(stop-start); + Serial.print(z); + Serial.print(" -> "); + Serial.println(x); + + // GAIN + Serial.print("Performance factor: "); + Serial.println((float)reference/(stop-start)); + Serial.println(); + + // lowerConstrainedMap + mapper.init(0, 1023, yy, zz); + start = micros(); + for (int i = 0; i < 10000; i++) + { + x = mapper.lowerConstrainedMap(z); + } + stop = micros(); + Serial.print("fastmap.lowerConstrainedMap():\t"); + Serial.println(stop-start); + Serial.print(z); + Serial.print(" -> "); + Serial.println(x); + + // GAIN + Serial.print("Performance factor: "); + Serial.println((float)reference/(stop-start)); + Serial.println(); + + // upperConstrainedMap + mapper.init(0, 1023, yy, zz); + start = micros(); + for (int i = 0; i < 10000; i++) + { + x = mapper.upperConstrainedMap(z); + } + stop = micros(); + Serial.print("fastmap.upperConstrainedMap():\t"); + Serial.println(stop-start); + Serial.print(z); + Serial.print(" -> "); + Serial.println(x); + + // GAIN + Serial.print("Performance factor: "); + Serial.println((float)reference/(stop-start)); + Serial.println(); + + + Serial.println("done..."); +} + +void loop() +{ +} +// +// END OF FILE +// diff --git a/libraries/FastMap/library.properties b/libraries/FastMap/library.properties index 0a273309..b4daa696 100644 --- a/libraries/FastMap/library.properties +++ b/libraries/FastMap/library.properties @@ -1,5 +1,5 @@ name=FastMap -version=0.1.6 +version=0.1.7 author=Rob Tillaart maintainer=Rob Tillaart sentence=Library with fast map function for Arduino.