Merged with trunk R1612.
[ardour.git] / libs / surfaces / mackie / scripts / mackie.rb
1 class String
2   def to_bytes
3     arr = []
4     each_byte{|x| arr << x}
5     arr
6   end
7 end
8
9 class Array
10   def to_hex
11     map{|x| "%2.0x" % x}
12   end
13
14   alias as_hex to_hex
15 end
16
17 class String
18   def to_b
19     to_i != 0 || %w{true t yes y}.include?( self.downcase )
20   end
21 end
22
23 class Fixnum
24   def to_hex
25     "%02x" % self
26   end
27 end
28
29 class Mackie
30   attr_accessor :file
31   
32         def initialize( file )
33                 @file = file
34         end
35         
36         # send and receive a sysex message
37   # after wrapping in the header and the eox byte
38         def sysex( msg )
39                 puts "Mackie write: #{msg.unpack('C*').to_hex.inspect}"
40                 write_sysex( msg )
41                 response = read_sysex
42                 puts "Mackie response: #{response.to_hex.inspect}"
43                 response[5..-1]
44         end
45         
46         # returns an array of bytes
47         def read_sysex
48           buf = []
49           while ( nc = @file.read( 1 ) )[0] != 0xf7
50       buf << nc[0]
51           end
52           buf
53   end
54   
55         # send and flush a sysex message
56   # after wrapping in the header and the eox byte
57   def write_sysex( msg )
58     @file.write( hdrlc + msg + "\xf7" )
59     @file.flush
60   end
61   
62   def write( msg )
63     @file.write msg
64     @file.flush
65   end
66   
67   def translate_seven_segment( char )
68     case char
69       when 0x40..0x60
70         char - 0x40
71       when 0x21..0x3f
72         char
73       else
74         0x00
75     end
76   end
77   
78   # display the msg (which can be only 2 characters)
79   # append the number of stops. Options are '..', '. ', '. ', '  '
80   def two_char( msg, stops = '  ' )
81     two = Array.new
82     two << translate_seven_segment( msg.upcase[0] )
83     two << translate_seven_segment( msg.upcase[1] )
84     
85     two[0] += 0x40 if stops[0] == '.'[0]
86     two[1] += 0x40 if stops[1] == '.'[0]
87     
88     midi_msg = [0xb0, 0x4a, two[1], 0x4b, two[0] ]
89     write midi_msg.pack( 'C*' )
90   end
91   
92   # send and receive the device initialisation
93   def init
94     response = sysex( "\x00" )
95
96     # decode host connection query
97     status = response[0]
98     raise( "expected 01, got " + response.inspect ) if status != 1
99     
100     serial = response[1..7]
101     challenge = response[8..11]
102
103     # send host connection reply
104     reply = "\x02" + serial.pack('C*') + challenge.pack('C*')
105     response = sysex reply
106
107     # decode host connection confirmation
108     status = response[0]
109     raise ( "expected 03, got " + response.inspect ) if status != 3
110   end
111
112         def hdrlc
113                 "\xf0\x00\x00\x66\x10"
114         end
115         
116         def hdrlcxt
117                 "\xf0\x00\x00\x66\x11"
118         end
119 end