Skip to content

Commit 67310e2

Browse files
committed
+ 0.1.02 fastMap first stable version
+ does as much math as possible in advance + 3 versions of constrained map
1 parent 9d290fe commit 67310e2

File tree

3 files changed

+169
-0
lines changed

3 files changed

+169
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// FILE: fastMapper.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.00
5+
// PURPOSE: demo of a faster mapper function
6+
// DATE: 2014-11-02
7+
// URL:
8+
//
9+
// Released to the public domain
10+
//
11+
12+
#include "fastMap.h"
13+
14+
uint32_t start;
15+
uint32_t stop;
16+
uint32_t reference;
17+
18+
volatile long zz = 3000, yy = 20000;
19+
volatile float x;
20+
21+
fastmap mapper;
22+
23+
void setup()
24+
{
25+
Serial.begin(115200);
26+
Serial.print("Start fastmap demo\nlib version: ");
27+
Serial.println(FASTMAP_LIB_VERSION);
28+
Serial.println();
29+
30+
// Get a non optimizable value;
31+
int z = analogRead(A0);
32+
33+
// REFERENCE
34+
start = micros();
35+
for (int i = 0; i < 10000; i++)
36+
{
37+
x = map(z, 0, 1023, yy, zz);
38+
}
39+
stop = micros();
40+
reference = stop-start;
41+
Serial.println(reference);
42+
Serial.print(z);
43+
Serial.print(" -> ");
44+
Serial.println(x);
45+
Serial.println();
46+
47+
// FASTMAP
48+
mapper.init(0, 1023, yy, zz);
49+
start = micros();
50+
for (int i = 0; i < 10000; i++)
51+
{
52+
x = mapper.map(z);
53+
}
54+
stop = micros();
55+
Serial.println(stop-start);
56+
Serial.print(z);
57+
Serial.print(" -> ");
58+
Serial.println(x);
59+
60+
// GAIN
61+
Serial.print("Performance factor: ");
62+
Serial.println(reference/(stop-start));
63+
}
64+
65+
void loop()
66+
{
67+
}
68+
//
69+
// END OF FILE
70+
//

libraries/fastMap/fastMap.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// FILE: fastMap.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.02
5+
// PURPOSE: class implementation of map function - library for Arduino
6+
// URL: http://forum.arduino.cc/index.php?topic=276194
7+
//
8+
// HISTORY:
9+
// 0.1.02 squized the code (first public version)
10+
// 0.1.01 refactor
11+
// 0.1.00 initial version
12+
//
13+
14+
#include "fastMap.h"
15+
16+
/////////////////////////////////////////////////////
17+
//
18+
// PUBLIC
19+
//
20+
21+
void fastmap::init(float in_min, float in_max, float out_min, float out_max)
22+
{
23+
_in_min = in_min;
24+
_in_max = in_max;
25+
_out_min = out_min;
26+
_out_max = out_max;
27+
_factor = (out_max - out_min)/(in_max - in_min);
28+
_base = out_min - in_min * _factor;
29+
// Serial.println(_in_min);
30+
// Serial.println(_in_max);
31+
// Serial.println(_out_min);
32+
// Serial.println(_out_max);
33+
// Serial.println(_factor);
34+
}
35+
36+
float fastmap::constrainedMap(float value)
37+
{
38+
if (value <= _in_min) return _out_min;
39+
if (value >= _in_max) return _out_max;
40+
return this->map(value);
41+
}
42+
43+
float fastmap::lowerConstrainedMap(float value)
44+
{
45+
if (value <= _in_min) return _out_min;
46+
return this->map(value);
47+
}
48+
49+
float fastmap::upperConstrainedMap(float value)
50+
{
51+
if (value >= _in_max) return _out_max;
52+
return this->map(value);
53+
}
54+
//
55+
// END OF FILE
56+
//

libraries/fastMap/fastMap.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// FILE: fastMap.h
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.02
5+
// PURPOSE: class implementation of map function - library for Arduino
6+
// URL: http://forum.arduino.cc/index.php?topic=276194
7+
//
8+
// HISTORY:
9+
// see fastMap.cpp file
10+
//
11+
12+
#ifndef fastMap_h
13+
#define fastMap_h
14+
15+
#if ARDUINO < 100
16+
#include <WProgram.h>
17+
#else
18+
#include <Arduino.h>
19+
#endif
20+
21+
#define FASTMAP_LIB_VERSION "0.1.02"
22+
23+
#define USE_CONSTRAINED_MAP
24+
25+
class fastmap
26+
{
27+
public:
28+
void init(float in_min, float in_max, float out_min, float out_max);
29+
30+
float inline map(float value) { return _base + value * _factor; }
31+
float constrainedMap(float value);
32+
float lowerConstrainedMap(float value);
33+
float upperConstrainedMap(float value);
34+
35+
private:
36+
float _in_min, _in_max, _out_min, _out_max;
37+
float _factor, _base;
38+
};
39+
40+
#endif
41+
//
42+
// END OF FILE
43+
//

0 commit comments

Comments
 (0)