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+ // Explicitely copy values over to avoid reference issues
29+ // (e.g. values = tempValues "nukes" the old values object)
30+ values. smoothing = tempValues. smoothing;
31+ values. uvLimit = tempValues. uvLimit;
32+ values. creepIncreasing = tempValues. creepIncreasing;
33+ values. creepDecreasing = tempValues. creepDecreasing;
34+ values. minimumDeltaUV = tempValues. minimumDeltaUV;
35+ values. lowerThresholdMinimum = tempValues. lowerThresholdMinimum;
36+ return true ;
37+ } catch (IOException e) {
38+ 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();
65+ return false ;
66+ }
67+ }
68+
69+ public void revertAllChannelsToDefaultValues () {
70+ values = new EmgSettingsValues ();
71+ settingsWereLoaded = true ;
72+ }
73+
74+ // Called in UI to control number of channels. This is set from the board when this class is instantiated.
75+ public int getChannelCount () {
76+ return channelCount;
77+ }
78+
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+ selectOutput (" Save EMG settings to file" , " storeEmgSettings" , fileToSave);
89+ }
90+
91+ // Avoid error with popup being in another thread.
92+ public void loadSettings () {
93+ StringBuilder settingsFilename = new StringBuilder (directoryManager. getSettingsPath());
94+ settingsFilename. append(" EmgSettings" );
95+ settingsFilename. append(" _" );
96+ settingsFilename. append(getChannelCount());
97+ settingsFilename. append(" Channels.json" );
98+ String filename = settingsFilename. toString();
99+ File fileToLoad = new File (filename);
100+ selectInput (" Select EMG settings file to load" , " loadEmgSettings" , fileToLoad);
101+ }
102+
103+ public boolean getSettingsWereLoaded () {
104+ return settingsWereLoaded;
105+ }
106+
107+ public void setSettingsWereLoaded (boolean settingsWereLoaded ) {
108+ this . settingsWereLoaded = settingsWereLoaded;
109+ }
110+ }
111+
112+ // Used by button in the EMG UI. Must be global and public. Called in above loadSettings method.
113+ public void loadEmgSettings (File selection ) {
114+ if (selection == null ) {
115+ output(" EMG Settings file not selected." );
116+ } else {
117+ if (dataProcessing. emgSettings. loadSettingsValues(selection. getAbsolutePath())) {
118+ outputSuccess(" EMG Settings Loaded!" );
119+ dataProcessing. emgSettings. setSettingsWereLoaded(true );
120+ }
121+ }
122+ }
123+
124+ // Used by button in the EMG UI. Must be global and public. Called in above storeSettings method.
125+ public void storeEmgSettings (File selection ) {
126+ if (selection == null ) {
127+ output(" EMG Settings file not selected." );
128+ } else {
129+ if (dataProcessing. emgSettings. saveToFile(selection. getAbsolutePath())) {
130+ outputSuccess(" EMG Settings Saved!" );
131+ } else {
132+ outputError(" Failed to save EMG Settings." );
133+ }
134+ }
135+ }
0 commit comments