-
Notifications
You must be signed in to change notification settings - Fork 15
/
microBit.js
375 lines (314 loc) · 10.7 KB
/
microBit.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
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
var ACCEL_SRV = 'e95d0753-251d-470a-a062-fa1922dfa9a8'
var ACCEL_DATA = 'e95dca4b-251d-470a-a062-fa1922dfa9a8'
var ACCEL_PERIOD = 'e95dfb24-251d-470a-a062-fa1922dfa9a8'
var MAGNETO_SRV = 'e95df2d8-251d-470a-a062-fa1922dfa9a8'
var MAGNETO_DATA = 'e95dfb11-251d-470a-a062-fa1922dfa9a8'
var MAGNETO_PERIOD = 'e95d386c-251d-470a-a062-fa1922dfa9a8'
var MAGNETO_BEARING = 'e95d9715-251d-470a-a062-fa1922dfa9a8'
var BTN_SRV = 'e95d9882-251d-470a-a062-fa1922dfa9a8'
var BTN_A_STATE = 'e95dda90-251d-470a-a062-fa1922dfa9a8'
var BTN_B_STATE = 'e95dda91-251d-470a-a062-fa1922dfa9a8'
var IO_PIN_SRV = 'e95d127b-251d-470a-a062-fa1922dfa9a8'
var IO_PIN_DATA = 'e95d8d00-251d-470a-a062-fa1922dfa9a8'
var IO_AD_CONFIG = 'e95d5899-251d-470a-a062-fa1922dfa9a8'
var IO_PIN_CONFIG = 'e95db9fe-251d-470a-a062-fa1922dfa9a8'
var IO_PIN_PWM = 'e95dd822-251d-470a-a062-fa1922dfa9a8'
var LED_SRV = 'e95dd91d-251d-470a-a062-fa1922dfa9a8'
var LED_STATE = 'e95d7b77-251d-470a-a062-fa1922dfa9a8'
var LED_TEXT = 'e95d93ee-251d-470a-a062-fa1922dfa9a8'
var LED_SCROLL = 'e95d0d2d-251d-470a-a062-fa1922dfa9a8'
var TEMP_SRV = 'e95d6100-251d-470a-a062-fa1922dfa9a8'
var TEMP_DATA = 'e95d9250-251d-470a-a062-fa1922dfa9a8'
var TEMP_PERIOD = 'e95d1b25-251d-470a-a062-fa1922dfa9a8'
class uBit {
constructor() {
this.accelerometer = {
x: 0,
y: 0,
z: 0
};
this.magnetometer_raw = {
x: 0,
y: 0,
z: 0
};
this.magnetometer_bearing = 0;
this.temperature = 0;
this.buttonA = 0;
this.buttonACallBack=function(){};
this.buttonB = 0;
this.buttonBCallBack=function(){};
this.connected = false;
this.onConnectCallback=function(){};
this.onDisconnectCallback=function(){};
this.onBLENotifyCallback=function(){};
this.characteristic = {
IO_PIN_DATA: {},
IO_AD_CONFIG: {},
IO_PIN_CONFIG: {},
IO_PIN_PWM: {},
LED_STATE: {},
LED_TEXT: {},
LED_SCROLL: {},
}
}
getTemperature() {
return this.temperature;
}
getAccelerometer() {
return this.accelerometer;
}
getBearing() {
return this.magnetometer_bearing;
}
getButtonA() {
return this.buttonA;
}
setButtonACallback(callbackFunction){
this.buttonACallBack=callbackFunction;
}
getButtonB() {
return this.buttonB;
}
setButtonBCallback(callbackFunction){
this.buttonBCallBack=callbackFunction;
}
onConnect(callbackFunction){
this.onConnectCallback=callbackFunction;
}
onDisconnect(callbackFunction){
this.onDisconnectCallback=callbackFunction;
}
onBleNotify(callbackFunction){
this.onBLENotifyCallback=callbackFunction;
}
writePin(pin) {
//something like this should work, but we need to create the correct buffer
//this.characteristic.IO_PIN_DATA.writeValue(data);
}
readPin(pin) {
}
writeMatrixIcon(icon) {
var ledMatrix = new Int8Array(5);
var buffer = [
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0']
]
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
buffer[i][7-j] = icon[i][4 - j]
}
}
for (var i = 0; i < 5; i++) {
var string = buffer[i].join("");
ledMatrix[i]=parseInt(string,2)
}
if(this.connected){
this.characteristic.LED_STATE.writeValue(ledMatrix)
.then(_ => {
})
.catch(error => {
console.log(error);
});
}
}
writeMatrixTextSpeed(speed){
var buffer= new Uint8Array(speed);
if(this.connected){
this.characteristic.LED_TEXT.writeValue(buffer)
.then(_ => {
})
.catch(error => {
console.log(error);
});
}
}
writeMatrixText(str){
var buffer= new Uint8Array(toUTF8Array(str));
if(this.connected){
this.characteristic.LED_TEXT.writeValue(buffer)
.then(_ => {
})
.catch(error => {
console.log(error);
});
}
}
onButtonA(){
this.buttonACallBack();
}
onButtonB(){
this.buttonBCallBack();
}
characteristic_updated(event) {
this.onBLENotifyCallback();
//BUTTON CHARACTERISTIC
if (event.target.uuid == BTN_A_STATE) {
//console.log("BTN_A_STATE", event.target.value.getInt8());
this.buttonA = event.target.value.getInt8();
if (this.buttonA){
this.onButtonA();
}
}
if (event.target.uuid == BTN_B_STATE) {
//console.log("BTN_B_STATE", event.target.value.getInt8());
this.buttonB = event.target.value.getInt8();
if (this.buttonB){
this.onButtonB();
}
}
//ACCELEROMETER CHARACTERISTIC
if (event.target.uuid == ACCEL_DATA) {
//true is for reading the bits as little-endian
//console.log("ACCEL_DATA_X", event.target.value.getInt16(0,true));
//console.log("ACCEL_DATA_Y", event.target.value.getInt16(2,true));
//console.log("ACCEL_DATA_Z", event.target.value.getInt16(4,true));
this.accelerometer.x = event.target.value.getInt16(0, true);
this.accelerometer.y = event.target.value.getInt16(2, true);
this.accelerometer.z = event.target.value.getInt16(4, true);
}
// MAGNETOMETER CHARACTERISTIC (raw data)
if (event.target.uuid == MAGNETO_DATA) {
// console.log("MAGNETO_DATA_X", event.target.value.getInt16(0,true));
// console.log("MAGNETO_DATA_Y", event.target.value.getInt16(2,true));
// console.log("MAGNETO_DATA_Z", event.target.value.getInt16(4,true));
this.magnetometer_raw.x = event.target.value.getInt16(0, true);
this.magnetometer_raw.y = event.target.value.getInt16(2, true);
this.magnetometer_raw.z = event.target.value.getInt16(4, true);
}
// MAGNETOMETER CHARACTERISTIC (bearing)
if (event.target.uuid == MAGNETO_BEARING) {
//console.log("BEARING", event.target.value.getInt16(0,true));
this.magnetometer_bearing = event.target.value.getInt16(0, true);
}
// TEMPERATURE CHARACTERISTIC
if (event.target.uuid == TEMP_DATA) {
//console.log("TEMP_DATA", event.target.value.getInt8());
this.temperature = event.target.value.getInt8();
}
}
searchDevice() {
filters: []
var options = {};
options.acceptAllDevices = true;
options.optionalServices = [ACCEL_SRV, MAGNETO_SRV, BTN_SRV, IO_PIN_SRV, LED_SRV, TEMP_SRV];
console.log('Requesting Bluetooth Device...');
console.log('with ' + JSON.stringify(options));
navigator.bluetooth.requestDevice(options)
.then(device => {
console.log('> Name: ' + device.name);
console.log('> Id: ' + device.id);
device.addEventListener('gattserverdisconnected', this.onDisconnectCallback);
// Attempts to connect to remote GATT Server.
return device.gatt.connect();
})
.then(server => {
// Note that we could also get all services that match a specific UUID by
// passing it to getPrimaryServices().
this.onConnectCallback();
console.log('Getting Services...');
return server.getPrimaryServices();
})
.then(services => {
console.log('Getting Characteristics...');
let queue = Promise.resolve();
services.forEach(service => {
queue = queue.then(_ => service.getCharacteristics().then(characteristics => {
console.log('> Service: ' + service.uuid);
characteristics.forEach(characteristic => {
console.log('>> Characteristic: ' + characteristic.uuid + ' ' +
getSupportedProperties(characteristic));
//need to store all the characteristic I want to write to be able to access them later.
switch (characteristic.uuid) {
case IO_PIN_DATA:
this.characteristic.IO_PIN_DATA = characteristic;
break;
case IO_AD_CONFIG:
this.characteristic.IO_AD_CONFIG = characteristic;
break;
case IO_PIN_CONFIG:
this.characteristic.IO_PIN_CONFIG = characteristic;
break;
case IO_PIN_PWM:
this.characteristic.IO_PIN_PWM = characteristic;
break;
case LED_STATE:
this.characteristic.LED_STATE = characteristic;
this.connected = true;
break;
case LED_TEXT:
this.characteristic.LED_TEXT = characteristic;
break;
case LED_SCROLL:
this.characteristic.LED_SCROLL = characteristic;
break;
default:
}
if (getSupportedProperties(characteristic).includes('NOTIFY')) {
characteristic.startNotifications().catch(err => console.log('startNotifications', err));
characteristic.addEventListener('characteristicvaluechanged',
this.characteristic_updated.bind(this));
}
});
}));
});
return queue;
}
)
.catch(error => {
console.log('Argh! ' + error);
});
}
}
/* Utils */
function isWebBluetoothEnabled() {
if (navigator.bluetooth) {
return true;
} else {
ChromeSamples.setStatus('Web Bluetooth API is not available.\n' +
'Please make sure the "Experimental Web Platform features" flag is enabled.');
return false;
}
}
function getSupportedProperties(characteristic) {
let supportedProperties = [];
for (const p in characteristic.properties) {
if (characteristic.properties[p] === true) {
supportedProperties.push(p.toUpperCase());
}
}
return '[' + supportedProperties.join(', ') + ']';
}
function toUTF8Array(str) {
var utf8 = [];
for (var i=0; i < str.length; i++) {
var charcode = str.charCodeAt(i);
if (charcode < 0x80) utf8.push(charcode);
else if (charcode < 0x800) {
utf8.push(0xc0 | (charcode >> 6),
0x80 | (charcode & 0x3f));
}
else if (charcode < 0xd800 || charcode >= 0xe000) {
utf8.push(0xe0 | (charcode >> 12),
0x80 | ((charcode>>6) & 0x3f),
0x80 | (charcode & 0x3f));
}
// surrogate pair
else {
i++;
// UTF-16 encodes 0x10000-0x10FFFF by
// subtracting 0x10000 and splitting the
// 20 bits of 0x0-0xFFFFF into two halves
charcode = 0x10000 + (((charcode & 0x3ff)<<10)
| (str.charCodeAt(i) & 0x3ff));
utf8.push(0xf0 | (charcode >>18),
0x80 | ((charcode>>12) & 0x3f),
0x80 | ((charcode>>6) & 0x3f),
0x80 | (charcode & 0x3f));
}
}
return utf8;
}