-
Notifications
You must be signed in to change notification settings - Fork 4
/
GanglionSerial.sc
79 lines (75 loc) · 1.86 KB
/
GanglionSerial.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//--supercollider openbci ganglion biosensing board (4-channels), communication via serial (bluetooth dongle)
GanglionSerial : Ganglion {
var <port, task;
*new {|port, baudrate= 115200, dataAction, replyAction, initAction, bufferSize= 512|
^super.new(dataAction, replyAction, initAction, bufferSize).initGanglionSerial(port, baudrate);
}
initGanglionSerial {|argPort, argBaudrate|
port= SerialPort(argPort ? "/dev/tty.OpenBCI-DM00DRM0", argBaudrate, crtscts:true);
CmdPeriod.add(this);
this.softReset;
task= Routine({this.prTask}).play(SystemClock);
}
close {
"%: stopping and closing serial port".format(this.class.name).postln;
task.stop;
port.close;
CmdPeriod.remove(this);
}
cmdPeriod {
this.close;
}
//--private
prCommand {|cmd| port.put(cmd)}
prCommandArray {|cmd| port.putAll(cmd)}
prTask {
var last3= [0, 0, 0];
var buffer= List(20);
var state= 0;
var reply, num, aux= (26..31);
0.1.wait;
//TODO unfinished
inf.do{|i|
var byte= port.read;
//byte.postln; //debug
buffer.add(byte);
switch(state,
0, {
if(byte==0, { //packet id
if(buffer.size>1, {
buffer= List(20);
buffer.add(byte);
});
state= 1;
}, {
last3[i%3]= byte;
if(last3==#[36, 36, 36], { //eot $$$
if(buffer[0]==65, { //TODO remove this when openbci fixes firmware upstream
buffer= buffer.drop(32);
//"temp fix applied".postln; //debug
});
reply= "";
(buffer.size-3).do{|i| reply= reply++buffer[i].asAscii};
if(reply.contains("OpenBCI Ganglion"), {
initAction.value(reply);
accel= #[0.0, 0.0, 0.0];
});
replyAction.value(reply);
buffer= List(20);
});
});
},
1, {
if(buffer.size>=20, {
state= 2;
});
},
2, {
buffer.postln;
buffer= List(20);
state= 0;
}
);
};
}
}