MCP: various work on the button binding GUI
[ardour.git] / libs / surfaces / mackie / pot.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3         Copyright (C) 2012 Paul Davis
4
5         This program is free software; you can redistribute it and/or modify
6         it under the terms of the GNU General Public License as published by
7         the Free Software Foundation; either version 2 of the License, or
8         (at your option) any later version.
9
10         This program is distributed in the hope that it will be useful,
11         but WITHOUT ANY WARRANTY; without even the implied warranty of
12         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13         GNU General Public License for more details.
14
15         You should have received a copy of the GNU General Public License
16         along with this program; if not, write to the Free Software
17         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include <cmath>
20
21 #include "pot.h"
22 #include "surface.h"
23 #include "control_group.h"
24
25 using namespace Mackie;
26
27 int const Pot::External = 0x2e; /* specific ID for "vpot" representing external control */
28 int const Pot::ID       = 0x10; /* base value for v-pot IDs */
29
30 Control*
31 Pot::factory (Surface& surface, int id, const char* name, Group& group)
32 {
33         Pot* p = new Pot (id, name, group);
34         surface.pots[id] = p;
35         surface.controls.push_back (p);
36         group.add (*p);
37         return p;
38 }
39
40 MidiByteArray
41 Pot::set_mode (Pot::Mode m)
42 {
43         mode = m;
44         return update_message ();
45 }
46
47 MidiByteArray
48 Pot::set_onoff (bool onoff)
49 {
50         on = onoff;
51         return update_message ();
52 }
53
54 MidiByteArray
55 Pot::set_all (float val, bool onoff, Mode m)
56 {
57         position = val;
58         on = onoff;
59         mode = m;
60         return update_message ();
61 }
62
63 MidiByteArray
64 Pot::update_message ()
65 {
66         // TODO do an exact calc for 0.50? To allow manually re-centering the port.
67
68         // center on or off
69         MIDI::byte msg =  (position > 0.45 && position < 0.55 ? 1 : 0) << 6;
70         
71         // mode
72         msg |=  (mode << 4);
73         
74         // position, but only if off hasn't explicitly been set
75
76         if  (on) {
77                 msg +=  (lrintf (position * 10.0) + 1) & 0x0f; // 0b00001111
78         }
79
80         /* outbound LED message requires 0x20 to be added to the LED's id
81          */
82
83         return MidiByteArray (3, 0xb0, 0x20 + id(), msg);
84
85 }
86