/
PluginHandler.cs
488 lines (448 loc) · 11.4 KB
/
PluginHandler.cs
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace lucidcode.LucidScribe.Plugin.Mattel.Mindflex
{
public static class Device
{
private static SerialPort serialPort;
private static bool initialized;
private static bool initError;
private static int attention;
private static int meditation;
private static int lowAlpha;
private static int highAlpha;
private static int lowBeta;
private static int highBeta;
private static int lowGamma;
private static int midGamma;
private static int delta;
private static int theta;
private static int index = 0;
private static int lastByte = -1;
private static int value = 0;
private static int[] packetData = new int[64];
public static Boolean Initialize()
{
try
{
if (!initialized && !initError)
{
PortForm formPort = new PortForm();
if (formPort.ShowDialog() == DialogResult.OK)
{
try
{
// Open the COM port
serialPort = new SerialPort(formPort.SelectedPort);
serialPort.BaudRate = 9600;
serialPort.Parity = Parity.None;
serialPort.DataBits = 8;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.None;
serialPort.ReadTimeout = 500;
serialPort.WriteTimeout = 500;
serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived);
serialPort.Open();
initialized = true;
}
catch (Exception ex)
{
if (!initError)
{
MessageBox.Show(ex.Message, "LucidScribe.InitializePlugin()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
initError = true;
}
}
else
{
initError = true;
return false;
}
}
return true;
}
catch (Exception ex)
{
initError = true;
throw (new Exception("The 'NeuroSky' plugin failed to initialize: " + ex.Message));
}
}
static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
while (serialPort.BytesToRead > 0)
{
int num = serialPort.ReadByte();
if ((lastByte == 170) & (num == 170)) //0xAA = 170 decimal
{
index = 1;
}
lastByte = num;
packetData[index] = num;
// Sample data
// 0 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 (index)
//aa aa 20 02 33 83 18 0b f3 7b 03 b2 1c 00 f4 56 00 f6 42 01 00 4a 01 0b e9 00 5d 19 00 6a 85 04 00 05 00 b5
//aa aa 20 02 33 83 18 04 d2 2b 00 32 5b 00 0c 56 00 08 49 00 04 f9 00 02 22 00 00 c3 00 00 7d 04 00 05 00 84
//aa aa 20 02 33 83 18 05 75 dc 00 97 ee 00 25 1f 00 9d f0 00 ac 67 00 8c 7d 00 3f 77 00 2e 74 04 00 05 00 06
//aa aa 20 02 33 83 18 0a db ef 11 72 b6 01 70 e4 03 16 4f 04 96 7d 06 b7 6f 0b 71 d0 09 68 34 04 00 05 00 28
//aa aa 20 02 33 83 18 0a b2 21 03 09 50 00 ed 5f 01 df 51 02 c2 69 02 21 6a 00 6c 94 00 59 32 04 00 05 00 2b
//Notes
// 02 flags signalQuality 05 flags meditation
//83 flags eeg power (8*3byte unsigned integers) 04 flags attention
if (index == 4)
{
//signalQuality = num;
}
if (index == 32)
{
attention = num * 4;
}
if (index == 34)
{
meditation = num * 4;
ProcessPacket();
index = -1; //soon to be 0
}
index++;
}
}
catch (Exception ex)
{
try
{
serialPort.DataReceived -= serialPort_DataReceived;
serialPort.Close();
}
catch (Exception ex2)
{
}
MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace, "OpenEEG.DataReceived()", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static void ProcessPacket()
{
int[] eegPower = new int[8];
for (byte i = 0; i < 35; i++) {
if (packetData[i] == 131)
{
for (int j = 0; j < 8; j++)
{
eegPower[j] = (packetData[++i] << 16) | (packetData[++i] << 8) | packetData[++i];
}
}
}
delta = eegPower[0] / 1000;
theta = eegPower[1] / 1000;
lowAlpha= eegPower[2] / 100;
highAlpha = eegPower[3] / 100;
lowBeta = eegPower[4] / 100;
highBeta = eegPower[5] / 100;
lowGamma = eegPower[6] / 100;
midGamma = eegPower[7] / 100;
}
public static Double GetREM()
{
return 0;
}
public static Double GetAttention()
{
return attention;
}
public static Double GetMeditation()
{
return meditation;
}
public static Double GetLowAlpha()
{
return lowAlpha;
}
public static Double GetHighAlpha()
{
return highAlpha;
}
public static Double GetLowBeta()
{
return lowBeta;
}
public static Double GetHighBeta()
{
return highBeta;
}
public static Double GetLowGamma()
{
return lowGamma;
}
public static Double GetMidGamma()
{
return midGamma;
}
public static Double GetDelta()
{
return delta;
}
public static Double GetTheta()
{
return theta;
}
}
namespace Delta
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF Delta"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetDelta();
if (dblValue > 999) { dblValue = 999; }
if (dblValue < 0) { dblValue = 0; }
return dblValue;
}
}
}
}
namespace RapidEyeMovement
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
List<int> m_arrHistory = new List<int>();
public override string Name
{
get { return "Mindflex REM"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
return 0;
}
}
}
}
namespace Attention
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF Attention"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetAttention();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
namespace Meditation
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF Meditation"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetMeditation();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
namespace Theta
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF Theta"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetTheta();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
namespace LowAlpha
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF Low Alpha"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetLowAlpha();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
namespace HighAlpha
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF High Alpha"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetHighAlpha();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
namespace LowBeta
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF Low Beta"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetLowBeta();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
namespace HighBeta
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF High Beta"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetHighBeta();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
namespace LowGamma
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF Low Gamma"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetLowGamma();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
namespace MidGamma
{
public class PluginHandler : lucidcode.LucidScribe.Interface.LucidPluginBase
{
public override string Name
{
get { return "MF Mid Gamma"; }
}
public override bool Initialize()
{
return Device.Initialize();
}
public override double Value
{
get
{
double dblValue = Device.GetMidGamma();
if (dblValue > 999) { dblValue = 999; }
return dblValue;
}
}
}
}
}