Skip to content

Commit 9955951

Browse files
Support get/set of analog period.
1 parent 748657e commit 9955951

File tree

5 files changed

+82
-20
lines changed

5 files changed

+82
-20
lines changed

src/board/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import {
88
MICROBIT_HAL_PIN_P0,
99
MICROBIT_HAL_PIN_P1,
1010
MICROBIT_HAL_PIN_P2,
11+
MICROBIT_HAL_PIN_P3,
1112
} from "./constants";
1213
import * as conversions from "./conversions";
1314
import { DataLogging } from "./data-logging";
1415
import { Display } from "./display";
1516
import { FileSystem } from "./fs";
1617
import { Microphone } from "./microphone";
17-
import { Pin } from "./pins";
18+
import { Pin, StubPin, TouchPin } from "./pins";
1819
import { Radio } from "./radio";
1920
import { RangeSensor, State } from "./state";
2021
import { ModuleWrapper } from "./wasm";
@@ -121,17 +122,24 @@ export class Board {
121122
),
122123
];
123124
this.pins = Array(33);
124-
this.pins[MICROBIT_HAL_PIN_FACE] = new Pin(
125+
this.pins[MICROBIT_HAL_PIN_FACE] = new TouchPin(
125126
"pinLogo",
126127
{
127128
element: this.svg.querySelector("#Logo")!,
128129
label: () => this.formattedMessage({ id: "touch-logo" }),
129130
},
130131
onChange
131132
);
132-
this.pins[MICROBIT_HAL_PIN_P0] = new Pin("pin0", null, onChange);
133-
this.pins[MICROBIT_HAL_PIN_P1] = new Pin("pin1", null, onChange);
134-
this.pins[MICROBIT_HAL_PIN_P2] = new Pin("pin2", null, onChange);
133+
this.pins[MICROBIT_HAL_PIN_P0] = new TouchPin("pin0", null, onChange);
134+
this.pins[MICROBIT_HAL_PIN_P1] = new TouchPin("pin1", null, onChange);
135+
this.pins[MICROBIT_HAL_PIN_P2] = new TouchPin("pin2", null, onChange);
136+
for (let pin = 3; pin <= 19; ++pin) {
137+
if (pin === 17 || pin === 18) {
138+
continue;
139+
}
140+
this.pins[pin] = new StubPin(`pin${pin}`);
141+
}
142+
135143
this.audio = new Audio();
136144
this.temperature = new RangeSensor("temperature", -5, 50, 21, "°C");
137145
this.accelerometer = new Accelerometer(onChange);

src/board/pins.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,61 @@
11
import { RangeSensor, State } from "./state";
22

3-
export class Pin {
3+
export interface Pin {
44
state: RangeSensor;
55

6+
updateTranslations(): void;
7+
8+
setValue(value: any): void;
9+
10+
isTouched(): boolean;
11+
12+
boardStopped(): void;
13+
14+
setAnalogPeriodUs(period: number): number;
15+
16+
getAnalogPeriodUs(): number;
17+
18+
}
19+
20+
abstract class BasePin implements Pin {
21+
state: RangeSensor;
22+
23+
// For now we just allow get/set of this value
24+
// but don't do anything with it.
25+
private analogPeriodUs: number = -1;
26+
27+
constructor(id: string) {
28+
this.state = new RangeSensor(id, 0, 1, 0, undefined)
29+
}
30+
31+
updateTranslations() {
32+
}
33+
34+
setValue(value: any): void {
35+
this.state.value = value;
36+
}
37+
38+
setAnalogPeriodUs(period: number) {
39+
this.analogPeriodUs = period;
40+
return 0;
41+
}
42+
43+
getAnalogPeriodUs() {
44+
return this.analogPeriodUs;
45+
}
46+
47+
isTouched(): boolean {
48+
return false;
49+
}
50+
51+
boardStopped() {
52+
}
53+
}
54+
55+
export class StubPin extends BasePin {
56+
}
57+
58+
export class TouchPin extends BasePin {
659
private _mouseDown: boolean = false;
760

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

2174
if (this.ui) {
2275
const { element, label } = this.ui;
@@ -76,7 +129,8 @@ export class Pin {
76129
}
77130

78131
private setValueInternal(value: any, internalChange: boolean) {
79-
this.state.setValue(value);
132+
super.setValue(value);
133+
80134
if (internalChange) {
81135
this.onChange({
82136
[this.id]: this.state,

src/jshal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ int mp_js_hal_button_get_presses(int button);
4848
bool mp_js_hal_button_is_pressed(int button);
4949

5050
bool mp_js_hal_pin_is_touched(int pin);
51+
int mp_js_hal_pin_get_analog_period_us(int pin);
52+
int mp_js_hal_pin_set_analog_period_us(int pin, int period);
5153

5254
int mp_js_hal_display_get_pixel(int x, int y);
5355
void mp_js_hal_display_set_pixel(int x, int y, int value);

src/jshal.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ mergeInto(LibraryManager.library, {
127127
return Module.board.pins[pin].isTouched();
128128
},
129129

130+
mp_js_hal_pin_get_analog_period_us: function (/** @type {number} */ pin) {
131+
return Module.board.pins[pin].getAnalogPeriodUs();
132+
},
133+
134+
mp_js_hal_pin_set_analog_period_us: function (/** @type {number} */ pin, /** @type {number} */ period) {
135+
return Module.board.pins[pin].setAnalogPeriodUs(period);
136+
},
137+
130138
mp_js_hal_display_get_pixel: function (
131139
/** @type {number} */ x,
132140
/** @type {number} */ y

src/microbithal_js.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,11 @@ int microbit_hal_pin_set_analog_period_us(int pin, int period) {
113113
mp_js_hal_audio_period_us(period);
114114
return 0;
115115
}
116-
// We don't support other pins yet but more practical to claim success as
117-
// this allows programs using servos to otherwise function.
118-
return 0;
116+
return mp_js_hal_pin_set_analog_period_us(pin, period);
119117
}
120118

121119
int microbit_hal_pin_get_analog_period_us(int pin) {
122-
/*
123-
int period = pin_obj[pin]->getAnalogPeriodUs();
124-
if (period != DEVICE_NOT_SUPPORTED) {
125-
return period;
126-
} else {
127-
return -1;
128-
}
129-
*/
130-
return -1;
120+
return mp_js_hal_pin_get_analog_period_us(pin);
131121
}
132122

133123
void microbit_hal_pin_set_touch_mode(int pin, int mode) {

0 commit comments

Comments
 (0)