-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
289 lines (267 loc) · 8.32 KB
/
index.html
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<html>
<head>
<title>Synaptics Sensor Data Demo</title>
</head>
<body>
<script src="jquery-1.9.1.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/3.6.0/firebase.js"></script>
<script>
// Initialize Firebase
firebase.initializeApp({
databaseURL: "https://cal-hacks-3.firebaseio.com"
});
</script>
<script>
baseline = [];
rezero = true;
go = true;
frameCount = -1;
lastSequence = -1;
lastTs = -1;
function updateFrameRate(ts, seq) {
if (lastSequence < 0) {
lastSequence = seq;
lastTs = ts;
frameCount = 0;
}
frameCount++;
diff = ts - lastTs;
if (diff < 0) {
diff += Math.pow(2,32);
}
if (diff > 1e6) {
framesPerSec = frameCount / (diff / 1e6);
//$("#framesPerSec").text(framesPerSec + " frames/sec");
lastSequence = seq;
lastTs = ts;
frameCount = 0;
}
}
function preProcess() {
//var rows =
if (rezero) {
rezero = false;
for (i = 0; i < rows; i++) {
baseline[i] = [];
for (j = 0; j < cols; j++) {
baseline[i][j] = img[i][j];
}
}
} else {
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
img[i][j] = baseline[i][j] - img[i][j];
}
}
}
max = 0; min = 4095;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
v = img[i][j];
if (v > max) {
max = v;
}
if (v < min) {
min = v;
}
}
}
}
function updateCanvas(img, rows, cols) {
if (rezero) {
rezero = false;
for (i = 0; i < rows; i++) {
baseline[i] = [];
for (j = 0; j < cols; j++) {
baseline[i][j] = img[i][j];
}
}
} else {
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
img[i][j] = baseline[i][j] - img[i][j];
}
}
}
max = 0; min = 4095;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
v = img[i][j];
if (v > max) {
max = v;
}
if (v < min) {
min = v;
}
}
}
}
var wasTouching = false;
var points = [];
function requestUpdate() {
$.getJSON("sensorData", function(json) {
if (go) {
window.setTimeout(requestUpdate, 1);
}
if (Object.keys(json).length > 0) {
//$("#rawData").text(JSON.stringify(json, null, 2));
//console.log(json["image"], json["rows"], json["cols"]);
updateFrameRate(json["timeStamp"], json["sequence"]);
updateCanvas(json["image"], json["rows"], json["cols"]);
var foundNewMax = getMax(json["image"]).value > 100;
if (wasTouching) {
if (foundNewMax) {
//keep recording
points.push(getMax(json["image"]).location);
} else {
//stop recording
//process gesture
processGesture();
}
} else { //not touching
if (foundNewMax) {
//start recording
points.push(getMax(json["image"]).location);
} else {
//do nothing
}
}
wasTouching = foundNewMax;
} else {
console.log("you done goofed");
}
});
}
function getMax(matrix) {
var xmax = 0;
var ymax = 0;
var max = -Infinity;
for (var i = 0; i < matrix.length; i++) {
for (var j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] > max) {
xmax = j;
ymax = i;
max = matrix[i][j];
}
}
}
console.log("Max value is " + matrix[ymax][xmax] + " at coordinate " + "(" + xmax + "," + ymax + ")");
//console.log(max);
try {
$("#framesPerSec").text("max=" + max + " at loc=" + "(" + xmax + "," + ymax + ")");
} catch (err) {}
var payload = {
location: [xmax, ymax],
value: max
};
return payload;
}
var gestureRef = firebase.database().ref();
function populateWithInfo() {
var xValues = [];
var yValues = [];
for (var i = 0; i < points.length; i++) {
xValues.push(points[i][0]);
yValues.push(points[i][1]);
}
var info = {
points: {
first: points[0],
last: points[points.length - 1]
},
boundingBox: {
min: {
x: Math.min(...xValues),
y: Math.min(...yValues) },
max: {
x: Math.max(...xValues),
y: Math.max(...yValues)
},
size: {
width: Math.max(...xValues) - Math.min(...xValues),
height: Math.max(...yValues) - Math.min(...yValues)
}
},
totalChange: {
x: points[points.length - 1][0] - points[0][0],
y: points[points.length - 1][1] - points[0][1]
}
}
return info;
}
function processGesture() {
var info = populateWithInfo();
var firstCoord = info.points.first;
var lastCoord = info.points.last;
var changeX = info.totalChange.x;
var changeY = info.totalChange.y;
var bboxWidth = info.boundingBox.size.width;
var bboxHeight = info.boundingBox.size.height;
var gesture;
//"scroll_up", "scroll_down", "stop", "new_tab", "go_back", "go_forward", "close_tab", "refresh"
//console.log(Math.abs(changeX), Math.abs(changeY), bboxWidth, bboxHeight);
console.log(changeX);
console.log(changeY);
if ((Math.abs(changeX) < 5 && Math.abs(changeY) < 5) && (bboxWidth > 10 && bboxHeight > 10)) {
gesture = "refresh";
} else if (changeX < -25 && changeY > 10) { //check for move diagonal top-left to bottom-right
gesture = "close_tab";
} else if (changeX > 10 && changeY < -10) { //check for move diagonal top-right to bottom-left
gesture = "new_tab";
} else if (changeY < -8 && Math.abs(changeX) < 25) { //check for move up
gesture = "scroll_up";
} else if (changeY > 8 && Math.abs(changeX) < 25) { //check for move down
gesture = "scroll_down";
} else if (changeX < -8 && Math.abs(changeY) < 25) { //check for move left
gesture = "go_back";
} else if (changeX > 8 && Math.abs(changeY) < 25) { //check for move right
gesture = "go_forward";
} else if ((Math.abs(changeX) < 5 && Math.abs(changeY) < 5) && (bboxWidth < 10 && bboxHeight < 10)) {
gesture = "stop";
}
gestureRef.set({"command": gesture});
console.log("gesture: " + gesture);
/*if (gesture !== "set") {
alert
}*/
points = [];
//detect for switch up down
/*for (index in points) {
var point = points[index];
}*/
}
function stopImages() {
$("#button1").text("Start");
$("#button1").off("click");
$("#button1").click(startImages);
go = false;
}
function startImages() {
$("#button1").text("Stop");
$("#button1").off("click");
$("#button1").click(stopImages);
go = true;
requestUpdate();
}
$( document ).ready(function() {
$("#button1").click(startImages);
$("#rezero").click(function() {
rezero = true;
});
});
</script>
<!--
<script>
chrome.tabs.create({url: chrome.extension.getURL('background.html')});
</script>
-->
<h1>JSON Dy's and Tejas Shah's Sensor Demo</h1>
<p id="framesPerSec"> </p>
<p>
<button id="button1">Start</button>
<button id="rezero">Rezero</button>
</p>
<p id="rawData"></p>
<!-- <canvas id="canvas"></canvas> -->
</body>
</html>