2017-01-03 23:37:27 +01:00
SLIPDecoder {
var deviceName , < > rate , < > port , decode , < > actions , < > addresses , < numAddresses ;
* new { | deviceName = " /dev/cu.SLAB_USBtoUART " , rate = 9600 , numAddresses = 3 |
^ super . new . initSLIPDecoder ( deviceName , rate , numAddresses ) ;
}
initSLIPDecoder { | a , b , c |
deviceName = a ;
rate = b ;
numAddresses = c ;
port = SerialPort ( deviceName , rate ) ;
2017-01-04 00:17:41 +01:00
actions = { } ! numAddresses ; / / second byte in the data array is its address if it ' s properly configured in OSC. THIS MIGHT BE DIFFERENT FOR DIFFERENT VERSIONS OF THE OSC PROTOCOL. This value is used to select between a number of actions which can be externally set. Each action is a function that takes the message contents as an argument. see ' decode ' below.
2017-01-03 23:37:27 +01:00
decode = { | data | / / function for decoding the properly - SLIP - decoded message .
var temp = 0 ! 15 , address , output ;
/ / data [ 1 ] . asAscii . postln ;
address = data [ 1 ] . asAscii . asString . asInt ;
/ / " : " . post ;
output = ( data [ data . size - 2 ] . asBinaryDigits . at ( [ 6 , 7 ] ) + + data [ data . size - 1 ] . asBinaryDigits ) . convertDigits ( 2 ) ;
actions [ address ] . value ( output ) ;
}
}
start {
fork {
/ / SLIP DECODING
/ / == == == == == == == == == =
/ /
/ / The packets are SLIP encoded using these special characters :
/ / end = 8 r300 ( 2 r11000000 or 0xc0 or 192 )
/ / esc = 8 r333 ( 2 r11011011 or 0xdb or 219 )
/ / esc_end = 8 r334 ( 2 r011011100 or 0xdc or 220 )
/ / esc_esc = 8 r335 ( 2 r011011101 or 0xdd or 221 )
2017-01-04 00:17:41 +01:00
/ / original code by Martin Marier & Fredrik Olofsson
2017-01-03 23:37:27 +01:00
var buffer , serialByte ;
var maxPacketSize = 64 ; / / 16 is enouch for 8 10 bit values .
var slipEND = 8 r300 ;
var slipESC = 8 r333 ;
var slipESC_END = 8 r334 ;
var slipESC_ESC = 8 r335 ;
buffer = Int8Array ( maxSize : maxPacketSize ) ;
{
serialByte = port . read ;
serialByte . switch (
slipEND , {
decode . value ( buffer ) ;
buffer = Int8Array ( maxSize : maxPacketSize ) ;
} ,
slipESC , {
serialByte = port . read ;
serialByte . switch (
slipESC_END , { buffer . add ( slipEND ) } ,
slipESC_ESC , { buffer . add ( slipESC ) } ,
{ " SLIP encoding error. " . error }
)
} ,
{ buffer . add ( serialByte ) }
) ;
} . loop
}
}
}