-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataLogger.java
More file actions
108 lines (90 loc) · 2.58 KB
/
DataLogger.java
File metadata and controls
108 lines (90 loc) · 2.58 KB
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
package org.usfirst.FTC5866.library;
/**
* Created by Olavi Kamppari on 9/9/2015.
*
* Added to Github on 11/16/2015 (https://github.com/OliviliK/FTC_Library/blob/master/DataLogger.java)
*/
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class DataLogger {
private Writer writer;
private StringBuffer lineBuffer;
private long msBase;
private long nsBase;
public DataLogger (String fileName) {
String directoryPath = "/sdcard/FIRST/DataLogger";
String filePath = directoryPath + "/" + fileName + ".csv";
new File(directoryPath).mkdir(); // Make sure that the directory exists
try {
writer = new FileWriter(filePath);
lineBuffer = new StringBuffer(128);
} catch (IOException e) {
}
msBase = System.currentTimeMillis();
nsBase = System.nanoTime();
addField("sec");
addField("d ms");
}
private void flushLineBuffer(){
long milliTime,nanoTime;
try {
lineBuffer.append('\n');
writer.write(lineBuffer.toString());
lineBuffer.setLength(0);
}
catch (IOException e){
}
milliTime = System.currentTimeMillis();
nanoTime = System.nanoTime();
addField(String.format("%.3f",(milliTime - msBase) / 1.0E3));
addField(String.format("%.3f",(nanoTime - nsBase) / 1.0E6));
nsBase = nanoTime;
}
public void closeDataLogger() {
try {
writer.close();
}
catch (IOException e) {
}
}
public void addField(String s) {
if (lineBuffer.length()>0) {
lineBuffer.append(',');
}
lineBuffer.append(s);
}
public void addField(char c) {
if (lineBuffer.length()>0) {
lineBuffer.append(',');
}
lineBuffer.append(c);
}
public void addField(boolean b) {
addField(b ? '1' : '0');
}
public void addField(byte b) {
addField(Byte.toString(b));
}
public void addField(short s) {
addField(Short.toString(s));
}
public void addField(long l) {
addField(Long.toString(l));
}
public void addField(float f) {
addField(Float.toString(f));
}
public void addField(double d) {
addField(Double.toString(d));
}
public void newLine() {
flushLineBuffer();
}
@Override
protected void finalize() throws Throwable {
closeDataLogger();
super.finalize();
}
}