popravljen decoder, init/start/stop
parent
aa89e70dd2
commit
fc469e0fa3
116
SLIPDecoder.sc
116
SLIPDecoder.sc
|
@ -13,16 +13,22 @@ SLIPDecoder {
|
|||
const slipESC_END = 0xDC;
|
||||
const slipESC_ESC = 0xDD;
|
||||
|
||||
// OSC bundle header "#bundle"
|
||||
// OSC DECODING
|
||||
// ============
|
||||
// bundle header "#bundle"
|
||||
// type separator ","
|
||||
const oscBundleHeader = #[35, 98, 117, 110, 100, 108, 101, 0];
|
||||
|
||||
// OSC type separator ","
|
||||
const oscTypeSeparator = 0x2C;
|
||||
|
||||
var deviceName, <>prependAddress, <>rate, <>port, decode, <>actions, firstRead, trace, stop, <>reader;
|
||||
// Buffer for OSC bundles/messages
|
||||
const bufferSize = 4096;
|
||||
|
||||
*new {|deviceName="/dev/ttyUSB0", rate=9600, prepend=""|
|
||||
^super.new.initSLIPDecoder(deviceName, rate, prepend);
|
||||
var <>deviceName, <>rate, <>prepend, <port, decode, trace, stop, <reader;
|
||||
|
||||
*new { |deviceName="/dev/ttyUSB0", rate=115200, prepend=""|
|
||||
deviceName.postln;
|
||||
//^super.newCopyArgs(deviceName, rate, prepend).init;
|
||||
^super.newCopyArgs(deviceName, rate, prepend);//.init;
|
||||
}
|
||||
|
||||
readInt32 { |byteArr|
|
||||
|
@ -59,25 +65,30 @@ SLIPDecoder {
|
|||
}
|
||||
|
||||
int2chr { |x|
|
||||
// Ce ni stevilo, vrni isto
|
||||
if ((x.isInteger.not), { ^x });
|
||||
// v razponu crk, stevilk in znakov vrni char, sicer kar cifro
|
||||
// ce je stevilo, v razponu crk, stevk in znakov, vrni char, sicer kar cifro
|
||||
if (x.isInteger && (x >= 0x20) && (x <= 0x7E),
|
||||
{ ^x.asAscii },
|
||||
{ ^x })
|
||||
}
|
||||
|
||||
initSLIPDecoder { |deviceName,rate,prepend|
|
||||
init {
|
||||
"init".postln;
|
||||
dump(this);
|
||||
deviceName.postln;
|
||||
rate.postln;
|
||||
port = SerialPort(deviceName, rate);
|
||||
prependAddress = prepend;
|
||||
actions = []; // Each action is a function that takes the message contents as an argument. see 'decode' below.
|
||||
firstRead = false;
|
||||
trace = false;
|
||||
stop = false;
|
||||
"init done".postln;
|
||||
}
|
||||
|
||||
trace { |val|
|
||||
trace = val;
|
||||
val;
|
||||
}
|
||||
|
||||
traceMsg { |...msg|
|
||||
if (trace, { "> ".post; msg.join(' ').postln; });
|
||||
}
|
||||
|
||||
// Function for decoding the properly-SLIP-decoded message.
|
||||
|
@ -90,12 +101,7 @@ SLIPDecoder {
|
|||
{
|
||||
var timetag, nextMsgLen, nextMsgStart, nextMsgEnd, bundlePart;
|
||||
|
||||
if (trace, {
|
||||
"".postln;
|
||||
"======".postln;
|
||||
"BUNDLE".postln;
|
||||
"======".postln;
|
||||
});
|
||||
this.traceMsg("BUNDLE");
|
||||
|
||||
// Next 8 bytes are a time tag (or null)
|
||||
/* @TODO handle timetags!
|
||||
|
@ -152,45 +158,85 @@ SLIPDecoder {
|
|||
/* @TODO implement strings, bytearrays */
|
||||
);
|
||||
});
|
||||
this.traceMsg("OSC", prepend ++ address, args);
|
||||
|
||||
// Send OSC message to the engine
|
||||
NetAddr.localAddr.sendMsg(prependAddress ++ address, *args);
|
||||
NetAddr.localAddr.sendMsg(prepend ++ address, *args);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
start {
|
||||
stop = false;
|
||||
reader = fork {
|
||||
var buffer, serialByte;
|
||||
this.traceMsg("Starting...");
|
||||
dump(this);
|
||||
// TODO fix restart
|
||||
if ((port == nil), {this.init;});
|
||||
if (port.isOpen.not, {this.init;});
|
||||
|
||||
var bufferSize = 1024; // 1KB
|
||||
this.traceMsg("opened");
|
||||
|
||||
reader = fork {
|
||||
var serialByte, buffer, firstRead;
|
||||
firstRead = true;
|
||||
|
||||
// Skip data before the first END character
|
||||
while({stop.not && firstRead}, {
|
||||
serialByte = port.read;
|
||||
|
||||
//this.traceMsg("Read byte");
|
||||
//this.traceMsg(serialByte.asAscii);
|
||||
|
||||
if (serialByte == slipEND, {
|
||||
buffer = Int8Array(maxSize: bufferSize);
|
||||
firstRead = false;
|
||||
}, {
|
||||
//this.traceMsg("Skip...")
|
||||
});
|
||||
});
|
||||
|
||||
// Start reading data
|
||||
this.traceMsg("First read!");
|
||||
while({stop.not}, {
|
||||
serialByte = port.read;
|
||||
//this.traceMsg("Checking", serialByte.asInteger, serialByte.asAscii);
|
||||
serialByte.switch(
|
||||
// on END, decode buffer
|
||||
slipEND, {
|
||||
if (firstRead && buffer.isEmpty.not,
|
||||
{
|
||||
//this.traceMsg("SLIP END, decoding ");
|
||||
//this.traceMsg(buffer);
|
||||
//3.wait;
|
||||
if (buffer.isEmpty.not, {
|
||||
this.traceMsg("decode!", buffer);
|
||||
this.decode(buffer);
|
||||
},
|
||||
{ firstRead = true; }
|
||||
);
|
||||
buffer = Int8Array(maxSize: bufferSize);
|
||||
});
|
||||
},
|
||||
slipESC, {
|
||||
serialByte = port.read;
|
||||
serialByte.switch(
|
||||
slipESC_END, { buffer.add(slipEND) },
|
||||
slipESC_ESC, { buffer.add(slipESC) },
|
||||
{"SLIP encoding error.".error }
|
||||
)
|
||||
slipESC_END, {
|
||||
//this.traceMsg("SLIP ESC and ESC_END");
|
||||
buffer.add(slipEND)
|
||||
},
|
||||
{ buffer.add(serialByte) }
|
||||
slipESC_ESC, {
|
||||
//this.traceMsg("SLIP ESC and ESC_ESC");
|
||||
buffer.add(slipESC)
|
||||
},
|
||||
{"SLIP encoding error.".error }
|
||||
);
|
||||
},
|
||||
{
|
||||
// Otherwise just add the byte
|
||||
//this.traceMsg(buffer);
|
||||
buffer.add(serialByte);
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
stop { reader.stop }
|
||||
stop {
|
||||
this.traceMsg("Stopping...");
|
||||
stop = true;
|
||||
port.close;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue