92 lines
2.1 KiB
C
Raw Normal View History

2020-11-27 11:20:37 +01:00
#pragma once
2014-11-18 19:32:33 +01:00
//
2021-01-29 12:31:58 +01:00
// FILE: MultiMap.h
2014-11-18 19:32:33 +01:00
// AUTHOR: Rob Tillaart
2022-11-18 15:09:05 +01:00
// VERSION: 0.1.6
2020-11-27 11:20:37 +01:00
// DATE: 2011-01-26
// PURPOSE: Arduino library for fast non-linear mapping or interpolation of values
// URL: https://github.com/RobTillaart/MultiMap
2014-11-18 19:32:33 +01:00
// URL: http://playground.arduino.cc/Main/MultiMap
2021-05-28 13:39:42 +02:00
2022-11-18 15:09:05 +01:00
#define MULTIMAP_LIB_VERSION (F("0.1.6"))
2014-11-18 19:32:33 +01:00
2021-01-29 12:31:58 +01:00
#include "Arduino.h"
2014-11-18 19:32:33 +01:00
2021-12-22 12:10:05 +01:00
2014-11-18 19:32:33 +01:00
// note: the in array should have increasing values
template<typename T>
2021-12-22 12:10:05 +01:00
T multiMap(T value, T* _in, T* _out, uint8_t size)
2014-11-18 19:32:33 +01:00
{
// take care the value is within range
2021-12-22 12:10:05 +01:00
// value = constrain(value, _in[0], _in[size-1]);
if (value <= _in[0]) return _out[0];
if (value >= _in[size-1]) return _out[size-1];
2014-11-18 19:32:33 +01:00
// search right interval
2021-12-22 12:10:05 +01:00
uint8_t pos = 1; // _in[0] already tested
while(value > _in[pos]) pos++;
2014-11-18 19:32:33 +01:00
// this will handle all exact "points" in the _in array
2021-12-22 12:10:05 +01:00
if (value == _in[pos]) return _out[pos];
2014-11-18 19:32:33 +01:00
// interpolate in the right segment for the rest
2021-12-22 12:10:05 +01:00
return (value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
2014-11-18 19:32:33 +01:00
}
2020-11-27 11:20:37 +01:00
/*
2022-11-18 15:09:05 +01:00
// 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
// implements a minimal cache
//
// note: the in array should have increasing values
2021-01-29 12:31:58 +01:00
2020-11-27 11:20:37 +01:00
template<typename T>
2021-12-22 12:10:05 +01:00
T multiMap(T value, T* _in, T* _out, uint8_t size)
2014-11-18 19:32:33 +01:00
{
2021-01-29 12:31:58 +01:00
static T lastvalue = -1;
static T cache = -1;
2014-11-18 19:32:33 +01:00
2021-12-22 12:10:05 +01:00
if (value == lastvalue)
2021-01-29 12:31:58 +01:00
{
return cache;
}
2021-12-22 12:10:05 +01:00
lastvalue = value;
2021-01-29 12:31:58 +01:00
// take care the value is within range
2021-12-22 12:10:05 +01:00
// value = constrain(value, _in[0], _in[size-1]);
if (value <= _in[0])
2021-01-29 12:31:58 +01:00
{
cache = _out[0];
}
2021-12-22 12:10:05 +01:00
else if (value >= _in[size-1])
2021-01-29 12:31:58 +01:00
{
cache = _out[size-1];
}
else
{
// search right interval; index 0 _in[0] already tested
uint8_t pos = 1;
2021-12-22 12:10:05 +01:00
while(value > _in[pos]) pos++;
2021-01-29 12:31:58 +01:00
// this will handle all exact "points" in the _in array
2021-12-22 12:10:05 +01:00
if (value == _in[pos])
2021-01-29 12:31:58 +01:00
{
cache = _out[pos];
}
2020-11-27 11:20:37 +01:00
else
2021-01-29 12:31:58 +01:00
{
// interpolate in the right segment for the rest
2021-12-22 12:10:05 +01:00
cache = (value - _in[pos-1]) * (_out[pos] - _out[pos-1]) / (_in[pos] - _in[pos-1]) + _out[pos-1];
2021-01-29 12:31:58 +01:00
}
}
return cache;
2014-11-18 19:32:33 +01:00
}
*/
2020-11-27 11:20:37 +01:00
2021-05-28 13:39:42 +02:00
2020-11-27 11:20:37 +01:00
// -- END OF FILE --
2021-12-22 12:10:05 +01:00