Skip to content

Commit 5843fad

Browse files
committed
Add EMGSettings to SessionSettings
1 parent cc2103b commit 5843fad

3 files changed

Lines changed: 30 additions & 148 deletions

File tree

OpenBCI_GUI/EmgSettings.pde

Lines changed: 16 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,35 @@ class EmgSettings {
1111
values = new EmgSettingsValues();
1212
}
1313

14-
public boolean loadSettingsValues(String filename) {
14+
public String getJson() {
15+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
16+
return gson.toJson(values);
17+
}
18+
19+
public boolean loadSettingsFromJson(String json) {
1520
try {
16-
File file = new File(filename);
17-
StringBuilder fileContents = new StringBuilder((int)file.length());
18-
Scanner scanner = new Scanner(file);
19-
while(scanner.hasNextLine()) {
20-
fileContents.append(scanner.nextLine() + System.lineSeparator());
21-
}
2221
Gson gson = new Gson();
23-
EmgSettingsValues tempValues = gson.fromJson(fileContents.toString(), EmgSettingsValues.class);
22+
EmgSettingsValues tempValues = gson.fromJson(json, EmgSettingsValues.class);
23+
24+
// Validate channel count matches
2425
if (tempValues.window.length != channelCount) {
25-
outputError("Emg Settings: Loaded EMG Settings file has different number of channels than the current board.");
26+
outputError("Emg Settings: Loaded EMG Settings JSON has different number of channels than the current board.");
2627
return false;
2728
}
28-
//Explicitely copy values over to avoid reference issues
29-
//(e.g. values = tempValues "nukes" the old values object)
29+
30+
// Explicitly copy values to avoid reference issues
3031
values.window = tempValues.window;
3132
values.uvLimit = tempValues.uvLimit;
3233
values.creepIncreasing = tempValues.creepIncreasing;
3334
values.creepDecreasing = tempValues.creepDecreasing;
3435
values.minimumDeltaUV = tempValues.minimumDeltaUV;
3536
values.lowerThresholdMinimum = tempValues.lowerThresholdMinimum;
37+
38+
settingsWereLoaded = true;
3639
return true;
37-
} catch (IOException e) {
40+
} catch (Exception e) {
3841
e.printStackTrace();
39-
File f = new File(filename);
40-
if (f.exists()) {
41-
if (f.delete()) {
42-
outputError("Emg Settings: Could not load EMG settings from disk. Deleting this file...");
43-
} else {
44-
outputError("Emg Settings: Error deleting old/broken EMG settings file! Please make sure the GUI has proper read/write permissions.");
45-
}
46-
}
47-
return false;
48-
}
49-
}
50-
51-
public String getJson() {
52-
Gson gson = new GsonBuilder().setPrettyPrinting().create();
53-
return gson.toJson(values);
54-
}
55-
56-
public boolean saveToFile(String filename) {
57-
String json = getJson();
58-
try {
59-
FileWriter writer = new FileWriter(filename);
60-
writer.write(json);
61-
writer.close();
62-
return true;
63-
} catch (IOException e) {
64-
e.printStackTrace();
42+
outputError("EmgSettings: Could not load EMG settings from JSON string.");
6543
return false;
6644
}
6745
}
@@ -76,69 +54,11 @@ class EmgSettings {
7654
return channelCount;
7755
}
7856

79-
//Avoid error with popup being in another thread.
80-
public void storeSettings() {
81-
StringBuilder settingsFilename = new StringBuilder(directoryManager.getSettingsPath());
82-
settingsFilename.append("EmgSettings");
83-
settingsFilename.append("_");
84-
settingsFilename.append(getChannelCount());
85-
settingsFilename.append("Channels.json");
86-
String filename = settingsFilename.toString();
87-
File fileToSave = new File(filename);
88-
FileChooser chooser = new FileChooser(
89-
FileChooserMode.SAVE,
90-
"storeEmgSettings",
91-
fileToSave,
92-
"Save EMG settings to file");
93-
}
94-
95-
//Avoid error with popup being in another thread.
96-
public void loadSettings() {
97-
StringBuilder settingsFilename = new StringBuilder(directoryManager.getSettingsPath());
98-
settingsFilename.append("EmgSettings");
99-
settingsFilename.append("_");
100-
settingsFilename.append(getChannelCount());
101-
settingsFilename.append("Channels.json");
102-
String filename = settingsFilename.toString();
103-
File fileToLoad = new File(filename);
104-
FileChooser chooser = new FileChooser(
105-
FileChooserMode.LOAD,
106-
"loadEmgSettings",
107-
fileToLoad,
108-
"Select EMG settings file to load");
109-
110-
}
111-
11257
public boolean getSettingsWereLoaded() {
11358
return settingsWereLoaded;
11459
}
11560

11661
public void setSettingsWereLoaded(boolean settingsWereLoaded) {
11762
this.settingsWereLoaded = settingsWereLoaded;
11863
}
119-
}
120-
121-
//Used by button in the EMG UI. Must be global and public. Called in above loadSettings method.
122-
public void loadEmgSettings(File selection) {
123-
if (selection == null) {
124-
output("EMG Settings file not selected.");
125-
} else {
126-
if (dataProcessing.emgSettings.loadSettingsValues(selection.getAbsolutePath())) {
127-
outputSuccess("EMG Settings Loaded!");
128-
dataProcessing.emgSettings.setSettingsWereLoaded(true);
129-
}
130-
}
131-
}
132-
133-
//Used by button in the EMG UI. Must be global and public. Called in above storeSettings method.
134-
public void storeEmgSettings(File selection) {
135-
if (selection == null) {
136-
output("EMG Settings file not selected.");
137-
} else {
138-
if (dataProcessing.emgSettings.saveToFile(selection.getAbsolutePath())) {
139-
outputSuccess("EMG Settings Saved!");
140-
} else {
141-
outputError("Failed to save EMG Settings.");
142-
}
143-
}
14464
}

