Skip to content

Add support for compass. #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/board/compass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { RangeSensor, State } from "./state";

type StateKeys = "compassX" | "compassY" | "compassZ" | "compassHeading";

export class Compass {
state: Pick<State, "compassX" | "compassY" | "compassZ" | "compassHeading">;

constructor() {
const min = -2_000_000;
const max = 2_000_000;
this.state = {
compassX: new RangeSensor("compassX", min, max, 0, "nT"),
compassY: new RangeSensor("compassY", min, max, 0, "nT"),
compassZ: new RangeSensor("compassZ", min, max, 0, "nT"),
compassHeading: new RangeSensor("compassHeading", 0, 360, 0, "deg"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit isn't documented in CODAL that I can see but MakeCode uses uBit.compass.heading() directly as as degrees value so this is all good.

};
}

setValue(id: StateKeys, value: any) {
this.state[id].setValue(value);
}

getFieldStrength() {
const x = this.state.compassX.value;
const y = this.state.compassY.value;
const z = this.state.compassZ.value;
return Math.sqrt(x * x + y * y + z * z);
}

initialize() {}

dispose() {}
}
21 changes: 19 additions & 2 deletions src/board/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import svgText from "../microbit-drawing.svg";
import { Accelerometer } from "./accelerometer";
import { Audio } from "./audio";
import { Button } from "./buttons";
import { Compass } from "./compass";
import {
MICROBIT_HAL_PIN_FACE,
MICROBIT_HAL_PIN_P0,
Expand All @@ -10,11 +11,11 @@ import {
} from "./constants";
import { Display } from "./display";
import { FileSystem } from "./fs";
import { WebAssemblyOperations } from "./wasm";
import { Microphone } from "./microphone";
import { Pin } from "./pins";
import { Radio } from "./radio";
import { EnumSensor, RangeSensor, Sensor, State } from "./state";
import { RangeSensor, State } from "./state";
import { WebAssemblyOperations } from "./wasm";

const stoppedOpactity = "0.5";

Expand Down Expand Up @@ -43,6 +44,7 @@ export class Board {
temperature: RangeSensor;
microphone: Microphone;
accelerometer: Accelerometer;
compass: Compass;
radio: Radio;

public serialInputBuffer: number[] = [];
Expand Down Expand Up @@ -89,6 +91,7 @@ export class Board {
this.audio = new Audio();
this.temperature = new RangeSensor("temperature", -5, 50, 21, "°C");
this.accelerometer = new Accelerometer(onChange);
this.compass = new Compass();
this.microphone = new Microphone(
this.svg.querySelector("#LitMicrophone")!,
onChange
Expand Down Expand Up @@ -124,6 +127,11 @@ export class Board {
accelerometerZ: this.accelerometer.state.accelerometerZ,
gesture: this.accelerometer.state.gesture,

compassX: this.compass.state.compassX,
compassY: this.compass.state.compassY,
compassZ: this.compass.state.compassZ,
compassHeading: this.compass.state.compassHeading,

lightLevel: this.display.lightLevel,
dataLogging: {
// Placeholder.
Expand All @@ -144,6 +152,13 @@ export class Board {
this.accelerometer.setValue(id, value);
break;
}
case "compassX":
case "compassY":
case "compassZ":
case "compassHeading": {
this.compass.setValue(id, value);
break;
}
case "buttonA": {
this.buttons[0].setValue(value);
break;
Expand Down Expand Up @@ -198,6 +213,7 @@ export class Board {
this.pins.forEach((p) => p.initialize());
this.display.initialize();
this.accelerometer.initialize(this.operations.gestureCallback!);
this.compass.initialize();
this.microphone.initialize(this.operations.soundLevelCallback!);
this.radio.initialize();
this.serialInputBuffer.length = 0;
Expand Down Expand Up @@ -292,6 +308,7 @@ export class Board {
this.pins.forEach((p) => p.dispose());
this.display.dispose();
this.accelerometer.dispose();
this.compass.dispose();
this.microphone.dispose();
this.radio.dispose();
this.serialInputBuffer.length = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/board/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface State {
accelerometerZ: RangeSensor;
gesture: EnumSensor;

compassX: RangeSensor;
compassY: RangeSensor;
compassZ: RangeSensor;
compassHeading: RangeSensor;

pin0: RangeSensor;
pin1: RangeSensor;
pin2: RangeSensor;
Expand Down
1 change: 1 addition & 0 deletions src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
<option value="audio">Audio</option>
<option value="background">Background music and display</option>
<option value="buttons">Buttons</option>
<option value="compass">Compass</option>
<option value="display">Display</option>
<option value="microphone">Microphone</option>
<option value="music">Music</option>
Expand Down
11 changes: 11 additions & 0 deletions src/examples/compass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Imports go at the top
from microbit import *

# Code in a 'while True:' loop repeats forever
while True:
print("x: ", compass.get_x())
print("y: ", compass.get_y())
print("z: ", compass.get_z())
print("heading: ", compass.heading())
print("field strength: ", compass.get_field_strength())
sleep(1000)
6 changes: 6 additions & 0 deletions src/jshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ int mp_js_hal_accelerometer_get_z(void);
int mp_js_hal_accelerometer_get_gesture(void);
void mp_js_hal_accelerometer_set_range(int r);

int mp_js_hal_compass_get_x(void);
int mp_js_hal_compass_get_y(void);
int mp_js_hal_compass_get_z(void);
int mp_js_hal_compass_get_field_strength(void);
int mp_js_hal_compass_get_heading(void);

void mp_js_hal_audio_set_volume(int value);
void mp_js_hal_audio_init(uint32_t sample_rate);
void mp_js_hal_audio_write_data(const uint8_t *buf, size_t num_samples);
Expand Down
20 changes: 20 additions & 0 deletions src/jshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ mergeInto(LibraryManager.library, {
board.accelerometer.setRange(r);
},

mp_js_hal_compass_get_x: function () {
return board.compass.state.compassX.value;
},

mp_js_hal_compass_get_y: function () {
return board.compass.state.compassY.value;
},

mp_js_hal_compass_get_z: function () {
return board.compass.state.compassZ.value;
},

mp_js_hal_compass_get_field_strength: function () {
return board.compass.getFieldStrength();
},

mp_js_hal_compass_get_heading: function () {
return board.compass.state.compassHeading.value;
},

mp_js_hal_audio_set_volume: function (value) {
board.audio.setVolume(value);
},
Expand Down
33 changes: 9 additions & 24 deletions src/microbithal_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,45 +337,30 @@ void microbit_hal_accelerometer_set_range(int r) {
}

int microbit_hal_compass_is_calibrated(void) {
/*
return uBit.compass.isCalibrated();
*/
return 0;
// Always calibrated in the simulator.
return 1;
}

void microbit_hal_compass_clear_calibration(void) {
/*
uBit.compass.clearCalibration();
*/
// No calibration to clear.
}

void microbit_hal_compass_calibrate(void) {
/*
uBit.compass.calibrate();
*/
// No calibration to set.
}

void microbit_hal_compass_get_sample(int axis[3]) {
/*
Sample3D sample = uBit.compass.getSample();
axis[0] = sample.x;
axis[1] = sample.y;
axis[2] = sample.z;
*/
axis[0] = mp_js_hal_compass_get_x();
axis[1] = mp_js_hal_compass_get_y();
axis[2] = mp_js_hal_compass_get_z();
}

int microbit_hal_compass_get_field_strength(void) {
/*
return uBit.compass.getFieldStrength();
*/
return 0;
return mp_js_hal_compass_get_field_strength();
}

int microbit_hal_compass_get_heading(void) {
/*
return uBit.compass.heading();
*/
return 0;
return mp_js_hal_compass_get_heading();
}

const uint8_t *microbit_hal_get_font_data(char c) {
Expand Down