2014-11-02 11:24:21 -05:00
|
|
|
//
|
|
|
|
// FILE: FastMap.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-12-17 08:46:34 -05:00
|
|
|
// VERSION: 0.3.3
|
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
|
|
|
//
|
|
|
|
// HISTORY:
|
2021-12-17 08:46:34 -05:00
|
|
|
// 0.3.3 2021-12-17 update library.json, license, minor edits
|
|
|
|
// 0.3.2 2020-12-21 add Arduino-CI + unit test
|
2021-01-29 06:31:58 -05:00
|
|
|
// 0.3.1 2020-08-31 updated documentation
|
|
|
|
// 0.3.0 2020-07-04 added fastMapDouble + test sketch.
|
|
|
|
// 0.2.1 2020-06-10 fix library.json; rename license
|
|
|
|
// 0.2.0 2020-03-21 #pragma once; readme.md; license.md
|
|
|
|
//
|
|
|
|
// 0.1.8 2017-07-27 revert double to float (issue 33)
|
|
|
|
// 0.1.7 2017-04-28 cleaned up, get examples working again
|
|
|
|
// 0.1.06 2015-03-08 replaced float by double (support ARM)
|
|
|
|
// 0.1.05 2014-11-02 stripped of bit mask experimental code
|
|
|
|
// 0.1.04 add back() - the inverse map
|
2021-12-17 08:46:34 -05:00
|
|
|
// tested with bit mask for constrain code (Performance was killed)
|
2021-01-29 06:31:58 -05:00
|
|
|
// 0.1.03 proper name
|
2021-12-17 08:46:34 -05:00
|
|
|
// 0.1.02 squeezed the code (first public version)
|
2021-01-29 06:31:58 -05:00
|
|
|
// 0.1.01 refactor
|
|
|
|
// 0.1.00 initial version
|
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
|
|
|
|
2017-07-15 12:28:01 -04:00
|
|
|
FastMap::FastMap()
|
|
|
|
{
|
|
|
|
init(0, 1, 0, 1);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2017-07-27 09:26:36 -04:00
|
|
|
void FastMap::init(float in_min, float in_max, float out_min, float out_max)
|
2014-11-02 11:24:21 -05:00
|
|
|
{
|
|
|
|
_in_min = in_min;
|
|
|
|
_in_max = in_max;
|
|
|
|
_out_min = out_min;
|
|
|
|
_out_max = out_max;
|
2014-11-02 16:20:21 -05:00
|
|
|
|
2014-11-02 11:24:21 -05:00
|
|
|
_factor = (out_max - out_min)/(in_max - in_min);
|
|
|
|
_base = out_min - in_min * _factor;
|
2014-11-02 16:20:21 -05:00
|
|
|
|
|
|
|
_backfactor = 1/_factor;
|
|
|
|
_backbase = in_min - out_min * _backfactor;
|
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
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
FastMapDouble::FastMapDouble()
|
|
|
|
{
|
|
|
|
init(0, 1, 0, 1);
|
|
|
|
}
|
|
|
|
|
2021-12-17 08:46:34 -05:00
|
|
|
|
2020-11-27 05:16:22 -05:00
|
|
|
void FastMapDouble::init(double in_min, double in_max, double out_min, double out_max)
|
|
|
|
{
|
|
|
|
_in_min = in_min;
|
|
|
|
_in_max = in_max;
|
|
|
|
_out_min = out_min;
|
|
|
|
_out_max = out_max;
|
|
|
|
|
|
|
|
_factor = (out_max - out_min)/(in_max - in_min);
|
|
|
|
_base = out_min - in_min * _factor;
|
|
|
|
|
|
|
|
_backfactor = 1/_factor;
|
|
|
|
_backbase = in_min - out_min * _backfactor;
|
|
|
|
}
|
|
|
|
|
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 --
|
|
|
|
|