-
Notifications
You must be signed in to change notification settings - Fork 4
/
DataBuffer.sc
78 lines (77 loc) · 2.06 KB
/
DataBuffer.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
//for collecting OpenBCI data and accelerometer values into buffers
DataBuffer {
var <board, <>size, dataFunc, accelFunc;
var <dataBuffer, <accelBuffer;
var <dataFull, <accelFull; //maxed out flags
var <>dataActive, <>accelActive;
*new {|board, maxSize= 1000, dataActive= true, accelActive= true|
^super.new.initDataBuffer(board, maxSize, dataActive, accelActive);
}
initDataBuffer {|argBoard, argMaxSize, argDataActive, argAccelActive|
board= argBoard;
size= argMaxSize;
dataActive= argDataActive;
accelActive= argAccelActive;
dataBuffer= List.new;
accelBuffer= List.new;
dataFull= false;
accelFull= false;
dataFunc= {|num, d, a|
if(dataActive, {
if(dataBuffer.size>=size, {
dataBuffer.pop;
if(dataFull.not, {
"%: buffer data full. call readData more often or increase size".format(board.class.name).warn;
dataFull= true;
});
});
dataBuffer.insert(0, d.copy);
});
};
accelFunc= {|a|
if(accelActive, {
if(accelBuffer.size>=size, {
accelBuffer.pop;
if(accelFull.not, {
"%: buffer accel full. call readAccel more often or increase size".format(board.class.name).warn;
accelFull= true;
});
});
accelBuffer.insert(0, a);
});
};
}
readData {
var copy= dataBuffer;
dataBuffer= List.new;
^copy.reverse;
}
readAccel {
var copy= accelBuffer;
accelBuffer= List.new;
^copy.reverse;
}
clear {
dataBuffer= List.new;
accelBuffer= List.new;
dataFull= false;
accelFull= false;
}
start {
"%: buffering started".format(board.class.name).postln;
board.dataAction= board.dataAction.removeFunc(dataFunc); //safety
board.accelAction= board.accelAction.removeFunc(accelFunc); //safety
board.dataAction= board.dataAction.addFunc(dataFunc);
board.accelAction= board.accelAction.addFunc(accelFunc);
CmdPeriod.add(this);
}
stop {
board.dataAction= board.dataAction.removeFunc(dataFunc);
board.accelAction= board.accelAction.removeFunc(accelFunc);
"%: buffering stopped".format(board.class.name).postln;
CmdPeriod.remove(this);
}
cmdPeriod {
this.stop;
}
}