| 19 | // note: the in array should have increasing values |
| 20 | template<typename T> |
| 21 | T 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 | /* |
no outgoing calls
no test coverage detected