forked from OpenBCI/OpenBCI_Cyton_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpenBCI_32bit_Library.cpp
4483 lines (4101 loc) · 124 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()
{
initializeVariables();
}
/**
* @description: The function the OpenBCI board will call in setup.
* @author: AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::begin(void)
{
// 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)
{
beginDebug(OPENBCI_BAUD_RATE);
}
void OpenBCI_32bit_Library::beginDebug(uint32_t baudRate)
{
// Bring the board up
boolean started = boardBeginDebug(baudRate);
if (started)
{
Serial1.println("Board up");
sendEOT();
}
else
{
Serial1.println("Board err");
sendEOT();
}
}
/**
* @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)
{
// TODO: Need to undo this comment out
// if (!Serial0) return false;
// if (!iSerial0.rx) return false;
if (iSerial0.rx && 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)
{
// TODO: Need to undo this comment out
// if (Serial1) return false;
// if (!iSerial1.rx) return false;
if (iSerial1.rx && 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 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 (curBoardMode == BOARD_MODE_DEBUG || curDebugMode == DEBUG_MODE_ON)
{
Serial1.print("pC: ");
Serial1.println(character);
}
if (checkMultiCharCmdTimer())
{ // we are in a multi char command
switch (getMultiCharCommand())
{
case MULTI_CHAR_CMD_PROCESSING_INCOMING_SETTINGS_CHANNEL:
processIncomingChannelSettings(character);
break;
case MULTI_CHAR_CMD_PROCESSING_INCOMING_SETTINGS_LEADOFF:
processIncomingLeadOffSettings(character);
break;
case MULTI_CHAR_CMD_SETTINGS_BOARD_MODE:
processIncomingBoardMode(character);
break;
case MULTI_CHAR_CMD_SETTINGS_SAMPLE_RATE:
processIncomingSampleRate(character);
break;
case MULTI_CHAR_CMD_INSERT_MARKER:
processInsertMarker(character);
break;
default:
break;
}
}
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 a multi char command with a timeout
startMultiCharCmdTimer(MULTI_CHAR_CMD_PROCESSING_INCOMING_SETTINGS_CHANNEL);
numberOfIncomingSettingsProcessedChannel = 1;
break;
// LEAD OFF IMPEDANCE DETECTION COMMANDS
case OPENBCI_CHANNEL_IMPEDANCE_SET:
startMultiCharCmdTimer(MULTI_CHAR_CMD_PROCESSING_INCOMING_SETTINGS_LEADOFF);
numberOfIncomingSettingsProcessedLeadOff = 1;
break;
case OPENBCI_CHANNEL_DEFAULT_ALL_SET: // reset all channel settings to default
if (!streaming)
{
printAll("updating channel settings to");
printAll(" 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();
}
else if (wifi.present && wifi.tx)
{
wifi.sendStringLast("No daisy to remove");
}
break;
case OPENBCI_CHANNEL_MAX_NUMBER_16: // use 16 channel mode
if (daisyPresent == false)
{
attachDaisy();
}
if (daisyPresent)
{
printAll("16");
}
else
{
printAll("8");
}
sendEOT();
break;
// STREAM DATA AND FILTER COMMANDS
case OPENBCI_STREAM_START: // stream data
if (curAccelMode == ACCEL_MODE_ON)
{
enable_accel(RATE_25HZ);
} // fire up the accelerometer if you want it
wifi.tx = commandFromSPI;
if (wifi.present && wifi.tx)
{
wifi.sendStringLast("Stream started");
iSerial0.tx = false;
}
// Reads if the command is not from the SPI port and we are not in debug mode
if (!commandFromSPI && !iSerial1.tx)
{
// If the sample rate is higher than 250, we need to drop down to 250Hz
// to not break the RFduino system that can't handle above 250SPS.
if (curSampleRate != SAMPLE_RATE_250)
{
streamSafeSetSampleRate(SAMPLE_RATE_250);
delay(50);
}
}
streamStart(); // turn on the fire hose
break;
case OPENBCI_STREAM_STOP: // stop streaming data
if (curAccelMode == ACCEL_MODE_ON)
{
disable_accel();
} // shut down the accelerometer if you're using it
wifi.tx = true;
streamStop();
if (wifi.present && wifi.tx)
{
wifi.sendStringLast("Stream stopped");
}
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)
{
printAll("Time stamp ON");
sendEOT();
}
curTimeSyncMode = TIME_SYNC_MODE_ON;
setCurPacketType();
break;
case OPENBCI_TIME_STOP:
// Stop the Sync
if (!streaming)
{
printAll("Time stamp OFF");
sendEOT();
}
curTimeSyncMode = TIME_SYNC_MODE_OFF;
setCurPacketType();
break;
// BOARD TYPE SET TYPE
case OPENBCI_BOARD_MODE_SET:
startMultiCharCmdTimer(MULTI_CHAR_CMD_SETTINGS_BOARD_MODE);
optionalArgCounter = 0;
break;
// Sample rate set
case OPENBCI_SAMPLE_RATE_SET:
startMultiCharCmdTimer(MULTI_CHAR_CMD_SETTINGS_SAMPLE_RATE);
break;
// Insert Marker into the EEG data stream
case OPENBCI_INSERT_MARKER:
startMultiCharCmdTimer(MULTI_CHAR_CMD_INSERT_MARKER);
break;
case OPENBCI_WIFI_ATTACH:
if (wifi.attach())
{
printSuccess();
printSerial("Wifi attached");
sendEOT();
}
else
{
printFailure();
printSerial("Wifi not attached");
sendEOT();
}
break;
case OPENBCI_WIFI_REMOVE:
if (wifi.remove())
{
printSuccess();
printSerial("Wifi removed");
}
else
{
printFailure();
printSerial("Wifi not removed");
}
sendEOT();
break;
case OPENBCI_WIFI_STATUS:
if (wifi.present)
{
printAll("Wifi present");
}
else
{
printAll("Wifi not present, send {");
printAll(" to attach the shield");
}
sendEOT();
break;
case OPENBCI_WIFI_RESET:
wifi.reset();
printSerial("Wifi soft reset");
sendEOT();
break;
case OPENBCI_GET_VERSION:
printAll("v3.1.2");
sendEOT();
break;
default:
return false;
}
}
return true;
}
/**
* Start the timer on multi char commands
* @param cmd {char} the command received on the serial stream. See enum MULTI_CHAR_COMMAND
* @returns void
*/
void OpenBCI_32bit_Library::startMultiCharCmdTimer(char cmd)
{
if (curDebugMode == DEBUG_MODE_ON)
{
Serial1.printf("Start multi char: %c\n", cmd);
}
isMultiCharCmd = true;
multiCharCommand = cmd;
multiCharCmdTimeout = millis() + MULTI_CHAR_COMMAND_TIMEOUT_MS;
}
/**
* End the timer on multi char commands
* @param None
* @returns void
*/
void OpenBCI_32bit_Library::endMultiCharCmdTimer(void)
{
isMultiCharCmd = false;
multiCharCommand = MULTI_CHAR_CMD_NONE;
}
/**
* Check for valid on multi char commands
* @param None
* @returns {boolean} true if a multi char commands is active and the timer is running, otherwise False
*/
boolean OpenBCI_32bit_Library::checkMultiCharCmdTimer(void)
{
if (isMultiCharCmd)
{
if (millis() < multiCharCmdTimeout)
return true;
else
{ // the timer has timed out - reset the multi char timeout
endMultiCharCmdTimer();
printAll("Timeout processing multi byte");
printAll(" message - please send all");
printAll(" commands at once as of v2");
sendEOT();
}
}
return false;
}
/**
* To be called at some point in every loop function
*/
void OpenBCI_32bit_Library::loop(void)
{
if (isMultiCharCmd)
{
checkMultiCharCmdTimer();
}
}
/**
* Gets the active multi char command
* @param None
* @returns {char} multiCharCommand
*/
char OpenBCI_32bit_Library::getMultiCharCommand(void)
{
return multiCharCommand;
}
boolean OpenBCI_32bit_Library::processCharWifi(char character)
{
commandFromSPI = true;
boolean retVal = processChar(character);
commandFromSPI = false;
return retVal;
}
/**
* Used to turn on or off the accel, will change the current packet type!
* @param yes {boolean} - True if you want to use it
*/
void OpenBCI_32bit_Library::useAccel(boolean yes)
{
curAccelMode = yes ? ACCEL_MODE_ON : ACCEL_MODE_OFF;
setCurPacketType();
}
/**
* Used to turn on or off time syncing/stamping, will change the current packet type!
* @param yes {boolean} - True if you want to use it
*/
void OpenBCI_32bit_Library::useTimeStamp(boolean yes)
{
curTimeSyncMode = yes ? TIME_SYNC_MODE_ON : TIME_SYNC_MODE_OFF;
setCurPacketType();
}
/**
* @description Reads a status register to see if there is new accelerometer
* data. This also takes into account if using accel or not.
* @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::accelWriteAxisDataSerial(void)
{
LIS3DH_writeAxisDataSerial();
}
/**
* @description Reads from the accelerometer to get new X, Y, and Z data.
*/
void OpenBCI_32bit_Library::accelWriteAxisDataWifi(void)
{
LIS3DH_writeAxisDataWifi();
}
/**
* @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
// Set serial 0 to true for rx and tx
beginPinsDefault();
beginSerial0();
delay(10);
// Startup the interrupt
boardBeginADSInterrupt();
// Do a soft reset
boardReset();
return true;
}
void OpenBCI_32bit_Library::boardBeginADSInterrupt(void)
{
// 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
}
/**
* @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)
{
boardBeginDebug(iSerial1.baudRate);
}
/**
* @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)
{
beginPinsDebug();
beginSerial0();
beginSerial1(baudRate);
curBoardMode = BOARD_MODE_DEBUG;
curDebugMode = DEBUG_MODE_ON;
// Startup for interrupt
boardBeginADSInterrupt();
// Do a soft reset
boardReset();
return true;
}
void OpenBCI_32bit_Library::beginPinsAnalog(void)
{
pinMode(11, INPUT);
pinMode(12, INPUT);
if (!wifi.present)
pinMode(13, INPUT);
}
void OpenBCI_32bit_Library::beginPinsDebug(void)
{
pinMode(OPENBCI_PIN_SERIAL1_TX, OUTPUT);
pinMode(OPENBCI_PIN_SERIAL1_RX, INPUT);
}
void OpenBCI_32bit_Library::beginPinsDefault(void)
{
pinMode(OPENBCI_PIN_LED, OUTPUT);
digitalWrite(OPENBCI_PIN_LED, HIGH);
pinMode(OPENBCI_PIN_PGC, OUTPUT);
}
void OpenBCI_32bit_Library::beginPinsDigital(void)
{
pinMode(11, INPUT);
pinMode(12, INPUT);
pinMode(17, INPUT);
if (!wifi.present)
pinMode(13, INPUT);
if (!wifi.present)
pinMode(18, INPUT);
}
/**
* Used to start Serial0
*/
void OpenBCI_32bit_Library::beginSerial0(void)
{
beginSerial0(OPENBCI_BAUD_RATE);
}
/**
* Used to start Serial0
*/
void OpenBCI_32bit_Library::beginSerial0(uint32_t baudRate)
{
// Initalize the serial port baud rate
if (Serial0)
Serial0.end();
Serial0.begin(baudRate);
iSerial0.tx = true;
iSerial0.rx = true;
iSerial0.baudRate = baudRate;
}
/**
* @description: The function the OpenBCI board will call in setup. This sets up the hardware serial port on D11 and D12
* @param `baudRate` {uint32_t} - The baud rate of the new secondary serial port
* @author: AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::beginSerial1(void)
{
beginSerial1(OPENBCI_BAUD_RATE);
}
/**
* @description: The function the OpenBCI board will call in setup. This sets up the hardware serial port on D11 and D12
* @param `baudRate` {uint32_t} - The baud rate of the new secondary serial port
* @author: AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::beginSerial1(uint32_t baudRate)
{
// Initalize the serial 1 port
if (Serial1)
Serial1.end();
Serial1.begin(baudRate);
iSerial1.tx = true;
iSerial1.rx = true;
iSerial1.baudRate = baudRate;
}
/**
* Used to safely end a serial0 port
*/
void OpenBCI_32bit_Library::endSerial0(void)
{
if (Serial0)
Serial0.end();
iSerial0.tx = false;
iSerial0.rx = false;
}
/**
* Used to safely end a serial1 port
*/
void OpenBCI_32bit_Library::endSerial1(void)
{
if (Serial1)
Serial1.end();
iSerial1.tx = false;
iSerial1.rx = false;
iSerial1.baudRate = OPENBCI_BAUD_RATE;
}
/**
* @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);
printlnAll("OpenBCI V3 8-16 channel");
printAll("On Board ADS1299 Device ID: 0x");
printlnHex(ADS_getDeviceID(ON_BOARD));
if (daisyPresent)
{ // library will set this in initialize() if daisy present and functional
printAll("On Daisy ADS1299 Device ID: 0x");
printlnHex(ADS_getDeviceID(ON_DAISY));
}
printAll("LIS3DH Device ID: 0x");
printlnHex(LIS3DH_getDeviceID());
printlnAll("Firmware: v3.1.2");
sendEOT();
delay(5);
wifi.reset();
}
/**
* @description: Simple method to send the EOT over serial...
* @author: AJ Keller (@pushtheworldllc)
*/
void OpenBCI_32bit_Library::sendEOT(void)
{
printSerial("$$$");
wifi.sendStringLast();
}
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();
}
else
{
printSuccess();
printAll("Configured internal");
printAll(" test signal.");
sendEOT();
}
}
void OpenBCI_32bit_Library::processIncomingBoardMode(char c)
{
if (c == OPENBCI_BOARD_MODE_SET)
{
printSuccess();
printAll(getBoardMode());
sendEOT();
}
else if (isDigit(c))
{
uint8_t digit = c - '0';
if (digit < BOARD_MODE_END_OF_MODES)
{
setBoardMode(digit);
printSuccess();
printAll(getBoardMode());
sendEOT();
}
else
{
printFailure();
printAll("board mode value");
printAll(" out of bounds.");
sendEOT();
}
}
else
{
printFailure();
printAll("invalid board mode value.");
sendEOT();
}
endMultiCharCmdTimer();
}
/**
* Used to set the board mode of the system.
* @param newBoardMode The board mode to swtich to
*/
void OpenBCI_32bit_Library::setBoardMode(uint8_t newBoardMode)
{
if (curBoardMode == (BOARD_MODE)newBoardMode)
return;
curBoardMode = (BOARD_MODE)newBoardMode;
switch (curBoardMode)
{
case BOARD_MODE_ANALOG:
curAccelMode = ACCEL_MODE_OFF;
beginPinsAnalog();
break;
case BOARD_MODE_DIGITAL:
curAccelMode = ACCEL_MODE_OFF;
beginPinsDigital();
break;
case BOARD_MODE_DEBUG:
curDebugMode = DEBUG_MODE_ON;
beginPinsDebug();
beginSerial1();
break;
case BOARD_MODE_DEFAULT:
curAccelMode = ACCEL_MODE_ON;
endSerial1();
beginPinsDefault();
endSerial0();
beginSerial0(OPENBCI_BAUD_RATE);
break;
case BOARD_MODE_MARKER:
curAccelMode = ACCEL_MODE_OFF;
break;
case BOARD_MODE_BLE:
endSerial0();
beginSerial0(OPENBCI_BAUD_RATE_BLE);
default:
break;
}
delay(10);
setCurPacketType();
}
void OpenBCI_32bit_Library::setSampleRate(uint8_t newSampleRateCode)
{
curSampleRate = (SAMPLE_RATE)newSampleRateCode;
initialize_ads();
}
const char *OpenBCI_32bit_Library::getSampleRate()
{
switch (curSampleRate)
{
case SAMPLE_RATE_16000:
return "16000";
case SAMPLE_RATE_8000:
return "8000";
case SAMPLE_RATE_4000:
return "4000";
case SAMPLE_RATE_2000:
return "2000";
case SAMPLE_RATE_1000:
return "1000";
case SAMPLE_RATE_500:
return "500";
case SAMPLE_RATE_250:
default:
return "250";
}
}
const char *OpenBCI_32bit_Library::getBoardMode(void)
{
switch (curBoardMode)
{
case BOARD_MODE_DEBUG:
return "debug";
case BOARD_MODE_ANALOG:
return "analog";
case BOARD_MODE_DIGITAL:
return "digital";
case BOARD_MODE_MARKER:
return "marker";
case BOARD_MODE_BLE:
return "BLE";
case BOARD_MODE_DEFAULT:
default:
return "default";
}
}
void OpenBCI_32bit_Library::processIncomingSampleRate(char c)
{
if (c == OPENBCI_SAMPLE_RATE_SET)
{
printSuccess();
printAll("Sample rate is ");
printAll(getSampleRate());
printAll("Hz");
sendEOT();
}
else if (isDigit(c))
{
uint8_t digit = c - '0';
if (digit <= SAMPLE_RATE_250)
{
streamSafeSetSampleRate((SAMPLE_RATE)digit);
if (!streaming)
{
printSuccess();
printAll("Sample rate is ");
printAll(getSampleRate());
printAll("Hz");
sendEOT();
}