/
Mindwave.cpp
125 lines (109 loc) · 3.23 KB
/
Mindwave.cpp
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
/*
Mindwave.cpp
Created by Adrià Navarro at Red Paper Heart
To contact, email hello@redpaperheart.com
Based on the sample code by Neursoky
http://developer.neurosky.com/docs/doku.php?id=mindwave_mobile_and_arduino
For installation and usage instructions check
https://github.com/redpaperheart/ArduinoMindwave
*/
#include "Mindwave.h"
void Mindwave::setup(){
debug = false;
newPacket = false;
//payloadData[64] = {0};
poorQuality = 250;
attention = 0;
meditation = 0;
lastReceivedPacket = 0;
timeOut = 5000;
Serial.begin(BAUDRATE);
}
// Non blocking read
byte Mindwave::readFirstByte() {
int byteRead = 0x00;
if(Serial.available()){
byteRead = Serial.read();
}
return byteRead;
}
// Read data from Serial UART
byte Mindwave::readOneByte() {
int byteRead;
int n = 0;
while(!Serial.available() && n < 10000) n++;
byteRead = Serial.read();
return byteRead;
}
// Parse bytes if available
void Mindwave::update(){
newPacket = false;
// Look for sync bytes
if(readFirstByte() == 170) {
if(readOneByte() == 170) {
byte payloadLength = readOneByte();
if(payloadLength > 169){ //Payload length can not be greater than 169
return;
}
byte generatedChecksum = 0;
for(int i = 0; i < payloadLength; i++) {
payloadData[i] = readOneByte(); //Read payload into memory
generatedChecksum += payloadData[i];
}
byte checksum = readOneByte(); //Read checksum byte from stream
generatedChecksum = 255 - generatedChecksum; //Take one's compliment of generated checksum
if(checksum == generatedChecksum) {
poorQuality = 200;
attention = 0;
meditation = 0;
for(int i = 0; i < payloadLength; i++) { // Parse the payload
switch (payloadData[i]) {
case 2:
i++;
poorQuality = payloadData[i];
newPacket = true;
break;
case 4:
i++;
attention = payloadData[i];
break;
case 5:
i++;
meditation = payloadData[i];
break;
case 0x80:
i = i + 3;
break;
case 0x83:
i = i + 25;
break;
default:
break;
}
}
if(newPacket){
if(debug){
Serial.print("PoorQuality: ");
Serial.print(poorQuality, DEC);
Serial.print(" Attention: ");
Serial.print(attention, DEC);
Serial.print(" Meditation: ");
Serial.print(meditation, DEC);
Serial.print(" Time since last packet: ");
Serial.print((millis() - lastReceivedPacket)/1000.0, 3);
Serial.print("\n");
}
lastReceivedPacket = millis();
}
else if(millis() - lastReceivedPacket > timeOut){
poorQuality = 200;
attention = 0;
meditation = 0;
}
}
else {
// Checksum Error
} // end if else for checksum
} // end if read 0xAA byte
} // end if read 0xAA byte
}