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