-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThnkrEegDecoder.c
463 lines (370 loc) · 11.5 KB
/
ThnkrEegDecoder.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include "ThnkrEegDecoder.h"
/* Declare private function prototypes */
int parsePacketPayload(
ThnkrEegDecoder *pParser
);
int parseDataRow(
ThnkrEegDecoder* pParser,
unsigned char* rowPtr
);
/*
* See header file for interface documentation.
*/
int ThnkrEegDecoderInit(
ThnkrEegDecoder* pParser,
unsigned char parserType,
void (*handleDataValueFunc) (
unsigned char extendedCodeLevel,
unsigned char code,
unsigned char numBytes,
const unsigned char *value,
void *customData
),
void *customData
) {
if(!pParser) return -1;
/* Initialize the parser's state based on the parser type */
switch(parserType) {
case THNKR_TYPE_PACKETS:
pParser->state = THNKR_STATE_SYNC;
break;
case THNKR_TYPE_2BYTERAW:
pParser->state = THNKR_STATE_WAIT_HIGH;
break;
default:
return -2;
}
/* Save parser type */
pParser->type = parserType;
/* Save user-defined handler function and data pointer */
pParser->handleDataValue = handleDataValueFunc;
pParser->customData = customData;
return 0;
}
int ThnkrEegDecoderParse(
ThnkrEegDecoder* pParser,
unsigned char byte
) {
int returnValue = 0;
if(!pParser) return -1;
// Pick handling according to current state...
switch(pParser->state) {
// Waiting for SyncByte
case THNKR_STATE_SYNC:
if( byte == THNKR_SYNC_BYTE ) {
pParser->state = THNKR_STATE_SYNC_CHECK;
}
break;
// Waiting for second SyncByte
case THNKR_STATE_SYNC_CHECK:
if(byte == THNKR_SYNC_BYTE) {
pParser->state = THNKR_STATE_PAYLOAD_LENGTH;
} else {
pParser->state = THNKR_STATE_SYNC;
}
break;
// Waiting for Data[] length
case THNKR_STATE_PAYLOAD_LENGTH:
pParser->payloadLength = byte;
if(pParser->payloadLength == MAX_PAYLOAD_SIZE) {
pParser->state = THNKR_STATE_SYNC;
returnValue = -2;
} else if(pParser->payloadLength > MAX_PAYLOAD_SIZE) {
pParser->state = THNKR_STATE_SYNC;
returnValue = -3;
} else {
pParser->payloadBytesReceived = 0;
pParser->payloadSum = 0;
pParser->state = THNKR_STATE_PAYLOAD;
}
break;
// Waiting for Payload[] bytes
case THNKR_STATE_PAYLOAD:
pParser->payload[pParser->payloadBytesReceived++] = byte;
pParser->payloadSum = (unsigned char)(pParser->payloadSum + byte);
if(pParser->payloadBytesReceived >= pParser->payloadLength) {
pParser->state = THNKR_STATE_CHKSUM;
}
break;
// Waiting for CKSUM byte
case THNKR_STATE_CHKSUM:
pParser->chksum = byte;
pParser->state = THNKR_STATE_SYNC;
if(pParser->chksum != ((~pParser->payloadSum) & 0xFF)) {
returnValue = -2;
} else {
returnValue = 1;
parsePacketPayload(pParser);
}
break;
// Waiting for high byte of 2-byte raw value
case THNKR_STATE_WAIT_HIGH:
// Check if current byte is a high byte
if((byte & THNKR_CODE_CONNECT) == THNKR_CODE_RAW_SIGNAL) {
// High byte recognized, will be saved as parser->lastByte
pParser->state = THNKR_STATE_WAIT_LOW;
}
break;
// Waiting for low byte of 2-byte raw value
case THNKR_STATE_WAIT_LOW:
// Check if current byte is a valid low byte
if((byte & THNKR_CODE_CONNECT) == THNKR_CODE_LOW_VALUE) {
// Stuff the high and low part of the raw value into an array
pParser->payload[0] = pParser->lastByte;
pParser->payload[1] = byte;
// Notify the handler function of received raw value
if(pParser->handleDataValue) {
pParser->handleDataValue(
0,
THNKR_CODE_RAW_SIGNAL,
2,
pParser->payload,
pParser->customData
);
}
returnValue = 1;
}
// Return to start state waiting for high
pParser->state = THNKR_STATE_WAIT_HIGH;
break;
// unrecognized state
default:
pParser->state = THNKR_STATE_SYNC;
returnValue = -5;
break;
}
// Save current byte
pParser->lastByte = byte;
return returnValue;
return 0;
}
int parsePacketPayload(
ThnkrEegDecoder* pParser
) {
unsigned char i = 0;
unsigned char extendedCodeLevel = 0;
unsigned char code = 0;
unsigned char numBytes = 0;
/* Parse all bytes from the payload[] */
while(i < pParser->payloadLength) {
/* Parse possible EXtended CODE bytes */
while(pParser->payload[i] == THNKR_EXCODE_BYTE) {
extendedCodeLevel++;
i++;
}
/* Parse CODE */
code = pParser->payload[i++];
/* Parse value length */
if(code >= THNKR_CODE_RAW_SIGNAL) {
numBytes = pParser->payload[i++];
} else {
numBytes = 1;
}
/* Call the callback function to handle the DataRow value */
if(pParser->handleDataValue) {
pParser->handleDataValue(
extendedCodeLevel,
code,
numBytes,
pParser->payload + i,
pParser->customData
);
}
i = (unsigned char)(i + numBytes);
}
return 0;
}
void handleDataValueFunc(
unsigned char extendedCodeLevel,
unsigned char code,
unsigned char valueLength,
const unsigned char *value,
void *customData
) {
if(extendedCodeLevel == 0) {
EegData eegItem = {
.attention = 0,
.meditation = 0,
.delta = 0.0f,
.theta = 0.0f,
.lAlpha = 0.0f,
.hAlpha = 0.0f,
.lBeta = 0.0f,
.hBeta = 0.0f,
.lGamma = 0.0f,
.mGamma = 0.0f
};
switch(code) {
case THNKR_CODE_ATTENTION:
eegItem.attention = value[0] & 0xFF;
break;
case THNKR_CODE_MEDITATION:
eegItem.meditation = value[0] & 0xFF;
break;
case THNKR_CODE_ASIC_EEG_POWER_INT:
/**
* This Data Value represents the current magnitude of 8 commonly-recognized types of EEG (brainwaves).
* It is the ASIC equivalent of EEG_POWER, with the main difference being that this Data Value is output as a series of
* eight 3-byte unsigned integers instead of 4-byte floating point numbers.
* These 3-byte unsigned integers are in big-endian format.
**/
eegItem.delta = (value[0] << 16) | (value[1] << 8) | value[2];
eegItem.theta = (value[3] << 16) | (value[4] << 8) | value[5];
eegItem.lAlpha = (value[6] << 16) | (value[7] << 8) | value[8];
eegItem.hAlpha = (value[9] << 16) | (value[10] << 8) | value[11];
eegItem.lBeta = (value[12] << 16) | (value[13] << 8) | value[14];
eegItem.hBeta = (value[15] << 16) | (value[16] << 8) | value[17];
eegItem.lGamma = (value[18] << 16) | (value[19] << 8) | value[20];
eegItem.mGamma = (value[21] << 16) | (value[22] << 8) | value[23];
eegDataQueue.push(&eegDataQueue, eegItem);
break;
/* Other [CODE]s */
default:
// not implemented yet
break;
}
}
}
char* getThnkrDataJSON() {
if(eegDataQueue.size == 0) return "";
EegData eegItem = eegDataQueue.pop(&eegDataQueue);
// {"attention":"","meditation":"","delta":"","theta":"","low_alpha":"","high_alpha":"","low_beta":"","high_beta":"","low_gamma":"","mid_gamma":""}
char num[25];
char* buf = (char*)malloc(255 * sizeof(char));
strncpy(buf, "{\"attention\":\"", 14);
snprintf(num, 25, "%d", eegItem.attention);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"meditation\":\"", 16);
snprintf(num, 25, "%d", eegItem.attention);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"delta\":\"", 11);
snprintf(num, 25, "%d", eegItem.delta);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"theta\":\"", 11);
snprintf(num, 25, "%d", eegItem.theta);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"low_alpha\":\"", 15);
snprintf(num, 25, "%d", eegItem.lAlpha);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"high_alpha\":\"", 16);
snprintf(num, 25, "%d", eegItem.hAlpha);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"low_beta\":\"", 14);
snprintf(num, 25, "%d", eegItem.lBeta);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"high_beta\":\"", 15);
snprintf(num, 25, "%d", eegItem.hBeta);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"low_gamma\":\"", 15);
snprintf(num, 25, "%d", eegItem.lGamma);
strncat(buf, num, strlen(num));
strncat(buf, "\",\"mid_gamma\":\"", 15);
snprintf(num, 25, "%d", eegItem.mGamma);
strncat(buf, num, strlen(num));
strncat(buf, "\"}\0", 3);
return buf;
}
int setInterfaceAttributes(
int fd,
int speed,
int parity
) {
struct termios tty;
memset(&tty, 0, sizeof(tty));
if(tcgetattr(fd, &tty) != 0) return errno;
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if(tcsetattr(fd, TCSANOW, &tty) != 0) return errno;
return 0;
};
void* initialize(void* args) {
eegDataQueue = createQueue();
char conBuf;
dev = open(PORT_NAME, O_RDWR | O_NOCTTY | O_SYNC);
setInterfaceAttributes(dev, BAUD_RATE, 0);
conBuf = (char)THNKR_CODE_DISCONNECT;
write(dev, (char*)&conBuf, 1);
sleep(5);
conBuf = (char)THNKR_CODE_AUTOCONNECT;
write(dev, (char*)&conBuf, 1);
sleep(5);
ThnkrEegDecoder parser;
ThnkrEegDecoderInit(&parser, THNKR_TYPE_PACKETS, handleDataValueFunc, NULL);
size_t bufSize = 1 * sizeof(char);
unsigned char buf = 0;
while(1) {
read(dev, &buf, bufSize);
ThnkrEegDecoderParse(&parser, buf);
}
}
void disconnectAndClose() {
if(dev != 0) {
write(dev, (char*)THNKR_CODE_DISCONNECT, 1);
close(dev);
}
}
void libmain() {
pthread_t tid = NULL;
int err = pthread_create(&tid, NULL, &initialize, NULL);
if(err != 0) printf("\ncan't create thread :[%s]", strerror(err));
}
void push(
Queue* queue,
EegData item
) {
if(queue->size > MAX_QUEUE_SIZE) {
queue->pop(queue);
}
Node* n = (Node*)malloc(sizeof(Node));
n->item = item;
n->next = NULL;
if (queue->head == NULL) { // no head
queue->head = n;
} else{
queue->tail->next = n;
}
queue->tail = n;
queue->size++;
}
EegData pop(
Queue* queue
) {
Node* head = queue->head;
EegData item = head->item;
queue->head = head->next;
queue->size--;
free(head);
return item;
}
EegData peek(
Queue* queue
) {
Node* head = queue->head;
return head->item;
}
Queue createQueue() {
Queue queue;
queue.size = 0;
queue.head = NULL;
queue.tail = NULL;
queue.push = &push;
queue.pop = &pop;
queue.peek = &peek;
return queue;
}