2022-10-21 06:17:48 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: moduloMap.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-10-23 04:36:41 -04:00
|
|
|
// VERSION: 0.1.1
|
2022-10-21 06:17:48 -04:00
|
|
|
// PURPOSE: Arduino library for modulo mapping
|
|
|
|
// DATE: 2022-10-15
|
|
|
|
// URL: https://github.com/RobTillaart/moduloMap
|
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
|
|
|
|
2022-10-23 04:36:41 -04:00
|
|
|
#define MODMAP_LIB_VERSION (F("0.1.1"))
|
2022-10-21 06:17:48 -04:00
|
|
|
|
|
|
|
|
|
|
|
class MODMAP
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MODMAP()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-10-23 04:36:41 -04:00
|
|
|
bool begin(float minimum, float maximum)
|
2022-10-21 06:17:48 -04:00
|
|
|
{
|
2022-10-23 04:36:41 -04:00
|
|
|
if (minimum >= maximum) return false;
|
2022-10-21 06:17:48 -04:00
|
|
|
_minimum = minimum;
|
|
|
|
_maximum = maximum;
|
|
|
|
_range = maximum - _minimum;
|
2022-10-23 04:36:41 -04:00
|
|
|
// _factor = 1/_range;
|
|
|
|
return true;
|
2022-10-21 06:17:48 -04:00
|
|
|
}
|
|
|
|
|
2022-10-23 04:36:41 -04:00
|
|
|
float getMinimum()
|
|
|
|
{
|
|
|
|
return _minimum;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getMaximum()
|
|
|
|
{
|
|
|
|
return _maximum;
|
|
|
|
}
|
|
|
|
|
|
|
|
float getRange()
|
|
|
|
{
|
|
|
|
return _range;
|
|
|
|
}
|
|
|
|
|
|
|
|
// AVR 36 us
|
2022-10-21 06:17:48 -04:00
|
|
|
float map(float value)
|
|
|
|
{
|
2022-10-23 04:36:41 -04:00
|
|
|
// add next line if most values are in range.
|
|
|
|
// if ((_minimum <= value) && (value < _maximum)) return value;
|
2022-10-21 06:17:48 -04:00
|
|
|
float mm = fmod((value - _minimum), _range) + _minimum;
|
2022-10-23 04:36:41 -04:00
|
|
|
if (mm < _minimum) mm += _range; // catch rounding errors
|
|
|
|
return mm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Think of it as how many rotations must a hoist make to free a rope of given length.
|
|
|
|
// Or how many rotations a cylinder has to make to roll up a rope of given length.
|
|
|
|
// This includes the minimum that already has rolled off.
|
|
|
|
float rotations(float value)
|
|
|
|
{
|
|
|
|
return (value - _minimum) / _range;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// develop
|
|
|
|
//
|
|
|
|
|
|
|
|
/* slower (AVR) = manual modulo.
|
|
|
|
// AVR 50 us
|
|
|
|
float map(float value)
|
|
|
|
{
|
|
|
|
float v = value;
|
|
|
|
int32_t t = (v - _minimum) * _factor;
|
|
|
|
float mm = v - t * _range;
|
2022-10-21 06:17:48 -04:00
|
|
|
if (mm < _minimum) mm += _range;
|
|
|
|
return mm;
|
|
|
|
}
|
2022-10-23 04:36:41 -04:00
|
|
|
*/
|
|
|
|
|
2022-10-21 06:17:48 -04:00
|
|
|
|
2022-10-23 04:36:41 -04:00
|
|
|
// for debugging / reference
|
|
|
|
// AVR 44 us
|
|
|
|
/*
|
|
|
|
float map(float value)
|
2022-10-21 06:17:48 -04:00
|
|
|
{
|
|
|
|
if ((_minimum <= value) && (value < _maximum)) return value;
|
2022-10-23 04:36:41 -04:00
|
|
|
float mm = fmod((value - _minimum), _range) + _minimum;
|
|
|
|
if (mm < _minimum) mm += _range; // catch rounding errors
|
2022-10-21 06:17:48 -04:00
|
|
|
return mm;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
float _minimum = 0;
|
2022-10-23 04:36:41 -04:00
|
|
|
float _maximum = 1; // not needed?
|
2022-10-21 06:17:48 -04:00
|
|
|
float _range = 1;
|
2022-10-23 04:36:41 -04:00
|
|
|
// float _factor = 1/_range;
|
2022-10-21 06:17:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// --- END OF FILE ---
|
|
|
|
|