OpenBCI_GUI/EmgSettingsUI.pde

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ class EmgSettingsUI extends PApplet implements Runnable {
2727
private boolean isFixedHeight;
2828
private int fixedHeight;
2929
private int[] dropdownYPositions;
30-
private final int NUM_FOOTER_OBJECTS = 3;
31-
private final int FOOTER_OBJECT_WIDTH = 45;
32-
private final int FOOTER_OBJECT_HEIGHT = 26;
33-
private int footerObjY;
34-
private int[] footerObjX = new int[NUM_FOOTER_OBJECTS];
3530

3631
private final color HEADER_COLOR = OPENBCI_BLUE;
3732
private final color BACKGROUND_COLOR = GREY_235;
@@ -61,10 +56,6 @@ class EmgSettingsUI extends PApplet implements Runnable {
6156

6257
private String[] channelLabels;
6358

64-
private Button saveButton;
65-
private Button loadButton;
66-
private Button defaultButton;
67-
6859
@Override
6960
public void run() {
7061
PApplet.runSketch(new String[] {HEADER_MESSAGE}, this);
@@ -235,17 +226,6 @@ class EmgSettingsUI extends PApplet implements Runnable {
235226
}
236227

237228
private void createAllUIObjects() {
238-
final int HALF_FOOTER_HEIGHT = (FOOTER_PADDING + (DROPDOWN_SPACER * 2)) / 2;
239-
footerObjY = y + h - HALF_FOOTER_HEIGHT - (FOOTER_OBJECT_HEIGHT / 2);
240-
int middle = x + w / 2;
241-
int halfObjWidth = FOOTER_OBJECT_WIDTH / 2;
242-
footerObjX[0] = middle - halfObjWidth - PADDING_12 - FOOTER_OBJECT_WIDTH;
243-
footerObjX[1] = middle - halfObjWidth;
244-
footerObjX[2] = middle + halfObjWidth + PADDING_12;
245-
createEmgSettingsSaveButton("saveEmgSettingsButton", "Save", footerObjX[0], footerObjY, FOOTER_OBJECT_WIDTH, FOOTER_OBJECT_HEIGHT);
246-
createEmgSettingsLoadButton("loadEmgSettingsButton", "Load", footerObjX[1], footerObjY, FOOTER_OBJECT_WIDTH, FOOTER_OBJECT_HEIGHT);
247-
createEmgSettingsDefaultButton("defaultEmgSettingsButton", "Reset", footerObjX[2], footerObjY, FOOTER_OBJECT_WIDTH, FOOTER_OBJECT_HEIGHT);
248-
249229
channelLabels = new String[channelCount];
250230
for (int i = 0; i < channelCount; i++) {
251231
channelLabels[i] = "Channel " + (i+1);
@@ -369,36 +349,6 @@ class EmgSettingsUI extends PApplet implements Runnable {
369349
}
370350
}
371351

372-
private void createEmgSettingsSaveButton(String name, String text, int _x, int _y, int _w, int _h) {
373-
saveButton = createButton(emgCp5, name, text, _x, _y, _w, _h, h5, 12, colorNotPressed, OPENBCI_DARKBLUE);
374-
saveButton.setBorderColor(OBJECT_BORDER_GREY);
375-
saveButton.onClick(new CallbackListener() {
376-
public void controlEvent(CallbackEvent theEvent) {
377-
dataProcessing.emgSettings.storeSettings();
378-
}
379-
});
380-
}
381-
382-
private void createEmgSettingsLoadButton(String name, String text, int _x, int _y, int _w, int _h) {
383-
loadButton = createButton(emgCp5, name, text, _x, _y, _w, _h, h5, 12, colorNotPressed, OPENBCI_DARKBLUE);
384-
loadButton.setBorderColor(OBJECT_BORDER_GREY);
385-
loadButton.onClick(new CallbackListener() {
386-
public void controlEvent(CallbackEvent theEvent) {
387-
dataProcessing.emgSettings.loadSettings();
388-
}
389-
});
390-
}
391-
392-
private void createEmgSettingsDefaultButton(String name, String text, int _x, int _y, int _w, int _h) {
393-
defaultButton = createButton(emgCp5, name, text, _x, _y, _w, _h, h5, 12, colorNotPressed, OPENBCI_DARKBLUE);
394-
defaultButton.setBorderColor(OBJECT_BORDER_GREY);
395-
defaultButton.onClick(new CallbackListener() {
396-
public void controlEvent(CallbackEvent theEvent) {
397-
dataProcessing.emgSettings.revertAllChannelsToDefaultValues();
398-
}
399-
});
400-
}
401-
402352
private void updateCp5Objects() {
403353
for (int i = 0; i < channelCount; i++) {
404354
//Fetch values from the EmgSettingsValues object

OpenBCI_GUI/SessionSettings.pde

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class SessionSettings {
4444
KEY_NETWORKING = "networking",
4545
KEY_CONTAINERS = "widgetContainerSettings",
4646
KEY_WIDGET_SETTINGS = "widgetSettings",
47-
KEY_FILTER_SETTINGS = "filterSettings";
47+
KEY_FILTER_SETTINGS = "filterSettings",
48+
KEY_EMG_SETTINGS = "emgSettings";
4849

4950
// File paths configuration
5051
private final String[][] SETTING_FILES = {
@@ -102,6 +103,8 @@ class SessionSettings {
102103
parseJSONObject(widgetManager.getWidgetSettingsAsJson()));
103104
saveSettingsJSONData.setJSONObject(KEY_FILTER_SETTINGS,
104105
parseJSONObject(filterSettings.getJson()));
106+
saveSettingsJSONData.setJSONObject(KEY_EMG_SETTINGS,
107+
parseJSONObject(dataProcessing.emgSettings.getJson()));
105108

106109
// Save to file
107110
saveJSONObject(saveSettingsJSONData, saveFilePath);
@@ -145,6 +148,7 @@ class SessionSettings {
145148
applyWidgetLayout();
146149
applyWidgetSettings();
147150
applyFilterSettings();
151+
applyEmgSettings();
148152
}
149153

150154
/**
@@ -227,7 +231,15 @@ class SessionSettings {
227231
}
228232

229233
private void applyFilterSettings() {
230-
filterSettings.loadSettingsFromJson(loadSettingsJSONData.getJSONObject(KEY_FILTER_SETTINGS).toString());
234+
JSONObject filterSettingsJSON = loadSettingsJSONData.getJSONObject(KEY_FILTER_SETTINGS);
235+
String filterSettingsString = filterSettingsJSON.toString();
236+
filterSettings.loadSettingsFromJson(filterSettingsString);
237+
}
238+
239+
private void applyEmgSettings() {
240+
JSONObject emgSettingsJSON = loadSettingsJSONData.getJSONObject(KEY_EMG_SETTINGS);
241+
String emgSettingsString = emgSettingsJSON.toString();
242+
dataProcessing.emgSettings.loadSettingsFromJson(emgSettingsString);
231243
}
232244

233245
/**

0 commit comments

Comments
 (0)