-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathOpenBCI_Radios.cpp
2102 lines (1889 loc) · 70.7 KB
/
OpenBCI_Radios.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
/***************************************************
This is a library for the OpenBCI 32bit RFduinoGZLL Device and host
Let us define two over arching operating modes / paradigms: Host and Device, where:
* Host is connected to PC via USB VCP (FTDI).
* Device is connectedd to uC (PIC32MX250F128B with UDB32-MX2-DIP).
OpenBCI invests time and resources providing this open source code,
please support OpenBCI and open-source hardware by purchasing
products from OpenBCI or donating on our downloads page!
Written by AJ Keller of Push The World LLC but much credit must also go to
Joel Murphy who with Conor Russomanno and Leif Percifield created the
original OpenBCI_32bit_Device.ino and OpenBCI_32bit_Host.ino files in the
Summer of 2014. Much of this code base is inspired directly from their work.
MIT license
****************************************************/
#include "OpenBCI_Radios.h"
// CONSTRUCTOR
OpenBCI_Radios_Class::OpenBCI_Radios_Class() {
// Set defaults
radioMode = OPENBCI_MODE_DEVICE; // Device mode
radioChannel = 25; // Channel 18
debugMode = false; // Set true if doing dongle-dongle sim
ackCounter = 0;
lastTimeHostHeardFromDevice = 0;
lastTimeSerialRead = 0;
systemUp = false;
}
/**
* @description The function that the radio will call in setup()
* @param: mode {unint8_t} - The mode the radio shall operate in
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::begin(uint8_t mode) {
// Save global radio mode
radioMode = mode;
// configure radio
configure(mode,radioChannel);
}
/**
* @description The function that the radio will call in setup()
* @param: mode {unint8_t} - The mode the radio shall operate in
* @param: channelNumber {uint32_t} - The channelNumber the RFduinoGZLL will
* use to communicate with the other RFduinoGZLL.
* NOTE: Must be from 0 - 25
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::begin(uint8_t mode, uint32_t channelNumber) {
// Save global radio mode
radioMode = mode;
// Restrict the channel to 0-25 inclusively
if (channelNumber > RFDUINOGZLL_CHANNEL_LIMIT_UPPER || channelNumber < RFDUINOGZLL_CHANNEL_LIMIT_LOWER) {
channelNumber = RFDUINOGZLL_CHANNEL_LIMIT_LOWER;
}
// configure radio
configure(mode,channelNumber);
}
/**
* @description Puts into debug mode and then call other function
* @param: mode {unint8_t} - The mode the radio shall operate in
* @param: channelNumber {int8_t} - The channelNumber the RFduinoGZLL will
* use to communicate with the other RFduinoGZLL.
* NOTE: Must be from 0 - 25
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::beginDebug(uint8_t mode, uint32_t channelNumber) {
debugMode = true;
begin(mode,channelNumber);
}
/**
* @description Private function to initialize the OpenBCI_Radios_Class object
* @param: mode [unint8_t] - The mode the radio shall operate in
* @param: channelNumber [int8_t] - The channelNumber the RFduinoGZLL will
* use to communicate with the other RFduinoGZLL
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::configure(uint8_t mode, uint32_t channelNumber) {
// Quickly check to see if in pass through mode, if so, call and dip out of func
if (mode == OPENBCI_MODE_PASS_THRU) {
configurePassThru();
} else { // we are either dealing with a Host or a Device
// We give the opportunity to call any 'universal' code, rather code, that
// gets set up the same on both Host and Device
// Check to see if we need to set the channel number
// this is only the case on the first run of the program
if (needToSetChannelNumber()) {
setChannelNumber(channelNumber);
}
RFduinoGZLL.channel = getChannelNumber();
radioChannel = getChannelNumber();
// Check to see if we need to set the poll time
// this is only the case on the first run of the program
if (needToSetPollTime()) {
setPollTime(OPENBCI_TIMEOUT_PACKET_POLL_MS);
}
pollTime = getPollTime();
// get the buffers ready
bufferRadioReset(bufferRadio);
// bufferRadioReset(bufferRadio + 1);
bufferRadioClean(bufferRadio);
// bufferRadioClean(bufferRadio + 1);
streamPacketBufferHead = 0;
streamPacketBufferTail = 0;
for (int i = 0; i < OPENBCI_NUMBER_STREAM_BUFFERS; i++) {
bufferStreamReset(streamPacketBuffer + i);
}
currentRadioBuffer = bufferRadio;
currentRadioBufferNum = 0;
bufferSerialReset(OPENBCI_NUMBER_SERIAL_BUFFERS);
// Diverge program execution based on Device or Host
switch (mode) {
case OPENBCI_MODE_DEVICE:
configureDevice(); // setup for Device
break;
default: // we default to OPENBCI_MODE_HOST
configureHost(); // setup for Host
break;
}
}
}
/**
* @description Private function to initialize the radio in Device mode
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::configureDevice(void) {
// Start the RFduinoGZLL in DEVICE0 mode
RFduinoGZLL.begin(RFDUINOGZLL_ROLE_DEVICE);
// Configure pins
if (debugMode) { // Dongle to Dongle debug mode
// BEGIN: To run host as device
pinMode(OPENBCI_PIN_HOST_RESET,INPUT);
pinMode(OPENBCI_PIN_HOST_LED,OUTPUT);
digitalWrite(OPENBCI_PIN_HOST_LED,HIGH);
Serial.begin(OPENBCI_BAUD_RATE_DEFAULT);
// END: To run host as device
} else {
// BEGIN: To run host normally
pinMode(OPENBCI_PIN_DEVICE_PCG, INPUT); //feel the state of the PIC with this pin
// Start the serial connection. On the device we must specify which pins are
// rx and tx, where:
// rx = GPIO3
// tx = GPIO2
Serial.begin(OPENBCI_BAUD_RATE_DEFAULT, 3, 2);
// END: To run host normally
}
timeOfLastMultipacketSendToHost = millis();
sendingMultiPacket = false;
streamPacketsHaveHeads = true;
pollRefresh();
}
/**
* @description Private function to initialize the radio in Host mode
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::configureHost(void) {
// Start the RFduinoGZLL in HOST mode
RFduinoGZLL.begin(RFDUINOGZLL_ROLE_HOST);
// Configure pins
pinMode(OPENBCI_PIN_HOST_RESET,INPUT);
pinMode(OPENBCI_PIN_HOST_LED,OUTPUT);
// pinMode(OPENBCI_PIN_HOST_TIME,OUTPUT);
//
// digitalWrite(OPENBCI_PIN_HOST_TIME,LOW);
// Turn LED on
digitalWrite(OPENBCI_PIN_HOST_LED,HIGH);
// Open the Serial connection
Serial.begin(OPENBCI_BAUD_RATE_DEFAULT);
packetInTXRadioBuffer = false;
sendSerialAck = false;
channelNumberSaveAttempted = false;
printMessageToDriverFlag = false;
systemUp = false;
}
/**
* @description Private function to initialize the radio in Pass Through mode
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::configurePassThru(void) {
// Configure the pins
pinMode(0,OUTPUT_D0H1); // output is highZ when logic 0, HIGH when logic 1
pinMode(1,OUTPUT_D0H1);
pinMode(OPENBCI_PIN_HOST_LED,OUTPUT);
digitalWrite(0,LOW);
digitalWrite(1,LOW);
}
/**
* @description Gets the channel number from non-volatile flash memory
* @returns {uint32_t} - The channel number from non-volatile memory
* @author AJ Keller (@pushtheworldllc)
*/
uint32_t OpenBCI_Radios_Class::getChannelNumber(void) {
return *ADDRESS_OF_PAGE(RFDUINOGZLL_FLASH_MEM_ADDR);
}
/**
* @description Gets the poll time from non-volatile flash memory
* @returns {uint32_t} - The poll time from non-volatile memory
* @author AJ Keller (@pushtheworldllc)
*/
uint32_t OpenBCI_Radios_Class::getPollTime(void) {
return *(ADDRESS_OF_PAGE(RFDUINOGZLL_FLASH_MEM_ADDR) + 1);
}
/**
* @description Reads from memory to see if the channel number needs to be set
* @return {boolean} True if the channel number needs to be set
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::needToSetChannelNumber(void) {
return getChannelNumber() == 0xFFFFFFFF;
}
/**
* @description Reads from memory to see if the poll time needs to be set
* @return {boolean} True if the poll time needs to be set
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::needToSetPollTime(void) {
return getPollTime() == 0xFFFFFFFF;
}
void OpenBCI_Radios_Class::revertToPreviousChannelNumber(void) {
RFduinoGZLL.end();
RFduinoGZLL.channel = previousRadioChannel;
RFduinoGZLL.begin(RFDUINOGZLL_ROLE_HOST);
radioChannel = previousRadioChannel;
}
/**
* @description Resets the poll time to the define OPENBCI_TIMEOUT_PACKET_POLL_MS
* @return {boolean} - see `::setPollTime()`
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::revertToDefaultPollTime(void) {
return setPollTime((uint32_t)OPENBCI_TIMEOUT_PACKET_POLL_MS);
}
/**
* @description Store a channel number to memory. Allows for the channel to be
* maintained even after power cycle.
* @param channelNumber {uint32_t} - The new channel to set to. Must be less
* than 25.
* @return {boolean} - If the channel was successfully flashed to memory. False
* when the channel number is out of bounds.
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::setChannelNumber(uint32_t channelNumber) {
if (channelNumber > RFDUINOGZLL_CHANNEL_LIMIT_UPPER) {
return false;
}
uint32_t *p = ADDRESS_OF_PAGE(RFDUINOGZLL_FLASH_MEM_ADDR);
boolean willSetPollTime = false;
uint32_t pollTime = 0xFFFFFFFF;
// If I don't need to set the channel number then grab that channel number
if (!needToSetPollTime()) {
pollTime = getPollTime();
willSetPollTime = true;
}
int rc;
if (flashNonVolatileMemory()) {
if (willSetPollTime) {
if (flashWrite(p + 1, pollTime) > 0) {
return false;
}
}
rc = flashWrite(p, channelNumber);
if (rc == 0) {
return true;
} else if (rc == 1) {
return false;
} else if (rc == 2) {
return false;
}
}
return false;
}
/**
* @description Store a poll time to memory. Allows for poll time to be retained
* after power down
* @param pollTime {uint32_t} - The new poll time to store to memory
* @return {boolean} - If the pollTime was successfully set
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::setPollTime(uint32_t pollTime) {
uint32_t *p = ADDRESS_OF_PAGE(RFDUINOGZLL_FLASH_MEM_ADDR);
boolean willSetChannel = false;
uint32_t chan = 0xFFFFFFFF;
// If I don't need to set the channel number then grab that channel number
if (!needToSetChannelNumber()) {
chan = getChannelNumber();
willSetChannel = true;
}
int rc;
if (flashNonVolatileMemory()) {
if (willSetChannel) {
if (flashWrite(p,chan)) {
return false;
}
}
rc = flashWrite(p + 1, pollTime); // Always stored 1 more than chan
if (rc == 0) {
return true;
} else if (rc == 1) {
return false;
} else if (rc == 2) {
return false;
}
}
return false;
}
/**
* @description Used to reset the non-volatile memory back to it's factory state so
* the parameters in `begin()` will be accepted.
* @return {boolean} - `true` if the memory was successfully reset, `false` if not...
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::flashNonVolatileMemory(void) {
uint32_t *p = ADDRESS_OF_PAGE(RFDUINOGZLL_FLASH_MEM_ADDR);
int rc = flashPageErase(PAGE_FROM_ADDRESS(p)); // erases 1k of flash
if (rc == 1) {
return false;
} else if (rc == 2) {
return false;
}
return true;
}
/********************************************/
/********************************************/
/************ HOST CODE ***************/
/********************************************/
/********************************************/
/**
* @description Private function to handle a request to read serial as a host
* @return {Boolean} - TRUE if there is data to read! FALSE if not...
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::didPCSendDataToHost(void) {
if (Serial.available() > 0) {
return true;
} else {
return false;
}
}
/**
* @description The first line of defense against a system that has lost it's
* device. The timeout is 15ms longer than the longest polltime (255) possible.
* @returns {boolean} - `true` if enough time has passed since last poll.
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::commsFailureTimeout(void) {
return millis() > (lastTimeHostHeardFromDevice + OPENBCI_TIMEOUT_COMMS_MS);
}
/**
* @descirption Answers the question of if a packet is ready to be sent. need
* to check and there is no packet in the TX Radio Buffer, there are in fact
* packets to send and enough time has passed.
* @returns {boolean} - True if there is a packet ready to send on the host
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::hostPacketToSend(void) {
return packetToSend() && (packetInTXRadioBuffer == false);
}
void OpenBCI_Radios_Class::printChannelNumber(char c) {
Serial.print("Channel number: "); Serial.print((int)c); Serial.write(c);
}
void OpenBCI_Radios_Class::printChannelNumberVerify(void) {
Serial.print("Verify channel number is less than 25");
}
void OpenBCI_Radios_Class::printBaudRateChangeTo(int b) {
Serial.print("Switch your baud rate to ");
Serial.print(b);
};
void OpenBCI_Radios_Class::printCommsTimeout(void) {
Serial.print("Communications timeout - Device failed to poll Host");
}
void OpenBCI_Radios_Class::printEOT(void) {
Serial.print("$$$");
}
void OpenBCI_Radios_Class::printFailure(void) {
Serial.print("Failure: ");
}
void OpenBCI_Radios_Class::printPollTime(char p) {
Serial.print("Poll time: "); Serial.print((int)p); Serial.write(p);
}
void OpenBCI_Radios_Class::printSuccess(void) {
Serial.print("Success: ");
}
void OpenBCI_Radios_Class::printValidatedCommsTimeout(void) {
printFailure();
printCommsTimeout();
printEOT();
}
/**
* @description Writes to the serial port a message that matches a specific code.
* @param {uint8_t} - The code to print Serial.write()
* Possible options:
* `HOST_MESSAGE_COMMS_DOWN` - Print the comms down message
* `HOST_MESSAGE_COMMS_DOWN_CHAN` - Print the message when the comms when down trying to change channels.
* `HOST_MESSAGE_COMMS_DOWN_POLL_TIME` - Print the messafe when the comms go down trying to change poll times.
* `HOST_MESSAGE_BAUD_FAST` - Baud rate swtiched to 230400
* `HOST_MESSAGE_BAUD_DEFAULT` - Baud rate swtiched to 115200
* `HOST_MESSAGE_BAUD_HYPER` - Baud rate swtiched to 921600
* `HOST_MESSAGE_SYS_UP` - Print the system up message
* `HOST_MESSAGE_SYS_DOWN` - Print the system down message
* `HOST_MESSAGE_CHAN` - Print the channel number message
* `HOST_MESSAGE_CHAN_OVERRIDE` - Print the host over ride message
* `HOST_MESSAGE_CHAN_VERIFY` - Print the need to verify the channel number you inputed message
* `HOST_MESSAGE_CHAN_GET_FAILURE` - The message to print when there is a comms timeout and to print just the Host channel number.
* `HOST_MESSAGE_CHAN_GET_SUCCESS` - The message to print when the Host and Device are communicating.
* `HOST_MESSAGE_POLL_TIME` - Prints the poll time when there is no comms.
* `HOST_MESSAGE_SERIAL_ACK` - Writes a serial ack (',') to the Driver/PC
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::printMessageToDriver(uint8_t code) {
switch (code) {
case HOST_MESSAGE_COMMS_DOWN:
printValidatedCommsTimeout();
break;
case HOST_MESSAGE_COMMS_DOWN_CHAN:
printFailure();
Serial.print("Channel Change Request");
printCommsTimeout();
printEOT();
break;
case HOST_MESSAGE_COMMS_DOWN_POLL_TIME:
printFailure();
Serial.print("Poll Time Change Request");
printCommsTimeout();
printEOT();
break;
case HOST_MESSAGE_SYS_UP:
printSuccess();
Serial.print("System is Up");
printEOT();
break;
case HOST_MESSAGE_SYS_DOWN:
printFailure();
Serial.print("System is Down");
printEOT();
break;
case HOST_MESSAGE_BAUD_FAST:
printSuccess();
printBaudRateChangeTo((int)OPENBCI_BAUD_RATE_FAST);
printEOT();
delay(2);
// Close the current serial connection
Serial.end();
// Open the Serial connection
Serial.begin(OPENBCI_BAUD_RATE_FAST);
break;
case HOST_MESSAGE_BAUD_DEFAULT:
printSuccess();
printBaudRateChangeTo((int)OPENBCI_BAUD_RATE_DEFAULT);
printEOT();
delay(2);
// Close the current serial connection
Serial.end();
// Open the Serial connection
Serial.begin(OPENBCI_BAUD_RATE_DEFAULT);
break;
case HOST_MESSAGE_BAUD_HYPER:
printSuccess();
printBaudRateChangeTo((int)OPENBCI_BAUD_RATE_HYPER);
printEOT();
delay(2);
// Close the current serial connection
Serial.end();
// Open the Serial connection
Serial.begin(OPENBCI_BAUD_RATE_HYPER);
break;
case HOST_MESSAGE_CHAN:
printValidatedCommsTimeout();
break;
case HOST_MESSAGE_CHAN_OVERRIDE:
radioChannel = getChannelNumber();
RFduinoGZLL.end();
RFduinoGZLL.channel = getChannelNumber();
RFduinoGZLL.begin(RFDUINOGZLL_ROLE_HOST);
printSuccess();
Serial.print("Host override - ");
printChannelNumber(getChannelNumber());
printEOT();
systemUp = false;
break;
case HOST_MESSAGE_CHAN_VERIFY:
printFailure();
printChannelNumberVerify();
printEOT();
break;
case HOST_MESSAGE_CHAN_GET_FAILURE:
printFailure();
Serial.print("Host on ");
printChannelNumber(getChannelNumber());
printEOT();
systemUp = false;
break;
case HOST_MESSAGE_CHAN_GET_SUCCESS:
printSuccess();
Serial.print("Host and Device on ");
printChannelNumber(getChannelNumber());
printEOT();
break;
case HOST_MESSAGE_POLL_TIME:
printSuccess();
printPollTime(radio.getPollTime());
printEOT();
break;
case HOST_MESSAGE_SERIAL_ACK:
// Messages to print
Serial.write(',');
break;
default:
break;
}
}
/**
* @description Used to process the the serial buffer if the device fails to poll the host
* more than 3 * pollTime.
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::bufferSerialProcessCommsFailure(void) {
systemUp = false;
if (isWaitingForNewChannelNumberConfirmation) {
isWaitingForNewChannelNumberConfirmation = false;
revertToPreviousChannelNumber();
msgToPrint = HOST_MESSAGE_COMMS_DOWN_CHAN;
printMessageToDriverFlag = true;
} else if (isWaitingForNewPollTimeConfirmation) {
isWaitingForNewPollTimeConfirmation = false;
msgToPrint = HOST_MESSAGE_COMMS_DOWN_POLL_TIME;
printMessageToDriverFlag = true;
} else {
if (bufferSerialHasData()) {
byte action = processOutboundBuffer(bufferSerial.packetBuffer);
if (action == ACTION_RADIO_SEND_NORMAL) {
msgToPrint = HOST_MESSAGE_COMMS_DOWN;
printMessageToDriverFlag = true;
}
bufferSerialReset(bufferSerial.numberOfPacketsToSend);
}
}
}
/**
* @description Used to process the the serial buffer if the device fails to poll the host
* more than 3 * pollTime. The single packet condition should be parsed because
* it may contain actionable queries to the OpenBCI Radio system.
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::processCommsFailureSinglePacket(void) {
// The first byte needs to match the command key to act on it
if (bufferSerial.packetBuffer->data[OPENBCI_HOST_PRIVATE_POS_KEY] == OPENBCI_HOST_PRIVATE_CMD_KEY) {
// Switch on the first byte of the first packet.
switch (bufferSerial.packetBuffer->data[OPENBCI_HOST_PRIVATE_POS_CODE]) {
case OPENBCI_HOST_CMD_CHANNEL_SET:
printMessageToDriver(HOST_MESSAGE_COMMS_DOWN);
break;
case OPENBCI_HOST_CMD_CHANNEL_SET_OVERIDE:
if (setChannelNumber((uint32_t)bufferSerial.packetBuffer->data[OPENBCI_HOST_PRIVATE_POS_PAYLOAD])) {
msgToPrint = HOST_MESSAGE_CHAN_OVERRIDE;
printMessageToDriverFlag = true;
} else {
printMessageToDriver(HOST_MESSAGE_CHAN_VERIFY);
}
break;
case OPENBCI_HOST_CMD_CHANNEL_GET:
printMessageToDriver(HOST_MESSAGE_CHAN_GET_FAILURE);
break;
case OPENBCI_HOST_CMD_BAUD_DEFAULT:
msgToPrint = HOST_MESSAGE_BAUD_DEFAULT;
printMessageToDriverFlag = true;
break;
case OPENBCI_HOST_CMD_BAUD_FAST:
msgToPrint = HOST_MESSAGE_BAUD_FAST;
printMessageToDriverFlag = true;
break;
case OPENBCI_HOST_CMD_BAUD_HYPER:
msgToPrint = HOST_MESSAGE_BAUD_HYPER;
printMessageToDriverFlag = true;
break;
case OPENBCI_HOST_CMD_POLL_TIME_GET:
printMessageToDriver(HOST_MESSAGE_COMMS_DOWN);
break;
case OPENBCI_HOST_CMD_SYS_UP:
// We were not able to get polled by the Device
printMessageToDriver(HOST_MESSAGE_SYS_DOWN);
break;
default:
printMessageToDriver(HOST_MESSAGE_COMMS_DOWN);
break;
}
} else {
printMessageToDriver(HOST_MESSAGE_COMMS_DOWN);
}
// Always clear the serial buffer
bufferSerialReset(1);
}
/**
* @description The host can recieve messaged from the PC/Driver that should
* never be sent out. So we process an outbound packet for this!
* @return {byte} - The action to be taken after exit:
* ACTION_RADIO_SEND_NORMAL - Send a packet like normal
* ACTION_RADIO_SEND_NONE - Take no action
* ACTION_RADIO_SEND_SINGLE_CHAR - Send a secret radio message from singleCharMsg buffer
* @author AJ Keller (@pushtheworldllc)
*/
byte OpenBCI_Radios_Class::processOutboundBuffer(PacketBuffer *buf) {
if (buf->positionWrite == 3) {
return processOutboundBufferCharDouble(buf->data);
} else if (buf->positionWrite == 4) {
return processOutboundBufferCharTriple(buf->data);
} else {
return ACTION_RADIO_SEND_NORMAL;
}
}
/**
* @description Called by the Host's on_recieve function if the out bound buffer
* has a single char in it.
* @param c {char} - The char in the outbound buffer.
* @return {byte} - The action to be taken after exit:
* ACTION_RADIO_SEND_NORMAL - Send a packet like normal
* ACTION_RADIO_SEND_NONE - Take no action
* ACTION_RADIO_SEND_SINGLE_CHAR - Send a secret radio message from singleCharMsg buffer
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::processOutboundBufferForTimeSync(void) {
if (bufferSerial.packetBuffer->positionWrite == 2) {
if ((char)bufferSerial.packetBuffer->data[1] == (char)OPENBCI_HOST_TIME_SYNC) {
if (systemUp) {
// Send a comma back to the PC/Driver
sendSerialAck = true;
// Add the byteId to the packet
bufferSerial.packetBuffer->data[0] = byteIdMake(false,0,bufferSerial.packetBuffer->data + 1, bufferSerial.packetBuffer->positionWrite - 1);
// Serial.print("Sending "); Serial.print((bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->positionWrite); Serial.println(" bytes");
RFduinoGZLL.sendToDevice(DEVICE0,(char *)bufferSerial.packetBuffer->data, bufferSerial.packetBuffer->positionWrite);
// Set flag
packetInTXRadioBuffer = true;
// Clear the buffer // TODO: Don't clear buffer here
bufferSerialReset(1);
return true;
} else {
msgToPrint = HOST_MESSAGE_COMMS_DOWN;
printMessageToDriverFlag = true;
// Clean the serial buffer
bufferSerialReset(1);
return false;
}
}
}
return false;
}
/**
* @description Called by the Host's on_recieve function if the out bound buffer
* has two chars in it.
* @param buffer {char *} - The char buffer
* @return {byte} - The action to be taken after exit:
* ACTION_RADIO_SEND_NORMAL - Send a packet like normal
* ACTION_RADIO_SEND_NONE - Take no action
* ACTION_RADIO_SEND_SINGLE_CHAR - Send a secret radio message from singleCharMsg buffer
* @author AJ Keller (@pushtheworldllc)
*/
byte OpenBCI_Radios_Class::processOutboundBufferCharDouble(char *buffer) {
// The first byte needs to match the command key to act on it
if (buffer[OPENBCI_HOST_PRIVATE_POS_KEY] == OPENBCI_HOST_PRIVATE_CMD_KEY) {
// Decode the char
switch (buffer[OPENBCI_HOST_PRIVATE_POS_CODE]) {
// Is the byte the command for a host channel number?
case OPENBCI_HOST_CMD_CHANNEL_GET:
if (systemUp) {
// Send the channel number back to the driver
msgToPrint = HOST_MESSAGE_CHAN_GET_SUCCESS;
printMessageToDriverFlag = true;
} else {
// Send the channel number back to the driver
msgToPrint = HOST_MESSAGE_CHAN_GET_FAILURE;
printMessageToDriverFlag = true;
}
// Clear the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
case OPENBCI_HOST_CMD_BAUD_DEFAULT:
msgToPrint = HOST_MESSAGE_BAUD_DEFAULT;
printMessageToDriverFlag = true;
// Clear the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
case OPENBCI_HOST_CMD_BAUD_FAST:
msgToPrint = HOST_MESSAGE_BAUD_FAST;
printMessageToDriverFlag = true;
// Clear the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
case OPENBCI_HOST_CMD_BAUD_HYPER:
msgToPrint = HOST_MESSAGE_BAUD_HYPER;
printMessageToDriverFlag = true;
// Clear the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
case OPENBCI_HOST_CMD_SYS_UP:
if (systemUp) {
msgToPrint = HOST_MESSAGE_SYS_UP;
printMessageToDriverFlag = true;
} else {
msgToPrint = HOST_MESSAGE_SYS_DOWN;
printMessageToDriverFlag = true;
}
// Clear the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
case OPENBCI_HOST_CMD_POLL_TIME_GET:
if (systemUp) {
// Send a time change request to the device
singleCharMsg[0] = (char)ORPM_GET_POLL_TIME;
// Clean the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_SINGLE_CHAR;
} else {
msgToPrint = HOST_MESSAGE_COMMS_DOWN;
printMessageToDriverFlag = true;
// Clean the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
}
default:
if (systemUp) {
return ACTION_RADIO_SEND_NORMAL;
} else {
msgToPrint = HOST_MESSAGE_COMMS_DOWN;
printMessageToDriverFlag = true;
// Clean the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
}
}
} else {
return ACTION_RADIO_SEND_NORMAL;
}
}
/**
* @description Called by the Host's on_recieve function if the out bound buffer
* has three chars in it.
* @param buffer {char *} - The char buffer
* @return {byte} - The action to be taken after exit:
* ACTION_RADIO_SEND_NORMAL - Send a packet like normal
* ACTION_RADIO_SEND_NONE - Take no action
* ACTION_RADIO_SEND_SINGLE_CHAR - Send a secret radio message from singleCharMsg buffer
* @author AJ Keller (@pushtheworldllc)
*/
byte OpenBCI_Radios_Class::processOutboundBufferCharTriple(char *buffer) {
// The first byte needs to match the command key to act on it
if (buffer[OPENBCI_HOST_PRIVATE_POS_KEY] == OPENBCI_HOST_PRIVATE_CMD_KEY) {
switch (buffer[OPENBCI_HOST_PRIVATE_POS_CODE]) {
// Is the first byte equal to the channel change request?
case OPENBCI_HOST_CMD_CHANNEL_SET:
if (!systemUp) {
msgToPrint = HOST_MESSAGE_COMMS_DOWN;
printMessageToDriverFlag = true;
// Clean the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
}
// Make sure the channel is within bounds (<25)
if (buffer[3] <= RFDUINOGZLL_CHANNEL_LIMIT_UPPER) {
// Save requested new channel number
radioChannel = (uint32_t)buffer[OPENBCI_HOST_PRIVATE_POS_PAYLOAD];
// Save the previous channel number
previousRadioChannel = getChannelNumber();
// Send a channel change request to the device
singleCharMsg[0] = (char)ORPM_CHANGE_CHANNEL_HOST_REQUEST;
// Clear the serial buffer
bufferSerialReset(1);
// Send a single char message
return ACTION_RADIO_SEND_SINGLE_CHAR;
} else {
// Clear the serial buffer
bufferSerialReset(1);
// Send back error message to the PC/Driver
msgToPrint = HOST_MESSAGE_CHAN_VERIFY;
printMessageToDriverFlag = true;
// Don't send a single char message
return ACTION_RADIO_SEND_NONE;
}
case OPENBCI_HOST_CMD_POLL_TIME_SET:
if (systemUp) {
// Save the new poll time
pollTime = (uint32_t)buffer[OPENBCI_HOST_PRIVATE_POS_PAYLOAD];
// Send a time change request to the device
singleCharMsg[0] = (char)ORPM_CHANGE_POLL_TIME_HOST_REQUEST;
// Clear the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_SINGLE_CHAR;
} else {
msgToPrint = HOST_MESSAGE_COMMS_DOWN;
printMessageToDriverFlag = true;
// Clean the serial buffer
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
}
case OPENBCI_HOST_CMD_CHANNEL_SET_OVERIDE:
if (setChannelNumber((uint32_t)buffer[OPENBCI_HOST_PRIVATE_POS_PAYLOAD])) {
radioChannel = (uint32_t)buffer[OPENBCI_HOST_PRIVATE_POS_PAYLOAD];
msgToPrint = HOST_MESSAGE_CHAN_OVERRIDE;
printMessageToDriverFlag = true;
} else {
msgToPrint = HOST_MESSAGE_CHAN_VERIFY;
printMessageToDriverFlag = true;
}
bufferSerialReset(1);
return ACTION_RADIO_SEND_NONE;
default:
return ACTION_RADIO_SEND_NORMAL;
}
} else {
return ACTION_RADIO_SEND_NORMAL;
}
}
/**
* @description Called from Host's on_recieve if a packet will be sent.
* @param `device` {device_t} - The device to send the packet to.
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::sendPacketToDevice(device_t device, boolean lockPacketSend) {
// Build byteId
int packetNumber = bufferSerial.numberOfPacketsToSend - bufferSerial.numberOfPacketsSent - 1;
byte radioAction = ACTION_RADIO_SEND_NORMAL;
// Is there only one packet to send and it's the first packet?
if (bufferSerial.numberOfPacketsToSend == 1 && packetNumber == 0) {
// Enter the process outbound buffer subroutine
radioAction = processOutboundBuffer(bufferSerial.packetBuffer);
}
// Make the byte id
char byteId;
switch (radioAction) {
case ACTION_RADIO_SEND_SINGLE_CHAR:
RFduinoGZLL.sendToDevice(device,singleCharMsg,1);
// Set flag
packetInTXRadioBuffer = true;
break;
case ACTION_RADIO_SEND_NORMAL:
// Save the byteId
byteId = byteIdMake(false,packetNumber,(bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->data + 1, (bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->positionWrite - 1);
// Add the byteId to the packet
(bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->data[0] = byteId;
// Serial.print("Sending "); Serial.print((bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->positionWrite); Serial.println(" bytes");
RFduinoGZLL.sendToDevice(device,(char *)(bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->data, (bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->positionWrite);
// Increment number of bytes sent
bufferSerial.numberOfPacketsSent++;
// Set flag
packetInTXRadioBuffer = true;
break;
default: // do nothing
break;
}
}
/********************************************/
/********************************************/
/*********** DEVICE CODE **************/
/********************************************/
/********************************************/
/**
* @description Private function to handle a request to read serial as a device
* @return {boolean} - `true` if there is data to read, `false` if not...
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::didPicSendDeviceSerialData(void) {
return Serial.available() > 0;
}
/**
* @description Sends a null byte to the host
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::sendPollMessageToHost(void) {
RFduinoGZLL.sendToHost(NULL,0);
}
/**
* @description Sends a one byte long message to the host
* @param msg {byte} - A single byte to send to the host
* @author AJ Keller (@pushtheworldllc)
*/
void OpenBCI_Radios_Class::sendRadioMessageToHost(byte msg) {
RFduinoGZLL.sendToHost((const char*)msg,1);
}
void OpenBCI_Radios_Class::setByteIdForPacketBuffer(int packetNumber) {
char byteId = byteIdMake(false,packetNumber,(bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->data + 1, (bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->positionWrite - 1);
// Add the byteId to the packet
(bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->data[0] = byteId;
}
/**
* @description Called from Devices to send a packet to Host. Uses global
* variables to send the correct packet.
* @returns {boolean} - The packet number sent.
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::sendPacketToHost(void) {
// Reset the stream buffers
bufferStreamReset();
int packetNumber = bufferSerial.numberOfPacketsToSend - bufferSerial.numberOfPacketsSent - 1;
// Make the byteId
char byteId = byteIdMake(false,packetNumber,(bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->data + 1, (bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->positionWrite - 1);
// Add the byteId to the packet
(bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->data[0] = byteId;
if (RFduinoGZLL.sendToHost((char *)(bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->data, (bufferSerial.packetBuffer + bufferSerial.numberOfPacketsSent)->positionWrite)) {
pollRefresh();
bufferSerial.numberOfPacketsSent++;
return true;
}
return false;
}
/**
* @description Test to see if a char follows the stream tail byte format
* @author AJ Keller (@pushtheworldllc)
*/
boolean OpenBCI_Radios_Class::isATailByte(uint8_t newChar) {
return (newChar >> 4) == 0xC;
}
/**
* @description Sends a soft reset command to the Pic 32 incase of an emergency.