67fcb65ef9be43c3d059fb9f687cf0e38f05a2f5
[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 Control*
28 Pot::factory (Surface& surface, int id, const char* name, Group& group)
29 {
30         Pot* p = new Pot (id, name, group);
31         surface.pots[id] = p;
32         surface.controls.push_back (p);
33         group.add (*p);
34         return p;
35 }
36
37 MidiByteArray
38 Pot::set_mode (Pot::Mode m)
39 {
40         mode = m;
41         return update_message ();
42 }
43
44 MidiByteArray
45 Pot::set_onoff (bool onoff)
46 {
47         on = onoff;
48         return update_message ();
49 }
50
51 MidiByteArray 
52 Pot::set_value (float normalized)
53 {
54         value = normalized;
55         return update_message ();
56 }
57
58 MidiByteArray
59 Pot::set_all (float val, bool onoff, Mode m)
60 {
61         value = val;
62         on = onoff;
63         mode = m;
64         return update_message ();
65 }
66
67 MidiByteArray
68 Pot::update_message ()
69 {
70         // TODO do an exact calc for 0.50? To allow manually re-centering the port.
71
72         // center on or off
73         MIDI::byte msg =  (value > 0.45 && value < 0.55 ? 1 : 0) << 6;
74         
75         // mode
76         msg |=  (mode << 4);
77         
78         // value, but only if off hasn't explicitly been set
79
80         if  (on) {
81                 msg +=  (lrintf (value * 10.0) + 1) & 0x0f; // 0b00001111
82         }
83
84         return MidiByteArray (3, 0xb0, raw_id(), msg);
85
86 }
87