Skip to content
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
43 changes: 38 additions & 5 deletions src/board/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,29 @@ import {
MICROBIT_HAL_PIN_P0,
MICROBIT_HAL_PIN_P1,
MICROBIT_HAL_PIN_P2,
MICROBIT_HAL_PIN_P3,
MICROBIT_HAL_PIN_P4,
MICROBIT_HAL_PIN_P5,
MICROBIT_HAL_PIN_P6,
MICROBIT_HAL_PIN_P7,
MICROBIT_HAL_PIN_P8,
MICROBIT_HAL_PIN_P9,
MICROBIT_HAL_PIN_P10,
MICROBIT_HAL_PIN_P11,
MICROBIT_HAL_PIN_P12,
MICROBIT_HAL_PIN_P13,
MICROBIT_HAL_PIN_P14,
MICROBIT_HAL_PIN_P15,
MICROBIT_HAL_PIN_P16,
MICROBIT_HAL_PIN_P19,
MICROBIT_HAL_PIN_P20,
} from "./constants";
import * as conversions from "./conversions";
import { DataLogging } from "./data-logging";
import { Display } from "./display";
import { FileSystem } from "./fs";
import { Microphone } from "./microphone";
import { Pin } from "./pins";
import { Pin, StubPin, TouchPin } from "./pins";
import { Radio } from "./radio";
import { RangeSensor, State } from "./state";
import { ModuleWrapper } from "./wasm";
Expand Down Expand Up @@ -121,17 +137,34 @@ export class Board {
),
];
this.pins = Array(33);
this.pins[MICROBIT_HAL_PIN_FACE] = new Pin(
this.pins[MICROBIT_HAL_PIN_FACE] = new TouchPin(
"pinLogo",
{
element: this.svg.querySelector("#Logo")!,
label: () => this.formattedMessage({ id: "touch-logo" }),
},
onChange
);
this.pins[MICROBIT_HAL_PIN_P0] = new Pin("pin0", null, onChange);
this.pins[MICROBIT_HAL_PIN_P1] = new Pin("pin1", null, onChange);
this.pins[MICROBIT_HAL_PIN_P2] = new Pin("pin2", null, onChange);
this.pins[MICROBIT_HAL_PIN_P0] = new TouchPin("pin0", null, onChange);
this.pins[MICROBIT_HAL_PIN_P1] = new TouchPin("pin1", null, onChange);
this.pins[MICROBIT_HAL_PIN_P2] = new TouchPin("pin2", null, onChange);
this.pins[MICROBIT_HAL_PIN_P3] = new StubPin("pin3");
this.pins[MICROBIT_HAL_PIN_P4] = new StubPin("pin4");
this.pins[MICROBIT_HAL_PIN_P5] = new StubPin("pin5");
this.pins[MICROBIT_HAL_PIN_P6] = new StubPin("pin6");
this.pins[MICROBIT_HAL_PIN_P7] = new StubPin("pin7");
this.pins[MICROBIT_HAL_PIN_P8] = new StubPin("pin8");
this.pins[MICROBIT_HAL_PIN_P9] = new StubPin("pin9");
this.pins[MICROBIT_HAL_PIN_P10] = new StubPin("pin10");
this.pins[MICROBIT_HAL_PIN_P11] = new StubPin("pin11");
this.pins[MICROBIT_HAL_PIN_P12] = new StubPin("pin12");
this.pins[MICROBIT_HAL_PIN_P13] = new StubPin("pin13");
this.pins[MICROBIT_HAL_PIN_P14] = new StubPin("pin14");
this.pins[MICROBIT_HAL_PIN_P15] = new StubPin("pin15");
this.pins[MICROBIT_HAL_PIN_P16] = new StubPin("pin16");
this.pins[MICROBIT_HAL_PIN_P19] = new StubPin("pin19");
this.pins[MICROBIT_HAL_PIN_P20] = new StubPin("pin20");

this.audio = new Audio();
this.temperature = new RangeSensor("temperature", -5, 50, 21, "°C");
this.accelerometer = new Accelerometer(onChange);
Expand Down
60 changes: 57 additions & 3 deletions src/board/pins.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
import { RangeSensor, State } from "./state";

export class Pin {
export interface Pin {
state: RangeSensor;

updateTranslations(): void;

setValue(value: any): void;

isTouched(): boolean;

boardStopped(): void;

setAnalogPeriodUs(period: number): number;

getAnalogPeriodUs(): number;
}

const initialAnalogPeriodUs = -1;

abstract class BasePin implements Pin {
state: RangeSensor;

// For now we just allow get/set of this value
// but don't do anything with it.
private analogPeriodUs: number = initialAnalogPeriodUs;

constructor(id: string) {
this.state = new RangeSensor(id, 0, 1, 0, undefined);
}

updateTranslations() {}

setValue(value: any): void {
this.state.setValue(value);
}

setAnalogPeriodUs(period: number) {
this.analogPeriodUs = period;
return 0;
}

getAnalogPeriodUs() {
return this.analogPeriodUs;
}

isTouched(): boolean {
return false;
}

boardStopped() {
this.analogPeriodUs = initialAnalogPeriodUs;
}
}

export class StubPin extends BasePin {}

export class TouchPin extends BasePin {
private _mouseDown: boolean = false;

private keyListener: (e: KeyboardEvent) => void;
Expand All @@ -16,7 +69,7 @@ export class Pin {
private ui: { element: SVGElement; label: () => string } | null,
private onChange: (changes: Partial<State>) => void
) {
this.state = new RangeSensor(id, 0, 1, 0, undefined);
super(id);

if (this.ui) {
const { element, label } = this.ui;
Expand Down Expand Up @@ -76,7 +129,8 @@ export class Pin {
}

private setValueInternal(value: any, internalChange: boolean) {
this.state.setValue(value);
super.setValue(value);

if (internalChange) {
this.onChange({
[this.id]: this.state,
Expand Down
2 changes: 2 additions & 0 deletions src/jshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ int mp_js_hal_button_get_presses(int button);
bool mp_js_hal_button_is_pressed(int button);

bool mp_js_hal_pin_is_touched(int pin);
int mp_js_hal_pin_get_analog_period_us(int pin);
int mp_js_hal_pin_set_analog_period_us(int pin, int period);

int mp_js_hal_display_get_pixel(int x, int y);
void mp_js_hal_display_set_pixel(int x, int y, int value);
Expand Down
8 changes: 8 additions & 0 deletions src/jshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ mergeInto(LibraryManager.library, {
return Module.board.pins[pin].isTouched();
},

mp_js_hal_pin_get_analog_period_us: function (/** @type {number} */ pin) {
return Module.board.pins[pin].getAnalogPeriodUs();
},

mp_js_hal_pin_set_analog_period_us: function (/** @type {number} */ pin, /** @type {number} */ period) {
return Module.board.pins[pin].setAnalogPeriodUs(period);
},

mp_js_hal_display_get_pixel: function (
/** @type {number} */ x,
/** @type {number} */ y
Expand Down
30 changes: 2 additions & 28 deletions src/microbithal_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,37 +113,11 @@ int microbit_hal_pin_set_analog_period_us(int pin, int period) {
mp_js_hal_audio_period_us(period);
return 0;
}

/*
// Calling setAnalogPeriodUs requires the pin to be in analog-out mode. So
// test for this mode by first calling getAnalogPeriodUs, and if it fails then
// attempt to configure the pin in analog-out mode by calling setAnalogValue.
if ((ErrorCode)pin_obj[pin]->getAnalogPeriodUs() == DEVICE_NOT_SUPPORTED) {
if (pin_obj[pin]->setAnalogValue(0) != DEVICE_OK) {
return -1;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No matter what I do I can't get this or the error branch below to trigger on a V2 device. So for now, the simulator calls always succeed.

@dpgeorge do you know how I can trigger these error branches?

Link to easier to read version: https://github.com/microbit-foundation/micropython-microbit-v2/blob/e4321a866e3f8ef48a7f5b5e96db99cc39b09757/src/codal_app/microbithal.cpp#L155

}
}

// Set the analog period.
if (pin_obj[pin]->setAnalogPeriodUs(period) == DEVICE_OK) {
return 0;
} else {
return -1;
}
*/
return -1;
return mp_js_hal_pin_set_analog_period_us(pin, period);
}

int microbit_hal_pin_get_analog_period_us(int pin) {
/*
int period = pin_obj[pin]->getAnalogPeriodUs();
if (period != DEVICE_NOT_SUPPORTED) {
return period;
} else {
return -1;
}
*/
return -1;
return mp_js_hal_pin_get_analog_period_us(pin);
}

void microbit_hal_pin_set_touch_mode(int pin, int mode) {
Expand Down