2014-11-02 11:24:21 -05:00
|
|
|
//
|
|
|
|
// FILE: FastMap.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-05 05:57:25 -04:00
|
|
|
// VERSION: 0.4.0
|
2017-07-15 12:28:01 -04:00
|
|
|
// PURPOSE: class with fast map function - library for Arduino
|
2020-11-27 05:16:22 -05:00
|
|
|
// URL: https://github.com/RobTillaart/FastMap
|
2014-11-02 11:24:21 -05:00
|
|
|
//
|
2022-11-05 05:57:25 -04:00
|
|
|
// HISTORY: see changelog.md
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2014-11-02 11:24:21 -05:00
|
|
|
|
|
|
|
#include "FastMap.h"
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2022-11-05 05:57:25 -04:00
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// FASTMAP
|
|
|
|
//
|
2017-07-15 12:28:01 -04:00
|
|
|
FastMap::FastMap()
|
|
|
|
{
|
2022-11-05 05:57:25 -04:00
|
|
|
init(0, 1, 0, 1);
|
2017-07-15 12:28:01 -04:00
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2022-11-05 05:57:25 -04:00
|
|
|
bool FastMap::init(float in_min, float in_max, float out_min, float out_max)
|
2014-11-02 11:24:21 -05:00
|
|
|
{
|
2022-11-05 05:57:25 -04:00
|
|
|
float _inRange = in_max - in_min;
|
|
|
|
float _outRange = out_max - out_min;
|
|
|
|
if ((_inRange == 0.0 ) || (_outRange == 0.0)) return false;
|
|
|
|
|
|
|
|
_in_min = in_min;
|
|
|
|
_in_max = in_max;
|
|
|
|
_out_min = out_min;
|
|
|
|
_out_max = out_max;
|
2014-11-02 16:20:21 -05:00
|
|
|
|
2022-11-05 05:57:25 -04:00
|
|
|
_factor = _outRange/_inRange;
|
|
|
|
_base = _out_min - _in_min * _factor;
|
2014-11-02 16:20:21 -05:00
|
|
|
|
2022-11-05 05:57:25 -04:00
|
|
|
_backfactor = _inRange/_outRange;
|
|
|
|
_backbase = _in_min - _out_min * _backfactor;
|
|
|
|
return true;
|
2014-11-02 11:24:21 -05:00
|
|
|
}
|
2014-11-02 16:20:21 -05:00
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2017-07-27 09:26:36 -04:00
|
|
|
float FastMap::constrainedMap(float value)
|
2014-11-02 11:24:21 -05:00
|
|
|
{
|
|
|
|
if (value <= _in_min) return _out_min;
|
|
|
|
if (value >= _in_max) return _out_max;
|
|
|
|
return this->map(value);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2017-07-27 09:26:36 -04:00
|
|
|
float FastMap::lowerConstrainedMap(float value)
|
2014-11-02 11:24:21 -05:00
|
|
|
{
|
|
|
|
if (value <= _in_min) return _out_min;
|
|
|
|
return this->map(value);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2017-07-27 09:26:36 -04:00
|
|
|
float FastMap::upperConstrainedMap(float value)
|
2014-11-02 11:24:21 -05:00
|
|
|
{
|
|
|
|
if (value >= _in_max) return _out_max;
|
|
|
|
return this->map(value);
|
|
|
|
}
|
2017-07-27 09:26:36 -04:00
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2022-11-05 05:57:25 -04:00
|
|
|
///////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// FASTMAP_DOUBLE
|
|
|
|
//
|
2020-11-27 05:16:22 -05:00
|
|
|
FastMapDouble::FastMapDouble()
|
|
|
|
{
|
|
|
|
init(0, 1, 0, 1);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2022-11-05 05:57:25 -04:00
|
|
|
bool FastMapDouble::init(double in_min, double in_max, double out_min, double out_max)
|
2020-11-27 05:16:22 -05:00
|
|
|
{
|
2022-11-05 05:57:25 -04:00
|
|
|
double _inRange = in_max - in_min;
|
|
|
|
double _outRange = out_max - out_min;
|
|
|
|
if ((_inRange == 0.0 ) || (_outRange == 0.0)) return false;
|
|
|
|
|
|
|
|
_in_min = in_min;
|
|
|
|
_in_max = in_max;
|
|
|
|
_out_min = out_min;
|
|
|
|
_out_max = out_max;
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-11-05 05:57:25 -04:00
|
|
|
_factor = _outRange/_inRange;
|
|
|
|
_base = _out_min - _in_min * _factor;
|
2020-11-27 05:16:22 -05:00
|
|
|
|
2022-11-05 05:57:25 -04:00
|
|
|
_backfactor = _inRange/_outRange;
|
|
|
|
_backbase = _in_min - _out_min * _backfactor;
|
|
|
|
return true;
|
2020-11-27 05:16:22 -05:00
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
double FastMapDouble::constrainedMap(double value)
|
|
|
|
{
|
|
|
|
if (value <= _in_min) return _out_min;
|
|
|
|
if (value >= _in_max) return _out_max;
|
|
|
|
return this->map(value);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
double FastMapDouble::lowerConstrainedMap(double value)
|
|
|
|
{
|
|
|
|
if (value <= _in_min) return _out_min;
|
|
|
|
return this->map(value);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
double FastMapDouble::upperConstrainedMap(double value)
|
|
|
|
{
|
|
|
|
if (value >= _in_max) return _out_max;
|
|
|
|
return this->map(value);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|