remove redundant MackieButtonHandler class, implement initial zoom support for cursor...
[ardour.git] / libs / surfaces / mackie / mackie_midi_builder.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3
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 of the License, or
7         (at your option) any later version.
8
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.
13
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
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "mackie_midi_builder.h"
19
20 #include <typeinfo>
21 #include <sstream>
22 #include <iomanip>
23 #include <algorithm>
24 #include <cmath>
25
26 #include "pbd/compose.h"
27
28 #include "ardour/debug.h"
29 #include "controls.h"
30 #include "control_group.h"
31
32 #include "strip.h"
33 #include "button.h"
34 #include "led.h"
35 #include "ledring.h"
36 #include "pot.h"
37 #include "fader.h"
38 #include "jog.h"
39 #include "meter.h"
40 #include "midi_byte_array.h"
41 #include "mackie_port.h"
42
43 using namespace PBD;
44 using namespace Mackie;
45 using namespace std;
46
47 #define NUCLEUS_DEBUG 1
48
49 MIDI::byte MackieMidiBuilder::calculate_pot_value (midi_pot_mode mode, const ControlState & state)
50 {
51         // TODO do an exact calc for 0.50? To allow manually re-centering the port.
52         
53         // center on or off
54         MIDI::byte retval =  (state.pos > 0.45 && state.pos < 0.55 ? 1 : 0) << 6;
55         
56         // mode
57         retval |=  (mode << 4);
58         
59         // value, but only if off hasn't explicitly been set
60         if  (state.led_state != off)
61                 retval +=  (int(state.pos * 10.0) + 1) & 0x0f; // 0b00001111
62         
63         return retval;
64 }
65
66 MidiByteArray MackieMidiBuilder::build_led_ring (const Pot & pot, const ControlState & state, midi_pot_mode mode )
67 {
68         return build_led_ring (pot.led_ring(), state, mode);
69 }
70
71 MidiByteArray MackieMidiBuilder::build_led_ring (const LedRing & led_ring, const ControlState & state, midi_pot_mode mode)
72 {
73         // The other way of doing this:
74         // 0x30 + pot/ring number (0-7)
75         //, 0x30 + led_ring.ordinal() - 1
76         return MidiByteArray  (3
77                 // the control type
78                 , midi_pot_id
79                 // the id
80                 , 0x20 + led_ring.raw_id()
81                 // the value
82                 , calculate_pot_value (mode, state)
83         );
84 }
85
86 MidiByteArray MackieMidiBuilder::build_led (const Button & button, LedState ls)
87 {
88         return build_led (button.led(), ls);
89 }
90
91 MidiByteArray MackieMidiBuilder::build_led (const Led & led, LedState ls)
92 {
93         MIDI::byte state = 0;
94         switch  (ls.state())
95         {
96                 case LedState::on:                      state = 0x7f; break;
97                 case LedState::off:                     state = 0x00; break;
98                 case LedState::none:                    state = 0x00; break; // actually, this should never happen.
99                 case LedState::flashing:        state = 0x01; break;
100         }
101         
102         return MidiByteArray  (3
103                 , midi_button_id
104                 , led.raw_id()
105                 , state
106         );
107 }
108
109 MidiByteArray MackieMidiBuilder::build_fader (const Fader & fader, float pos)
110 {
111         int posi = int (0x3fff * pos);
112         
113         return MidiByteArray  (3
114                 , midi_fader_id | fader.raw_id()
115                 // lower-order bits
116                 , posi & 0x7f
117                 // higher-order bits
118                 ,  (posi >> 7)
119         );
120 }
121
122 MidiByteArray MackieMidiBuilder::zero_strip (SurfacePort & port, const Strip & strip)
123 {
124         Group::Controls::const_iterator it = strip.controls().begin();
125         MidiByteArray retval;
126
127         for (; it != strip.controls().end(); ++it) {
128                 Control & control = **it;
129                 if  (control.accepts_feedback())
130                         retval << zero_control (control);
131         }
132         
133         // These must have sysex headers
134
135         /* XXX: not sure about this check to only display stuff for strips of index < 8 */
136         if (strip.index() < 8) {
137                 retval << strip_display_blank (port, strip, 0);
138                 retval << strip_display_blank (port, strip, 1);
139         }
140         
141         return retval;
142 }
143
144 MidiByteArray MackieMidiBuilder::zero_control (const Control & control)
145 {
146         switch (control.type()) {
147         case Control::type_button:
148                 return build_led ((Button&)control, off);
149                 
150         case Control::type_led:
151                 return build_led ((Led&)control, off);
152                 
153         case Control::type_fader:
154                 return build_fader ((Fader&)control, 0.0);
155                 
156         case Control::type_pot:
157                 return build_led_ring (dynamic_cast<const Pot&> (control), off);
158                 
159         case Control::type_led_ring:
160                 return build_led_ring (dynamic_cast<const LedRing&> (control), off);
161                 
162         case Control::type_meter:
163                 return const_cast<Meter&>(dynamic_cast<const Meter&>(control)).update_message (0.0);
164                 
165         default:
166                 ostringstream os;
167                 os << "Unknown control type " << control << " in Strip::zero_control";
168                 throw MackieControlException (os.str());
169         }
170 }
171
172 char translate_seven_segment (char achar)
173 {
174         achar = toupper (achar);
175         if  (achar >= 0x40 && achar <= 0x60)
176                 return achar - 0x40;
177         else if  (achar >= 0x21 && achar <= 0x3f)
178       return achar;
179         else
180       return 0x00;
181 }
182
183 MidiByteArray MackieMidiBuilder::two_char_display (const std::string & msg, const std::string & dots)
184 {
185         if  (msg.length() != 2) throw MackieControlException ("MackieMidiBuilder::two_char_display: msg must be exactly 2 characters");
186         if  (dots.length() != 2) throw MackieControlException ("MackieMidiBuilder::two_char_display: dots must be exactly 2 characters");
187         
188         MidiByteArray bytes (5, 0xb0, 0x4a, 0x00, 0x4b, 0x00);
189         
190         // chars are understood by the surface in right-to-left order
191         // could also exchange the 0x4a and 0x4b, above
192         bytes[4] = translate_seven_segment (msg[0]) +  (dots[0] == '.' ? 0x40 : 0x00);
193         bytes[2] = translate_seven_segment (msg[1]) +  (dots[1] == '.' ? 0x40 : 0x00);
194         
195         return bytes;
196 }
197
198 MidiByteArray MackieMidiBuilder::two_char_display (unsigned int value, const std::string & /*dots*/)
199 {
200         ostringstream os;
201         os << setfill('0') << setw(2) << value % 100;
202         return two_char_display (os.str());
203 }
204
205 MidiByteArray MackieMidiBuilder::strip_display_blank (SurfacePort & port, const Strip & strip, unsigned int line_number)
206 {
207         // 6 spaces, not 7 because strip_display adds a space where appropriate
208         return strip_display (port, strip, line_number, "      ");
209 }
210
211 MidiByteArray MackieMidiBuilder::strip_display (SurfacePort & port, const Strip & strip, unsigned int line_number, const std::string & line)
212 {
213         assert (line_number <= 1);
214
215         MidiByteArray retval;
216         uint32_t index = strip.index() % port.strips();
217
218         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display index: %1, line %2 = %3\n", strip.index(), line_number, line));
219
220         // sysex header
221         retval << port.sysex_hdr();
222         
223         // code for display
224         retval << 0x12;
225         // offset (0 to 0x37 first line, 0x38 to 0x6f for second line)
226         retval << (index * 7 + (line_number * 0x38));
227         
228         // ascii data to display
229         retval << line;
230         // pad with " " out to 6 chars
231         for (int i = line.length(); i < 6; ++i) {
232                 retval << ' ';
233         }
234         
235         // column spacer, unless it's the right-hand column
236         if (strip.index() < 7) {
237                 retval << ' ';
238         }
239
240         // sysex trailer
241         retval << MIDI::eox;
242         
243         DEBUG_TRACE (DEBUG::MackieControl, string_compose ("MackieMidiBuilder::strip_display midi: %1\n", retval));
244
245         return retval;
246 }
247         
248 MidiByteArray MackieMidiBuilder::all_strips_display (SurfacePort & /*port*/, std::vector<std::string> & /*lines1*/, std::vector<std::string> & /*lines2*/)
249 {
250         MidiByteArray retval;
251         retval << 0x12 << 0;
252         // NOTE remember max 112 bytes per message, including sysex headers
253         retval << "Not working yet";
254         return retval;
255 }
256
257 MidiByteArray MackieMidiBuilder::timecode_display (SurfacePort & port, const std::string & timecode, const std::string & last_timecode)
258 {
259         // if there's no change, send nothing, not even sysex header
260         if  (timecode == last_timecode) return MidiByteArray();
261         
262         // length sanity checking
263         string local_timecode = timecode;
264
265         // truncate to 10 characters
266         if  (local_timecode.length() > 10) {
267                 local_timecode = local_timecode.substr (0, 10);
268         }
269
270         // pad to 10 characters
271         while  (local_timecode.length() < 10) { 
272                 local_timecode += " ";
273         }
274                 
275         // find the suffix of local_timecode that differs from last_timecode
276         std::pair<string::const_iterator,string::iterator> pp = mismatch (last_timecode.begin(), last_timecode.end(), local_timecode.begin());
277         
278         MidiByteArray retval;
279         
280         // sysex header
281         retval << port.sysex_hdr();
282         
283         // code for timecode display
284         retval << 0x10;
285         
286         // translate characters. These are sent in reverse order of display
287         // hence the reverse iterators
288         string::reverse_iterator rend = reverse_iterator<string::iterator> (pp.second);
289         for  (string::reverse_iterator it = local_timecode.rbegin(); it != rend; ++it) {
290                 retval << translate_seven_segment (*it);
291         }
292         
293         // sysex trailer
294         retval << MIDI::eox;
295         
296         return retval;
297 }