forked from RobTillaart/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e81fca5
commit e7316bd
Showing
4 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// FILE: FastMap.cpp | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.03 | ||
// PURPOSE: class implementation of map function - library for Arduino | ||
// URL: http://forum.arduino.cc/index.php?topic=276194 | ||
// | ||
// HISTORY: | ||
// 0.1.03 proper name | ||
// 0.1.02 squized the code (first public version) | ||
// 0.1.01 refactor | ||
// 0.1.00 initial version | ||
// | ||
|
||
#include "FastMap.h" | ||
|
||
///////////////////////////////////////////////////// | ||
// | ||
// PUBLIC | ||
// | ||
|
||
void FastMap::init(float in_min, float in_max, float out_min, float out_max) | ||
{ | ||
_in_min = in_min; | ||
_in_max = in_max; | ||
_out_min = out_min; | ||
_out_max = out_max; | ||
_factor = (out_max - out_min)/(in_max - in_min); | ||
_base = out_min - in_min * _factor; | ||
} | ||
|
||
float FastMap::constrainedMap(float value) | ||
{ | ||
if (value <= _in_min) return _out_min; | ||
if (value >= _in_max) return _out_max; | ||
return this->map(value); | ||
} | ||
|
||
float FastMap::lowerConstrainedMap(float value) | ||
{ | ||
if (value <= _in_min) return _out_min; | ||
return this->map(value); | ||
} | ||
|
||
float FastMap::upperConstrainedMap(float value) | ||
{ | ||
if (value >= _in_max) return _out_max; | ||
return this->map(value); | ||
} | ||
// | ||
// END OF FILE | ||
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// FILE: FastMap.h | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.03 | ||
// PURPOSE: class implementation of map function - library for Arduino | ||
// URL: http://forum.arduino.cc/index.php?topic=276194 | ||
// | ||
// HISTORY: | ||
// see fastMap.cpp file | ||
// | ||
|
||
#ifndef FastMap_h | ||
#define FastMap_h | ||
|
||
#if ARDUINO < 100 | ||
#include <WProgram.h> | ||
#else | ||
#include <Arduino.h> | ||
#endif | ||
|
||
#define FASTMAP_LIB_VERSION "0.1.03" | ||
|
||
#define USE_CONSTRAINED_MAP | ||
|
||
class FastMap | ||
{ | ||
public: | ||
void init(float in_min, float in_max, float out_min, float out_max); | ||
|
||
float inline map(float value) { return _base + value * _factor; } | ||
float constrainedMap(float value); | ||
float lowerConstrainedMap(float value); | ||
float upperConstrainedMap(float value); | ||
|
||
private: | ||
float _in_min, _in_max, _out_min, _out_max; | ||
float _factor, _base; | ||
}; | ||
|
||
#endif | ||
// | ||
// END OF FILE | ||
// |
49 changes: 49 additions & 0 deletions
49
libraries/FastMap/examples/constrainedMapDemo/constrainedMapDemo.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// FILE: constrainedMap.ino | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.01 | ||
// PURPOSE: demo of FastMap class ==> merge map and constrain functions | ||
// DATE: 2014-11-02 | ||
// URL: | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#include "FastMap.h" | ||
|
||
FastMap mapper; | ||
|
||
void setup() | ||
{ | ||
Serial.begin(115200); | ||
Serial.print("Start constrainedMap demo\nlib version: "); | ||
Serial.println(FASTMAP_LIB_VERSION); | ||
Serial.println(); | ||
|
||
mapper.init(0, 10, 0, 300); | ||
|
||
Serial.println("I\tMAP\tLCM\tUCM\tCM"); | ||
for (int i = -5; i < 20; i++) | ||
{ | ||
float a = map(i, 0, 10, 0, 300); | ||
float b = mapper.lowerConstrainedMap(i); | ||
float c = mapper.upperConstrainedMap(i); | ||
float d = mapper.constrainedMap(i); | ||
Serial.print(i); | ||
Serial.print("\t"); | ||
Serial.print(a); | ||
Serial.print("\t"); | ||
Serial.print(b); | ||
Serial.print("\t"); | ||
Serial.print(c); | ||
Serial.print("\t"); | ||
Serial.println(d); | ||
} | ||
Serial.println("\ndone..."); | ||
} | ||
|
||
void loop() | ||
{ | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// | ||
// FILE: fastMapDemo.ino | ||
// AUTHOR: Rob Tillaart | ||
// VERSION: 0.1.02 | ||
// PURPOSE: demo of FastMap class ==> a faster map function | ||
// DATE: 2014-11-02 | ||
// URL: | ||
// | ||
// Released to the public domain | ||
// | ||
|
||
#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.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.println(stop-start); | ||
Serial.print(z); | ||
Serial.print(" -> "); | ||
Serial.println(x); | ||
|
||
// GAIN | ||
Serial.print("Performance factor: "); | ||
Serial.println(reference/(stop-start)); | ||
} | ||
|
||
void loop() | ||
{ | ||
} | ||
// | ||
// END OF FILE | ||
// |