forked from orgicus/Mindwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMindwave.cpp
More file actions
186 lines (153 loc) · 5.19 KB
/
Mindwave.cpp
File metadata and controls
186 lines (153 loc) · 5.19 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
#include "Mindwave.h"
Mindwave::Mindwave() {}
void Mindwave::update(Stream &stream, MindwaveDataCallback onData, MindwaveBlinkCallback onBlink) {
if ((millis() - piekTime) > 450 && piekDetected == true) {
if(onBlink != NULL) onBlink();
piekDetected = false;
}
while (stream.available()) {
switch (_state) {
case MINDWAVE_WAIT_FOR_FIRST_A:
if (stream.read() == 0xAA) _state = MINDWAVE_WAIT_FOR_SECOND_A;
break;
case MINDWAVE_WAIT_FOR_SECOND_A:
if (stream.read() == 0xAA) _state = MINDWAVE_WAIT_FOR_PAYLOAD_LENGTH;
break;
case MINDWAVE_WAIT_FOR_PAYLOAD_LENGTH:
_payloadLength = stream.read();
if (_payloadLength > MINDWAVE_PAYLOAD_MAX_SIZE) {
_state = MINDWAVE_WAIT_FOR_FIRST_A;
return;
}
_generatedChecksum = _payloadIndex = 0;
_state = MINDWAVE_WAIT_FOR_PAYLOAD;
break;
case MINDWAVE_WAIT_FOR_PAYLOAD:
if (_payloadIndex < _payloadLength) {
_payloadData[_payloadIndex] = stream.read();
_generatedChecksum += _payloadData[_payloadIndex];
_payloadIndex++;
} else {
_state = MINDWAVE_WAIT_FOR_CHECKSUM;
}
break;
case MINDWAVE_WAIT_FOR_CHECKSUM:
_checksum = stream.read();
_generatedChecksum = 255 - _generatedChecksum;
if (_checksum == _generatedChecksum) {
parsePayload(onData, onBlink);
_state = MINDWAVE_WAIT_FOR_FIRST_A;
} else {
_state = MINDWAVE_WAIT_FOR_FIRST_A;
}
break;
}
}
}
void Mindwave::parsePayload(MindwaveDataCallback onData, MindwaveBlinkCallback onBlink) {
_poorQuality = 200;
_attention = 0;
_meditation = 0;
for (int j = 0; j < MINDWAVE_EEG_SIZE; j++) _eeg[j] = 0;
for (int i = 0; i < _payloadLength; i++) { // Parse the payload
switch (_payloadData[i]) {
case 2:
i++;
_poorQuality = _payloadData[i];
_bigPacket = true;
break;
case 4:
i++;
_attention = _payloadData[i];
break;
case 5:
i++;
_meditation = _payloadData[i];
break;
case 0x80: // Raw data for blink
if (_payloadData[i + 1] == 2) {
Hilf = ((long)_payloadData[i + 2] * 256 + (long)_payloadData[i + 3]); // read the most significant two bytes and form a signed number of it
if (Hilf > 32767) Hilf -= (long)(65535);
Data[_i] = (int)Hilf;
// PiekP is a gliding sum over 50 values of Data 71 values of Data in the past, 71 values are reserved for the minus peak PiekM
PiekP += Data[(512 + _i - 71) % 512];
PiekP -= Data[(512 + _i - 50 - 71) % 512];
// Test, if PiekP exceeds a certain value and the youngest value of PiekP is negative and it has no huge values
if ((PiekP > 3000) && (Data[(512 + _i - 70) % 512] < 0) && (PiekP < 13000)) { // The next eye blink detection is enabled only after a certain elapse time
if (millis() - piekTime > 100) { //time
PiekM = 0;
// After detecting a positive peak PiekP the following 70 values are summed up and tested, if more negative than a certain value
for (int j = 1; j <= 70; j++)
PiekM += (int)(Data[(512 + _i + j - 70) % 512]);
//Sometimes big negative numbers appear, which are suppressed by a limit for the negative values, if they are to huge
if (PiekM < -3000 && PiekM > -11000) {
if ((millis() - piekTime) < 400)_n++; else _n = 1;
piekTime = millis(); // piekTime is the time at which the eye blink has been detected
piekDetected = true; // piekDetected is set true, when an eye blink has been detected
}// end if PiekM (eyeblink detected)
}//end elapse time
}// end PiekP detect
_i++;
if (_i >= 512) _i = 0;
}
i = i + 3;
break;
case 0x83:
i++;
for (int j = 0; j < MINDWAVE_EEG_SIZE; j++) {
_eeg[j] = ((int)_payloadData[++i] << 16) | ((int)_payloadData[++i] << 8) | (int)_payloadData[++i];
}
break;
default:
break;
} // switch
} // for loop
if (_bigPacket) {
_lastUpdate = millis() - _lastReceivedPacket;
_lastReceivedPacket = millis();
onData();
}
_bigPacket = false;
}
int Mindwave::attention() {
return _attention;
}
int Mindwave::meditation() {
return _meditation;
}
int Mindwave::quality() {
return (100 - (_poorQuality / 2));
}
long Mindwave::lastUpdate() {
return _lastUpdate;
}
int* Mindwave::eeg() {
return _eeg;
}
int Mindwave::delta() {
return _eeg[0];
}
int Mindwave::theta() {
return _eeg[1];
}
int Mindwave::lowAlpha() {
return _eeg[2];
}
int Mindwave::highAlpha() {
return _eeg[3];
}
int Mindwave::lowBeta() {
return _eeg[4];
}
int Mindwave::highBeta() {
return _eeg[5];
}
int Mindwave::lowGamma() {
return _eeg[6];
}
int Mindwave::midGamma() {
return _eeg[7];
}
int Mindwave::blink() {
return _n;
}