Merged with trunk R1612.
[ardour.git] / libs / surfaces / mackie / scripts / host.rb
1 #! /usr/bin/ruby
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 require 'controls.rb'
19 require 'mackie.rb'
20
21 while !File.exist? ARGV[0]
22   sleep 0.010
23 end
24
25 #mapping_csv = ARGV[1] || "mackie-controls.csv"
26 mapping_csv = ARGV[1]
27 puts "mapping_csv: #{mapping_csv}"
28 puts ""
29
30 file = File.open ARGV[0], 'r+'
31 mck = Mackie.new( file )
32
33 # send device query
34 response = mck.sysex( "\x00" )
35 puts "response: #{response.to_hex}"
36
37 # decode host connection query
38 status = response[0]
39 if status != 1
40   puts "expected 01, got " + response.to_hex.inspect
41   exit(1)
42 end
43 serial = response[1..7]
44 challenge = response[8..11]
45 puts <<EOF
46 serial: #{serial.to_hex.inspect}
47 challenge: #{challenge.to_hex.inspect}
48 EOF
49
50 # send host connection reply
51 response = mck.sysex( "\x02" + serial.pack('C*') + challenge.pack('C*') )
52
53 # decode host connection confirmation
54 status = response[0]
55 if status != 3
56   puts "expected 03, got " + response.to_hex.inspect
57   exit(1)
58 end
59
60 serial = response[1..7]
61 puts <<EOF
62 serial: #{serial.to_hex.inspect}
63 EOF
64
65 # faders to minimum. bcf2000 doesn't respond
66 #file.write( hdr + "\x61\xf7" )
67
68 # all leds off. bcf2000 doesn't respond
69 #file.write( hdr + "\x62\xf7" )
70
71 # get version. comes back as ASCII bytes
72 version = mck.sysex( "\x13\x00" )
73 puts "version: #{version.map{|x| x.chr}}"
74
75 # write a welcome message. bcf2000 responds with exact
76 # string but doesn't display anything
77 # 0 offset,
78 #~ file.write hdr + "\x12\x3fLCDE\xf7"
79 #~ file.flush
80 #~ answer = read_sysex file
81 #~ puts "answer: #{answer[hdr.length..-1].map{|x| x.chr}}"
82
83 # write to BBT display
84 #~ file.write hdr + "\x10LCDE\xf7"
85 #~ file.flush
86 #~ bbt = []
87 #~ while ( nc = file.read( 1 ) )[0] != 0xf7
88   #~ bbt << nc[0]
89 #~ end
90 #~ puts "bbt: #{bbt[hdr.length..-1].map{|x| x.chr}}"
91
92 # write 7-segment display
93 #~ file.write hdr + "\x11LCDE\xf7"
94 #~ file.flush
95
96 # go offline. bcf2000 doesn't respond
97 #~ file.write( hdr + "\x0f\x7f\xf7" )
98 #~ file.flush
99
100 sf = Surface.new
101 control_data = ""
102 File.open( mapping_csv ) { |f| control_data = f.read }
103 sf.parse( control_data )
104
105 # send all faders to 0, but bounce them first
106 # otherwise the bcf gets confused
107 sf.midis[0xe0].values.find_all{|x| x.class == Fader}.each do |x|
108   bytes = Array.new
109   bytes[0] = 0xe0 + x.ordinal - 1
110   bytes[1] = 0x1
111   bytes[2] = 0x1
112   file.write bytes.pack( 'C*' )
113   bytes[0] = 0xe0 + x.ordinal - 1
114   bytes[1] = 0x0
115   bytes[2] = 0x0
116   file.write bytes.pack( 'C*' )
117 end
118 file.flush
119
120 # respond to control movements
121 while bytes = mck.file.read( 3 )
122   print "received: %02.x %02.x %02.x" % [ bytes[0], bytes[1], bytes[2] ]
123   midi_type = bytes[0] & 0b11110000
124
125   control_id = sf.types[midi_type].mask_for_id( bytes )
126   control = sf.midis[midi_type][control_id]
127   
128   print " Control Type: %-7s, " % sf.types[midi_type]
129   print "id: %4i" % control_id
130   print ", control: %15s" % ( control ? control.name : "nil control" )
131   print ", %15s" % ( control ? control.group.name : "nil group" )
132   print "\n"
133 end