Skip to content

Commit 640dd6c

Browse files
Add support for compass. (#42)
1 parent 95d12d8 commit 640dd6c

File tree

8 files changed

+104
-26
lines changed

8 files changed

+104
-26
lines changed

src/board/compass.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { RangeSensor, State } from "./state";
2+
3+
type StateKeys = "compassX" | "compassY" | "compassZ" | "compassHeading";
4+
5+
export class Compass {
6+
state: Pick<State, "compassX" | "compassY" | "compassZ" | "compassHeading">;
7+
8+
constructor() {
9+
const min = -2_000_000;
10+
const max = 2_000_000;
11+
this.state = {
12+
compassX: new RangeSensor("compassX", min, max, 0, "nT"),
13+
compassY: new RangeSensor("compassY", min, max, 0, "nT"),
14+
compassZ: new RangeSensor("compassZ", min, max, 0, "nT"),
15+
compassHeading: new RangeSensor("compassHeading", 0, 360, 0, "deg"),
16+
};
17+
}
18+
19+
setValue(id: StateKeys, value: any) {
20+
this.state[id].setValue(value);
21+
}
22+
23+
getFieldStrength() {
24+
const x = this.state.compassX.value;
25+
const y = this.state.compassY.value;
26+
const z = this.state.compassZ.value;
27+
return Math.sqrt(x * x + y * y + z * z);
28+
}
29+
30+
initialize() {}
31+
32+
dispose() {}
33+
}

src/board/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import svgText from "../microbit-drawing.svg";
22
import { Accelerometer } from "./accelerometer";
33
import { Audio } from "./audio";
44
import { Button } from "./buttons";
5+
import { Compass } from "./compass";
56
import {
67
MICROBIT_HAL_PIN_FACE,
78
MICROBIT_HAL_PIN_P0,
@@ -10,11 +11,11 @@ import {
1011
} from "./constants";
1112
import { Display } from "./display";
1213
import { FileSystem } from "./fs";
13-
import { WebAssemblyOperations } from "./wasm";
1414
import { Microphone } from "./microphone";
1515
import { Pin } from "./pins";
1616
import { Radio } from "./radio";
17-
import { EnumSensor, RangeSensor, Sensor, State } from "./state";
17+
import { RangeSensor, State } from "./state";
18+
import { WebAssemblyOperations } from "./wasm";
1819

1920
const stoppedOpactity = "0.5";
2021

@@ -43,6 +44,7 @@ export class Board {
4344
temperature: RangeSensor;
4445
microphone: Microphone;
4546
accelerometer: Accelerometer;
47+
compass: Compass;
4648
radio: Radio;
4749

4850
public serialInputBuffer: number[] = [];
@@ -89,6 +91,7 @@ export class Board {
8991
this.audio = new Audio();
9092
this.temperature = new RangeSensor("temperature", -5, 50, 21, "°C");
9193
this.accelerometer = new Accelerometer(onChange);
94+
this.compass = new Compass();
9295
this.microphone = new Microphone(
9396
this.svg.querySelector("#LitMicrophone")!,
9497
onChange
@@ -124,6 +127,11 @@ export class Board {
124127
accelerometerZ: this.accelerometer.state.accelerometerZ,
125128
gesture: this.accelerometer.state.gesture,
126129

130+
compassX: this.compass.state.compassX,
131+
compassY: this.compass.state.compassY,
132+
compassZ: this.compass.state.compassZ,
133+
compassHeading: this.compass.state.compassHeading,
134+
127135
lightLevel: this.display.lightLevel,
128136
dataLogging: {
129137
// Placeholder.
@@ -144,6 +152,13 @@ export class Board {
144152
this.accelerometer.setValue(id, value);
145153
break;
146154
}
155+
case "compassX":
156+
case "compassY":
157+
case "compassZ":
158+
case "compassHeading": {
159+
this.compass.setValue(id, value);
160+
break;
161+
}
147162
case "buttonA": {
148163
this.buttons[0].setValue(value);
149164
break;
@@ -198,6 +213,7 @@ export class Board {
198213
this.pins.forEach((p) => p.initialize());
199214
this.display.initialize();
200215
this.accelerometer.initialize(this.operations.gestureCallback!);
216+
this.compass.initialize();
201217
this.microphone.initialize(this.operations.soundLevelCallback!);
202218
this.radio.initialize();
203219
this.serialInputBuffer.length = 0;
@@ -292,6 +308,7 @@ export class Board {
292308
this.pins.forEach((p) => p.dispose());
293309
this.display.dispose();
294310
this.accelerometer.dispose();
311+
this.compass.dispose();
295312
this.microphone.dispose();
296313
this.radio.dispose();
297314
this.serialInputBuffer.length = 0;

src/board/state.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export interface State {
1919
accelerometerZ: RangeSensor;
2020
gesture: EnumSensor;
2121

22+
compassX: RangeSensor;
23+
compassY: RangeSensor;
24+
compassZ: RangeSensor;
25+
compassHeading: RangeSensor;
26+
2227
pin0: RangeSensor;
2328
pin1: RangeSensor;
2429
pin2: RangeSensor;

src/demo.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
8484
<option value="audio">Audio</option>
8585
<option value="background">Background music and display</option>
8686
<option value="buttons">Buttons</option>
87+
<option value="compass">Compass</option>
8788
<option value="display">Display</option>
8889
<option value="microphone">Microphone</option>
8990
<option value="music">Music</option>

src/examples/compass.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Imports go at the top
2+
from microbit import *
3+
4+
# Code in a 'while True:' loop repeats forever
5+
while True:
6+
print("x: ", compass.get_x())
7+
print("y: ", compass.get_y())
8+
print("z: ", compass.get_z())
9+
print("heading: ", compass.heading())
10+
print("field strength: ", compass.get_field_strength())
11+
sleep(1000)

src/jshal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ int mp_js_hal_accelerometer_get_z(void);
5959
int mp_js_hal_accelerometer_get_gesture(void);
6060
void mp_js_hal_accelerometer_set_range(int r);
6161

62+
int mp_js_hal_compass_get_x(void);
63+
int mp_js_hal_compass_get_y(void);
64+
int mp_js_hal_compass_get_z(void);
65+
int mp_js_hal_compass_get_field_strength(void);
66+
int mp_js_hal_compass_get_heading(void);
67+
6268
void mp_js_hal_audio_set_volume(int value);
6369
void mp_js_hal_audio_init(uint32_t sample_rate);
6470
void mp_js_hal_audio_write_data(const uint8_t *buf, size_t num_samples);

src/jshal.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,26 @@ mergeInto(LibraryManager.library, {
139139
board.accelerometer.setRange(r);
140140
},
141141

142+
mp_js_hal_compass_get_x: function () {
143+
return board.compass.state.compassX.value;
144+
},
145+
146+
mp_js_hal_compass_get_y: function () {
147+
return board.compass.state.compassY.value;
148+
},
149+
150+
mp_js_hal_compass_get_z: function () {
151+
return board.compass.state.compassZ.value;
152+
},
153+
154+
mp_js_hal_compass_get_field_strength: function () {
155+
return board.compass.getFieldStrength();
156+
},
157+
158+
mp_js_hal_compass_get_heading: function () {
159+
return board.compass.state.compassHeading.value;
160+
},
161+
142162
mp_js_hal_audio_set_volume: function (value) {
143163
board.audio.setVolume(value);
144164
},

src/microbithal_js.c

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -337,45 +337,30 @@ void microbit_hal_accelerometer_set_range(int r) {
337337
}
338338

339339
int microbit_hal_compass_is_calibrated(void) {
340-
/*
341-
return uBit.compass.isCalibrated();
342-
*/
343-
return 0;
340+
// Always calibrated in the simulator.
341+
return 1;
344342
}
345343

346344
void microbit_hal_compass_clear_calibration(void) {
347-
/*
348-
uBit.compass.clearCalibration();
349-
*/
345+
// No calibration to clear.
350346
}
351347

352348
void microbit_hal_compass_calibrate(void) {
353-
/*
354-
uBit.compass.calibrate();
355-
*/
349+
// No calibration to set.
356350
}
357351

358352
void microbit_hal_compass_get_sample(int axis[3]) {
359-
/*
360-
Sample3D sample = uBit.compass.getSample();
361-
axis[0] = sample.x;
362-
axis[1] = sample.y;
363-
axis[2] = sample.z;
364-
*/
353+
axis[0] = mp_js_hal_compass_get_x();
354+
axis[1] = mp_js_hal_compass_get_y();
355+
axis[2] = mp_js_hal_compass_get_z();
365356
}
366357

367358
int microbit_hal_compass_get_field_strength(void) {
368-
/*
369-
return uBit.compass.getFieldStrength();
370-
*/
371-
return 0;
359+
return mp_js_hal_compass_get_field_strength();
372360
}
373361

374362
int microbit_hal_compass_get_heading(void) {
375-
/*
376-
return uBit.compass.heading();
377-
*/
378-
return 0;
363+
return mp_js_hal_compass_get_heading();
379364
}
380365

381366
const uint8_t *microbit_hal_get_font_data(char c) {

0 commit comments

Comments
 (0)