-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
118 lines (80 loc) · 3.06 KB
/
sketch.js
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
// Copyright (c) 2018 p5ble
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
// The serviceUuid must match the serviceUuid of the device you would like to connect
const serviceUuidA = "72f99129-592c-4ed2-b6e2-8754c03c2f0f";
const serviceUuidB = "a9edf38d-296c-4bd7-9289-eb70656c4fe2";
let gyroXa = 10, gyroYa = 0, gyroZa = 0;
let gyroXb = 10, gyroYb = 0, gyroZb = 0;
let xCharactersticA;
let xCharactersticB;
let myBLEa, myBLEb;
function setup() {
// Create a p5ble class
myBLEa = new p5ble();
myBLEb = new p5ble();
let cnv = createCanvas(500, 200);
//cnv.position(0, 0, 'fixed');
textSize(20);
textAlign(LEFT, CENTER);
// Create a 'Connect' button
const connectAButton = createButton('Connect A')
connectAButton.parent(document.getElementById("container"))
connectAButton.position(10, 10);
connectAButton.mousePressed(connectToBleA);
const connectBButton = createButton('Connect B')
connectBButton.parent(document.getElementById("container"))
connectBButton.position(100, 10);
connectBButton.mousePressed(connectToBleB);
console.log("P5 setup done")
}
function connectToBleA() {
// Connect to a device by passing the service UUID
myBLEa.connect(serviceUuidA, gotCharacteristicsA);
}
function connectToBleB() {
// Connect to a device by passing the service UUID
myBLEb.connect(serviceUuidB, gotCharacteristicsB);
}
// A function that will be called once got characteristics
function gotCharacteristicsA(error, characteristics) {
if (error) console.log('error: ', error);
console.log('characteristics: ', characteristics);
xCharactersticA = characteristics[0];
myBLEa.read(xCharactersticA, 'string',gotXValueA);
}
// A function that will be called once got characteristics
function gotCharacteristicsB(error, characteristics) {
if (error) console.log('error: ', error);
console.log('characteristics: ', characteristics);
xCharactersticB = characteristics[0];
// Read the value of the first characteristic
myBLEb.read(xCharactersticB, 'string', gotXValueB);
}
// A function that will be called once got values
function gotXValueA(error, value) {
if (error) console.log('error: ', error);
gyroA = value;
const accValues = gyroA.split(" ");
gyroXa = accValues[0]
gyroYa = accValues[1]
gyroZa = accValues[2]
console.log('value xa: ', gyroXa);
console.log('value ya: ', gyroYa);
console.log('value za: ', gyroZa);
myBLEa.read(xCharactersticA,'string',gotXValueA);
}
function gotXValueB(error, value) {
if (error) console.log('error: ', error);
gyroB = value;
const accValues = gyroB.split(" ");
// console.log('array: ', accValues);
gyroXb = accValues[0]
gyroYb = accValues[1]
gyroZb = accValues[2]
console.log('value xb: ', gyroXb);
console.log('value yb: ', gyroYb);
console.log('value zb: ', gyroZb);
myBLEb.read(xCharactersticB,'string',gotXValueB);
}