11#pragma once
22//
3- // FILE: multimap .h
3+ // FILE: MultiMap .h
44// AUTHOR: Rob Tillaart
5- // VERSION: 0.1.2
5+ // VERSION: 0.1.3
66// DATE: 2011-01-26
77// PURPOSE: Arduino library for fast non-linear mapping or interpolation of values
88// URL: https://github.com/RobTillaart/MultiMap
99// URL: http://playground.arduino.cc/Main/MultiMap
1010//
11- // HISTORY:
12- // 0.0.1 2011-01-26 initial version (see forum)
13- // .....
14- // 0.1.0 2015-03-29
15- // 0.1.1 2020-04-09
16- // 0.1.2 2020-06-19 fix library.json
17- //
11+ // HISTORY:
12+ // 0.0.1 2011-01-26 initial version (see forum)
13+ // .....
14+ // 0.1.0 2015-03-29
15+ // 0.1.1 2020-04-09
16+ // 0.1.2 2020-06-19 fix library.json
17+ // 0.1.3 2021-01-02 add arduino-CI
1818//
1919
20- #define MULTIMAP_LIB_VERSION " 0.1.2 "
20+ #define MULTIMAP_LIB_VERSION " 0.1.3 "
2121
22- #include < Arduino.h>
22+ #include " Arduino.h"
2323
2424// note: the in array should have increasing values
2525template <typename T>
@@ -44,46 +44,49 @@ T multiMap(T val, T* _in, T* _out, uint8_t size)
4444
4545/*
4646 * speed optimized version if inputs do not change often e.g. 2 2 2 2 2 3 3 3 3 5 5 5 5 5 5 8 8 8 8 5 5 5 5 5
47- *
47+ *
4848// note: the in array should have increasing values
49+
4950template<typename T>
5051T multiMap(T val, T* _in, T* _out, uint8_t size)
5152{
52-
53- static T lastvalue = -1;
54- static T cache = -1;
55-
56- if (val == lastvalue) return cache;
57- lastvalue = val;
53+ static T lastvalue = -1;
54+ static T cache = -1;
5855
59- // take care the value is within range
60- // val = constrain(val, _in[0], _in[size-1]);
61- if (val <= _in[0])
62- {
63- cache = _out[0];
64- }
65- else if (val >= _in[size-1])
66- {
67- cache = _out[size-1];
68- }
56+ if (val == lastvalue)
57+ {
58+ return cache;
59+ }
60+ lastvalue = val;
61+
62+ // take care the value is within range
63+ // val = constrain(val, _in[0], _in[size-1]);
64+ if (val <= _in[0])
65+ {
66+ cache = _out[0];
67+ }
68+ else if (val >= _in[size-1])
69+ {
70+ cache = _out[size-1];
71+ }
72+ else
73+ {
74+ // search right interval; index 0 _in[0] already tested
75+ uint8_t pos = 1;
76+ while(val > _in[pos]) pos++;
77+
78+ // this will handle all exact "points" in the _in array
79+ if (val == _in[pos])
80+ {
81+ cache = _out[pos];
82+ }
6983 else
70- {
71- // search right interval; index 0 _in[0] already tested
72- uint8_t pos = 1;
73- while(val > _in[pos]) pos++;
74-
75- // this will handle all exact "points" in the _in array
76- if (val == _in[pos])
77- {
78- cache = _out[pos];
79- }
80- else
81- {
82- // interpolate in the right segment for the rest
83- cache = (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
84- }
85- }
86- return cache;
84+ {
85+ // interpolate in the right segment for the rest
86+ cache = (val - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
87+ }
88+ }
89+ return cache;
8790}
8891*/
8992
0 commit comments