Skip to content

Commit 3f9f6d6

Browse files
committed
Refactor and clean up SessionSettings.pde with initial Save/Load settings (WIP)
1 parent adeba05 commit 3f9f6d6

14 files changed

Lines changed: 354 additions & 805 deletions

OpenBCI_GUI/ControlPanel.pde

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class ControlPanel {
266266
sb.append("OpenBCISession_");
267267
sb.append(dataLogger.getSessionName());
268268
sb.append(File.separator);
269-
sessionSettings.setSessionPath(sb.toString());
269+
dataLogger.setSessionPath(sb.toString());
270270
}
271271

272272
public void setBrainFlowStreamerOutput() {
@@ -910,10 +910,8 @@ class SessionDataBox {
910910
createODFButton("odfButton", "OpenBCI", dataLogger.getDataLoggerOutputFormat(), x + padding, y + padding*2 + 18 + 58, (w-padding*3)/2, 24);
911911
createBDFButton("bdfButton", "BDF+", dataLogger.getDataLoggerOutputFormat(), x + padding*2 + (w-padding*3)/2, y + padding*2 + 18 + 58, (w-padding*3)/2, 24);
912912

913-
createMaxDurationDropdown("maxFileDuration", Arrays.asList(sessionSettings.fileDurations));
914-
915-
916-
913+
List<String> fileDurationList = EnumHelper.getEnumStrings(OdfFileDuration.class);
914+
createMaxDurationDropdown("maxFileDuration", fileDurationList, odfFileDuration);
917915
}
918916

919917
public void update() {
@@ -1012,10 +1010,10 @@ class SessionDataBox {
10121010
});
10131011
}
10141012

