-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathControlPanel.pde
More file actions
2525 lines (2228 loc) · 101 KB
/
ControlPanel.pde
File metadata and controls
2525 lines (2228 loc) · 101 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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
//////////////////////////////////////////////////////////////////////////
//
// System Control Panel
// - Select serial port from dropdown
// - Select default configuration (EEG, EKG, EMG)
// - Select Electrode Count (8 vs 16)
// - Select data mode (synthetic, playback file, real-time)
// - Record data? (y/n)
// - select output location
// - link to help guide
// - buttons to start/stop/reset application
//
// Written by: Conor Russomanno (Oct. 2014)
// Refactored by: Richard Waltman (Nov. 2020)
//
//////////////////////////////////////////////////////////////////////////
import controlP5.*;
import openbci_gui_helpers.*;
import java.io.IOException;
import java.util.List;
import openbci_gui_helpers.GanglionError;
import com.vmichalak.protocol.ssdp.Device;
import com.vmichalak.protocol.ssdp.SSDPClient;
//------------------------------------------------------------------------//
// Main Control Panel Class //
//------------------------------------------------------------------------//
class ControlPanel {
public int x, y, w, h;
public boolean isOpen;
//various control panel elements that are unique to specific datasources
DataSourceBox dataSourceBox;
SerialBox serialBox;
ComPortBox comPortBox;
public SessionDataBox dataLogBoxCyton;
ChannelCountBox channelCountBox;
InitBox initBox;
SyntheticChannelCountBox synthChannelCountBox;
RecentPlaybackBox recentPlaybackBox;
PlaybackFileBox playbackFileBox;
StreamingBoardBox streamingBoardBox;
BLEBox bleBox;
public SessionDataBox dataLogBoxGanglion;
InterfaceBoxGanglion interfaceBoxGanglion;
SampleRateGanglionBox sampleRateGanglionBox;
SDBox sdBox;
BrainFlowStreamerBox bfStreamerBoxCyton;
BrainFlowStreamerBox bfStreamerBoxGanglion;
BrainFlowStreamerBox bfStreamerBoxSynthetic;
ChannelPopup channelPopup;
RadioConfigBox rcBox;
//Track Dynamic and Static WiFi mode in Control Panel
final public String WIFI_DYNAMIC = "dynamic";
final public String WIFI_STATIC = "static";
private String wifiSearchStyle = WIFI_DYNAMIC;
boolean drawStopInstructions;
int globalPadding; //design feature: passed through to all box classes as the global spacing .. in pixels .. for all elements/subelements
boolean convertingSD = false;
private final int PAD_3 = 3;
ControlPanel(OpenBCI_GUI mainClass) {
x = PAD_3;
y = PAD_3 + topNav.controlPanelCollapser.getHeight();
w = topNav.controlPanelCollapser.getWidth();
h = height - int(helpWidget.h);
isOpen = false;
globalPadding = 10; //controls the padding of all elements on the control panel
dataSourceBox = new DataSourceBox(x, y, w, h, globalPadding);
interfaceBoxGanglion = new InterfaceBoxGanglion(x + w, dataSourceBox.y, w, h, globalPadding);
comPortBox = new ComPortBox(x+w*2, y, w, h, globalPadding);
rcBox = new RadioConfigBox(x+w, y + comPortBox.h, w, h, globalPadding);
serialBox = new SerialBox(x + w, dataSourceBox.y, w, h, globalPadding);
channelCountBox = new ChannelCountBox(x + w, (serialBox.y + serialBox.h), w, h, globalPadding);
dataLogBoxCyton = new SessionDataBox(x + w, (channelCountBox.y + channelCountBox.h), w, h, globalPadding, DATASOURCE_CYTON, dataLogger.getDataLoggerOutputFormat(), "sessionNameCyton");
bfStreamerBoxCyton = new BrainFlowStreamerBox(x + w, (dataLogBoxCyton.y + dataLogBoxCyton.h), w, h, globalPadding, "bfStreamerCyton");
sdBox = new SDBox(x + w, (bfStreamerBoxCyton.y + bfStreamerBoxCyton.h), w, h, globalPadding);
int playbackWidth = int(w * 1.35);
playbackFileBox = new PlaybackFileBox(x + w, dataSourceBox.y, playbackWidth, h, globalPadding);
recentPlaybackBox = new RecentPlaybackBox(x + w, (playbackFileBox.y + playbackFileBox.h), playbackWidth, h, globalPadding);
synthChannelCountBox = new SyntheticChannelCountBox(x + w, dataSourceBox.y, w, h, globalPadding);
bfStreamerBoxSynthetic = new BrainFlowStreamerBox(x + w, (synthChannelCountBox.y + synthChannelCountBox.h), w, h, globalPadding, "bfStreamerSynthetic");
streamingBoardBox = new StreamingBoardBox(x + w, dataSourceBox.y, w, h, globalPadding);
channelPopup = new ChannelPopup(x+w, y, w, h, globalPadding);
initBox = new InitBox(x, (dataSourceBox.y + dataSourceBox.h), w, h, globalPadding);
// Ganglion
bleBox = new BLEBox(x + w, interfaceBoxGanglion.y + interfaceBoxGanglion.h, w, h, globalPadding);
dataLogBoxGanglion = new SessionDataBox(x + w, (bleBox.y + bleBox.h), w, h, globalPadding, DATASOURCE_GANGLION, dataLogger.getDataLoggerOutputFormat(), "sessionNameGanglion");
bfStreamerBoxGanglion = new BrainFlowStreamerBox(x + w, (dataLogBoxGanglion.y + dataLogBoxGanglion.h), w, h, globalPadding, "bfStreamerGanglion");
sampleRateGanglionBox = new SampleRateGanglionBox(x + w, (bfStreamerBoxGanglion.y + bfStreamerBoxGanglion.h), w, h, globalPadding);
}
public void resetListItems(){
comPortBox.serialList.activeItem = -1;
bleBox.bleList.activeItem = -1;
}
public void open(){
isOpen = true;
topNav.controlPanelCollapser.setOn();
topNav.setDropdownMenuIsOpen(true);
}
public void close(){
isOpen = false;
topNav.controlPanelCollapser.setOff();
topNav.setDropdownMenuIsOpen(false);
}
public String getWifiSearchStyle() {
return wifiSearchStyle;
}
private void setWiFiSearchStyle(String s) {
wifiSearchStyle = s;
}
public void update() {
//update all boxes if they need to be
dataSourceBox.update();
serialBox.update();
bleBox.update();
dataLogBoxCyton.update();
channelCountBox.update();
synthChannelCountBox.update();
//update playback box sizes when dropdown is selected
recentPlaybackBox.update();
playbackFileBox.update();
streamingBoardBox.update();
bfStreamerBoxCyton.update();
bfStreamerBoxGanglion.update();
bfStreamerBoxSynthetic.update();
sdBox.update();
rcBox.update();
comPortBox.update();
initBox.update();
channelPopup.update();
dataLogBoxGanglion.update();
interfaceBoxGanglion.update();
}
public void draw() {
initBox.draw();
if (systemMode == 10) {
drawStopInstructions = true;
}
if (systemMode != 10) { // only draw control panel boxes if system running is false
dataSourceBox.draw();
drawStopInstructions = false;
//Carefully draw certain boxes based on UI/UX flow... let each box handle what is drawn inside with localCp5 instances
if (eegDataSource == DATASOURCE_CYTON) { //when data source is from OpenBCI
serialBox.draw();
channelCountBox.y = serialBox.y + serialBox.h;
if (rcBox.isShowing) {
comPortBox.draw();
rcBox.draw();
comPortBox.serialList.setVisible(true);
if (channelPopup.wasClicked()) {
channelPopup.draw();
}
}
dataLogBoxCyton.y = channelCountBox.y + channelCountBox.h;
bfStreamerBoxCyton.y = dataLogBoxCyton.y + dataLogBoxCyton.h;
sdBox.y = bfStreamerBoxCyton.y + bfStreamerBoxCyton.h;
channelCountBox.draw();
sdBox.draw();
bfStreamerBoxCyton.draw();
dataLogBoxCyton.draw(); //Drawing here allows max file size dropdown to be drawn on top
} else if (eegDataSource == DATASOURCE_PLAYBACKFILE) { //when data source is from playback file
recentPlaybackBox.draw();
playbackFileBox.draw();
} else if (eegDataSource == DATASOURCE_SYNTHETIC) { //synthetic
synthChannelCountBox.draw();
bfStreamerBoxSynthetic.draw();
} else if (eegDataSource == DATASOURCE_GANGLION) {
if (selectedProtocol == BoardProtocol.NONE) {
interfaceBoxGanglion.draw();
} else {
interfaceBoxGanglion.draw();
if (selectedProtocol == BoardProtocol.BLED112 || selectedProtocol == BoardProtocol.NATIVE_BLE) {
bleBox.y = interfaceBoxGanglion.y + interfaceBoxGanglion.h;
dataLogBoxGanglion.y = bleBox.y + bleBox.h;
bleBox.draw();
}
bfStreamerBoxGanglion.y = dataLogBoxGanglion.y + dataLogBoxGanglion.h;
bfStreamerBoxGanglion.draw();
dataLogBoxGanglion.draw(); //Drawing here allows max file size dropdown to be drawn on top
}
} else if (eegDataSource == DATASOURCE_STREAMING) {
streamingBoardBox.draw();
}
}
//draw the box that tells you to stop the system in order to edit control settings
if (drawStopInstructions) {
pushStyle();
fill(style.getBoxColor());
strokeWeight(1);
stroke(style.getBoxStrokeColor());
rect(x, y, w, dataSourceBox.h); //draw background of box
String stopInstructions = "Press the \"STOP SESSION\" button to change your data source or edit system settings.";
textAlign(CENTER, TOP);
textFont(p4, 14);
fill(style.getTextColor());
text(stopInstructions, x + globalPadding*2, y + globalPadding*3, w - globalPadding*4, dataSourceBox.h - globalPadding*4);
popStyle();
}
}
public void hideRadioPopoutBox() {
rcBox.isShowing = false;
comPortBox.isShowing = false;
serialBox.popOutRadioConfigButton.setOff();
rcBox.closeSerialPort();
}
private void hideChannelListCP() {
channelPopup.setClicked(false);
}
public void fetchSessionNameTextfieldAllBoards() {
String s = "";
if (eegDataSource == DATASOURCE_CYTON) {
// Store the current text field value of "Session Name" to be passed along to dataFiles
s = dataLogBoxCyton.getSessionTextfieldString();
} else if (eegDataSource == DATASOURCE_GANGLION) {
s = dataLogBoxGanglion.getSessionTextfieldString();
} else {
s = directoryManager.getFileNameDateTime();
}
dataLogger.setSessionName(s);
StringBuilder sb = new StringBuilder(directoryManager.getRecordingsPath());
sb.append("OpenBCISession_");
sb.append(dataLogger.getSessionName());
sb.append(File.separator);
dataLogger.setSessionPath(sb.toString());
}
public void setBrainFlowStreamerOutput() {
if (getIsBrainFlowSteamerDefaultFileOutput()) {
dataLogger.setBfWriterDefaultFolder();
}
if (eegDataSource == DATASOURCE_CYTON) {
brainflowStreamer = bfStreamerBoxCyton.getBrainFlowStreamerString();
} else if (eegDataSource == DATASOURCE_GANGLION) {
brainflowStreamer = bfStreamerBoxGanglion.getBrainFlowStreamerString();
} else if (eegDataSource == DATASOURCE_SYNTHETIC) {
brainflowStreamer = bfStreamerBoxSynthetic.getBrainFlowStreamerString();
}
}
private boolean getIsBrainFlowSteamerDefaultFileOutput() {
boolean b = false;
if (eegDataSource == DATASOURCE_CYTON) {
b = bfStreamerBoxCyton.getIsBrainFlowStreamerDefaultLocation();
} else if (eegDataSource == DATASOURCE_GANGLION) {
b = bfStreamerBoxGanglion.getIsBrainFlowStreamerDefaultLocation();
} else if (eegDataSource == DATASOURCE_SYNTHETIC) {
b = bfStreamerBoxSynthetic.getIsBrainFlowStreamerDefaultLocation();
}
return b;
}
}; //end of ControlPanel class
//==============================================================================//
// BELOW ARE THE CLASSES FOR THE VARIOUS //
// CONTROL PANEL BOXES (control widgets) //
//==============================================================================//
class DataSourceBox {
public int x, y, w, h, padding; //size and position
private final int NUM_ITEMS = 5;
private int boxHeight = 24;
private int spacing = 43;
private ControlP5 datasource_cp5;
private MenuList sourceList;
private boolean initialUpdate = false;
DataSourceBox(int _x, int _y, int _w, int _h, int _padding) {
x = _x;
y = _y;
w = _w;
h = spacing + (NUM_ITEMS * boxHeight);
padding = _padding;
//Instantiate local cp5 for this box
datasource_cp5 = new ControlP5(ourApplet);
datasource_cp5.setGraphics(ourApplet, 0,0);
datasource_cp5.setAutoDraw(false);
createDatasourceList(datasource_cp5, "DataSourceList", x + padding, y + padding*2 + 13, w - padding*2, NUM_ITEMS * boxHeight, p3);
}
public void update() {
if (!initialUpdate) {
initialUpdate = true;
if (eegDataSource >= 0) {
sourceList.selectItem(0); //Select the first item in the list on startup
}
}
}
public void draw() {
pushStyle();
fill(boxColor);
stroke(boxStrokeColor);
strokeWeight(1);
rect(x, y, w, h);
fill(OPENBCI_DARKBLUE);
textFont(h3, 16);
textAlign(LEFT, TOP);
text("DATA SOURCE", x + padding, y + padding);
popStyle();
datasource_cp5.draw();
}
private void createDatasourceList(ControlP5 _cp5, String name, int _x, int _y, int _w, int _h, PFont font) {
sourceList = new MenuList(_cp5, name, _w, _h, font);
sourceList.setPosition(_x, _y);
sourceList.addItem("CYTON (live)", DATASOURCE_CYTON);
sourceList.addItem("GANGLION (live)", DATASOURCE_GANGLION);
sourceList.addItem("PLAYBACK (from file)", DATASOURCE_PLAYBACKFILE);
sourceList.addItem("SYNTHETIC (algorithmic)", DATASOURCE_SYNTHETIC);
sourceList.addItem("STREAMING (from external)", DATASOURCE_STREAMING);
sourceList.scrollerLength = 10;
sourceList.addCallback(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
if (theEvent.getAction() == ControlP5.ACTION_BROADCAST) {
Map bob = sourceList.getItem(int(sourceList.getValue()));
String str = (String)bob.get("headline"); // Get the text displayed in the MenuList
int newDataSource = (int)bob.get("value");
sessionSettings.controlEventDataSource = str; //Used for output message on system start
eegDataSource = newDataSource;
//Reset protocol
selectedProtocol = BoardProtocol.NONE;
//Perform this check in a way that ignores order of items in the menulist
if (eegDataSource == DATASOURCE_CYTON) {
controlPanel.channelCountBox.set8ChanButtonActive();
selectedProtocol = BoardProtocol.SERIAL;
} else if (eegDataSource == DATASOURCE_GANGLION) {
updateGlobalChannelCount(4);
controlPanel.interfaceBoxGanglion.resetGanglionSelectedProtocol();
} else if (eegDataSource == DATASOURCE_PLAYBACKFILE) {
//GUI auto detects number of channels for playback when file is selected
} else if (eegDataSource == DATASOURCE_STREAMING) {
//do nothing for now
} else if (eegDataSource == DATASOURCE_SYNTHETIC) {
controlPanel.synthChannelCountBox.set8ChanButtonActive();
}
}
}
});
}
};
class SerialBox {
public int x, y, w, h, padding; //size and position
private ControlP5 cytonsb_cp5;
private Button autoConnectButton;
private Button popOutRadioConfigButton;
SerialBox(int _x, int _y, int _w, int _h, int _padding) {
x = _x;
y = _y;
w = _w;
h = 70;
padding = _padding;
//Instantiate local cp5 for this box
cytonsb_cp5 = new ControlP5(ourApplet);
cytonsb_cp5.setGraphics(ourApplet, 0,0);
cytonsb_cp5.setAutoDraw(false);
createAutoConnectButton("cytonAutoConnectButton", "AUTO-CONNECT", x + padding, y + padding*3 + 4, w - padding*3 - 70, 24);
createRadioConfigButton("cytonRadioConfigButton", "Settings", x + w - 70 - padding, y + padding*3 + 4, 70, 24);
}
public void update() {
}
public void draw() {
pushStyle();
fill(boxColor);
stroke(boxStrokeColor);
strokeWeight(1);
rect(x, y, w, h);
fill(OPENBCI_DARKBLUE);
textFont(h3, 16);
textAlign(LEFT, TOP);
text("SERIAL CONNECT", x + padding, y + padding);
popStyle();
cytonsb_cp5.draw();
}
private Button createSBButton(String name, String text, int _x, int _y, int _w, int _h) {
return createButton(cytonsb_cp5, name, text, _x, _y, _w, _h, 0, p5, 12, colorNotPressed, OPENBCI_DARKBLUE, BUTTON_HOVER, BUTTON_PRESSED, OPENBCI_DARKBLUE, 0);
}
private void createAutoConnectButton(String name, String text, int _x, int _y, int _w, int _h) {
autoConnectButton = createSBButton(name, text, _x, _y, _w, _h);
autoConnectButton.setColorBackground(TURN_ON_GREEN);
autoConnectButton.onRelease(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
controlPanel.comPortBox.attemptAutoConnectCyton();
}
});
autoConnectButton.setDescription("Attempt to auto-connect to Cyton. Try \"Manual\" if this does not work.");
}
private void createRadioConfigButton(String name, String text, int _x, int _y, int _w, int _h) {
popOutRadioConfigButton = createSBButton(name, text, _x, _y, _w, _h);
popOutRadioConfigButton.setSwitch(true);
popOutRadioConfigButton.onRelease(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
boolean showRadioBox = popOutRadioConfigButton.isOn();
if (!showRadioBox) {
controlPanel.hideRadioPopoutBox();
} else {
controlPanel.rcBox.isShowing = true;
controlPanel.rcBox.print_onscreen(controlPanel.rcBox.initial_message);
}
}
});
popOutRadioConfigButton.setDescription("Having trouble connecting to Cyton? Click here to access Radio Configuration tools.");
}
};
class ComPortBox {
public int x, y, w, h, padding; //size and position
public boolean isShowing;
private ControlP5 cytoncpb_cp5;
private Button refreshCytonDongles;
public MenuList serialList;
public RadioConfig cytonRadioCfg;
private boolean midAutoScan = false;
private boolean midAutoScanCheck2 = false;
ComPortBox(int _x, int _y, int _w, int _h, int _padding) {
x = _x;
y = _y;
w = _w + 10;
h = 140 + _padding;
padding = _padding;
isShowing = false;
cytonRadioCfg = new RadioConfig();
//Instantiate local cp5 for this box
cytoncpb_cp5 = new ControlP5(ourApplet);
cytoncpb_cp5.setGraphics(ourApplet, 0,0);
cytoncpb_cp5.setAutoDraw(false);
createRefreshCytonDonglesButton("refreshCytonDonglesButton", "REFRESH LIST", x + padding, y + padding*4 + 72 + 8, w - padding*2, 24);
createCytonDongleList(cytoncpb_cp5, "cytonDongleList", x + padding, y + padding*3 + 8, w - padding*2, 72, p3);
}
public void update() {
serialList.updateMenu();
//Allow two drawing/update cycles to pass so that overlay can be drawn
//This lets users know that auto-scan is working and GUI is not frozen
if (midAutoScan) {
if (midAutoScanCheck2) {
cytonAutoConnect_AutoScan();
midAutoScanCheck2 = false;
midAutoScan = midAutoScanCheck2;
}
midAutoScanCheck2 = midAutoScan;
}
}
public void draw() {
pushStyle();
fill(boxColor);
stroke(boxStrokeColor);
strokeWeight(1);
rect(x, y, w, h);
fill(OPENBCI_DARKBLUE);
textFont(h3, 16);
textAlign(LEFT, TOP);
text("SERIAL/COM PORT", x + padding, y + padding);
popStyle();
cytoncpb_cp5.draw();
}
private void createRefreshCytonDonglesButton(String name, String text, int _x, int _y, int _w, int _h) {
refreshCytonDongles = createButton(cytoncpb_cp5, name, text, _x, _y, _w, _h);
refreshCytonDongles.onRelease(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
refreshPortListCyton();
}
});
}
private void createCytonDongleList(ControlP5 _cp5, String name, int _x, int _y, int _w, int _h, PFont font) {
serialList = new MenuList(_cp5, name, _w, _h, font);
serialList.setPosition(_x, _y);
serialList.addCallback(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
if (theEvent.getAction() == ControlP5.ACTION_BROADCAST) {
Map bob = serialList.getItem(int(serialList.getValue()));
cytonDonglePortName = (String)bob.get("subline");
output("ControlPanel: Selected OpenBCI Port " + cytonDonglePortName);
}
}
});
}
//This is called when the Auto-Connect button is pressed in another Control Panel Box
public void attemptAutoConnectCyton() {
println("\n-------------------------------------------------\nControlPanel: Attempting to Auto-Connect to Cyton\n-------------------------------------------------\n");
LinkedList<String> comPorts = getCytonComPorts();
if (!comPorts.isEmpty()) {
cytonDonglePortName = comPorts.getFirst();
if (cytonRadioCfg.get_channel()) {
controlPanel.initBox.initButtonPressed();
} else {
outputWarn("Found a Cyton dongle, but could not connect to the board. Auto-Scanning now...");
midAutoScan = true;
}
} else {
outputWarn("No Cyton dongles were found.");
}
}
//If Cyton dongle exists, and fails to connect, try to Auto-Scan in the background to align Cyton/Dongle Channel
//This is called after overlay has a chance to draw on top to inform users the GUI is working and not crashed
private void cytonAutoConnect_AutoScan() {
if (cytonRadioCfg.scan_channels()) {
println("Successfully connected to Cyton using " + cytonDonglePortName);
controlPanel.initBox.initButtonPressed();
} else {
outputError("Unable to connect to Cyton. Please check hardware and power source.");
}
}
//Refresh the Cyton Dongle list
public void refreshPortListCyton(){
serialList.items.clear();
Thread thread = new Thread(){
public void run(){
refreshCytonDongles.getCaptionLabel().setText("SEARCHING...");
LinkedList<String> comPorts = getCytonComPorts();
for (String comPort : comPorts) {
serialList.addItem("(Cyton) " + comPort, comPort, "");
}
serialList.updateMenu();
refreshCytonDongles.getCaptionLabel().setText("REFRESH LIST");
}
};
thread.start();
}
private LinkedList<String> getCytonComPorts() {
final String[] names = {"FT231X USB UART", "VCP"};
final SerialPort[] comPorts = SerialPort.getCommPorts();
LinkedList<String> results = new LinkedList<String>();
for (SerialPort comPort : comPorts) {
for (String name : names) {
if (comPort.toString().startsWith(name)) {
// on macos need to drop tty ports
if (isMac() && comPort.getSystemPortName().startsWith("tty")) {
continue;
}
String found = "";
if (isMac() || isLinux()) found += "/dev/";
found += comPort.getSystemPortName();
println("ControlPanel: Found Cyton Dongle on COM port: " + found);
results.add(found);
}
}
}
return results;
}
public boolean isAutoScanningForCytonSerial() {
return midAutoScan;
}
};
class BLEBox {
public int x, y, w, h, padding; //size and position
private volatile boolean bleIsRefreshing = false;
private ControlP5 bleBox_cp5;
private MenuList bleList;
private Button refreshBLE;
Map<String, String> bleMACAddrMap = new HashMap<String, String>();
BLEBox(int _x, int _y, int _w, int _h, int _padding) {
x = _x;
y = _y;
w = _w;
h = 140 + _padding;
padding = _padding;
//Instantiate local cp5 for this box
bleBox_cp5 = new ControlP5(ourApplet);
bleBox_cp5.setGraphics(ourApplet, 0,0);
bleBox_cp5.setAutoDraw(false);
createRefreshBLEButton("refreshGanglionBLEButton", "START SEARCH", x + padding, y + padding*4 + 72 + 8, w - padding*5, 24);
createGanglionBLEMenuList(bleBox_cp5, "bleList", x + padding, y + padding*3 + 8, w - padding*2, 72, p3);
}
public void update() {
bleList.updateMenu();
}
public void draw() {
pushStyle();
fill(boxColor);
stroke(boxStrokeColor);
strokeWeight(1);
rect(x, y, w, h);
fill(OPENBCI_DARKBLUE);
textFont(h3, 16);
textAlign(LEFT, TOP);
text("BLE DEVICES", x + padding, y + padding);
popStyle();
if (bleIsRefreshing) {
//Display spinning cog gif
image(loadingGIF_blue, w + 225, refreshBLE.getPosition()[1] + 4, 20, 20);
} else {
//Draw small grey circle
pushStyle();
fill(#999999);
ellipseMode(CENTER);
ellipse(w + 225 + 10, refreshBLE.getPosition()[1] + 12, 12, 12);
popStyle();
}
bleBox_cp5.draw();
}
private void refreshGanglionNativeList() {
if (bleIsRefreshing) {
output("Search for Ganglions using Native Bluetooth is in progress.");
return;
}
output("Refreshing available Ganglions using Native Bluetooth...");
bleList.items.clear();
Thread thread = new Thread(){
public void run(){
refreshBLE.getCaptionLabel().setText("SEARCHING...");
bleIsRefreshing = true;
try {
bleMACAddrMap = GUIHelper.scan_for_ganglions (3);
for (Map.Entry<String, String> entry : bleMACAddrMap.entrySet ())
{
bleList.addItem(entry.getKey(), entry.getValue(), "");
bleList.updateMenu();
println("Found Ganglion Board: " + entry.getKey() + " " + entry.getValue());
}
} catch (GanglionError e)
{
e.printStackTrace();
}
refreshBLE.getCaptionLabel().setText("START SEARCH");
bleIsRefreshing = false;
}
};
thread.start();
}
private void refreshGanglionBLEList() {
if (bleIsRefreshing) {
output("Search for Ganglions using BLED112 Dongle is in progress.");
return;
}
output("Refreshing available Ganglions using BLED112 Dongle...");
bleList.items.clear();
Thread thread = new Thread(){
public void run(){
refreshBLE.getCaptionLabel().setText("SEARCHING...");
bleIsRefreshing = true;
final String comPort = getBLED112Port();
if (comPort != null) {
try {
bleMACAddrMap = GUIHelper.scan_for_ganglions (comPort, 3);
for (Map.Entry<String, String> entry : bleMACAddrMap.entrySet ())
{
bleList.addItem(entry.getKey(), comPort, "");
bleList.updateMenu();
}
} catch (GanglionError e)
{
e.printStackTrace();
}
} else {
outputError("No BLED112 Dongle Found");
}
refreshBLE.getCaptionLabel().setText("START SEARCH");
bleIsRefreshing = false;
}
};
thread.start();
}
public String getBLED112Port() {
String name = "Low Energy Dongle";
SerialPort[] comPorts = SerialPort.getCommPorts();
for (int i = 0; i < comPorts.length; i++) {
if (comPorts[i].toString().equals(name)) {
String found = "";
if (isMac() || isLinux()) found += "/dev/";
found += comPorts[i].getSystemPortName().toString();
println("ControlPanel: Found BLED112 Dongle on COM port: " + found);
return found;
}
}
return null;
}
private void createRefreshBLEButton(String name, String text, int _x, int _y, int _w, int _h) {
refreshBLE = createButton(bleBox_cp5, name, text, _x, _y, _w, _h);
refreshBLE.onRelease(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
if (selectedProtocol == BoardProtocol.BLED112) {
refreshGanglionBLEList();
} else {
refreshGanglionNativeList();
}
}
});
}
private void createGanglionBLEMenuList(ControlP5 _cp5, String name, int _x, int _y, int _w, int _h, PFont font) {
bleList = new MenuList(_cp5, name, _w, _h, font);
bleList.setPosition(_x, _y);
bleList.addCallback(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
if (theEvent.getAction() == ControlP5.ACTION_BROADCAST) {
Map bob = bleList.getItem(int(bleList.getValue()));
ganglion_portName = (String)bob.get("headline");
output("Ganglion Device Name = " + ganglion_portName);
}
}
});
}
};
class InterfaceBoxGanglion {
public int x, y, w, h, padding; //size and position
private ControlP5 ifbg_cp5;
private Button protocolGanglionNativeBLE;
private Button protocolBLED112Ganglion;
InterfaceBoxGanglion(int _x, int _y, int _w, int _h, int _padding) {
x = _x;
y = _y;
w = _w;
padding = _padding;
h = (24 + _padding) * 3;
int buttonHeight = 24;
//Instantiate local cp5 for this box
ifbg_cp5 = new ControlP5(ourApplet);
ifbg_cp5.setGraphics(ourApplet, 0,0);
ifbg_cp5.setAutoDraw(false);
createGanglionNativeBLEButton("protocolNativeBLEGanglion", "Bluetooth (Native)", false, x + padding, y + padding * 3 + 4, w - padding * 2, 24);
createBLED112Button("protocolBLED112Ganglion", "Bluetooth (BLED112 Dongle)", false, x + padding, y + (padding * 4) + 24 + 4, w - padding * 2, 24);
}
public void update() {}
public void draw() {
pushStyle();
fill(boxColor);
stroke(boxStrokeColor);
strokeWeight(1);
rect(x, y, w, h);
fill(OPENBCI_DARKBLUE);
textFont(h3, 16);
textAlign(LEFT, TOP);
text("PICK TRANSFER PROTOCOL", x + padding, y + padding);
popStyle();
ifbg_cp5.draw();
}
private Button createIFBGButton(String name, String text, boolean isToggled, int _x, int _y, int _w, int _h) {
final Button b = createButton(ifbg_cp5, name, text, _x, _y, _w, _h);
b.setSwitch(true); //This turns the button into a switch
if (isToggled) {
b.setOn();
}
return b;
}
private void createGanglionNativeBLEButton(String name, String text, boolean isToggled, int _x, int _y, int _w, int _h) {
protocolGanglionNativeBLE = createIFBGButton(name, text, isToggled, _x, _y, _w, _h);
protocolGanglionNativeBLE.onRelease(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
controlPanel.bleBox.bleList.items.clear();
selectedProtocol = BoardProtocol.NATIVE_BLE;
controlPanel.bleBox.refreshGanglionNativeList();
protocolGanglionNativeBLE.setOn();
protocolBLED112Ganglion.setOff();
}
});
}
private void createBLED112Button(String name, String text, boolean isToggled, int _x, int _y, int _w, int _h) {
protocolBLED112Ganglion = createIFBGButton(name, text, isToggled, _x, _y, _w, _h);
protocolBLED112Ganglion.onRelease(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
controlPanel.bleBox.bleList.items.clear();
selectedProtocol = BoardProtocol.BLED112;
controlPanel.bleBox.refreshGanglionBLEList();
protocolGanglionNativeBLE.setOff();
protocolBLED112Ganglion.setOn();
}
});
}
public void resetGanglionSelectedProtocol() {
protocolGanglionNativeBLE.setOff();
protocolBLED112Ganglion.setOff();
selectedProtocol = BoardProtocol.NONE;
}
};
class SessionDataBox {
public int x, y, w, h, padding; //size and position
private int datasource;
private final int bdfModeHeight = 127;
private int odfModeHeight;
private ControlP5 sessionData_cp5;
private int maxDurTextWidth = 82;
private int maxDurText_x = 0;
private Textfield sessionNameTextfield;
private Button autoSessionName;
private Button outputODF;
private Button outputBDF;
private ScrollableList maxDurationDropdown;
private String odfMessage = "Output has been set to OpenBCI Data Format (CSV).";
private String bdfMessage = "Output has been set to BioSemi Data Format (BDF+).";
SessionDataBox (int _x, int _y, int _w, int _h, int _padding, int _dataSource, int output, String textfieldName) {
datasource = _dataSource;
odfModeHeight = bdfModeHeight + 24 + _padding;
x = _x;
y = _y;
w = _w;
h = odfModeHeight;
padding = _padding;
maxDurText_x = x + padding;
maxDurTextWidth += padding*5 + 1;
//Instantiate local cp5 for this box
sessionData_cp5 = new ControlP5(ourApplet);
sessionData_cp5.setGraphics(ourApplet, 0,0);
sessionData_cp5.setAutoDraw(false);
createSessionNameTextfield(textfieldName);
//button to autogenerate file name based on time/date
createAutoSessionNameButton("autoSessionName", "GENERATE SESSION NAME", x + padding, y + 66, w-(padding*2), 24);
createODFButton("odfButton", "OpenBCI", dataLogger.getDataLoggerOutputFormat(), x + padding, y + padding*2 + 18 + 58, (w-padding*3)/2, 24);
createBDFButton("bdfButton", "BDF+", dataLogger.getDataLoggerOutputFormat(), x + padding*2 + (w-padding*3)/2, y + padding*2 + 18 + 58, (w-padding*3)/2, 24);
List<String> fileDurationList = EnumHelper.getEnumStrings(OdfFileDuration.class);
createMaxDurationDropdown("maxFileDuration", fileDurationList, odfFileDuration);
}
public void update() {
textfieldUpdateHelper.checkTextfield(sessionNameTextfield);
//Update the position of UI elements here, as this changes when user selects WiFi mode
sessionNameTextfield.setPosition(x + 60, y + 32);
autoSessionName.setPosition(x + padding, y + 66);
outputODF.setPosition(x + padding, y + padding*2 + 18 + 58);
outputBDF.setPosition(x + padding*2 + (w-padding*3)/2, y + padding*2 + 18 + 58);
boolean odfIsSelected = dataLogger.getDataLoggerOutputFormat() == dataLogger.OUTPUT_SOURCE_ODF;
h = odfIsSelected ? odfModeHeight : bdfModeHeight;
maxDurationDropdown.setVisible(odfIsSelected);
if (odfIsSelected && outputBDF.isOn()) {
outputODF.setOn();
outputBDF.setOff();
} else if (!odfIsSelected && outputODF.isOn()) {
outputBDF.setOn();
outputODF.setOff();
}
}
public void draw() {
pushStyle();
fill(boxColor);
stroke(boxStrokeColor);
strokeWeight(1);
rect(x, y, w, h);
fill(OPENBCI_DARKBLUE);
textFont(h3, 16);
textAlign(LEFT, TOP);
text("SESSION DATA", x + padding, y + padding);
textFont(p4, 14);
text("Name", x + padding, y + padding*2 + 14);
popStyle();
boolean odfIsSelected = dataLogger.getDataLoggerOutputFormat() == dataLogger.OUTPUT_SOURCE_ODF;
maxDurationDropdown.setVisible(odfIsSelected);
if (odfIsSelected) {
pushStyle();
//draw backgrounds to dropdown scrollableLists ... unfortunately ControlP5 doesn't have this by default, so we have to hack it to make it look nice...
//Dropdown is drawn at the end of ControlPanel.draw()
fill(OPENBCI_DARKBLUE);
maxDurationDropdown.setPosition(x + maxDurTextWidth, int(outputODF.getPosition()[1]) + 24 + padding);
//Carefully draw some text to the left of above dropdown, otherwise this text moves when changing WiFi mode
int extraPadding = 20;
fill(OPENBCI_DARKBLUE);
textFont(p4, 14);
text("Max File Duration", maxDurText_x, y + h - 24 - padding + extraPadding);
popStyle();
}
sessionData_cp5.draw();
}
private void createSessionNameTextfield(String name) {
//Create textfield to allow user to type custom session folder name
sessionNameTextfield = sessionData_cp5.addTextfield(name)
.setPosition(x + 60, y + 32)
.setCaptionLabel("")
.setSize(187, 26)
.setFont(f2)
.setFocus(false)
.setColor(color(26, 26, 26))
.setColorBackground(color(255, 255, 255)) // text field bg color
.setColorValueLabel(OPENBCI_DARKBLUE) // text color
.setColorForeground(OPENBCI_DARKBLUE) // border color when not selected
.setColorActive(isSelected_color) // border color when selected
.setColorCursor(color(26, 26, 26))
.setText(directoryManager.getFileNameDateTime())
.align(5, 10, 20, 40)
.setAutoClear(false); //Don't clear textfield when pressing Enter key
//Clear textfield on double click
sessionNameTextfield.onDoublePress(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
output("SessionData: Enter your custom session name.");
sessionNameTextfield.clear();
}
});
//Autogenerate session name if user presses Enter key and textfield value is null
sessionNameTextfield.addCallback(new CallbackListener() {
public void controlEvent(CallbackEvent theEvent) {
if (theEvent.getAction() == ControlP5.ACTION_BROADCAST && sessionNameTextfield.getText().equals("")) {
autogenerateSessionName();
}