Repository navigation
Expand file tree
/
Copy pathDataCaptureClause.pde
More file actions
139 lines (122 loc) · 3.44 KB
/
Copy pathDataCaptureClause.pde
File metadata and controls
139 lines (122 loc) · 3.44 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
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
import ketai.data.*;
import ketai.sensors.*;
KetaiSensor sensor;
KetaiSQLite db;
boolean isCapturing = false;
float G = 9.80665;
String CREATE_DB_SQL =
"CREATE TABLE data ( time INTEGER PRIMARY KEY, x FLOAT, y FLOAT, z FLOAT);";
void setup()
{
db = new KetaiSQLite(this);
sensor = new KetaiSensor(this);
orientation(LANDSCAPE);
textAlign(CENTER, CENTER);
textSize(72);
rectMode(CENTER);
frameRate(15);
if ( db.connect() )
{
if (!db.tableExists("data"))
db.execute(CREATE_DB_SQL);
}
sensor.start();
}
void draw() {
background(78, 93, 75);
if (isCapturing)
text("Recording data...\n(press MENU button to stop)" +"\n\n" +
"Current Data count: " + db.getDataCount(), width/2, height/2);
else
plotData();
}
void keyPressed() {
if (keyCode == BACK) {
db.execute( "DELETE FROM data" );
key = 0;
}
else if (keyCode == MENU) {
if (isCapturing)
isCapturing = false;
else
isCapturing = true;
}
}
void onAccelerometerEvent(float x, float y, float z, long time, int accuracy)
{
if (db.connect() && isCapturing)
{
if (!db.execute(
"INSERT into data (`time`,`x`,`y`,`z`) VALUES ('" +
System.currentTimeMillis() + "', '" + x + "', '" + y + "', '" + z + "')"
)
)
println("Failed to record data!" );
}
}
void plotData()
{
if (db.connect())
{
pushStyle();
textAlign(LEFT);
line(0, height/2, width, height/2);
line(mouseX, 0, mouseX, height);
noStroke();
db.query("SELECT * FROM data");
long myMin = Long.parseLong(db.getFieldMin("data", "time")); // 1
long myMax = Long.parseLong(db.getFieldMax("data", "time")); // 2
while (db.next ())
{
long t = db.getLong("time"); // 3
float x = db.getFloat("x");
float y = db.getFloat("y");
float z = db.getFloat("z");
float plotX = 0;
float plotY = 0;
fill(255, 0, 0);
plotX = mapLong(t, myMin, myMax, 0, width); // 4
plotY = map(x, -2*G, 2*G, 0, height);
ellipse(plotX, plotY, 3, 3);
if (abs(mouseX-plotX) < 1)
text(nfp(x, 2, 2), plotX, plotY);
fill(0, 255, 0);
plotY = map(y, -2*G, 2*G, 0, height);
ellipse(plotX, plotY, 3, 3);
if (abs(mouseX-plotX) < 1)
text(nfp(y, 2, 2), plotX, plotY);
fill(0, 0, 255);
plotY = map(z, -2*G, 2*G, 0, height);
ellipse(plotX, plotY, 3, 3);
if (abs(mouseX-plotX) < 1)
{
text(nfp(z, 2, 2), plotX, plotY);
fill(0);
text("#" + t, mouseX, height); // 4
}
}
noFill();
stroke(255);
db.query("SELECT * FROM data WHERE z > 9.5 AND abs(x) < 0.3 AND abs(y) < 0.3"); // 5
while (db.next ())
{
long t = db.getLong("time");
float x = db.getFloat("x");
float y = db.getFloat("y");
float z = db.getFloat("z");
float plotX, plotY = 0;
plotX = mapLong(t, myMin, myMax, 0, width);
plotY = map(x, -2*G, 2*G, 0, height);
ellipse(plotX, plotY, 10, 10); // 5
plotY = map(y, -2*G, 2*G, 0, height);
ellipse(plotX, plotY, 10, 10);
plotY = map(z, -2*G, 2*G, 0, height);
ellipse(plotX, plotY, 10, 10);
}
popStyle();
}
}
// map() helper method for values of type long
float mapLong(long value, long istart, long istop, float ostart, float ostop) { // 6
return (float)(ostart + (ostop - ostart) * (value - istart) / (istop - istart));
}