1015-
private void createMaxDurationDropdown(String name, List<String> _items){
1013+
private void createMaxDurationDropdown(String name, List<String> _items, OdfFileDuration defaultValue) {
10161014
maxDurationDropdown = sessionData_cp5.addScrollableList(name)
10171015
.setOpen(false)
1018-
.setColor(sessionSettings.dropdownColors)
1016+
.setColor(dropdownColorsGlobal)
10191017
.setOutlineColor(150)
10201018
//.setColorBackground(OPENBCI_BLUE) // text field bg color
10211019
.setColorValueLabel(OPENBCI_DARKBLUE) // text color
@@ -1033,7 +1031,7 @@ class SessionDataBox {
10331031
maxDurationDropdown
10341032
.getCaptionLabel() //the caption label is the text object in the primary bar
10351033
.toUpperCase(false) //DO NOT AUTOSET TO UPPERCASE!!!
1036-
.setText(sessionSettings.fileDurations[sessionSettings.defaultOBCIMaxFileSize])
1034+
.setText(defaultValue.getString())
10371035
.setFont(p4)
10381036
.setSize(14)
10391037
.getStyle() //need to grab style before affecting the paddingTop
@@ -1042,7 +1040,7 @@ class SessionDataBox {
10421040
maxDurationDropdown
10431041
.getValueLabel() //the value label is connected to the text objects in the dropdown item bars
10441042
.toUpperCase(false) //DO NOT AUTOSET TO UPPERCASE!!!
1045-
.setText(sessionSettings.fileDurations[sessionSettings.defaultOBCIMaxFileSize])
1043+
.setText(defaultValue.getString())
10461044
.setFont(h5)
10471045
.setSize(12) //set the font size of the item bars to 14pt
10481046
.getStyle() //need to grab style before affecting the paddingTop
@@ -1052,7 +1050,7 @@ class SessionDataBox {
10521050
public void controlEvent(CallbackEvent theEvent) {
10531051
if (theEvent.getAction() == ControlP5.ACTION_BROADCAST) {
10541052
int n = (int)(theEvent.getController()).getValue();
1055-
sessionSettings.setLogFileDurationChoice(n);
1053+
dataLogger.setLogFileDurationChoice(n);
10561054
println("ControlPanel: Chosen Recording Duration: " + n);
10571055
} else if (theEvent.getAction() == ControlP5.ACTION_ENTER) {
10581056
lockOutsideElements(true);
@@ -1775,7 +1773,7 @@ class BrainFlowStreamerBox {
17751773
private void createDropdown(String name){
17761774
bfFileSaveOption = bfStreamerCp5.addScrollableList(name)
17771775
.setOpen(false)
1778-
.setColor(sessionSettings.dropdownColors)
1776+
.setColor(dropdownColorsGlobal)
17791777
.setOutlineColor(150)
17801778
.setSize(167, (dataWriterBfEnum.values().length + 1) * 24)
17811779
.setBarHeight(24) //height of top/primary bar
@@ -2164,7 +2162,7 @@ class SDBox {
21642162

21652163
sdList = cp5_sdBox.addScrollableList(name)
21662164
.setOpen(false)
2167-
.setColor(sessionSettings.dropdownColors)
2165+
.setColor(dropdownColorsGlobal)
21682166
.setOutlineColor(150)
21692167
.setSize(w - padding*2, 2*24)//temporary size
21702168
.setBarHeight(24) //height of top/primary bar

OpenBCI_GUI/DataLogger.pde

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ class DataLogger {
88
public final int OUTPUT_SOURCE_ODF = 1; // The OpenBCI CSV Data Format
99
public final int OUTPUT_SOURCE_BDF = 2; // The BDF data format http://www.biosemi.com/faq/file_format.htm
1010
private int outputDataSource;
11+
private String sessionPath = "";
12+
private boolean logFileIsOpen = false;
13+
private long logFileStartTime;
14+
private long logFileMaxDurationNano = -1;
1115

1216
DataLogger() {
1317
//Default to OpenBCI CSV Data Format
@@ -33,7 +37,7 @@ class DataLogger {
3337

3438
private void saveNewData() {
3539
//If data is available, save to playback file...
36-
if(!sessionSettings.isLogFileOpen()) {
40+
if(!isLogFileOpen()) {
3741
return;
3842
}
3943

@@ -54,21 +58,21 @@ class DataLogger {
5458
}
5559

5660
public void limitRecordingFileDuration() {
57-
if (sessionSettings.isLogFileOpen() && outputDataSource == OUTPUT_SOURCE_ODF && sessionSettings.maxLogTimeReached()) {
61+
if (isLogFileOpen() && outputDataSource == OUTPUT_SOURCE_ODF && maxLogTimeReached()) {
5862
println("DataLogging: Max recording duration reached for OpenBCI data format. Creating a new recording file in the session folder.");
5963
closeLogFile();
6064
openNewLogFile(directoryManager.getFileNameDateTime());
61-
sessionSettings.setLogFileStartTime(System.nanoTime());
65+
setLogFileStartTime(System.nanoTime());
6266
}
6367
}
6468

6569
public void onStartStreaming() {
6670
if (outputDataSource > OUTPUT_SOURCE_NONE && eegDataSource != DATASOURCE_PLAYBACKFILE) {
6771
//open data file if it has not already been opened
68-
if (!sessionSettings.isLogFileOpen()) {
72+
if (!isLogFileOpen()) {
6973
openNewLogFile(directoryManager.getFileNameDateTime());
7074
}
71-
sessionSettings.setLogFileStartTime(System.nanoTime());
75+
setLogFileStartTime(System.nanoTime());
7276
}
7377

7478
//Print BrainFlow Streamer Info here after ODF and BDF println
@@ -111,7 +115,7 @@ class DataLogger {
111115
// Do nothing...
112116
break;
113117
}
114-
sessionSettings.setLogFileIsOpen(true);
118+
setLogFileIsOpen(true);
115119
}
116120

117121
/**
@@ -159,7 +163,7 @@ class DataLogger {
159163
// Do nothing...
160164
break;
161165
}
162-
sessionSettings.setLogFileIsOpen(false);
166+
setLogFileIsOpen(false);
163167
}
164168

165169
private void closeLogFileBDF() {
@@ -188,22 +192,61 @@ class DataLogger {
188192
sessionName = s;
189193
}
190194

191-
public final String getSessionName() {
195+
public String getSessionName() {
192196
return sessionName;
193197
}
198+
199+
200+
public void setSessionPath (String _path) {
201+
sessionPath = _path;
202+
}
203+
204+
public String getSessionPath() {
205+
return sessionPath;
206+
}
207+
194208

195209
public void setBfWriterFolder(String _folderName, String _folderPath) {
196210
fileWriterBF.setBrainFlowStreamerFolderName(_folderName, _folderPath);
197211
}
198212

199213
public void setBfWriterDefaultFolder() {
200-
if (sessionSettings.getSessionPath() != "") {
201-
sessionSettings.setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + sessionName);
214+
if (getSessionPath() != "") {
215+
setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + sessionName);
202216
}
203-
fileWriterBF.setBrainFlowStreamerFolderName(sessionName, sessionSettings.getSessionPath());
217+
fileWriterBF.setBrainFlowStreamerFolderName(sessionName, getSessionPath());
204218
}
205219

206220
public String getBfWriterFilePath() {
207221
return fileWriterBF.getBrainFlowStreamerRecordingFileName();
208222
}
223+
224+
225+
private void setLogFileIsOpen(boolean _toggle) {
226+
logFileIsOpen = _toggle;
227+
}
228+
229+
private boolean isLogFileOpen() {
230+
return logFileIsOpen;
231+
}
232+
233+
private void setLogFileStartTime(long _time) {
234+
logFileStartTime = _time;
235+
verbosePrint("Settings: LogFileStartTime = " + _time);
236+
}
237+
238+
public void setLogFileDurationChoice(int n) {
239+
int fileDurationMinutes = odfFileDuration.values()[n].getValue();
240+
logFileMaxDurationNano = fileDurationMinutes * 1000000000L * 60;
241+
println("Settings: LogFileMaxDuration = " + fileDurationMinutes + " minutes");
242+
}
243+
244+
//Only called during live mode && using OpenBCI Data Format
245+
private boolean maxLogTimeReached() {
246+
if (logFileMaxDurationNano < 0) {
247+
return false;
248+
} else {
249+
return (System.nanoTime() - logFileStartTime) > (logFileMaxDurationNano);
250+
}
251+
}
209252
};

OpenBCI_GUI/DataWriterBF.pde

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class DataWriterBF {
4848
}
4949

5050
public void setBrainFlowStreamerFolderName(String _folderName, String _folderPath) {
51+
//FIX ME ?
5152
//sessionSettings.setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + _sessionName + File.separator);
5253
folderName = _folderName;
5354
folderPath = _folderPath;

OpenBCI_GUI/DataWriterODF.pde

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public class DataWriterODF {
77
protected String headerFirstLineString = "%OpenBCI Raw EXG Data";
88

99
DataWriterODF(String _sessionName, String _fileName) {
10-
sessionSettings.setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + _sessionName + File.separator);
11-
fname = sessionSettings.getSessionPath();
10+
dataLogger.setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + _sessionName + File.separator);
11+
fname = dataLogger.getSessionPath();
1212
fname += fileNamePrependString;
1313
fname += _fileName;
1414
fname += ".txt";
@@ -21,8 +21,8 @@ public class DataWriterODF {
2121
DataWriterODF(String _sessionName, String _fileName, String _fileNamePrependString, String _headerFirstLineString) {
2222
fileNamePrependString = _fileNamePrependString;
2323
headerFirstLineString = _headerFirstLineString;
24-
sessionSettings.setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + _sessionName + File.separator);
25-
fname = sessionSettings.getSessionPath();
24+
dataLogger.setSessionPath(directoryManager.getRecordingsPath() + "OpenBCISession_" + _sessionName + File.separator);
25+
fname = dataLogger.getSessionPath();
2626
fname += fileNamePrependString;
2727
fname += _fileName;
2828
fname += ".txt";

OpenBCI_GUI/EnumHelper.pde

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//Used for Widget Dropdown Enums
2+
interface IndexingInterface {
3+
public int getIndex();
4+
public String getString();
5+
}
6+
7+
/**
8+
* Helper class for working with IndexingInterface enums
9+
*/
10+
public static class EnumHelper {
11+
/**
12+
* Generic method to get enum strings as a list
13+
*/
14+
public static <T extends IndexingInterface> List<String> getListAsStrings(T[] values) {
15+
List<String> enumStrings = new ArrayList<>();
16+
for (T enumValue : values) {
17+
enumStrings.add(enumValue.getString());
18+
}
19+
return enumStrings;
20+
}
21+
22+
/**
23+
* Get list of strings for an enum class that implements IndexingInterface
24+
*/
25+
public static <T extends Enum<T> & IndexingInterface> List<String> getEnumStrings(Class<T> enumClass) {
26+
return getListAsStrings(enumClass.getEnumConstants());
27+
}
28+
}

OpenBCI_GUI/FileDurationEnum.pde

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public enum OdfFileDuration implements IndexingInterface {
2+
FIVE_MINUTES (0, 5, "5 Minutes"),
3+
FIFTEEN_MINUTES (1, 15, "15 Minutes"),
4+
THIRTY_MINUTES (2, 30, "30 Minutes"),
5+
SIXTY_MINUTES (3, 60, "60 Minutes"),
6+
ONE_HUNDRED_TWENTY_MINUTES (4, 120, "120 Minutes"),
7+
NO_LIMIT (5, -1, "No Limit");
8+
9+
private int index;
10+
private int duration;
11+
private String label;
12+
13+
OdfFileDuration(int _index, int _duration, String _label) {
14+
this.index = _index;
15+
this.duration = _duration;
16+
this.label = _label;
17+
}
18+
19+
@Override
20+
public String getString() {
21+
return label;
22+
}
23+
24+
@Override
25+
public int getIndex() {
26+
return index;
27+
}
28+
29+
public int getValue() {
30+
return duration;
31+
}
32+
}

OpenBCI_GUI/OpenBCI_GUI.pde

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ String sdData_fname = "N/A"; //only used if loading input data from a sd file
121121
DataSource currentBoard = new BoardNull();
122122

123123
DataLogger dataLogger = new DataLogger();
124+
OdfFileDuration odfFileDuration = OdfFileDuration.SIXTY_MINUTES;
124125

125126
// Intialize interface protocols
126127
InterfaceSerial iSerial = new InterfaceSerial(); //This is messy, half-deprecated code. See comments in InterfaceSerial.pde - Nov. 2020
@@ -256,6 +257,8 @@ final color SIGNAL_CHECK_YELLOW = color(221, 178, 13); //Same color as yellow ch
256257
final color SIGNAL_CHECK_YELLOW_LOWALPHA = color(221, 178, 13, 150);
257258
final color SIGNAL_CHECK_RED = BOLD_RED;
258259
final color SIGNAL_CHECK_RED_LOWALPHA = color(224, 56, 45, 150);
260+
public CColor dropdownColorsGlobal = new CColor();
261+
259262

260263
//Channel Colors -- Defaulted to matching the OpenBCI electrode ribbon cable
261264
//Channel Colors -- Defaulted to matching the OpenBCI electrode ribbon cable
@@ -379,6 +382,13 @@ void setup() {
379382

380383
openbciLogoCog = loadImage("obci-logo-blu-cog.png");
381384

385+
dropdownColorsGlobal.setActive((int)BUTTON_PRESSED); //bg color of box when pressed
386+
dropdownColorsGlobal.setForeground((int)BUTTON_HOVER); //when hovering over any box (primary or dropdown)
387+
dropdownColorsGlobal.setBackground((int)color(255)); //bg color of boxes (including primary)
388+
dropdownColorsGlobal.setCaptionLabel((int)color(1, 18, 41)); //color of text in primary box
389+
// dropdownColorsGlobal.setValueLabel((int)color(1, 18, 41)); //color of text in all dropdown boxes
390+
dropdownColorsGlobal.setValueLabel((int)color(100)); //color of text in all dropdown boxes
391+
382392
// check if the current directory is writable
383393
File dummy = new File(sketchPath());
384394
if (!dummy.canWrite()) {
@@ -1063,7 +1073,6 @@ void systemInitSession() {
10631073
//Global function to update the number of channels
10641074
void updateGlobalChannelCount(int _channelCount) {
10651075
globalChannelCount = _channelCount;
1066-
sessionSettings.sessionSettingsChannelCount = _channelCount; //used in SoftwareSettings.pde only
10671076
fftBuff = new ddf.minim.analysis.FFT[globalChannelCount]; //reinitialize the FFT buffer
10681077
println("OpenBCI_GUI: Channel count set to " + str(globalChannelCount));
10691078
}
@@ -1076,7 +1085,7 @@ void introAnimation() {
10761085
float transparency = 0;
10771086

10781087
if (millis() >= sessionSettings.introAnimationInit) {
1079-
transparency = map(millis() - sessionSettings.introAnimationInit, t1, sessionSettings.introAnimationDuration, 0, 255);
1088+
transparency = map(millis() - sessionSettings.introAnimationInit, t1, sessionSettings.INTRO_ANIMATION_DURATION, 0, 255);
10801089
verbosePrint(String.valueOf(transparency));
10811090
tint(255, transparency);
10821091
//draw OpenBCI Logo Front & Center
@@ -1090,7 +1099,7 @@ void introAnimation() {
10901099
}
10911100

10921101
//Exit intro animation when the duration has expired AND the Control Panel is ready
1093-
if ((millis() >= sessionSettings.introAnimationInit + sessionSettings.introAnimationDuration)
1102+
if ((millis() >= sessionSettings.introAnimationInit + sessionSettings.INTRO_ANIMATION_DURATION)
10941103
&& controlPanel != null) {
10951104
systemMode = SYSTEMMODE_PREINIT;
10961105
controlPanel.open();

0 commit comments

Comments
 (0)