-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths2microbit-ble.js
622 lines (570 loc) · 21.1 KB
/
s2microbit-ble.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
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
"use strict";
// https://github.com/sandeepmistry/node-bbc-microbit/blob/master/API.md
// Some part of the code is from
// https://github.com/jaafreitas/scratch-microbit-extension/
const BBCMicrobit = require('bbc-microbit');
let device = null;
let microbitConnected = false;
const BUTTON_VALUE_MAPPER = ['Not Pressed', 'Pressed', 'Long Press'];
let debug = false;
// functionality
let useButtons = true;
let useTemperature = true;
let useAccelerometer = true;
let useMagnetometer = true; // false
let usePins = true;
// states and values
let buttonState = null;
let matrixState = null;
let pinValue = null;
let pinMode = null;
let temperature = null;
let magnetometerBearing = null;
let magnetometer = null;
let accelerometer = null;
let prev_acc_z = null;
let ledBuffer = null;
let deviceName = null;
const PIN_NOTSET = 0xFF;
const PINMODE_OUTPUT_DIGITAL = 0x00;
const PINMODE_INPUT = 0x01;
const PINMODE_ANALOG = 0x02;
const PINMODE_ANALOG_INPUT = 0x03;
// LED matrix patterns (new Buffer has been deprecated: https://nodejs.org/api/buffer.html#buffer_buffer)
const LED_PATTERNS = [
{name: 'HAPPY', value: Buffer.from([0b00000, 0b01010, 0b00000, 0b10001, 0b01110])},
{name: 'SAD', value: Buffer.from([0b00000, 0b01010, 0b00000, 0b01110, 0b10001])},
{name: 'ANGRY', value: Buffer.from([0b10001, 0b01010, 0b00000, 0b11111, 0b10101])},
{name: 'SMILE', value: Buffer.from([0b00000, 0b00000, 0b00000, 0b10001, 0b01110])},
{name: 'HEART', value: Buffer.from([0b01010, 0b11111, 0b11111, 0b01110, 0b00100])},
{name: 'CONFUSED', value: Buffer.from([0b00000, 0b01010, 0b00000, 0b01010, 0b10101])},
{name: 'ASLEEP', value: Buffer.from([0b00000, 0b11011, 0b00000, 0b01110, 0b00000])},
{name: 'SURPRISED', value: Buffer.from([0b01010, 0b00000, 0b00100, 0b01010, 0b00100])},
{name: 'SILLY', value: Buffer.from([0b10001, 0b00000, 0b11111, 0b00011, 0b00011])},
{name: 'FABULOUS', value: Buffer.from([0b11111, 0b11011, 0b00000, 0b01010, 0b01110])},
{name: 'MEH', value: Buffer.from([0b01010, 0b00000, 0b00010, 0b00100, 0b01000])},
{name: 'YES', value: Buffer.from([0b00000, 0b00001, 0b00010, 0b10100, 0b01000])},
{name: 'NO', value: Buffer.from([0b10001, 0b01010, 0b00100, 0b01010, 0b10001])},
{name: 'TRIANGLE', value: Buffer.from([0b00000, 0b00100, 0b01010, 0b11111, 0b00000])},
{name: 'DIAMOND', value: Buffer.from([0b00100, 0b01010, 0b10001, 0b01010, 0b00100])},
{name: 'DIAMOND_SMALL', value: Buffer.from([0b00000, 0b00100, 0b01010, 0b00100, 0b00000])},
{name: 'SQUARE', value: Buffer.from([0b11111, 0b10001, 0b10001, 0b10001, 0b11111])},
{name: 'SQUARE_SMALL', value: Buffer.from([0b00000, 0b01110, 0b01010, 0b01110, 0b00000])},
{name: 'TARGET', value: Buffer.from([0b00100, 0b01110, 0b11011, 0b01110, 0b00100])},
{name: 'STICKFIGURE', value: Buffer.from([0b00100, 0b11111, 0b00100, 0b01010, 0b10001])},
{name: 'RABBIT', value: Buffer.from([0b10100, 0b10100, 0b11110, 0b11010, 0b11110])},
{name: 'COW', value: Buffer.from([0b10001, 0b10001, 0b11111, 0b01110, 0b00100])},
{name: 'ROLLERSKATE', value: Buffer.from([0b00011, 0b00011, 0b11111, 0b11111, 0b01010])},
{name: 'HOUSE', value: Buffer.from([0b00100, 0b01110, 0b11111, 0b01110, 0b01010])},
{name: 'SNAKE', value: Buffer.from([0b11000, 0b11011, 0b01010, 0b01110, 0b00000])},
{name: 'ARROW_N', value: Buffer.from([0b00100, 0b01110, 0b10101, 0b00100, 0b00100])},
{name: 'ARROW_NE', value: Buffer.from([0b00111, 0b00011, 0b00101, 0b01000, 0b10000])},
{name: 'ARROW_E', value: Buffer.from([0b00100, 0b00010, 0b11111, 0b00010, 0b00100])},
{name: 'ARROW_SE', value: Buffer.from([0b10000, 0b01000, 0b00101, 0b00011, 0b00111])},
{name: 'ARROW_S', value: Buffer.from([0b00100, 0b00100, 0b10101, 0b01110, 0b00100])},
{name: 'ARROW_SW', value: Buffer.from([0b00001, 0b00010, 0b10100, 0b11000, 0b11100])},
{name: 'ARROW_W', value: Buffer.from([0b00100, 0b01000, 0b11111, 0b01000, 0b00100])},
{name: 'ARROW_NW', value: Buffer.from([0b11100, 0b11000, 0b10100, 0b00010, 0b00001])},
{name: 'HEART_SMALL', value: Buffer.from([0b000000, 0b01010, 0b01110, 0b00100, 0b00000])},
{name: 'TRIANGLE_LEFT', value: Buffer.from([0b10000, 0b11000, 0b10100, 0b10010, 0b11111])},
{name: 'CHESSBOARD', value: Buffer.from([0b01010, 0b10101, 0b01010, 0b10101, 0b01010])},
{name: 'PITCHFORK', value: Buffer.from([0b10101, 0b10101, 0b11111, 0b00100, 0b00100])},
{name: 'XMAS', value: Buffer.from([0b00100, 0b01110, 0b00100, 0b01110, 0b11111])},
{name: 'TSHIRT', value: Buffer.from([0b11011, 0b11111, 0b01110, 0b01110, 0b01110])},
{name: 'SWORD', value: Buffer.from([0b00100, 0b00100, 0b00100, 0b01110, 0b00100])},
{name: 'UMBRELLA', value: Buffer.from([0b01110, 0b11111, 0b00100, 0b10100, 0b01100])},
{name: 'DUCK', value: Buffer.from([0b01100, 0b11100, 0b01111, 0b01110, 0b00000])},
{name: 'TORTOISE', value: Buffer.from([0b00000, 0b01110, 0b11111, 0b01010, 0b00000])},
{name: 'BUTTERFLY', value: Buffer.from([0b11011, 0b11111, 0b00100, 0b11111, 0b11011])},
{name: 'GIRAFFE', value: Buffer.from([0b11000, 0b01000, 0b01000, 0b01110, 0b01010])},
{name: 'SKULL', value: Buffer.from([0b01110, 0b10101, 0b11111, 0b01110, 0b01110])},
{name: 'MUSIC_CROTCHET', value: Buffer.from([0b00100, 0b00100, 0b00100, 0b11100, 0b11100])},
{name: 'MUSIC_QUAVER', value: Buffer.from([0b00100, 0b00110, 0b00101, 0b11100, 0b11100])},
{name: 'MUSIC_QUAVERS', value: Buffer.from([0b01111, 0b01001, 0b01001, 0b11011, 0b11011])},
{name: 'SCISSORS', value: Buffer.from([0b11001, 0b11010, 0b00100, 0b11010, 0b11001])},
{name: 'PACMAN', value: Buffer.from([0b01111, 0b11010, 0b11100, 0b11000, 0b01111])},
{name: 'GHOST', value: Buffer.from([0b01110, 0b10101, 0b11111, 0b11111, 0b10101])}
];
let LED_PATTERN_MAP = { }; // map : pattern name -> value
function createLedPatternMap() {
for (var i=0; i < LED_PATTERNS.length; i++){
LED_PATTERN_MAP[LED_PATTERNS[i].name] = LED_PATTERNS[i].value; // value=reference
}
}
createLedPatternMap();
// Initialization
function initValues () {
console.log("Initialize values...");
buttonState = {A: 0, B: 0};
matrixState = [0, 0, 0, 0, 0];
// The array has space for P0 to P20 (including P17 and P18).
pinValue = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// 00(0):D-Out, 01(1):D-In, 10(2):A-Out, 11(3):A-In
pinMode = [PIN_NOTSET, PIN_NOTSET, PIN_NOTSET,
PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET,
PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET,
PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET, PIN_NOTSET];
// Initialize LED matrix
ledBuffer = Buffer.alloc(5);
LED_PATTERN_MAP['YES'].copy(ledBuffer); // copy the value (do not pass by reference)
// Initialize sensor data
temperature = 0;
magnetometerBearing = 0;
magnetometer = { 'x': 0, 'y': 0, 'z': 0 };
accelerometer = { 'x': 0, 'y': 0, 'z': 0 };
prev_acc_z = 0;
}
initValues();
// Discover microbit
console.log("=== BBC micro:bit Scratch 2.0 offline extension ===");
var id = 'dd628ee75dfe';
//var id = 'd5a250cd6035';
function microbitScanner() {
console.log('microbit: scanning...');
//BBCMicrobit.discoverAll(onDiscover); // find all microbits
//BBCMicrobit.discoverById(id, microbitFound);
BBCMicrobit.discover(microbitFound);
}
// Callback of discoverAll
function onDiscover(microbit) {
console.log(" found microbit : " + microbit);
}
microbitScanner();
// Show pin settings
function showPinSetting(microbit) {
microbit.readPinAdConfiguration(function(error, value) {
console.log("pinsetting AD: " + value);
});
microbit.readPinIoConfiguration(function(error, value) {
console.log("pinsetting IO: " + value);
});
}
// Initialize 0-2 pin setting to Analog-Input
function initializePinSetting(microbit) {
/*
microbit.writePinAdConfiguration(0x07, function(error) {
console.log("writePinAdConfiguration (error): ", error);
microbit.writePinIoConfiguration(0x07, function(error) {
console.log("writePinIoConfiguration (error): ", error);
// microbit.subscribePinData(function(error) {
// console.log("subscribePinData (error): ", error);
microbit.readPin(function(error, value) { // triger pinDataChange...
showPinSetting(microbit);
for (var pin=0; pin <= 2; pin++) {
pinMode[pin] = PINMODE_ANALOG_INPUT;
}
});
});
});
// });
*/
for (var pin=0; pin <= 2; pin++) {
setupPinMode({pin: pin, ADmode: 'analog', IOmode: 'input'});
}
}
// Callback when discovered
function microbitFound(microbit) {
console.log('microbit: discovered %s', microbit); // microbit.id, microbit.address
// Events from microbit
microbit.on('disconnect', function() {
microbitConnected = false;
device = null;
console.log('microbit: disconnected. microbitConnected= ' + microbitConnected);
microbitScanner();
});
microbit.on('buttonAChange', function(value) {
if (debug) { console.log('microbit: button A', BUTTON_VALUE_MAPPER[value]); }
buttonState['A']= value;
});
microbit.on('buttonBChange', function(value) {
if (debug) { console.log('microbit: button B', BUTTON_VALUE_MAPPER[value]); }
buttonState['B']= value;
});
microbit.on('pinDataChange', function(pin, value) {
if (debug) { console.log('microbit: pinDataChange pin %d, value %d', pin, value); }
pinValue[pin] = value;
});
microbit.on('temperatureChange', function(value) {
if (debug) { console.log('microbit: temperature %d', value); }
temperature = value;
});
microbit.on('magnetometerBearingChange', function(value) {
console.log('microbit: magnetometer bearing %d', value);
if (debug) { console.log('microbit: magnetometer bearing %d', value); }
magnetometerBearing = value;
});
microbit.on('magnetometerChange', function(x, y, z) {
// console.log('microbit: orig magnetometer %d, %d, %d', x, y, z);
x = x.toFixed(2);
y = y.toFixed(2);
z = z.toFixed(2);
//if (debug) { console.log('microbit: magnetometer %d, %d, %d', x, y, z); }
magnetometer = { 'x': x, 'y': y, 'z': z };
});
microbit.on('accelerometerChange', function(x, y, z) {
// console.log('microbit: orig accelerometer %d, %d, %d', x, y, z);
x = x.toFixed(2);
y = y.toFixed(2);
z = z.toFixed(2);
//if (debug) { console.log('microbit: accelerometer %d, %d, %d', x, y, z); }
accelerometer = { 'x': x, 'y': y, 'z': z };
});
// When connected
console.log('microbit: connecting...');
microbit.connectAndSetUp(function() {
microbitConnected = true;
device = microbit;
console.log('microbit: connected ' + microbitConnected);
if (useButtons) {
microbit.subscribeButtons(function(error) {
console.log('microbit: subscribed to buttons');
});
}
if (useTemperature) {
microbit.writeTemperaturePeriod(1000, function() {
microbit.subscribeTemperature(function(error) {
console.log('microbit: subscribed to temperature');
});
});
}
if (useMagnetometer) {
microbit.writeMagnetometerPeriod(160, function() {
// Use either of Bearing or XYZ
/*
microbit.subscribeMagnetometerBearing(function(error) {
console.log('microbit: subscribed to magnetometer bearing');
});
*/
microbit.subscribeMagnetometer(function(error) {
console.log('microbit: subscribed to magnetometer');
});
});
}
if (useAccelerometer) {
microbit.writeAccelerometerPeriod(160, function() {
microbit.subscribeAccelerometer(function(error) {
console.log('microbit: subscribed to accelerometer');
});
});
}
if (usePins) {
microbit.subscribePinData(function(error) {
console.log('microbit: subscribePinData');
initializePinSetting(microbit); // Initialize pin 0-2
});
}
// Read device name
microbit.readDeviceName(function(error, devicename) {
console.log('microbit deviceName: ' + devicename);
deviceName = devicename;
});
// Initial pattern
writeLedBuffer();
if (exserver === null) {
startHTTPServer();
}
});
}
// ================= HTTP server =======================
const express = require('express');
let exapp = express();
let exserver = null;
function startHTTPServer(){
exserver = exapp.listen(50209, function(){
console.log("Server started... listening port " + exserver.address().port);
});
}
//--- Routing: Responses to HTTP requrests from Scratch 2.0
exapp.get('/scroll/:text', function(req, res) {
if (device) {
// text is a string that must be 20 characters or less
var txt = req.params.text.substring(0, 20);
device.writeLedText(txt, function(error) {
console.log('microbit: display %s', txt);
});
}
res.send('OK');
});
// Reset from scratch
exapp.get('/reset_all', function(req, res){
console.log('reset_all is called');
initValues();
initializePinSetting(device); // Initialize pin 0-2
res.send('OK');
});
// LED matrix (image pattern)
function writeLedBuffer(error) {
device.writeLedMatrixState(ledBuffer, function(error) {
console.log("writeLedBuffer: buf= ", ledBuffer);
});
}
// LED display preset image
exapp.get('/display_image/:name', function(req, res) {
if (device) {
var name = req.params.name;
if (name.charAt(2) == '_') { // non-English
LED_PATTERNS[name.substr(0,2)-1].value.copy(ledBuffer);
} else { // English
LED_PATTERN_MAP[name].copy(ledBuffer);
}
console.log('microbit: [display_image] name= ' + name);
writeLedBuffer();
}
res.send('OK');
});
// LED dot
exapp.get('/write_pixel/:x/:y/:value', function(req, res){
if (device){
var val = req.params.value;
if (val >= 1) {
val = 1;
}else{
val = 0;
}
var x = req.params.x;
if (x < 0){
x = 0;
}
if (x > 4){
x = 4;
}
var y = req.params.y;
if (y < 0){
y = 0;
}
if (y > 4){
y = 4;
}
ledBuffer[y] &= ~(0x01<<(4-x)); // clear the pixel (set 0)
ledBuffer[y] |= val<<(4-x); // set the pixel to 'val'
console.log('microbit: [write_pixel] val=%d to (%d, %d)', val, x, y);
writeLedBuffer();
}
res.send('OK');
});
// LED display custom pattern
exapp.get('/display_pattern/:binstr', function(req, res) {
if (device) {
console.log('microbit: [display_pattern] str= %s', req.params.binstr);
// check
if ( ! /^[01]{5} [01]{5} [01]{5} [01]{5} [01]{5}$/.test(req.params.binstr) ) {
console.log('error: illegal pattern');
res.send('ERROR');
return;
}
var linearray = req.params.binstr.split(' ');
// check
/*
for (var s in linearray) {
console.log('s= %s', s);
}
if (linearray.length != 5) {
console.log('error: illegal array length= %d', linearray.length);
res.send('ERROR');
return;
}
*/
for (var y=0; y < 5; y++) {
ledBuffer.writeUInt8(parseInt(linearray[y], 2), y);
console.log('buf[%d] = %d', y, ledBuffer[y]);
}
writeLedBuffer();
}
res.send('OK');
});
// clear LED
exapp.get('/display_clear', function(req, res){
if (device){
ledBuffer.fill(0);
console.log('microbit: [display_clear]');
writeLedBuffer();
}
res.send('OK');
});
// PIN I/O
// Should we use wait block?
exapp.get('/setup_pin/:pin/:admode/:iomode', function(req, res) {
console.log('setup_pin is called');
if (device) {
var pin = req.params.pin;
if(pin < 0 || pin > 20 ){
console.log('[setup_pin] error: pin number (%d) is out of range', pin);
res.send('ERROR');
return;
}
// pinMode[pin] = PIN_NOTSET; // once reset mode
var admode = req.params.admode;
if (admode.charAt(0) == 'D') {
admode = 'digital';
} else if(admode.charAt(0) == 'A') {
admode = 'analog';
} else {
console.log('[setup_pin] error: no such ADmode: %s', admode);
res.send('ERROR');
return;
}
var iomode = req.params.iomode;
if (iomode.charAt(0) == 'I') {
iomode = 'input';
} else if (iomode.charAt(0) == 'O') {
iomode = 'output';
} else {
console.log('[setup_pin] error: no such IOmode: %s', iomode);
res.send('ERROR');
return;
}
setupPinMode({pin: pin, ADmode: admode, IOmode: iomode});
}
res.send('OK');
});
exapp.get('/digital_write/:pin/:value', function(req, res) {
if (device) {
var pin = req.params.pin;
if(pin < 0 || pin > 20 ){
console.log('error: pin number (%d) is out of range', pin);
res.send('ERROR');
return;
}
if ( (pinMode[pin] & PINMODE_INPUT) == PINMODE_INPUT || (pinMode[pin] & PINMODE_ANALOG) == PINMODE_ANALOG ) {
console.log("[digital_write] setup pin mode : current pinMode[%d]= %d", pin, pinMode[pin]);
setupPinMode({pin: pin, ADmode: 'digital', IOmode: 'output'});
}else{
var val = req.params.value;
if(val >= 1) {
val = 1;
}else{
val = 0;
}
device.writePin(pin, val, function(error) {
console.log('microbit: [digital_write] pin %d, val %d', pin, val);
});
}
}
res.send('OK');
});
exapp.get('/analog_write/:pin/:value', function(req, res) {
if (device) {
var pin = req.params.pin;
if(pin < 0 || pin > 20 ){
console.log('error: pin number (%d) is out of range', pin);
res.send('ERROR');
return;
}
if ( (pinMode[pin] & PINMODE_INPUT) == PINMODE_INPUT || (pinMode[pin] & PINMODE_ANALOG) != PINMODE_ANALOG ) {
console.log('[analog_write] setup pin mode : current pinMode[%d]= %d', pin, pinMode[pin]);
setupPinMode({pin: pin, ADmode: 'analog', IOmode:' output'});
}else{
var val = req.params.value;
if(val > 255) {
val = 255;
}
if(val < 0) {
val = 0;
}
device.writePin(pin, val, function(error) {
console.log('microbit: [analog_write] pin %d, val %d', pin, val);
});
}
}
res.send('OK');
});
// Response to polloing
exapp.get('/poll', function(req, res) {
var reply = '';
reply += 'button_a_pressed ' + (buttonState['A']!=0) + '\n';
reply += 'button_b_pressed ' + (buttonState['B']!=0) + '\n';
for (var pin=0; pin <= 20; pin++){
if ((pinMode[pin] != PIN_NOTSET) && (pinMode[pin] & PINMODE_INPUT)){
if (pinMode[pin] & PINMODE_ANALOG){
reply += 'analog_read/' + pin + ' ' + pinValue[pin] + '\n';
}else{
reply += 'digital_read/' + pin + ' ' + pinValue[pin] + '\n';
}
}
}
if (accelerometer['x'] > 0) {
reply += 'tilted_right true\ntilted_left false\n';
} else {
reply += 'tilted_right false\ntilted_left true\n';
}
if (accelerometer['y'] > 0) {
reply += 'tilted_up true\ntilted_down false\n';
} else {
reply += 'tilted_up false\ntilted_down true\n';
}
if ( Math.abs(accelerometer['z'] - prev_acc_z) > 0.7 ) {
reply += 'shaken true\n';
} else {
reply += 'shaken false\n';
}
prev_acc_z = accelerometer['z'];
// sensor values
reply += 'temperature ' + temperature + '\n';
reply += 'magBearing ' + magnetometerBearing + '\n';
reply += 'mag_x ' + magnetometer['x'] + '\n';
reply += 'mag_y ' + magnetometer['y'] + '\n';
reply += 'mag_z ' + magnetometer['z'] + '\n';
reply += 'acc_x ' + accelerometer['x'] + '\n';
reply += 'acc_y ' + accelerometer['y'] + '\n';
reply += 'acc_z ' + accelerometer['z'] + '\n';
res.send(reply);
if (debug) { console.log(reply); }
});
// =============================================================
// Setting up pin mode (analog/digital and input/output)
function setupPinMode(data) {
if (device) { // && (pinMode[data.pin]==PIN_NOTSET)// setup only once
console.log("setupPinMode: pin %d is originally configured as: %d", data.pin, pinMode[data.pin]);
function log(data) {
console.log('microbit: setup pin %d as %s %s', data.pin, data.ADmode, data.IOmode);
}
// SubscribeData
function subscribe(device, data) {
// device.subscribePinData(function(error) {
log(data);
// It will trigger a pinDataChange.
device.readPin(data.pin, function(error, value) {
showPinSetting(device);
});
// });
}
// UnsubscribeData
function unsubscribe(device) {
// device.unsubscribePinData(function(error) {
log(data);
showPinSetting(device);
// });
}
pinMode[data.pin] = PINMODE_OUTPUT_DIGITAL;
if (data.IOmode == 'input') {
pinMode[data.pin] += PINMODE_INPUT;
device.pinInput(data.pin, function(error) {
if (data.ADmode == 'analog') {
pinMode[data.pin] += PINMODE_ANALOG;
device.pinAnalog(data.pin, function(error) {
// console.log('subscribe analog input: pinMode= %d', pinMode[data.pin]);
subscribe(device, data);
});
} else {
device.pinDigital(data.pin, function(error) {
// console.log('subscribe digital input: pinMode= %d', pinMode[data.pin]);
subscribe(device, data);
});
};
});
} else {
device.pinOutput(data.pin, function(error) {
if (data.ADmode == 'analog') {
pinMode[data.pin] += PINMODE_ANALOG;
device.pinAnalog(data.pin, function(error) {
unsubscribe(device);
});
} else {
device.pinDigital(data.pin, function(error) {
unsubscribe(device);
});
}
});
}
}
}