forked from guanix/processing-neurosky
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheegPort.java
More file actions
228 lines (200 loc) · 6.06 KB
/
eegPort.java
File metadata and controls
228 lines (200 loc) · 6.06 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// eeg states, indicates the next value we are expecting
import processing.serial.*;
import processing.core.*;
import java.nio.ByteBuffer;
import org.apache.commons.collections.buffer.CircularFifoBuffer;
public class eegPort {
Serial serialPort;
// Parsing state machine
// The name describes the data we are currently waiting for
public static enum readState {
SYNC1,
SYNC2,
LENGTH,
DATA,
CHECKSUM
}
public static enum dongleState {
DISCONNECTED,
STANDBY,
CONNECTED
}
// various state variables for display of EEG information
public int packetLength = 0;
public int packetCode = 0;
public int poorSignal = 255;
public int readIndex = 0;
public int attention = 0;
public int meditation = 0;
public int lastEvent = 0;
public int lastAttention = 0;
public int lastMeditation = 0;
int vectorBytesLeft = 0;
public dongleState portDongleState = dongleState.DISCONNECTED;
public readState portReadState = readState.SYNC1;
public int rawSequence = 0;
public int vectorSequence = 0;
public int failedChecksumCount = 0;
int readBuffer[];
CircularFifoBuffer rawDataBuffer;
CircularFifoBuffer vectorBuffer;
CircularFifoBuffer attentionBuffer;
CircularFifoBuffer meditationBuffer;
// buffer for raw values
PApplet app;
public class rawObs {
int timestamp;
int sequence;
short rawValue;
}
public class vectorObs {
int timestamp;
int sequence;
int vectorValue;
}
public eegPort(PApplet applet, Serial serial) {
app = applet;
serialPort = serial;
rawDataBuffer = new CircularFifoBuffer(4096);
vectorBuffer = new CircularFifoBuffer(4096);
attentionBuffer = new CircularFifoBuffer(3600);
meditationBuffer = new CircularFifoBuffer(3600);
}
public void refresh() {
portReadState = readState.SYNC1;
serialPort.write((byte)0xc1);
app.delay(200);
serialPort.write((byte)0xc2);
}
int signedByte(int inByte) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(inByte);
return bb.get(3);
}
public void serialByte(int inByte) {
switch (portReadState) {
case SYNC1:
readBuffer = new int[170];
packetLength = 0;
readIndex = 0;
if (inByte == 170) {
portReadState = readState.SYNC2;
}
break;
case SYNC2:
if (inByte == 170) {
portReadState = readState.LENGTH;
}
break;
case LENGTH:
packetLength = inByte;
if (packetLength > 169 || packetLength <= 0) {
portReadState = readState.SYNC1;
} else {
readIndex = 0;
portReadState = readState.DATA;
// app.println("reading " + bytesLeft + " bytes");
}
break;
case DATA:
readBuffer[readIndex++] = inByte;
if (readIndex == packetLength) {
portReadState = readState.CHECKSUM;
}
break;
case CHECKSUM:
portReadState = readState.SYNC1;
// run checksum
int checksum = 0;
for (int i = 0; i < packetLength; i++) {
checksum = (checksum + readBuffer[i])%256;
}
if (255 - checksum != inByte) {
// app.println("checksum fail, calculated " + checksum + ", provided " + inByte +
// " for length " + packetLength);
failedChecksumCount++;
} else if (packetLength > 0) {
lastEvent = app.millis();
parse();
}
break;
}
}
public void parse() {
int inByte;
for (int i = 0; i < packetLength; i++) {
switch(readBuffer[i]) {
case 212: // standby
portDongleState = dongleState.STANDBY;
break;
case 208: // connected
portDongleState = dongleState.CONNECTED;
break;
case 2: // poor signal
inByte = readBuffer[++i];
if (inByte == 255 && poorSignal == 0) {
inByte = 49;
} else if (inByte < 200 && poorSignal == 200) {
inByte = 254;
}
poorSignal = inByte;
break;
case 4: // attention
inByte = signedByte(readBuffer[++i]);
if (inByte > 0) {
attention = inByte;
}
lastAttention = app.millis();
attentionBuffer.add(attention);
break;
case 5: // meditation
inByte = signedByte(readBuffer[++i]);
if (inByte > 0) {
meditation = inByte;
}
lastMeditation = app.millis();
meditationBuffer.add(meditation);
break;
case 128: // raw value
int rawRowLength = readBuffer[++i];
int rawA = readBuffer[++i];
int rawB = readBuffer[++i];
ByteBuffer bbA = ByteBuffer.allocate(4);
bbA.putInt(rawA);
ByteBuffer bbB = ByteBuffer.allocate(4);
bbB.putInt(rawB);
ByteBuffer bb = ByteBuffer.allocate(2);
// value from NeuroSky is little endian, so swap around
bb.put(1, bbA.get(3));
bb.put(0, bbB.get(3));
short rawValue = bb.getShort(0);
rawSequence++;
rawObs obs = new rawObs();
obs.sequence = rawSequence;
obs.timestamp = app.millis();
obs.rawValue = rawValue;
rawDataBuffer.add(obs);
break;
case 131: // vector
int vectorLength = readBuffer[++i];
if (vectorLength != 24) {
// something wrong
break;
}
for (int j = 0; j < 8; j++) {
int vecA = readBuffer[++i];
int vecB = readBuffer[++i];
int vecC = readBuffer[++i];
vectorObs vobs = new vectorObs();
vobs.timestamp = app.millis();
vobs.sequence = ++vectorSequence;
vobs.vectorValue = vecA*255*255 + vecB*255 + vecC;
vectorBuffer.add(vobs);
}
default: // unknown
// app.println("unknown code " + packetCode);
break;
}
}
}
}