Fixes for the iCON Qcon mcp device - LED rings. Submitted by Michal Barhon : mbarhon...
[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 ArdourSurface;
26 using namespace Mackie;
27
28 int const Pot::External = 0x2e; /* specific ID for "vpot" representing external control */
29 int const Pot::ID       = 0x10; /* base value for v-pot IDs */
30
31 Control*
32 Pot::factory (Surface& surface, int id, const char* name, Group& group)
33 {
34         Pot* p = new Pot (id, name, group);
35         surface.pots[id] = p;
36         p->is_qcon = surface.get_qcon_flag();
37         surface.controls.push_back (p);
38         group.add (*p);
39         return p;
40 }
41
42 MidiByteArray
43 Pot::set (float val, bool onoff, Mode mode)
44 {
45         // TODO do an exact calc for 0.50? To allow manually re-centering the port.
46
47         MIDI::byte msg;
48
49         // center on if val is "very close" to 0.50
50         if( !is_qcon ) {
51                 // center the position and shift bits for standard mackie surface
52                 msg =  (val > 0.48 && val < 0.58 ? 1 : 0) << 6; 
53         } else {        //center the position and don't shift anything for qcon - TODO: on center position lit the center LED on the ring
54                 if(val > 0.48 && val < 0.58) {
55                         val = 0.50;             
56                 }
57
58                 // set msg
59                 msg = val;
60         }       
61
62
63         // Pot/LED mode
64         if( !is_qcon ) {
65                 // Mackie mode - Supports all ring modes
66                 msg |=  (mode << 4);
67         } else {        
68                 // Qcon rotary mode - Only "DOT" mode? - TODO: Investigate how to proper set vpot rings to different modes on qcon
69                 msg |=  (0 << 4);
70         }
71
72         /*
73          * Even though a width value may be negative, there is
74          * technically still width there, it is just reversed,
75          * so make sure to show it on the LED ring appropriately.
76          */
77         if (val < 0){
78           val = val * -1;
79         }
80
81         // val, but only if off hasn't explicitly been set
82         if (onoff) {
83                 if (mode == spread) {
84                         msg |=  (lrintf (val * 6)) & 0x0f; // 0b00001111
85                 } else {
86                         msg |=  (lrintf (val * 10.0) + 1) & 0x0f; // 0b00001111
87                 }
88         }
89
90         /* outbound LED message requires 0x20 to be added to the LED's id
91          */
92
93         return MidiByteArray (3, 0xb0, 0x20 + id(), msg);
94
95 }
96