-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataFiles.pde
188 lines (164 loc) · 5.59 KB
/
dataFiles.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
////////////////////////////////////////////////////////////
// Class: OutputFile_rawtxt
// Purpose: handle file creation and writing for the text log file
// Created: Chip Audette May 2, 2014
//
//write data to a text file
public class OutputFile_rawtxt {
PrintWriter output;
String fname;
private int rowsWritten;
private float TimeSeries;
OutputFile_rawtxt(float fs_Hz) {
//build up the file name
fname = "SavedData"+System.getProperty("file.separator")+"OpenBCI-RAW-";
//add year month day to the file name
fname = fname + year() + "-";
if (month() < 10) fname=fname+"0";
fname = fname + month() + "-";
if (day() < 10) fname = fname + "0";
fname = fname + day();
//add hour minute sec to the file name
fname = fname + "_";
if (hour() < 10) fname = fname + "0";
fname = fname + hour() + "-";
if (minute() < 10) fname = fname + "0";
fname = fname + minute() + "-";
if (second() < 10) fname = fname + "0";
fname = fname + second();
//add the extension
//fname = fname + ".txt";
fname = fname + ".csv";
//open the file
output = createWriter(fname);
//add the header
writeHeader(fs_Hz);
//init the counter
rowsWritten = 0;
TimeSeries = 0;
}
//variation on constructor to have custom name
OutputFile_rawtxt(float fs_Hz, String _fileName) {
fname = "SavedData"+System.getProperty("file.separator")+"OpenBCI-RAW-";
fname += _fileName;
fname += ".csv";
//fname += ".txt";
output = createWriter(fname); //open the file
writeHeader(fs_Hz); //add the header
rowsWritten = 0; //init the counter
TimeSeries = 0;
}
public void writeHeader(float fs_Hz) {
output.println("%OpenBCI Raw EEG Data");
output.println("%");
output.println("%Sample Rate = " + fs_Hz + " Hz");
output.println("%First Column = Time (ms)");
output.println("%Other Columns = EEG data in microvolts followed by Accel Data (in G) interleaved with Aux Data");
output.flush();
}
// public void writeRawData_dataPacket(DataPacket_ADS1299 data, float scale_to_uV, float scale_for_aux) {
// writeRawData_dataPacket(data, scale_to_uV, data.values.length);
// }
public void writeRawData_dataPacket(DataPacket_ADS1299 data, float scale_to_uV, float scale_for_aux) {
if (output != null) {
//output.print(Integer.toString(data.sampleIndex));
output.print(TimeSeries);
writeValues(data.values,scale_to_uV);
scale_for_aux = 1.0;
writeValues(data.auxValues,scale_for_aux);
output.println(); rowsWritten++;
TimeSeries = TimeSeries + 4;
//output.flush();
}
}
private void writeValues(int[] values, float scale_fac) {
int nVal = values.length;
for (int Ival = 0; Ival < nVal; Ival++) {
output.print(", ");
output.print(String.format("%.2f", scale_fac * float(values[Ival])));
}
}
public void closeFile() {
output.flush();
output.close();
}
public int getRowsWritten() {
return rowsWritten;
}
}
///////////////////////////////////////////////////////////////
// Class: Table_CSV
// Purpose: Extend the Table class to handle data files with comment lines
// Created: Chip Audette May 2, 2014
//
// Usage: Only invoke this object when you want to read in a data
// file in CSV format. Read it in at the time of creation via
//
// String fname = "myfile.csv";
// TableCSV myTable = new TableCSV(fname);
//
//import java.io.*;
//import processing.core.PApplet;
class Table_CSV extends Table {
Table_CSV(String fname) throws IOException {
init();
readCSV(PApplet.createReader(createInput(fname)));
}
//this function is nearly completely copied from parseBasic from Table.java
void readCSV(BufferedReader reader) throws IOException {
boolean header=false; //added by Chip, May 2, 2014;
boolean tsv = false; //added by Chip, May 2, 2014;
String line = null;
int row = 0;
if (rowCount == 0) {
setRowCount(10);
}
//int prev = 0; //-1;
try {
while ( (line = reader.readLine ()) != null) {
//added by Chip, May 2, 2014 to ignore lines that are comments
if (line.charAt(0) == '%') {
//println("Table_CSV: readCSV: ignoring commented line...");
continue;
}
if (row == getRowCount()) {
setRowCount(row << 1);
}
if (row == 0 && header) {
setColumnTitles(tsv ? PApplet.split(line, '\t') : splitLineCSV(line));
header = false;
}
else {
setRow(row, tsv ? PApplet.split(line, '\t') : splitLineCSV(line));
row++;
}
// this is problematic unless we're going to calculate rowCount first
if (row % 10000 == 0) {
/*
if (row < rowCount) {
int pct = (100 * row) / rowCount;
if (pct != prev) { // also prevents "0%" from showing up
System.out.println(pct + "%");
prev = pct;
}
}
*/
try {
// Sleep this thread so that the GC can catch up
Thread.sleep(10);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
catch (Exception e) {
throw new RuntimeException("Error reading table on line " + row, e);
}
// shorten or lengthen based on what's left
if (row != getRowCount()) {
setRowCount(row);
}
}
}