MCPcopy Create free account
hub / github.com/RobTillaart/Arduino / multiMap

Function multiMap

libraries/MultiMap/MultiMap.h:21–37  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

19// note: the in array should have increasing values
20template<typename T>
21T multiMap(T value, T* _in, T* _out, uint8_t size)
22{
23 // take care the value is within range
24 // value = constrain(value, _in[0], _in[size-1]);
25 if (value <= _in[0]) return _out[0];
26 if (value >= _in[size-1]) return _out[size-1];
27
28 // search right interval
29 uint8_t pos = 1; // _in[0] already tested
30 while(value > _in[pos]) pos++;
31
32 // this will handle all exact "points" in the _in array
33 if (value == _in[pos]) return _out[pos];
34
35 // interpolate in the right segment for the rest
36 return (value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
37}
38
39
40/*

Callers 2

Calls

no outgoing calls

Tested by

no test coverage detected