-
Notifications
You must be signed in to change notification settings - Fork 1
/
SDConverter.pde
106 lines (97 loc) · 3.01 KB
/
SDConverter.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//////////////////////////////////
//
// This file contains code used to convert HEX files (stored by OpenBCI on the local SD) into
// text files that can be used for PLAYBACK mode.
// Created: Conor Russomanno - 10/22/14 (based on code written by Joel Murphy summer 2014)
//
//////////////////////////////////
//variables for SD file conversion
BufferedReader dataReader;
String dataLine;
PrintWriter dataWriter;
String convertedLine;
String thisLine;
String h;
float[] intData = new float[20];
String logFileName;
long thisTime;
long thatTime;
public void convertSDFile() {
println("");
try {
dataLine = dataReader.readLine();
}
catch (IOException e) {
e.printStackTrace();
dataLine = null;
}
if (dataLine == null) {
// Stop reading because of an error or file is empty
thisTime = millis() - thatTime;
controlPanel.convertingSD = false;
println("nothing left in file");
println("SD file conversion took "+thisTime+" mS");
dataWriter.flush();
dataWriter.close();
} else {
// println(dataLine);
String[] hexNums = splitTokens(dataLine, ",");
if (hexNums[0].charAt(0) == '%') {
// println(dataLine);
dataWriter.println(dataLine);
println(dataLine);
} else {
for (int i=0; i<hexNums.length; i++) {
h = hexNums[i];
if (i > 0) {
if (h.charAt(0) > '7') { // if the number is negative
h = "FF" + hexNums[i]; // keep it negative
} else { // if the number is positive
h = "00" + hexNums[i]; // keep it positive
}
if (i > 8) { // accelerometer data needs another byte
if (h.charAt(0) == 'F') {
h = "FF" + h;
} else {
h = "00" + h;
}
}
}
// println(h); // use for debugging
if (h.length()%2 == 0) { // make sure this is a real number
intData[i] = unhex(h);
} else {
intData[i] = 0;
}
//if not first column(sample #) or columns 9-11 (accelerometer), convert to uV
if (i>=1 && i<=8) {
intData[i] *= openBCI.get_scale_fac_uVolts_per_count();
}
//print the current channel value
dataWriter.print(intData[i]);
if (i < hexNums.length-1) {
//print "," separator
dataWriter.print(",");
}
}
//println();
dataWriter.println();
}
}
}
void createPlaybackFileFromSD() {
logFileName = "data/EEG_Data/SDconverted-"+getDateString()+".txt";
dataWriter = createWriter(logFileName);
dataWriter.println("%OBCI Data Log - " + getDateString());
}
void sdFileSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
println("User selected " + selection.getAbsolutePath());
dataReader = createReader(selection.getAbsolutePath()); // ("positions.txt");
controlPanel.convertingSD = true;
println("Timing SD file conversion...");
thatTime = millis();
}
}