/* MicroDexed MicroDexed is a port of the Dexed sound engine (https://github.com/asb2m10/dexed) for the Teensy-3.5/3.6 with audio shield. Dexed ist heavily based on https://github.com/google/music-synthesizer-for-android (c)2018 H. Wirtz This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #ifndef SOFTEN_VALUE_H_INCLUDED #define SOFTEN_VALUE_H_INCLUDED 1 template class SoftenValue { public: void init(T value, T minimum, T maximum) { _from = value; _to = value; _steps = 0; _diff = 0.0; _min = minimum; _max = maximum; } void update(T to, uint16_t steps) { _to = to; _steps = steps; _calculate(); } void update_to(T to) { _to = to; _calculate(); } void update_steps(uint16_t steps) { _steps = steps; _calculate(); } void tick(void) { if (_steps > 0) { _from -= _diff; _steps--; #ifdef DEBUG Serial.print(F("Steps: ")); Serial.print(_steps, DEC); Serial.print(F(" Diff=")); Serial.println(_diff, 5); #endif } } bool running(void) { if (_steps > 0) return (true); else return (false); } uint16_t steps(void) { return (_steps); } T diff(void) { return (_diff); } T value(void) { if (_steps == 0) return (_to); if (_from < _min) _from = _min; else if (_from > _max) _from = _max; if (std::is_same::value) return (_from); else return (round(_from)); } protected: float _from; float _to; uint16_t _steps; float _diff; T _min; T _max; void _calculate(void) { if (_from == _to || _steps <= 0) { _steps = 0; _diff = 0.0; } else _diff = (_from - _to) / _steps; #ifdef DEBUG Serial.print(F("Update SoftenValue: from=")); Serial.print(_from, 5); Serial.print(F(" to=")); Serial.print(_to, 5); Serial.print(F(" diff=")); Serial.print(_diff, 5); Serial.print(F(" steps=")); Serial.println(_steps, DEC); #endif } }; #endif