Skip to content

Commit cb212e9

Browse files
committed
Add Save/Load for EMG Settings
1 parent 1fff6cd commit cb212e9

7 files changed

Lines changed: 303 additions & 87 deletions

File tree

OpenBCI_GUI/DataProcessing.pde

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class DataProcessing {
9393
float avgPowerInBins[][];
9494
float headWidePower[];
9595

96-
public EmgValues emgValues;
96+
public EmgSettings emgSettings;
9797

9898
DataProcessing(int NCHAN, float sample_rate_Hz) {
9999
nchan = NCHAN;
@@ -104,7 +104,7 @@ class DataProcessing {
104104
avgPowerInBins = new float[nchan][processing_band_low_Hz.length];
105105
headWidePower = new float[processing_band_low_Hz.length];
106106

107-
emgValues = new EmgValues();
107+
emgSettings = new EmgSettings();
108108
}
109109

110110
//Process data on a channel-by-channel basis
@@ -297,6 +297,6 @@ class DataProcessing {
297297
}
298298

299299
//Compute EMG values independent of widgets
300-
emgValues.process(dataProcessingFilteredBuffer);
300+
emgSettings.values.process(dataProcessingFilteredBuffer);
301301
}
302302
}

OpenBCI_GUI/EmgSettings.pde

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
class EmgSettings {
2+
3+
public EmgSettingsValues values;
4+
5+
private int channelCount;
6+
7+
private boolean settingsWereLoaded = false;
8+
9+
EmgSettings() {
10+
channelCount = currentBoard.getNumEXGChannels();
11+
values = new EmgSettingsValues();
12+
}
13+
14+
public boolean loadSettingsValues(String filename) {
15+
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+
}
22+
Gson gson = new Gson();
23+
EmgSettingsValues tempValues = gson.fromJson(fileContents.toString(), EmgSettingsValues.class);
24+
if (tempValues.smoothing.length != channelCount) {
25+
outputError("Emg Settings: Loaded EMG Settings file has different number of channels than the current board.");
26+
return false;
27+
}
28+
values = tempValues;
29+
return true;
30+
} catch (IOException e) {
31+
e.printStackTrace();
32+
File f = new File(filename);
33+
if (f.exists()) {
34+
if (f.delete()) {
35+
outputError("Emg Settings: Could not load EMG settings from disk. Deleting this file...");
36+
} else {
37+
outputError("Emg Settings: Error deleting old/broken EMG settings file! Please make sure the GUI has proper read/write permissions.");
38+
}
39+
}
40+
return false;
41+
}
42+
}
43+
44+
public String getJson() {
45+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
46+
return gson.toJson(values);
47+
}
48+
49+
public boolean saveToFile(String filename) {
50+
String json = getJson();
51+
try {
52+
FileWriter writer = new FileWriter(filename);
53+
writer.write(json);
54+
writer.close();
55+
return true;
56+
} catch (IOException e) {
57+
e.printStackTrace();
58+
return false;
59+
}
60+
}
61+
62+
public void revertAllChannelsToDefaultValues() {
63+
values = new EmgSettingsValues();
64+
settingsWereLoaded = true;
65+
}
66+
67+
//Called in UI to control number of channels. This is set from the board when this class is instantiated.
68+
public int getChannelCount() {
69+
return channelCount;
70+
}
71+
72+
//Avoid error with popup being in another thread.
73+
public void storeSettings() {
74+
StringBuilder settingsFilename = new StringBuilder(directoryManager.getSettingsPath());
75+
settingsFilename.append("EmgSettings");
76+
settingsFilename.append("_");
77+
settingsFilename.append(getChannelCount());
78+
settingsFilename.append("Channels.json");
79+
String filename = settingsFilename.toString();
80+
File fileToSave = new File(filename);
81+
selectOutput("Save EMG settings to file", "storeEmgSettings", fileToSave);
82+
}
83+
84+
//Avoid error with popup being in another thread.
85+
public void loadSettings() {
86+
StringBuilder settingsFilename = new StringBuilder(directoryManager.getSettingsPath());
87+
settingsFilename.append("EmgSettings");
88+
settingsFilename.append("_");
89+
settingsFilename.append(getChannelCount());
90+
settingsFilename.append("Channels.json");
91+
String filename = settingsFilename.toString();
92+
File fileToLoad = new File(filename);
93+
selectInput("Select EMG settings file to load", "loadEmgSettings", fileToLoad);
94+
}
95+
96+
public boolean getSettingsWereLoaded() {
97+
return settingsWereLoaded;
98+
}
99+
100+
public void setSettingsWereLoaded(boolean settingsWereLoaded) {
101+
this.settingsWereLoaded = settingsWereLoaded;
102+
}
103+
}
104+
105+
//Used by button in the EMG UI. Must be global and public. Called in above loadSettings method.
106+
public void loadEmgSettings(File selection) {
107+
if (selection == null) {
108+
output("EMG Settings file not selected.");
109+
} else {
110+
if (dataProcessing.emgSettings.loadSettingsValues(selection.getAbsolutePath())) {
111+
outputSuccess("EMG Settings Loaded!");
112+
dataProcessing.emgSettings.setSettingsWereLoaded(true);
113+
}
114+
}
115+
}
116+
117+
//Used by button in the EMG UI. Must be global and public. Called in above storeSettings method.
118+
public void storeEmgSettings(File selection) {
119+
if (selection == null) {
120+
output("EMG Settings file not selected.");
121+
} else {
122+
if (dataProcessing.emgSettings.saveToFile(selection.getAbsolutePath())) {
123+
outputSuccess("EMG Settings Saved!");
124+
} else {
125+
outputError("Failed to save EMG Settings.");
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)