-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
579 lines (528 loc) · 24.2 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
.sensor {
height: 400px;
}
</style>
<script src="https://d3js.org/d3.v4.js"></script>
<script src="./data_common.js"></script>
<script src="./data_display.js"></script>
<script src="./data_sensors.js"></script>
<script src="./nrfconnect.js"></script>
<script>
var timer_id = null;
var update_data_display_period = 500; // ms
var connected = false;
var services_discovered = false;
var selected_device;
var connected_server;
var notifications_enabled = false;
var magneto_enabled = false;
// presence of services of characteristics
var has_device_service = false;
var has_frame_data = false;
var has_info_data = false;
var has_config_data = false;
// characteristic UUIDs
DEVICE_SERVICE = '0000f00d-0000-1000-8000-00805f9b34fb';
FRAME_UUID = '0000accc-1212-efde-1523-785fef13d123';
INFO_UUID = '0000acca-1212-efde-1523-785fef13d123';
CONFIG_UUID = '0000accb-1212-efde-1523-785fef13d123';
// cached characteristics
var frame_data;
var info_data;
var config_data;
// for performance measure
var old_time = -1;
var max_delay = 0;
var sum_delay = 0;
var nb_notif = 0;
var start_date = 0;
var sensors_data = [];
function setNotificationsStatus(status) {
if (status == true) {
start_date = +Date.now();
sensors_data = [];
timer_id = setInterval(function() { updateDataDisplay(sensors_data)}, update_data_display_period);
} else {
old_time = -1;
max_delay = 0;
nb_notif = 0;
sum_delay = 0;
clearInterval(timer_id);
}
notifications_enabled = status;
document.getElementById('status_notifications').innerHTML = status;
}
function setConnectedStatus(status) {
connected = status;
document.getElementById('status_connected').innerHTML = status;
if (status == true) {
document.getElementById('btn_scan').innerHTML = "Disconnect";
} else {
document.getElementById('btn_scan').innerHTML = "Discover Devices";
}
}
function setDiscoveryStatus(status) {
services_discovered = status;
document.getElementById('status_discovered').innerHTML = status;
}
function discoverDevicesOrDisconnect() {
console.log("discoverDevicesOrDisconnect");
if (!connected) {
discoverDevices();
} else {
selected_device.gatt.disconnect();
resetUI();
}
}
function discoverDevices() {
console.log("discoverDevices");
var options = {
filters: [ {namePrefix: 'Panda'}, {namePrefix: 'TedKo'}],
optionalServices: [DEVICE_SERVICE],
}
navigator.bluetooth.requestDevice(options)
.then(device => {
console.log('> Name: ' + device.name);
console.log('> Id: ' + device.id);
console.log('> Connected: ' + device.gatt.connected);
selected_device = device;
console.log(selected_device);
connect();
})
.catch(error => {
alert('ERROR: ' + error);
console.log('ERROR : ' + error);
})
}
function check_characteristic(has_char) {
// state validation
if (!connected) {
alert("Error: Discover and connect to a device before using this function");
return false;
}
if (!services_discovered) {
alert("Error: Service discovery has not yet completed");
return false;
}
if (!has_device_service) {
alert("Error: The connected device does not contain the device service");
return false;
}
if (!has_char) {
alert("Error: The connected device does not contain the device information characteristic");
return false;
}
return true;
}
function readInfo() {
console.log("readInfo");
if (!check_characteristic(has_info_data)) {
return;
}
info_data.readValue()
.then(value => {
data = new Uint8Array(value.buffer);
console.log(data);
version = 'Software Version: ' + data[8] + '.' + data[9] + '.' + data[10];
document.getElementById('device_info').innerHTML = version;
})
.catch(error => {
console.log('Error: ' + error);
alert('Error: ' + error);
return;
});
}
function readFrame() {
console.log("readFrame");
// state validation
if (!check_characteristic(has_frame_data)) {
return;
}
frame_data.readValue()
.then(value => {
data = new Uint8Array(value.buffer);
console.log(data);
})
.catch(error => {
console.log('Error: ' + error);
alert('Error: ' + error);
return;
});
}
function toogleMagneto() {
console.log("toggleMagneto");
if (!check_characteristic(has_config_data)) {
return;
}
let buffer = new ArrayBuffer(1);
let buffer_view = new DataView(buffer);
if (!magneto_enabled) {
buffer_view.setUint8(0, 0x00);
config_data.writeValue(buffer)
.then(_ => {
console.log("magneto enabled");
magneto_enabled = true;
})
.catch(error => {
console.log('Error: ' + error);
alert('Error: ' + error);
return;
});
} else {
buffer_view.setUint8(0, 0x01);
config_data.writeValue(buffer)
.then(_ => {
console.log("magneto disabled");
magneto_enabled = false;
})
.catch(error => {
console.log('Error: ' + error);
alert('Error: ' + error);
return;
});
}
}
function toogleTransport() {
console.log("toggleTransport");
if (!check_characteristic(has_config_data)) {
return;
}
let buffer = config_data.readValue()
.then(value => {
buffer = value.buffer;
let buffer_view = new DataView(buffer);
let current = buffer_view.getUint8(0);
current = current | 0x02; // enable transport mode
buffer_view.setUint8(0, current);
config_data.writeValue(buffer)
.then(_ => {
console.log("transport mode enabled");
})
.catch(error => {
console.log('Error: ' + error);
alert('Error: ' + error);
return;
});
});
}
function toogleSensorNotifications() {
console.log("toggleSensorNotifications");
if (!check_characteristic(has_frame_data)) {
return;
}
if (!notifications_enabled) {
frame_data.startNotifications()
.then(_ => {
console.log('frame notifications started');
setNotificationsStatus(true);
frame_data.addEventListener('characteristicvaluechanged',
onFrameData);
})
.catch(error => {
console.log('Error: ' + error);
alert('Error: ' + error);
return;
})
} else {
frame_data.stopNotifications()
.then(_ => {
console.log('frame notifications stopped');
updateMeasureData();
setNotificationsStatus(false);
frame_data.removeEventListener('characteristicvaluechanged',
onFrameData);
})
.catch(error => {
console.log('Could not stop frame_data notifications: ' + error);
})
}
}
function discoverSvcsAndChars() {
console.log("discoverSvcsAndChars server=" + connected_server);
connected_server.getPrimaryServices()
.then(services => {
has_device_service = false;
services_discovered = 0;
service_count = services.length;
console.log("Got " + service_count + " services");
services.forEach(service => {
if (service.uuid == DEVICE_SERVICE) {
has_device_service = true;
}
console.log('Getting Characteristics for service ' + service.uuid);
service.getCharacteristics().then(characteristics => {
console.log('> Service: ' + service.uuid);
services_discovered++;
characteristics_discovered = 0;
characteristics_count = characteristics.length;
characteristics.forEach(characteristic => {
characteristics_discovered++;
console.log('>> Characteristic: ' + characteristic.uuid);
if (characteristic.uuid == FRAME_UUID) {
console.log('>>> Frame');
frame_data = characteristic;
has_frame_data = true;
}
if (characteristic.uuid == INFO_UUID) {
console.log('>>> Info');
info_data = characteristic;
has_info_data = true;
}
if (characteristic.uuid == CONFIG_UUID) {
console.log('>>> Config');
config_data = characteristic;
has_config_data = true;
}
if (services_discovered == service_count && characteristics_discovered == characteristics_count) {
console.log("FINISHED DISCOVERY");
setDiscoveryStatus(true);
}
})
})
})
})
}
function connect() {
if (connected == false) {
console.log("connecting");
selected_device.gatt.connect().then(
function(server) {
console.log("Connected to " + server.device.id);
console.log("connected=" + server.connected);
setConnectedStatus(true);
connected_server = server;
selected_device.addEventListener(
'gattserverdisconnected',
onDisconnected);
discoverSvcsAndChars();
},
function (error) {
console.log("ERROR: could not connect - " + error);
alert("ERROR: could not connect - " + error);
setConnectedStatus(false);
}
);
}
}
function updateMeasureData() {
let mean_delay = sum_delay / nb_notif;
document.getElementById('frame_max_delay').innerHTML = max_delay;
document.getElementById('frame_mean_delay').innerHTML = mean_delay;
document.getElementById('frame_nb_notif').innerHTML = nb_notif;
}
function onDisconnected() {
setConnectedStatus(false);
setDiscoveryStatus(false);
document.getElementById('device_info').innerHTML = "";
}
function onFrameData(event) {
buffer = event.target.value.buffer;
dataview = new DataView(buffer);
atime = dataview.getUint32(dataview.byteLength - 5, false);
// console.log("atime: " + atime);
if (old_time != -1) {
diff_time = atime - old_time;
if (diff_time > max_delay) {
max_delay = diff_time;
console.log("New max delay: " + max_delay);
// Update UI
document.getElementById('frame_max_delay').innerHTML = max_delay;
}
if (diff_time < 0) {
console.log("ERROR (" + nb_notif + ") : "+ diff_time + " || " + atime + " || " + old_time);
console.log("ERROR " + new Uint8Array(buffer));
} else {
sum_delay += diff_time;
nb_notif += 1;
}
cur_date = +Date.now() - start_date;
sensors_data.push({'received_time': cur_date, 'time': atime, 'data': buffer});
}
old_time = atime;
}
function resetUI() {
setConnectedStatus(false);
setDiscoveryStatus(false);
setNotificationsStatus(false);
document.getElementById('device_info').innerHTML = "";
}
function encode(s) {
var out = [];
for ( var i = 0; i < s.length; i++ ) {
out[i] = s.charCodeAt(i);
}
return new Uint8Array( out );
}
function exportJson(obj, file_name) {
let str = JSON.stringify(obj, null, 2);
let data = encode(str);
// what to return in order to show download window?
let el = document.getElementById("downloadAnchorElem");
let blob = new Blob([data], {
type: 'application/octet-stream'
})
let url = URL.createObjectURL(blob);
el.setAttribute("href", url);
el.setAttribute("download", file_name);
el.click();
}
function exportFramesJson() {
let data = []
sensors_data.forEach(dd => {
let raw_values = new Uint8Array(dd.data);
values = [];
raw_values.forEach(rr => { values.push(+rr)});
data.push({'received_time': dd.received_time, 'time': dd.time, 'data': values});
});
return exportJson(data, "frames.json");
}
function exportDeltaTimeJson() {
let delta_time = computeDeltaTime(sensors_data);
return exportJson(delta_time, "deltaTime.json");
}
function handleFileNRFSelect(evt) {
let file = evt.target.files[0];
let reader = new FileReader();
sensors_data = [];
reader.onload = (function(theFile) {
return function(e) {
console.log("File read");
log_text = this.result;
sensors_data = convert_nrfconnect_log(log_text);
updateDataDisplay(sensors_data);
};
})(file);
// Read data file
reader.readAsText(file);
}
</script>
</head>
<body>
<h1>Web Bluetooth</h1>
<h2>Status</h2>
<table border="1">
<tr>
<td>
<b>Connected</b>
</td>
<td>
<b>Service Discovery Completed</b>
</td>
<td>
<b>Notifications</b>
</td>
</tr>
<tr>
<td id="status_connected">false</td>
<td id="status_discovered">false</td>
<td id="status_notifications">false</td>
</tr>
</table>
<h2>Device Discovery and Connection</h2>
<button type="button" class="btn btn-primary" id="btn_scan" onclick="discoverDevicesOrDisconnect()">Discover Devices</button>
<hr>
<h2>Device info</h2>
<button type="button" class="btn btn-primary" id="btn_readInfo" onclick="readInfo()">Read Info</button>
<div id="device_info"></div>
<button type="button" class="btn btn-primary" id="btn_magneto" onclick="toogleMagneto()">Toggle Magneto</button>
<button type="button" class="btn btn-primary" id="btn_magneto" onclick="toogleTransport()">Toggle Transport</button>
<h2>Notifications</h2>
<button type="button" class="btn btn-primary" id="btn_notify" onclick="toogleSensorNotifications()">Toggle Notifications</button>
<div>
Maximum delay: <span id="frame_max_delay"></span>
</div>
<div>
Nb notifications: <span id="frame_nb_notif"></span>
</div>
<div>
Mean delay: <span id="frame_mean_delay"></span>
</div>
<hr>
<div class="container w-100">
<div class="row">
<div class="col">
<button type="button" class="btn btn-primary btn-lg btn-block" id="btn_analyze" onclick="updateDataDisplay(sensors_data)">Update Data display</button>
</div>
<div class="col">
<button type="button" class="btn btn-primary btn-lg btn-block" id="exportFramesJSON" onclick="exportFramesJson();">Export frames json</button>
</div>
<div class="col">
<button type="button" class="btn btn-primary btn-lg btn-block" id="exportDeltaTimeJSON" onclick="exportDeltaTimeJson();">Export delta json</button>
</div>
<div class="col">
<!-- <button id="importData" onclick="importDataNRF()">Import data from NrfConnect</button> -->
<input type="file" class="btn btn-lg btn-block" id="fileNRF" name="fileNRFConnect" />
</div>
</div>
</div>
<hr>
<div class="container">
<div id="data_analyze" class="column">
<h2>Delta time</h2>
<div class="row">
<div id="data_delta_frame_time" class="w-100 time sensor"></div>
</div>
<hr>
<div id="cuisse">
<h2>Cuisse</h2>
<div class="row">
<div id="data_accelero_cuisse" class="w-100 accelero cuisse sensor"></div>
</div>
<div class="row">
<div id="data_gyro_cuisse" class="w-100 gyro cuisse sensor"></div>
</div>
<hr>
</div>
<div id="mollet">
<h2>Mollet</h2>
<div class="row">
<div id="data_accelero_mollet" class="w-100 accelero mollet sensor"></div>
</div>
<div class="row">
<div id="data_gyro_mollet" class="w-100 gyro mollet sensor"></div>
</div>
<hr>
</div>
<h2>Received time</h2>
<div class="row">
<div id="data_received_time" class="w-100 time sensor"></div>
</div>
<hr>
<div id="magneto">
<h2>Magneto</h2>
<div class="row">
<div id="data_magneto_cuisse" class="w-100 magneto cuisse sensor"></div>
</div>
<div class="row">
<div id="data_magneto_mollet" class="w-100 magneto mollet sensor"></div>
</div>
</div>
</div>
</div>
<a id="downloadAnchorElem" style="display:none"></a>
<script>
document.getElementById('fileNRF').addEventListener('change', handleFileNRFSelect, false);
//We will build a basic function to handle window resizing.
function resize() {
width = document.getElementById('data_delta_frame_time').clientWidth;
height = document.getElementById('data_delta_frame_time').clientHeight;
d3.select('#data_delta_frame_time svg')
.attr('width', width)
.attr('height', height);
}
window.onresize = resize;
//Call our resize function if the window size is changed.
</script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>