2 * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #ifndef _ardour_surfaces_m2encoder_h_
20 #define _ardour_surfaces_m2encoder_h_
23 #include "pbd/signals.h"
25 namespace ArdourSurface {
27 class M2EncoderInterface
30 M2EncoderInterface () {}
31 virtual ~M2EncoderInterface () {}
34 PBD::Signal1<void, int> changed;
35 virtual float value () const { return 0.f; }
36 virtual float range () const { return 0.f; }
38 /* internal API - called from device thread */
39 virtual bool set_value (unsigned int v) { return false; }
42 class M2Encoder : public M2EncoderInterface
45 M2Encoder (unsigned int upper = 1000)
46 : M2EncoderInterface ()
47 , _upper (upper /* limit, exclusive. eg [0..15]: 16 */)
49 , _initialized (false)
52 _wrapcnt = std::max (3U, upper / 6);
55 float value () const { return _value / (_upper - 1.f); }
56 float range () const { return (_upper - 1.f); }
58 bool set_value (unsigned int v) {
70 if (v < _wrapcnt && _value > _upper - _wrapcnt) {
71 // wrap around max -> min
72 delta = v + _upper - _value;
74 else if (_value < _wrapcnt && v > _upper - _wrapcnt) {
75 // wrap around min -> max
76 delta = v - _upper - _value;
90 unsigned int _wrapcnt;
95 #endif /* _ardour_surfaces_m2encoder_h_ */