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