Skip to content

Commit f1053e0

Browse files
committed
Add WidgetWithSettings to Accelerometer, AnalogRead, FFT, and BandPower widgets
1 parent b5d8c32 commit f1053e0

5 files changed

Lines changed: 204 additions & 114 deletions

File tree

OpenBCI_GUI/W_Accelerometer.pde

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212
////////////////////////////////////////////////////
1313

14-
class W_Accelerometer extends Widget {
14+
class W_Accelerometer extends WidgetWithSettings {
1515
//To see all core variables/methods of the Widget class, refer to Widget.pde
1616
color graphStroke = color(210);
1717
color graphBG = color(245);
@@ -49,11 +49,7 @@ class W_Accelerometer extends Widget {
4949
widgetTitle = "Accelerometer";
5050

5151
accelBoard = (AccelerometerCapableBoard)currentBoard;
52-
53-
//Make dropdowns
54-
addDropdown("accelerometerVerticalScaleDropdown", "Vert Scale", EnumHelper.getEnumStrings(AccelerometerVerticalScale.class), verticalScale.getIndex());
55-
addDropdown("accelerometerHorizontalScaleDropdown", "Window", EnumHelper.getEnumStrings(AccelerometerHorizontalScale.class), horizontalScale.getIndex());
56-
52+
5753
setGraphDimensions();
5854
polarYMaxMin = adjustYMaxMinBasedOnSource();
5955

@@ -68,6 +64,20 @@ class W_Accelerometer extends Widget {
6864
createAccelModeButton("accelModeButton", "Turn Accel. Off", (int)(x + 1), (int)(y0 + NAV_HEIGHT + 1), 120, NAV_HEIGHT - 3, p5, 12, colorNotPressed, OPENBCI_DARKBLUE);
6965
}
7066

67+
@Override void initWidgetSettings() {
68+
super.initWidgetSettings();
69+
widgetSettings.set(AccelerometerVerticalScale.class, AccelerometerVerticalScale.AUTO)
70+
.set(AccelerometerHorizontalScale.class, AccelerometerHorizontalScale.FIVE_SEC)
71+
.saveDefaults();
72+
initDropdown(AccelerometerVerticalScale.class, "accelerometerVerticalScaleDropdown", "Vert Scale");
73+
initDropdown(AccelerometerHorizontalScale.class, "accelerometerHorizontalScaleDropdown", "Window");
74+
}
75+
76+
@Override void applySettings() {
77+
updateDropdownLabel(AccelerometerVerticalScale.class, "accelerometerVerticalScaleDropdown");
78+
updateDropdownLabel(AccelerometerHorizontalScale.class, "accelerometerHorizontalScaleDropdown");
79+
}
80+
7181
float adjustYMaxMinBasedOnSource() {
7282
float _yMaxMin;
7383
if (eegDataSource == DATASOURCE_CYTON) {
@@ -265,13 +275,15 @@ class W_Accelerometer extends Widget {
265275
}
266276

267277
public void setVerticalScale(int n) {
268-
verticalScale = AccelerometerVerticalScale.values()[n];
269-
accelerometerBar.adjustVertScale(verticalScale.getValue());
278+
widgetSettings.setByIndex(AccelerometerVerticalScale.class, n);
279+
int verticalScaleValue = widgetSettings.get(AccelerometerVerticalScale.class).getValue();
280+
accelerometerBar.adjustVertScale(verticalScaleValue);
270281
}
271282

272283
public void setHorizontalScale(int n) {
273-
horizontalScale = AccelerometerHorizontalScale.values()[n];
274-
accelerometerBar.adjustTimeAxis(horizontalScale.getValue());
284+
widgetSettings.setByIndex(AccelerometerHorizontalScale.class, n);
285+
int horizontalScaleValue = widgetSettings.get(AccelerometerHorizontalScale.class).getValue();
286+
accelerometerBar.adjustTimeAxis(horizontalScaleValue);
275287
}
276288

277289
};

OpenBCI_GUI/W_AnalogRead.pde

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// //
99
////////////////////////////////////////////////////////////////////////
1010

11-
class W_AnalogRead extends Widget {
11+
class W_AnalogRead extends WidgetWithSettings {
1212
private float arPadding;
1313
// values for actual time series chart (rectangle encompassing all analogReadBars)
1414
private float ar_x, ar_y, ar_h, ar_w;
@@ -18,8 +18,6 @@ class W_AnalogRead extends Widget {
1818

1919
private final int NUM_ANALOG_READ_BARS = 3;
2020
private AnalogReadBar[] analogReadBars;
21-
private AnalogReadHorizontalScale horizontalScale = AnalogReadHorizontalScale.FIVE_SEC;
22-
private AnalogReadVerticalScale verticalScale = AnalogReadVerticalScale.ONE_THOUSAND_FIFTY;
2321

2422
private boolean allowSpillover = false;
2523

@@ -33,9 +31,6 @@ class W_AnalogRead extends Widget {
3331

3432
analogBoard = (AnalogCapableBoard)currentBoard;
3533

36-
addDropdown("analogReadVerticalScaleDropdown", "Vert Scale", EnumHelper.getEnumStrings(AnalogReadVerticalScale.class), verticalScale.getIndex());
37-
addDropdown("analogReadHorizontalScaleDropdown", "Window", EnumHelper.getEnumStrings(AnalogReadHorizontalScale.class), horizontalScale.getIndex());
38-
3934
plotBottomWell = 45.0; //this appears to be an arbitrary vertical space adds GPlot leaves at bottom, I derived it through trial and error
4035
arPadding = 10.0;
4136
ar_x = float(x) + arPadding;
@@ -52,13 +47,32 @@ class W_AnalogRead extends Widget {
5247
AnalogReadBar tempBar = new AnalogReadBar(ourApplet, i+5, int(ar_x), analogReadBarY, int(ar_w), analogReadBarHeight); //int _channelNumber, int _x, int _y, int _w, int _h
5348
analogReadBars[i] = tempBar;
5449
}
55-
56-
setVerticalScale(verticalScale.getIndex());
57-
setHorizontalScale(horizontalScale.getIndex());
50+
51+
int verticalScaleValue = widgetSettings.get(AnalogReadVerticalScale.class).getValue();
52+
int horizontalScaleValue = widgetSettings.get(AnalogReadHorizontalScale.class).getValue();
53+
applyVerticalScale(verticalScaleValue);
54+
applyHorizontalScale(horizontalScaleValue);
5855

5956
createAnalogModeButton("analogModeButton", "Turn Analog Read On", (int)(x0 + 1), (int)(y0 + NAV_HEIGHT + 1), 128, NAV_HEIGHT - 3, p5, 12, colorNotPressed, OPENBCI_DARKBLUE);
6057
}
6158

59+
@Override
60+
protected void initWidgetSettings() {
61+
super.initWidgetSettings();
62+
widgetSettings.set(AnalogReadVerticalScale.class, AnalogReadVerticalScale.ONE_THOUSAND_FIFTY)
63+
.set(AnalogReadHorizontalScale.class, AnalogReadHorizontalScale.FIVE_SEC)
64+
.saveDefaults();
65+
66+
initDropdown(AnalogReadVerticalScale.class, "analogReadVerticalScaleDropdown", "Vert Scale");
67+
initDropdown(AnalogReadHorizontalScale.class, "analogReadHorizontalScaleDropdown", "Window");
68+
}
69+
70+
@Override
71+
protected void applySettings() {
72+
updateDropdownLabel(AnalogReadVerticalScale.class, "analogReadVerticalScaleDropdown");
73+
updateDropdownLabel(AnalogReadHorizontalScale.class, "analogReadHorizontalScaleDropdown");
74+
}
75+
6276
public void update() {
6377
super.update();
6478

@@ -159,16 +173,26 @@ class W_AnalogRead extends Widget {
159173
}
160174

161175
public void setVerticalScale(int n) {
162-
verticalScale = verticalScale.values()[n];
163-
for(int i = 0; i < analogReadBars.length; i++) {
164-
analogReadBars[i].adjustVertScale(verticalScale.getValue());
165-
}
176+
widgetSettings.setByIndex(AnalogReadVerticalScale.class, n);
177+
int verticalScaleValue = widgetSettings.get(AnalogReadVerticalScale.class).getValue();
178+
applyVerticalScale(verticalScaleValue);
166179
}
167180

168181
public void setHorizontalScale(int n) {
169-
horizontalScale = horizontalScale.values()[n];
182+
widgetSettings.setByIndex(AnalogReadHorizontalScale.class, n);
183+
int horizontalScaleValue = widgetSettings.get(AnalogReadHorizontalScale.class).getValue();
184+
applyHorizontalScale(horizontalScaleValue);
185+
}
186+
187+
private void applyVerticalScale(int value) {
188+
for(int i = 0; i < analogReadBars.length; i++) {
189+
analogReadBars[i].adjustVertScale(value);
190+
}
191+
}
192+
193+
private void applyHorizontalScale(int value) {
170194
for(int i = 0; i < analogReadBars.length; i++) {
171-
analogReadBars[i].adjustTimeAxis(horizontalScale.getValue());
195+
analogReadBars[i].adjustTimeAxis(value);
172196
}
173197
}
174198
};

OpenBCI_GUI/W_BandPower.pde

Lines changed: 54 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// //
1515
////////////////////////////////////////////////////////////////////////////////////////////////////////
1616

17-
class W_BandPower extends Widget {
17+
class W_BandPower extends WidgetWithSettings {
1818
// indexes
1919
private final int DELTA = 0; // 1-4 Hz
2020
private final int THETA = 1; // 4-8 Hz
@@ -30,11 +30,7 @@ class W_BandPower extends Widget {
3030
public ExGChannelSelect bpChanSelect;
3131
private boolean prevChanSelectIsVisible = false;
3232

33-
private List<controlP5.Controller> cp5ElementsToCheck = new ArrayList<controlP5.Controller>();
34-
35-
BPAutoClean autoClean = BPAutoClean.OFF;
36-
BPAutoCleanThreshold autoCleanThreshold = BPAutoCleanThreshold.FIFTY;
37-
BPAutoCleanTimer autoCleanTimer = BPAutoCleanTimer.THREE_SECONDS;
33+
private List<controlP5.Controller> cp5ElementsToCheck;
3834

3935
int[] autoCleanTimers;
4036
boolean[] previousThresholdCrossed;
@@ -46,28 +42,6 @@ class W_BandPower extends Widget {
4642
autoCleanTimers = new int[currentBoard.getNumEXGChannels()];
4743
previousThresholdCrossed = new boolean[currentBoard.getNumEXGChannels()];
4844

49-
//Add channel select dropdown to this widget
50-
bpChanSelect = new ExGChannelSelect(ourApplet, x, y, w, navH);
51-
bpChanSelect.activateAllButtons();
52-
53-
cp5ElementsToCheck.addAll(bpChanSelect.getCp5ElementsForOverlapCheck());
54-
55-
List<String> autoCleanList = EnumHelper.getEnumStrings(BPAutoClean.class);
56-
List<String> autoCleanThresholdList = EnumHelper.getEnumStrings(BPAutoCleanThreshold.class);
57-
List<String> autoCleanTimerList = EnumHelper.getEnumStrings(BPAutoCleanTimer.class);
58-
List<String> smoothingFactorList = EnumHelper.getEnumStrings(FFTSmoothingFactor.class);
59-
List<String> filteredEnumList = EnumHelper.getEnumStrings(FFTFilteredEnum.class);
60-
61-
//Add settings dropdowns
62-
addDropdown("bandPowerAutoCleanDropdown", "AutoClean", autoCleanList, autoClean.getIndex());
63-
addDropdown("bandPowerAutoCleanThresholdDropdown", "Threshold", autoCleanThresholdList, autoCleanThreshold.getIndex());
64-
addDropdown("bandPowerAutoCleanTimerDropdown", "Timer", autoCleanTimerList, autoCleanTimer.getIndex());
65-
//These two dropdowns also have to mirror the settings in the FFT widget
66-
FFTSmoothingFactor smoothingFactor = globalFFTSettings.getSmoothingFactor();
67-
FFTFilteredEnum filteredEnum = globalFFTSettings.getFilteredEnum();
68-
addDropdown("bandPowerSmoothingDropdown", "Smooth", smoothingFactorList, smoothingFactor.getIndex());
69-
addDropdown("bandPowerDataFilteringDropdown", "Filtered?", filteredEnumList, filteredEnum.getIndex());
70-
7145
// Setup for the BandPower plot
7246
bp_plot = new GPlot(ourApplet, x, y-NAV_HEIGHT, w, h+NAV_HEIGHT);
7347
// bp_plot.setPos(x, y+NAV_HEIGHT);
@@ -110,6 +84,46 @@ class W_BandPower extends Widget {
11084
bp_plot.getHistogram().setFontColor(OPENBCI_DARKBLUE);
11185
}
11286

87+
@Override
88+
protected void initWidgetSettings() {
89+
super.initWidgetSettings();
90+
widgetSettings.set(BPAutoClean.class, BPAutoClean.OFF)
91+
.set(BPAutoCleanThreshold.class, BPAutoCleanThreshold.FIFTY)
92+
.set(BPAutoCleanTimer.class, BPAutoCleanTimer.THREE_SECONDS)
93+
.set(FFTSmoothingFactor.class, globalFFTSettings.getSmoothingFactor())
94+
.set(FFTFilteredEnum.class, globalFFTSettings.getFilteredEnum());
95+
96+
initDropdown(BPAutoClean.class, "bandPowerAutoCleanDropdown", "Auto Clean");
97+
initDropdown(BPAutoCleanThreshold.class, "bandPowerAutoCleanThresholdDropdown", "Threshold");
98+
initDropdown(BPAutoCleanTimer.class, "bandPowerAutoCleanTimerDropdown", "Timer");
99+
initDropdown(FFTSmoothingFactor.class, "bandPowerSmoothingDropdown", "Smooth");
100+
initDropdown(FFTFilteredEnum.class, "bandPowerDataFilteringDropdown", "Filtered?");
101+
102+
bpChanSelect = new ExGChannelSelect(ourApplet, x, y, w, navH);
103+
bpChanSelect.activateAllButtons();
104+
cp5ElementsToCheck = new ArrayList<controlP5.Controller>();
105+
cp5ElementsToCheck.addAll(bpChanSelect.getCp5ElementsForOverlapCheck());
106+
saveActiveChannels(bpChanSelect.getActiveChannels());
107+
widgetSettings.saveDefaults();
108+
}
109+
110+
@Override
111+
protected void applySettings() {
112+
updateDropdownLabel(BPAutoClean.class, "bandPowerAutoCleanDropdown");
113+
updateDropdownLabel(BPAutoCleanThreshold.class, "bandPowerAutoCleanThresholdDropdown");
114+
updateDropdownLabel(BPAutoCleanTimer.class, "bandPowerAutoCleanTimerDropdown");
115+
updateDropdownLabel(FFTSmoothingFactor.class, "bandPowerSmoothingDropdown");
116+
updateDropdownLabel(FFTFilteredEnum.class, "bandPowerDataFilteringDropdown");
117+
applyActiveChannels(bpChanSelect);
118+
}
119+
120+
@Override
121+
protected void updateChannelSettings() {
122+
if (bpChanSelect != null) {
123+
saveActiveChannels(bpChanSelect.getActiveChannels());
124+
}
125+
}
126+
113127
public void update() {
114128
super.update();
115129

@@ -189,6 +203,9 @@ class W_BandPower extends Widget {
189203
}
190204

191205
private void autoCleanByEnableDisableChannels() {
206+
BPAutoClean autoClean = widgetSettings.get(BPAutoClean.class);
207+
BPAutoCleanThreshold autoCleanThreshold = widgetSettings.get(BPAutoCleanThreshold.class);
208+
BPAutoCleanTimer autoCleanTimer = widgetSettings.get(BPAutoCleanTimer.class);
192209
if (autoClean == BPAutoClean.OFF) {
193210
return;
194211
}
@@ -215,30 +232,18 @@ class W_BandPower extends Widget {
215232
}
216233
}
217234

218-
public BPAutoClean getAutoClean() {
219-
return autoClean;
220-
}
221-
222-
public BPAutoCleanThreshold getAutoCleanThreshold() {
223-
return autoCleanThreshold;
224-
}
225-
226-
public BPAutoCleanTimer getAutoCleanTimer() {
227-
return autoCleanTimer;
228-
}
229-
230235
public void setAutoClean(int n) {
231-
autoClean = autoClean.values()[n];
236+
widgetSettings.setByIndex(BPAutoClean.class, n);
232237
Arrays.fill(previousThresholdCrossed, false);
233238
Arrays.fill(autoCleanTimers, 0);
234239
}
235240

236241
public void setAutoCleanThreshold(int n) {
237-
autoCleanThreshold = autoCleanThreshold.values()[n];
242+
widgetSettings.setByIndex(BPAutoCleanThreshold.class, n);
238243
}
239244

240245
public void setAutoCleanTimer(int n) {
241-
autoCleanTimer = autoCleanTimer.values()[n];
246+
widgetSettings.setByIndex(BPAutoCleanTimer.class, n);
242247
}
243248

244249
//Called in DataProcessing.pde to update data even if widget is closed
@@ -261,13 +266,13 @@ class W_BandPower extends Widget {
261266
}
262267

263268
public void setSmoothingDropdownFrontend(FFTSmoothingFactor _smoothingFactor) {
264-
String s = _smoothingFactor.getString();
265-
cp5_widget.getController("bandPowerSmoothingDropdown").getCaptionLabel().setText(s);
269+
widgetSettings.set(FFTSmoothingFactor.class, _smoothingFactor);
270+
updateDropdownLabel(FFTSmoothingFactor.class, "bandPowerSmoothingDropdown");
266271
}
267272

268273
public void setFilteringDropdownFrontend(FFTFilteredEnum _filteredEnum) {
269-
String s = _filteredEnum.getString();
270-
cp5_widget.getController("bandPowerDataFilteringDropdown").getCaptionLabel().setText(s);
274+
widgetSettings.set(FFTFilteredEnum.class, _filteredEnum);
275+
updateDropdownLabel(FFTFilteredEnum.class, "bandPowerDataFilteringDropdown");
271276
}
272277
};
273278

@@ -286,11 +291,13 @@ public void bandPowerAutoCleanTimerDropdown(int n) {
286291
public void bandPowerSmoothingDropdown(int n) {
287292
globalFFTSettings.setSmoothingFactor(FFTSmoothingFactor.values()[n]);
288293
FFTSmoothingFactor smoothingFactor = globalFFTSettings.getSmoothingFactor();
294+
((W_BandPower) widgetManager.getWidget("W_BandPower")).setSmoothingDropdownFrontend(smoothingFactor);
289295
((W_Fft) widgetManager.getWidget("W_Fft")).setSmoothingDropdownFrontend(smoothingFactor);
290296
}
291297

292298
public void bandPowerDataFilteringDropdown(int n) {
293299
globalFFTSettings.setFilteredEnum(FFTFilteredEnum.values()[n]);
294300
FFTFilteredEnum filteredEnum = globalFFTSettings.getFilteredEnum();
301+
((W_BandPower) widgetManager.getWidget("W_BandPower")).setFilteringDropdownFrontend(filteredEnum);
295302
((W_Fft) widgetManager.getWidget("W_Fft")).setFilteringDropdownFrontend(filteredEnum);
296303
}

0 commit comments

Comments
 (0)