-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compass.hx
79 lines (66 loc) · 1.67 KB
/
Compass.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package examples;
import js.Node.console;
import js.Node.process;
class Compass {
var period = 160; // ms
var last_compass_point_name = "";
var COMPASS_POINT_DELTA = 22.5;
var COMPASS_POINTS = [
"N",
"NNE",
"NE",
"ENE",
"E",
"ESE",
"SE",
"SSE",
"S",
"SSW",
"SW",
"WSW",
"W",
"WNW",
"NW",
"NNW"
];
public function new () {
console.log('Scanning for microbit');
BBCMicrobit.discover(function(microbit) {
console.log('\tdiscovered microbit: id = %s, address = %s', microbit.id, microbit.address);
microbit.on('disconnect', function() {
console.log('\tmicrobit disconnected!');
process.exit(0);
});
microbit.on('magnetometerBearingChange', function(bearing) {
var point_name = compassPoint(bearing);
if (point_name != last_compass_point_name) {
console.log('\t Compass Direction: %s', point_name);
last_compass_point_name = point_name;
}
});
console.log('connecting to microbit');
microbit.connectAndSetUp(function() {
console.log('\tconnected to microbit');
console.log('setting magnetometer period to %d ms', period);
microbit.writeMagnetometerPeriod(period, function() {
console.log('\tmagnetometer period set');
console.log('subscribing to magnetometer bearing');
microbit.subscribeMagnetometerBearing(function() {
console.log('\tsubscribed to magnetometer bearing');
});
});
});
});
}
function compassPoint(bearing):String {
var d = bearing / COMPASS_POINT_DELTA;
var name_inx = Math.floor(d);
if (d - name_inx > 0.5) {
name_inx++;
}
if (name_inx > 15) {
name_inx = 0;
}
return COMPASS_POINTS[name_inx];
}
}