-
Notifications
You must be signed in to change notification settings - Fork 82
/
OpenBCI_32bit_Library.cpp
2857 lines (2494 loc) · 102 KB
/
OpenBCI_32bit_Library.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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
OpenBCI 32bit Library
Place the containing folder into your libraries folder insdie the arduino folder in your Documents folder
This library will work with a single OpenBCI 32bit board, or
an OpenBCI 32bit board with an OpenBCI Daisy Module attached.
*/
#include "OpenBCI_32bit_Library.h"
/***************************************************/
/** PUBLIC METHODS *********************************/
/***************************************************/
// CONSTRUCTOR
OpenBCI_32bit_Library::OpenBCI_32bit_Library() {
curBoardMode = OPENBCI_BOARD_MODE_DEFAULT;
daisyPresent = false;
sniffMode = false;
useAccel = false;
useAux = false;
channelDataAvailable = false;
initializeVariables();
}
/**
* @description: The function the OpenBCI board will call in setup.
* @author: AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::begin(void) {
curBoardMode = OPENBCI_BOARD_MODE_DEFAULT;
// Bring the board up
boardBegin();
}
/**
* @description: The function the OpenBCI board will call in setup. Turns sniff mode
* on and allows you to tap into the serial port that is broken out on the OpenBCI
* 32bit board. You must alter this file:
* On Mac:
* `/Users/username/Documents/Arduino/hardware/chipkit-core/pic32/variants/openbci/Board_Defs.h`
* On Windows:
* `C:\Users\username\Documents\Arduino\hardware\chipkit-core\pic32\variants\openbci\Board_Defs.h`
* Specifically lines `311` and `313` from `7` and `10` to `11` and `12` for
* `_SER1_TX_PIN` and `_SER1_RX_PIN` respectively. Check out this sweet gif if
* you are a visual person http://g.recordit.co/3jH01sMD6Y.gif
* You will need to reflash your board! But now you can connect to pins `11`
* `12` via a FTDI serial port driver, really any serial to USB driver would
* work. Remember to use 3V3, 115200 baud, and have a common ground!
* @author: AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::beginDebug(void) {
// Bring the board up
boolean started = boardBeginDebug();
curBoardMode = OPENBCI_BOARD_MODE_DEBUG;
sniffMode = true;
if (Serial1) {
if (started) {
Serial1.println("Board up");
} else {
Serial1.println("Board err");
}
}
}
/**
* @description: The function the OpenBCI board will call in setup. This sets up the hardware serial port on D11 and D12
* @author: AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_32bit_Library::beginSecondarySerial(void) {
// Initalize the serial 1 port
Serial1.begin(OPENBCI_BAUD_RATE);
return true;
}
/**
* @description Called in every `loop()` and checks `Serial0`
* @returns {boolean} - `true` if there is data ready to be read
*/
boolean OpenBCI_32bit_Library::hasDataSerial0(void) {
if (Serial0.available()) {
return true;
} else {
return false;
}
}
/**
* @description Called in every `loop()` and checks `Serial0`
* @returns {boolean} - `true` if there is data ready to be read
*/
boolean OpenBCI_32bit_Library::hasDataSerial1(void) {
if (Serial1.available()) {
return true;
} else {
return false;
}
}
/**
* @description Called if `hasDataSerial0` is true, returns a char from `Serial0`
* @returns {char} - A char from the serial port
*/
char OpenBCI_32bit_Library::getCharSerial0(void) {
return Serial0.read();
}
/**
* @description Called if `hasDataSerial1` is true, returns a char from `Serial1`
* @returns {char} - A char from the serial port
*/
char OpenBCI_32bit_Library::getCharSerial1(void) {
return Serial1.read();
}
/**
* @description If `isSerialAvailableForRead()` is `true` then this function is
* called. Reads from `Serial0` first and foremost, which comes from the RFduino.
* If `sniffMode` is true and `Serial0` didn't have any data, we will try to
* read from `Serial1`. If both are not available then we will return a `0x00`
* which is NOT a command that the system will recognize, aka this function has
* many safe guards.
* @returns {char} - The character from the serial port.
*/
// char OpenBCI_32bit_Library::readOneSerialChar(void) {
// if (Serial0.available()) {
// return Serial0.read();
// } else if (sniffMode && Serial1.available()) {
// return Serial1.read();
// } else {
// return 0x00;
// }
// }
/**
* @description Public function for sending data to the PC
* @param data {char *} - The data you want to send
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::writeSerial(char *data, int len) {
for (int i = 0; i < len; i++) {
Serial0.write(data[i]);
}
}
/**
* @description While processing incoming multi byte messages these will turn
* true.
* @return {boolean} - True if processing a message and false otherwise
*/
boolean OpenBCI_32bit_Library::isProcessingMultibyteMsg(void) {
return isProcessingIncomingSettingsChannel || isProcessingIncomingSettingsLeadOff || settingBoardMode;
}
/**
* @description Process one char at a time from serial port. This is the main
* command processor for the OpenBCI system. Considered mission critical for
* normal operation.
* @param `character` {char} - The character to process.
* @return {boolean} - `true` if the command was recognized, `false` if not
*/
boolean OpenBCI_32bit_Library::processChar(char character) {
if (sniffMode && Serial1) {
Serial1.print("pC: "); Serial1.println(character);
}
if (isProcessingMultibyteMsg()) {
if (isProcessingIncomingSettingsChannel) {
processIncomingChannelSettings(character);
} else if (isProcessingIncomingSettingsLeadOff) {
processIncomingLeadOffSettings(character);
} else if (settingBoardMode) {
processIncomingBoardMode(character);
}
} else { // Normal...
switch (character){
//TURN CHANNELS ON/OFF COMMANDS
case OPENBCI_CHANNEL_OFF_1:
streamSafeChannelDeactivate(1);
break;
case OPENBCI_CHANNEL_OFF_2:
streamSafeChannelDeactivate(2);
break;
case OPENBCI_CHANNEL_OFF_3:
streamSafeChannelDeactivate(3);
break;
case OPENBCI_CHANNEL_OFF_4:
streamSafeChannelDeactivate(4);
break;
case OPENBCI_CHANNEL_OFF_5:
streamSafeChannelDeactivate(5);
break;
case OPENBCI_CHANNEL_OFF_6:
streamSafeChannelDeactivate(6);
break;
case OPENBCI_CHANNEL_OFF_7:
streamSafeChannelDeactivate(7);
break;
case OPENBCI_CHANNEL_OFF_8:
streamSafeChannelDeactivate(8);
break;
case OPENBCI_CHANNEL_OFF_9:
streamSafeChannelDeactivate(9);
break;
case OPENBCI_CHANNEL_OFF_10:
streamSafeChannelDeactivate(10);
break;
case OPENBCI_CHANNEL_OFF_11:
streamSafeChannelDeactivate(11);
break;
case OPENBCI_CHANNEL_OFF_12:
streamSafeChannelDeactivate(12);
break;
case OPENBCI_CHANNEL_OFF_13:
streamSafeChannelDeactivate(13);
break;
case OPENBCI_CHANNEL_OFF_14:
streamSafeChannelDeactivate(14);
break;
case OPENBCI_CHANNEL_OFF_15:
streamSafeChannelDeactivate(15);
break;
case OPENBCI_CHANNEL_OFF_16:
streamSafeChannelDeactivate(16);
break;
case OPENBCI_CHANNEL_ON_1:
streamSafeChannelActivate(1);
break;
case OPENBCI_CHANNEL_ON_2:
streamSafeChannelActivate(2);
break;
case OPENBCI_CHANNEL_ON_3:
streamSafeChannelActivate(3);
break;
case OPENBCI_CHANNEL_ON_4:
streamSafeChannelActivate(4);
break;
case OPENBCI_CHANNEL_ON_5:
streamSafeChannelActivate(5);
break;
case OPENBCI_CHANNEL_ON_6:
streamSafeChannelActivate(6);
break;
case OPENBCI_CHANNEL_ON_7:
streamSafeChannelActivate(7);
break;
case OPENBCI_CHANNEL_ON_8:
streamSafeChannelActivate(8);
break;
case OPENBCI_CHANNEL_ON_9:
streamSafeChannelActivate(9);
break;
case OPENBCI_CHANNEL_ON_10:
streamSafeChannelActivate(10);
break;
case OPENBCI_CHANNEL_ON_11:
streamSafeChannelActivate(11);
break;
case OPENBCI_CHANNEL_ON_12:
streamSafeChannelActivate(12);
break;
case OPENBCI_CHANNEL_ON_13:
streamSafeChannelActivate(13);
break;
case OPENBCI_CHANNEL_ON_14:
streamSafeChannelActivate(14);
break;
case OPENBCI_CHANNEL_ON_15:
streamSafeChannelActivate(15);
break;
case OPENBCI_CHANNEL_ON_16:
streamSafeChannelActivate(16);
break;
// TEST SIGNAL CONTROL COMMANDS
case OPENBCI_TEST_SIGNAL_CONNECT_TO_GROUND:
activateAllChannelsToTestCondition(ADSINPUT_SHORTED,ADSTESTSIG_NOCHANGE,ADSTESTSIG_NOCHANGE);
break;
case OPENBCI_TEST_SIGNAL_CONNECT_TO_PULSE_1X_SLOW:
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_1X,ADSTESTSIG_PULSE_SLOW);
break;
case OPENBCI_TEST_SIGNAL_CONNECT_TO_PULSE_1X_FAST:
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_1X,ADSTESTSIG_PULSE_FAST);
break;
case OPENBCI_TEST_SIGNAL_CONNECT_TO_DC:
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_DCSIG);
break;
case OPENBCI_TEST_SIGNAL_CONNECT_TO_PULSE_2X_SLOW:
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_PULSE_SLOW);
break;
case OPENBCI_TEST_SIGNAL_CONNECT_TO_PULSE_2X_FAST:
activateAllChannelsToTestCondition(ADSINPUT_TESTSIG,ADSTESTSIG_AMP_2X,ADSTESTSIG_PULSE_FAST);
break;
// CHANNEL SETTING COMMANDS
case OPENBCI_CHANNEL_CMD_SET: // This is the first byte that tells us to expect more commands
isProcessingIncomingSettingsChannel = true;
numberOfIncomingSettingsProcessedChannel = 1;
break;
// LEAD OFF IMPEDANCE DETECTION COMMANDS
case OPENBCI_CHANNEL_IMPEDANCE_SET:
isProcessingIncomingSettingsLeadOff = true;
numberOfIncomingSettingsProcessedLeadOff = 1;
break;
case OPENBCI_CHANNEL_DEFAULT_ALL_SET: // reset all channel settings to default
if(!streaming) {
Serial0.println("updating channel settings to default");
sendEOT();
}
streamSafeSetAllChannelsToDefault();
break;
case OPENBCI_CHANNEL_DEFAULT_ALL_REPORT: // report the default settings
reportDefaultChannelSettings();
break;
// DAISY MODULE COMMANDS
case OPENBCI_CHANNEL_MAX_NUMBER_8: // use 8 channel mode
if(daisyPresent){
removeDaisy();
}
break;
case OPENBCI_CHANNEL_MAX_NUMBER_16: // use 16 channel mode
if(daisyPresent == false){
attachDaisy();
}
if(daisyPresent){
Serial0.print("16");
}else{
Serial0.print("8");
}
sendEOT();
break;
// STREAM DATA AND FILTER COMMANDS
case OPENBCI_STREAM_START: // stream data
if(useAccel){
enable_accel(RATE_25HZ);
} // fire up the accelerometer if you want it
streamStart(); // turn on the fire hose
break;
case OPENBCI_STREAM_STOP: // stop streaming data
if(useAccel){
disable_accel();
} // shut down the accelerometer if you're using it
streamStop();
break;
// INITIALIZE AND VERIFY
case OPENBCI_MISC_SOFT_RESET:
boardReset(); // initialize ADS and read device IDs
break;
// QUERY THE ADS AND ACCEL REGITSTERS
case OPENBCI_MISC_QUERY_REGISTER_SETTINGS:
if (!streaming) {
printAllRegisters(); // print the ADS and accelerometer register values
}
break;
// TIME SYNC
case OPENBCI_TIME_SET:
// Set flag to send time packet
if (streaming) {
sendTimeSyncUpPacket = true;
} else {
Serial0.print("Time stamp ON");
sendEOT();
}
timeSynced = true;
break;
case OPENBCI_TIME_STOP:
// Stop the Sync
timeSynced = false;
if (!streaming) {
Serial0.print("Time stamp OFF");
sendEOT();
}
break;
// PACKET SET TYPE
case OPENBCI_BOARD_MODE_SET:
// Tell this function which case that is
settingBoardMode = true;
break;
default:
return false;
}
}
return true;
}
/**
* @description Reads a status register to see if there is new accelerometer
* data.
* @returns {boolean} `true` if the accelerometer has new data.
*/
boolean OpenBCI_32bit_Library::accelHasNewData(void) {
return LIS3DH_DataAvailable();
}
/**
* @description Reads from the accelerometer to get new X, Y, and Z data. Updates
* the global array `axisData`.
*/
void OpenBCI_32bit_Library::accelUpdateAxisData(void) {
LIS3DH_updateAxisData();
}
/**
* @description Reads from the accelerometer to get new X, Y, and Z data.
*/
void OpenBCI_32bit_Library::accelWriteAxisData(void) {
LIS3DH_writeAxisData();
}
/**
* @description: This is a function that is called once and confiures all pins on
* the PIC32 uC
* @author: AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_32bit_Library::boardBegin(void) {
// Initalize the serial port baud rate
Serial0.begin(OPENBCI_BAUD_RATE);
pinMode(OPENBCI_PIN_LED, OUTPUT);
pinMode(OPENBCI_PIN_PGC, OUTPUT);
// Startup for interrupt
setIntVector(_EXTERNAL_4_VECTOR, ADS_DRDY_Service); // connect interrupt to ISR
setIntPriority(_EXTERNAL_4_VECTOR, 4, 0); // set interrupt priority and sub priority
clearIntFlag(_EXTERNAL_4_IRQ); // these two need to be done together
setIntEnable(_EXTERNAL_4_IRQ); // clear any flags before enabing the irq
// Do a soft reset
boardReset();
return true;
}
/**
* @description: This is a function that is called once and confiures all pins on
* the PIC32 uC
* @author: AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_32bit_Library::boardBeginDebug(void) {
// Initalize the serial port baud rate
Serial0.begin(OPENBCI_BAUD_RATE);
// Initalize the serial debug port
Serial1.begin(OPENBCI_BAUD_RATE);
// Startup for interrupt
setIntVector(_EXTERNAL_4_VECTOR, ADS_DRDY_Service); // connect interrupt to ISR
setIntPriority(_EXTERNAL_4_VECTOR, 4, 0); // set interrupt priority and sub priority
clearIntFlag(_EXTERNAL_4_IRQ); // these two need to be done together
setIntEnable(_EXTERNAL_4_IRQ); // clear any flags before enabing the irq
// Do a soft reset
boardReset();
return true;
}
/**
* @description: This is a function that is called once and confiures the Pic to run in secondary serial mode
* @param baudRate {int} - The baudRate you want the secondary serial port to run at.
* @author: AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_32bit_Library::boardBeginDebug(int baudRate) {
// Initalize the serial port baud rate
Serial0.begin(OPENBCI_BAUD_RATE);
// Initalize the serial debug port
Serial1.begin(baudRate);
// Do a soft reset
boardReset();
return true;
}
/**
* @description: This is a function that can be called multiple times, this is
* what we refer to as a `soft reset`. You will hear/see this
* many times.
* @author: AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::boardReset(void) {
initialize(); // initalizes accelerometer and on-board ADS and on-daisy ADS if present
delay(500);
configureLeadOffDetection(LOFF_MAG_6NA, LOFF_FREQ_31p2HZ);
Serial0.println("OpenBCI V3 8-16 channel");
Serial0.print("On Board ADS1299 Device ID: 0x"); Serial0.println(ADS_getDeviceID(ON_BOARD),HEX);
if(daisyPresent){ // library will set this in initialize() if daisy present and functional
Serial0.print("On Daisy ADS1299 Device ID: 0x"); Serial0.println(ADS_getDeviceID(ON_DAISY),HEX);
}
Serial0.print("LIS3DH Device ID: 0x"); Serial0.println(LIS3DH_getDeviceID(),HEX);
Serial0.println("Firmware: v2.0.1");
sendEOT();
}
/**
* @description: Simple method to send the EOT over serial...
* @author: AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::sendEOT(void) {
Serial0.print("$$$");
}
void OpenBCI_32bit_Library::activateAllChannelsToTestCondition(byte testInputCode, byte amplitudeCode, byte freqCode)
{
boolean wasStreaming = streaming;
// Stop streaming if you are currently streaming
if (streaming) {
streamStop();
}
//set the test signal to the desired state
configureInternalTestSignal(amplitudeCode,freqCode);
//change input type settings for all channels
changeInputType(testInputCode);
// Restart stream if need be
if (wasStreaming) {
streamStart();
}
}
/**
* @description When a 'z' is found on the serial port, we jump to this function
* where we continue to read from the serial port and read the
* remaining 4 bytes.
* @param `character` - {char} - The character you want to process...
*/
void OpenBCI_32bit_Library::processIncomingBoardMode(char c) {
if (isValidBoardType(c)) {
curBoardMode = c;
if (!streaming) {
Serial0.print("Success: Board type set");
sendEOT();
}
} else {
if (!streaming) {
Serial0.print("Failure: invalid board mode");
sendEOT();
}
}
settingBoardMode = false;
}
/**
* @description When a 'x' is found on the serial port, we jump to this function
* where we continue to read from the serial port and read the
* remaining 7 bytes.
*/
void OpenBCI_32bit_Library::processIncomingChannelSettings(char character) {
if (character == OPENBCI_CHANNEL_CMD_LATCH && numberOfIncomingSettingsProcessedChannel < OPENBCI_NUMBER_OF_BYTES_SETTINGS_CHANNEL - 1) {
// We failed somehow and should just abort
numberOfIncomingSettingsProcessedChannel = 0;
// put flag back down
isProcessingIncomingSettingsChannel = false;
if (!streaming) {
Serial0.print("Channel setting failure: too few chars"); sendEOT();
}
return;
}
switch (numberOfIncomingSettingsProcessedChannel) {
case 1: // channel number
currentChannelSetting = getChannelCommandForAsciiChar(character);
break;
case 2: // POWER_DOWN
channelSettings[currentChannelSetting][POWER_DOWN] = getNumberForAsciiChar(character);
break;
case 3: // GAIN_SET
channelSettings[currentChannelSetting][GAIN_SET] = getGainForAsciiChar(character);
break;
case 4: // INPUT_TYPE_SET
channelSettings[currentChannelSetting][INPUT_TYPE_SET] = getNumberForAsciiChar(character);
break;
case 5: // BIAS_SET
channelSettings[currentChannelSetting][BIAS_SET] = getNumberForAsciiChar(character);
break;
case 6: // SRB2_SET
channelSettings[currentChannelSetting][SRB2_SET] = getNumberForAsciiChar(character);
break;
case 7: // SRB1_SET
channelSettings[currentChannelSetting][SRB1_SET] = getNumberForAsciiChar(character);
break;
case 8: // 'X' latch
if (character != OPENBCI_CHANNEL_CMD_LATCH) {
if (!streaming) {
Serial0.print("Err: 9th char not ");
Serial0.println(OPENBCI_CHANNEL_CMD_LATCH);
sendEOT();
}
// We failed somehow and should just abort
numberOfIncomingSettingsProcessedChannel = 0;
// put flag back down
isProcessingIncomingSettingsChannel = false;
}
break;
default: // should have exited
if (!streaming) {
Serial0.print("Err: too many chars");
sendEOT();
}
// We failed somehow and should just abort
numberOfIncomingSettingsProcessedChannel = 0;
// put flag back down
isProcessingIncomingSettingsChannel = false;
return;
}
// increment the number of bytes processed
numberOfIncomingSettingsProcessedChannel++;
if (numberOfIncomingSettingsProcessedChannel == (OPENBCI_NUMBER_OF_BYTES_SETTINGS_CHANNEL)) {
// We are done processing channel settings...
if (!streaming) {
Serial0.print("Channel set for "); Serial0.println(currentChannelSetting + 1); sendEOT();
}
if (sniffMode && Serial1) {
Serial1.print("Channel set for "); Serial1.println(currentChannelSetting + 1);
}
// Set channel settings
streamSafeChannelSettingsForChannel(currentChannelSetting + 1, channelSettings[currentChannelSetting][POWER_DOWN], channelSettings[currentChannelSetting][GAIN_SET], channelSettings[currentChannelSetting][INPUT_TYPE_SET], channelSettings[currentChannelSetting][BIAS_SET], channelSettings[currentChannelSetting][SRB2_SET], channelSettings[currentChannelSetting][SRB1_SET]);
// Reset
numberOfIncomingSettingsProcessedChannel = 0;
// put flag back down
isProcessingIncomingSettingsChannel = false;
}
}
/**
* @description When a 'z' is found on the serial port, we jump to this function
* where we continue to read from the serial port and read the
* remaining 4 bytes.
* @param `character` - {char} - The character you want to process...
*/
void OpenBCI_32bit_Library::processIncomingLeadOffSettings(char character) {
if (character == OPENBCI_CHANNEL_IMPEDANCE_LATCH && numberOfIncomingSettingsProcessedLeadOff < OPENBCI_NUMBER_OF_BYTES_SETTINGS_LEAD_OFF - 1) {
// We failed somehow and should just abort
// reset numberOfIncomingSettingsProcessedLeadOff
numberOfIncomingSettingsProcessedLeadOff = 0;
// put flag back down
isProcessingIncomingSettingsLeadOff = false;
if (!streaming) {
Serial0.print("Lead off failure: too few chars"); sendEOT();
}
return;
}
switch (numberOfIncomingSettingsProcessedLeadOff) {
case 1: // channel number
currentChannelSetting = getChannelCommandForAsciiChar(character);
break;
case 2: // pchannel setting
leadOffSettings[currentChannelSetting][PCHAN] = getNumberForAsciiChar(character);
break;
case 3: // nchannel setting
leadOffSettings[currentChannelSetting][NCHAN] = getNumberForAsciiChar(character);
break;
case 4: // 'Z' latch
if (character != OPENBCI_CHANNEL_IMPEDANCE_LATCH) {
if (!streaming) {
Serial0.print("Err: 5th char not ");
Serial0.println(OPENBCI_CHANNEL_IMPEDANCE_LATCH);
sendEOT();
}
// We failed somehow and should just abort
// reset numberOfIncomingSettingsProcessedLeadOff
numberOfIncomingSettingsProcessedLeadOff = 0;
// put flag back down
isProcessingIncomingSettingsLeadOff = false;
}
break;
default: // should have exited
if (!streaming) {
Serial0.print("Err: too many chars ");
sendEOT();
}
// We failed somehow and should just abort
// reset numberOfIncomingSettingsProcessedLeadOff
numberOfIncomingSettingsProcessedLeadOff = 0;
// put flag back down
isProcessingIncomingSettingsLeadOff = false;
return;
}
// increment the number of bytes processed
numberOfIncomingSettingsProcessedLeadOff++;
if (numberOfIncomingSettingsProcessedLeadOff == (OPENBCI_NUMBER_OF_BYTES_SETTINGS_LEAD_OFF)) {
// We are done processing lead off settings...
if (!streaming) {
Serial0.print("Lead off set for "); Serial0.println(currentChannelSetting + 1); sendEOT();
}
if (sniffMode && Serial1) {
Serial1.print("Lead off set for "); Serial1.println(currentChannelSetting + 1);
}
// Set lead off settings
streamSafeLeadOffSetForChannel(currentChannelSetting + 1,leadOffSettings[currentChannelSetting][PCHAN],leadOffSettings[currentChannelSetting][NCHAN]);
// reset numberOfIncomingSettingsProcessedLeadOff
numberOfIncomingSettingsProcessedLeadOff = 0;
// put flag back down
isProcessingIncomingSettingsLeadOff = false;
}
}
boolean OpenBCI_32bit_Library::isValidBoardType(char c) {
switch (c) {
case OPENBCI_BOARD_MODE_DEFAULT:
case OPENBCI_BOARD_MODE_DEBUG:
case OPENBCI_BOARD_MODE_WIFI:
case OPENBCI_BOARD_MODE_INPUT_ANALOG:
case OPENBCI_BOARD_MODE_INPUT_DIGITAL:
return true;
default:
return false;
}
}
// <<<<<<<<<<<<<<<<<<<<<<<<< BOARD WIDE FUNCTIONS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
void OpenBCI_32bit_Library::initialize(){
pinMode(SD_SS,OUTPUT); digitalWrite(SD_SS,HIGH); // de-select SDcard if present
pinMode(BOARD_ADS, OUTPUT); digitalWrite(BOARD_ADS,HIGH);
pinMode(DAISY_ADS, OUTPUT); digitalWrite(DAISY_ADS,HIGH);
pinMode(LIS3DH_SS,OUTPUT); digitalWrite(LIS3DH_SS,HIGH);
spi.begin();
spi.setSpeed(4000000); // use 4MHz for ADS and LIS3DH
spi.setMode(DSPI_MODE0); // default to SD card mode!
initialize_ads(); // hard reset ADS, set pin directions
initialize_accel(SCALE_4G); // set pin directions, G scale, DRDY interrupt, power down
initializeVariables();
}
// void __USER_ISR ADS_DRDY_Service() {
void __USER_ISR ADS_DRDY_Service() {
clearIntFlag(_EXTERNAL_4_IRQ); // clear the irq, or else it will continually interrupt!
if(bitRead(PORTA,0) == 0){
board.channelDataAvailable = true;
}
}
void OpenBCI_32bit_Library::initializeVariables(void) {
streaming = false;
sendTimeSyncUpPacket = false;
timeSynced = false;
isProcessingIncomingSettingsChannel = false;
isProcessingIncomingSettingsLeadOff = false;
settingBoardMode = false;
numberOfIncomingSettingsProcessedChannel = 0;
numberOfIncomingSettingsProcessedLeadOff = 0;
currentChannelSetting = 0;
timeOffset = 0;
timeSetCharArrived = 0;
streamPacketType = (char)OPENBCI_PACKET_TYPE_V3;
verbosity = false; // when verbosity is true, there will be Serial feedback
}
void OpenBCI_32bit_Library::printAllRegisters(){
if(!isRunning){
Serial0.println("\nBoard ADS Registers");
printADSregisters(BOARD_ADS);
if(daisyPresent){
Serial0.println("\nDaisy ADS Registers");
printADSregisters(DAISY_ADS);
}
Serial0.println("\nLIS3DH Registers");
LIS3DH_readAllRegs();
sendEOT();
}
}
/**
* @description Writes channel data and `axisData` array to serial port in
* the correct stream packet format.
*
* Adds stop byte `OPENBCI_EOP_STND_ACCEL`. See `OpenBCI_32bit_Library_Definitions.h`
*/
void OpenBCI_32bit_Library::sendChannelDataWithAccel(void) {
Serial0.write('A'); // 0x41
Serial0.write(sampleCounter); // 1 byte
ADS_writeChannelData(); // 24 bytes
accelWriteAxisData(); // 6 bytes
Serial0.write(OPENBCI_EOP_STND_ACCEL); // 0xC0
sampleCounter++;
}
/**
* @description Writes channel data and `auxData` array to serial port in
* the correct stream packet format.
*
* Adds stop byte `OPENBCI_EOP_STND_RAW_AUX`. See `OpenBCI_32bit_Library_Definitions.h`
*/
void OpenBCI_32bit_Library::sendChannelDataWithRawAux(void) {
Serial0.write('A'); // 1 byte
Serial0.write(sampleCounter); // 1 byte
ADS_writeChannelData(); // 24 bytes
writeAuxData(); // 6 bytes
Serial0.write(OPENBCI_EOP_STND_RAW_AUX); // 0xC1 - 1 byte
sampleCounter++;
}
/**
* @description Writes channel data, `axisData` array, and 4 byte unsigned time
* stamp in ms to serial port in the correct stream packet format.
*
* `axisData` will be split up and sent on the samples with `sampleCounter` of
* 7, 8, and 9 for X, Y, and Z respectively. Driver writers parse accordingly.
*
* If the global variable `sendTimeSyncUpPacket` is `true` (set by `processChar`
* getting a time sync set `<` command) then:
* Adds stop byte `OPENBCI_EOP_ACCEL_TIME_SET` and sets `sendTimeSyncUpPacket`
* to `false`.
* Else if `sendTimeSyncUpPacket` is `false` then:
* Adds stop byte `OPENBCI_EOP_ACCEL_TIME_SYNCED`
*/
void OpenBCI_32bit_Library::sendChannelDataWithTimeAndAccel(void) {
Serial0.write('A');
Serial0.write(sampleCounter); // 1 byte
ADS_writeChannelData(); // 24 bytes
// send two bytes of either accel data or blank
switch (sampleCounter % 10) {
case ACCEL_AXIS_X: // 7
LIS3DH_writeAxisDataForAxis(ACCEL_AXIS_X);
break;
case ACCEL_AXIS_Y: // 8
LIS3DH_writeAxisDataForAxis(ACCEL_AXIS_Y);
break;
case ACCEL_AXIS_Z: // 9
LIS3DH_writeAxisDataForAxis(ACCEL_AXIS_Z);
break;
default:
Serial0.write((byte)0x00); // high byte
Serial0.write((byte)0x00); // low byte
break;
}
writeTimeCurrent(); // 4 bytes
if (sendTimeSyncUpPacket) {
sendTimeSyncUpPacket = false;
Serial0.write(OPENBCI_EOP_ACCEL_TIME_SET); // 0xC3
} else {
Serial0.write(OPENBCI_EOP_ACCEL_TIME_SYNCED); // 0xC4
}
sampleCounter++;
}
/**
* @description Writes channel data, `auxData[0]` 2 bytes, and 4 byte unsigned
* time stamp in ms to serial port in the correct stream packet format.
*
* If the global variable `sendTimeSyncUpPacket` is `true` (set by `processChar`
* getting a time sync set `<` command) then:
* Adds stop byte `OPENBCI_EOP_RAW_AUX_TIME_SET` and sets `sendTimeSyncUpPacket`
* to `false`.
* Else if `sendTimeSyncUpPacket` is `false` then:
* Adds stop byte `OPENBCI_EOP_RAW_AUX_TIME_SYNCED`
*/
void OpenBCI_32bit_Library::sendChannelDataWithTimeAndRawAux(void) {
Serial0.print('A');
Serial0.write(sampleCounter); // 1 byte
ADS_writeChannelData(); // 24 bytes
Serial0.write(highByte(auxData[0])); // 2 bytes of aux data
Serial0.write(lowByte(auxData[0]));
writeTimeCurrent(); // 4 bytes
if (sendTimeSyncUpPacket) {
sendTimeSyncUpPacket = false;
Serial0.write(OPENBCI_EOP_RAW_AUX_TIME_SET); // 0xC5
} else {
Serial0.write(OPENBCI_EOP_RAW_AUX_TIME_SYNCED); // 0xC6
}
sampleCounter++;
}
/**
* @description Writes channel data, aux data, and footer to serial port. This
* is the old way to send channel data. Based on global variables `useAux`
* and `useAccel` Must keep for portability. Will look to deprecate in 3.0.0.
*
* If `useAccel` is `true` then sends data from `axisData` array and sets the
* contents of `axisData` to `0`.
* If `useAux` is `true` then sends data from `auxData` array and sets the
* contents of `auxData` to `0`.
*
* Adds stop byte `OPENBCI_EOP_STND_ACCEL`. See `OpenBCI_32bit_Library_Definitions.h`
*/
void OpenBCI_32bit_Library::sendChannelData(void) {
Serial0.print('A');
Serial0.write(sampleCounter); // 1 byte
ADS_writeChannelData(); // 24 bytes
if(useAux){
writeAuxData(); // 6 bytes of aux data
} else if(useAccel){ // or
LIS3DH_writeAxisData(); // 6 bytes of accelerometer data
} else{
for(int i=0; i<6; i++){
Serial0.write((byte)0x00);
}
}
Serial0.write(OPENBCI_EOP_STND_ACCEL); // 0xF0
sampleCounter++;
}
void OpenBCI_32bit_Library::writeAuxData(){
for(int i=0; i<3; i++){
Serial0.write(highByte(auxData[i])); // write 16 bit axis data MSB first
Serial0.write(lowByte(auxData[i])); // axisData is array of type short (16bit)
auxData[i] = 0; // reset auxData bytes to 0
}
}
void OpenBCI_32bit_Library::writeTimeCurrent(void) {
uint32_t newTime = millis(); // serialize the number, placing the MSB in lower packets
for (int j = 3; j >= 0; j--) {
Serial0.write(newTime >> (j*8));
}
}
//SPI communication method
byte OpenBCI_32bit_Library::xfer(byte _data)
{
byte inByte;
inByte = spi.transfer(_data);
return inByte;
}