-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.html
138 lines (120 loc) · 4.08 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
<!DOCTYPE html>
<html>
<head>
<title>Web Bluetooth Accelerometer Plotter</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.js"></script>
<script src="https://unpkg.com/p5ble@0.0.5/dist/p5.ble.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.1.0/dygraph.min.js"></script>
<!-- <script src="smooth-plotter.js"></script> -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/dygraph/2.1.0/dygraph.min.css" />
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0" />
</head>
<body>
<div id="div_g" style="width:150vh; height:40vh;"></div>
<script>
const serviceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
//const serviceUuid = "180f0000-0000-0000-0000-000000000000";
let myCharacteristic;
let myValue = 0;
let myBLE;
var data = [];
var g;
var dataPoints = 100;
//var plotFlag = true;
//Graphing
$(document).ready(function () {
//var data = [];
var t = new Date();
for (var i = dataPoints; i >= 0; i--) {
var x = new Date(t.getTime() - i * 1000);
data.push([x, NaN, NaN, NaN]);
}
g = new Dygraph(document.getElementById("div_g"), data,
{
//color: 'red',
strokeWidth: 2,
//rollPeriod: 10,
drawPoints: true,
//showRoller: true,
valueRange: [-6, 6],
labels: ['Time', 'X', 'Y', 'Z'],
colors: ['#3B3B98', '#ff5e57', '#1dd1a1'],
fillGraph: true,
pointSize: 3,
stepPlot: false,
ylabel: "X, Y, Z"
//pixelsPerLabel: 10,
});
});
function plot(ax, ay, az) {
// if (plotFlag === true){
// g.updateOptions({'file': []});
// var t = new Date();
// for (var i = dataPoints; i >= 0; i--) {
// var x = new Date(t.getTime() - i * 1000);
// data.push([x, NaN, NaN, NaN]);
// }
// g.updateOptions({'file': data});
// plotFlag = false;
// }
//My test
var d = new Date(); // current time
var x = ax;
var y = ay;
var z = az;
data.shift();
// if (dataPoints>0){
// dataPoints-=1;
// data.shift();
// data.push([d, x, y, z]);
// }
// else{
// data.push([d, x, y, z]);
// }
data.push([d, x, y, z]);
g.updateOptions({ 'file': data });
}
// Bluetooth
function setup() {
// Create a p5ble class
myBLE = new p5ble();
createCanvas(300, 200);
textSize(20);
textAlign(CENTER, CENTER);
// Create a 'Connect' button
const connectButton = createButton('Connect')
connectButton.mousePressed(connectToBle);
}
function connectToBle() {
// Connect to a device by passing the service UUID
myBLE.connect(serviceUuid, gotCharacteristics);
}
// A function that will be called once got characteristics
function gotCharacteristics(error, characteristics) {
if (error) console.log('error: ', error);
console.log('characteristics: ', characteristics);
myCharacteristic = characteristics[0];
// Read the value of the first characteristic
myBLE.read(myCharacteristic, 'string', gotValue);
//plot(myCharacteristic);
}
// A function that will be called once got values
function gotValue(error, value) {
if (error) console.log('error: ', error);
console.log('value: ', value);
myValue = value;
var acceleration = myValue.split('|');
console.log('Acceleration: ', acceleration);
plot(parseFloat(acceleration[0]), parseFloat(acceleration[1]), parseFloat(acceleration[2]));
// After getting a value, call p5ble.read() again to get the value again
myBLE.read(myCharacteristic, 'string', gotValue);
}
function draw() {
background(250);
text(myValue, 150, 100);
}
</script>
</body>
</html>