-
Notifications
You must be signed in to change notification settings - Fork 0
/
PinBlink.hx
55 lines (42 loc) · 1.22 KB
/
PinBlink.hx
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
package examples;
import js.Node.console;
import js.Node.process;
import BBCMicrobit;
class PinBlink {
var pin = 0;
var interval = 1000; // ms
var pinValue = 0;
var microbit:MicrobitObj;
public function new () {
console.log('Scanning for microbit');
BBCMicrobit.discover(function(microbit) {
console.log('\tdiscovered microbit: id = %s, address = %s', microbit.id, microbit.address);
this.microbit = microbit;
microbit.on('disconnect', function() {
console.log('\tmicrobit disconnected!');
process.exit(0);
});
console.log('connecting to microbit');
microbit.connectAndSetUp(function() {
console.log('\tconnected to microbit');
console.log('setting pin %d as output', pin);
microbit.pinOutput(pin, function() {
console.log('\tpin set as output');
console.log('setting pin %d as digital', pin);
microbit.pinDigital(pin, function() {
console.log('\tpin set as digital');
togglePin();
});
});
});
});
}
function togglePin() {
pinValue = (pinValue == 0) ? 1 : 0;
console.log('writing %d to pin %d', pinValue, pin);
microbit.writePin(pin, pinValue, function() {
console.log('\tdone');
untyped setTimeout(togglePin, interval);
});
}
}