Skip to content

Commit ad4621f

Browse files
committed
Add WidgetWithSettings to Focus widget
1 parent c3ab090 commit ad4621f

1 file changed

Lines changed: 75 additions & 47 deletions

File tree

OpenBCI_GUI/W_Focus.pde

Lines changed: 75 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import brainflow.DataFilter;
2121
import brainflow.LogLevels;
2222
import brainflow.MLModel;
2323

24-
class W_Focus extends Widget {
24+
class W_Focus extends WidgetWithSettings {
25+
2526
private ExGChannelSelect focusChanSelect;
2627
private boolean prevChanSelectIsVisible = false;
2728
private AuditoryNeurofeedback auditoryNeurofeedback;
@@ -42,11 +43,6 @@ class W_Focus extends Widget {
4243

4344
private FifoChannelBar focusBar;
4445
private float focusBarHardYAxisLimit = 1.05f; //Provide slight "breathing room" to avoid GPlot error when metric value == 1.0
45-
private FocusXLim xLimit = FocusXLim.TEN;
46-
private FocusMetric focusMetric = FocusMetric.RELAXATION;
47-
private FocusClassifier focusClassifier = FocusClassifier.REGRESSION;
48-
private FocusThreshold focusThreshold = FocusThreshold.EIGHT_TENTHS;
49-
private FocusColors focusColors = FocusColors.GREEN;
5046

5147
private int[] exgChannels;
5248
private int channelCount;
@@ -61,18 +57,12 @@ class W_Focus extends Widget {
6157
private final int GRAPH_PADDING = 30;
6258
private color cBack, cDark, cMark, cFocus, cWave, cPanel;
6359

64-
List<controlP5.Controller> cp5ElementsToCheck = new ArrayList<controlP5.Controller>();
60+
List<controlP5.Controller> cp5ElementsToCheck;
6561

6662
W_Focus() {
6763
super();
6864
widgetTitle = "Focus";
69-
70-
//Add channel select dropdown to this widget
71-
focusChanSelect = new ExGChannelSelect(ourApplet, x, y, w, navH);
72-
focusChanSelect.activateAllButtons();
7365

74-
cp5ElementsToCheck.addAll(focusChanSelect.getCp5ElementsForOverlapCheck());
75-
7666
auditoryNeurofeedback = new AuditoryNeurofeedback(x + PAD_FIVE, y + PAD_FIVE, w/2 - PAD_FIVE*2, navBarHeight/2);
7767
cp5ElementsToCheck.add((controlP5.Controller)auditoryNeurofeedback.startStopButton);
7868
cp5ElementsToCheck.add((controlP5.Controller)auditoryNeurofeedback.modeButton);
@@ -83,15 +73,6 @@ class W_Focus extends Widget {
8373

8474
// initialize graphics parameters
8575
onColorChange();
86-
87-
List<String> metricList = EnumHelper.getEnumStrings(FocusMetric.class);
88-
List<String> thresholdList = EnumHelper.getEnumStrings(FocusThreshold.class);
89-
List<String> xLimitList = EnumHelper.getEnumStrings(FocusXLim.class);
90-
91-
dropdownWidth = 60; //Override the default dropdown width for this widget
92-
addDropdown("focusMetricDropdown", "Metric", metricList, focusMetric.getIndex());
93-
addDropdown("focusThresholdDropdown", "Threshold", thresholdList, focusThreshold.getIndex());
94-
addDropdown("focusWindowDropdown", "Window", xLimitList, xLimit.getIndex());
9576

9677
//Create data table
9778
dataGrid = new Grid(NUM_TABLE_ROWS, NUM_TABLE_COLUMNS, cellHeight);
@@ -106,11 +87,58 @@ class W_Focus extends Widget {
10687

10788
//create our focus graph
10889
updateGraphDims();
109-
focusBar = new FifoChannelBar(ourApplet, "Metric Value", xLimit.getValue(), focusBarHardYAxisLimit, graphX, graphY, graphW, graphH, ACCEL_X_COLOR, FocusXLim.TWENTY.getValue());
90+
int xLimitValue = widgetSettings.get(FocusXLim.class).getValue();
91+
focusBar = new FifoChannelBar(ourApplet, "Metric Value", xLimitValue, focusBarHardYAxisLimit, graphX, graphY, graphW, graphH, ACCEL_X_COLOR, FocusXLim.TWENTY.getValue());
11092

11193
initBrainFlowMetric();
11294
}
11395

96+
@Override
97+
protected void initWidgetSettings() {
98+
super.initWidgetSettings();
99+
100+
widgetSettings.set(FocusXLim.class, FocusXLim.TEN)
101+
.set(FocusMetric.class, FocusMetric.RELAXATION)
102+
.set(FocusClassifier.class, FocusClassifier.REGRESSION)
103+
.set(FocusThreshold.class, FocusThreshold.EIGHT_TENTHS)
104+
.set(FocusColors.class, FocusColors.GREEN);
105+
106+
dropdownWidth = 60; //Override the default dropdown width for this widget
107+
initDropdown(FocusMetric.class, "focusMetricDropdown", "Metric");
108+
initDropdown(FocusThreshold.class, "focusThresholdDropdown", "Threshold");
109+
initDropdown(FocusXLim.class, "focusWindowDropdown", "Window");
110+
111+
//Add channel select dropdown to this widget
112+
cp5ElementsToCheck = new ArrayList<controlP5.Controller>();
113+
focusChanSelect = new ExGChannelSelect(ourApplet, x, y, w, navH);
114+
focusChanSelect.activateAllButtons();
115+
saveActiveChannels(focusChanSelect.getActiveChannels());
116+
cp5ElementsToCheck.addAll(focusChanSelect.getCp5ElementsForOverlapCheck());
117+
118+
widgetSettings.saveDefaults();
119+
}
120+
121+
@Override
122+
protected void applySettings() {
123+
//Apply settings to dropdowns
124+
updateDropdownLabel(FocusXLim.class, "focusWindowDropdown");
125+
updateDropdownLabel(FocusMetric.class, "focusMetricDropdown");
126+
updateDropdownLabel(FocusThreshold.class, "focusThresholdDropdown");
127+
applyHorizontalScale();
128+
initBrainFlowMetric();
129+
130+
//Apply settings to channel select dropdown
131+
applyActiveChannels(focusChanSelect);
132+
}
133+
134+
@Override
135+
protected void updateChannelSettings() {
136+
//Save active channels to settings
137+
if (focusChanSelect != null) {
138+
saveActiveChannels(focusChanSelect.getActiveChannels());
139+
}
140+
}
141+
114142
public void update() {
115143
super.update();
116144

@@ -228,7 +256,8 @@ class W_Focus extends Widget {
228256
//Returns a metric value from 0. to 1. When there is an error, returns -1.
229257
private double updateFocusState() {
230258
try {
231-
int windowSize = currentBoard.getSampleRate() * xLimit.getValue();
259+
int xLimitValue = widgetSettings.get(FocusXLim.class).getValue();
260+
int windowSize = currentBoard.getSampleRate() * xLimitValue;
232261
// getData in GUI returns data in shape ndatapoints x nchannels, in BrainFlow its transposed
233262
List<double[]> currentData = currentBoard.getData(windowSize);
234263

@@ -289,6 +318,7 @@ class W_Focus extends Widget {
289318
strokeColor = cDark;
290319
sb.append("Not ");
291320
}
321+
FocusMetric focusMetric = widgetSettings.get(FocusMetric.class);
292322
sb.append(focusMetric.getIdealStateString());
293323
//Draw status graphic
294324
pushStyle();
@@ -304,6 +334,11 @@ class W_Focus extends Widget {
304334
}
305335

306336
private void initBrainFlowMetric() {
337+
if (mlModel != null) {
338+
endSession();
339+
}
340+
FocusMetric focusMetric = widgetSettings.get(FocusMetric.class);
341+
FocusClassifier focusClassifier = widgetSettings.get(FocusClassifier.class);
307342
BrainFlowModelParams modelParams = new BrainFlowModelParams(
308343
focusMetric.getMetric().get_code(),
309344
focusClassifier.getClassifier().get_code()
@@ -326,6 +361,7 @@ class W_Focus extends Widget {
326361
}
327362

328363
private void onColorChange() {
364+
FocusColors focusColors = widgetSettings.get(FocusColors.class);
329365
switch(focusColors) {
330366
case GREEN:
331367
cBack = #ffffff; //white
@@ -362,25 +398,28 @@ class W_Focus extends Widget {
362398
updateAuditoryNeurofeedbackPosition();
363399
}
364400

365-
public void setFocusHorizScale(int n) {
366-
xLimit = xLimit.values()[n];
367-
focusBar.adjustTimeAxis(xLimit.getValue());
401+
public void setFocusHorizontalScale(int n) {
402+
widgetSettings.setByIndex(FocusXLim.class, n);
403+
applyHorizontalScale();
368404
}
369405

370406
public void setMetric(int n) {
371-
focusMetric = focusMetric.values()[n];
372-
endSession();
407+
widgetSettings.setByIndex(FocusMetric.class, n);
373408
initBrainFlowMetric();
374409
}
375410

376411
public void setClassifier(int n) {
377-
focusClassifier = focusClassifier.values()[n];
378-
endSession();
412+
widgetSettings.setByIndex(FocusClassifier.class, n);
379413
initBrainFlowMetric();
380414
}
381415

416+
private void applyHorizontalScale() {
417+
int windowValue = widgetSettings.get(FocusXLim.class).getValue();
418+
focusBar.adjustTimeAxis(windowValue);
419+
}
420+
382421
public void setThreshold(int n) {
383-
focusThreshold = focusThreshold.values()[n];
422+
widgetSettings.setByIndex(FocusThreshold.class, n);
384423
}
385424

386425
public int getMetricExceedsThreshold() {
@@ -394,19 +433,8 @@ class W_Focus extends Widget {
394433
//Called in DataProcessing.pde to update data even if widget is closed
395434
public void updateFocusWidgetData() {
396435
metricPrediction = updateFocusState();
397-
predictionExceedsThreshold = metricPrediction > focusThreshold.getValue();
398-
}
399-
400-
public FocusMetric getFocusMetric() {
401-
return focusMetric;
402-
}
403-
404-
public FocusThreshold getFocusThreshold() {
405-
return focusThreshold;
406-
}
407-
408-
public FocusXLim getFocusWindow() {
409-
return xLimit;
436+
float focusThresholdValue = widgetSettings.get(FocusThreshold.class).getValue();
437+
predictionExceedsThreshold = metricPrediction > focusThresholdValue;
410438
}
411439

412440
public void clear() {
@@ -415,11 +443,11 @@ class W_Focus extends Widget {
415443
dataGrid.setString(df.format(metricPrediction), 0, 1);
416444
focusBar.update(metricPrediction);
417445
}
418-
}; //end of class
446+
};
419447

420448
//The following global functions are used by the Focus widget dropdowns. This method is the least amount of code.
421449
public void focusWindowDropdown(int n) {
422-
((W_Focus) widgetManager.getWidget("W_Focus")).setFocusHorizScale(n);
450+
((W_Focus) widgetManager.getWidget("W_Focus")).setFocusHorizontalScale(n);
423451
}
424452

425453
public void focusMetricDropdown(int n) {

0 commit comments

Comments
 (0)