Skip to content

Commit b9f4557

Browse files
Allow setting analog period on pins (#79)
Improve pin stubs so servo code doesn't explode. See #76
1 parent 066736c commit b9f4557

File tree

5 files changed

+107
-36
lines changed

5 files changed

+107
-36
lines changed

src/board/index.ts

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,29 @@ import {
88
MICROBIT_HAL_PIN_P0,
99
MICROBIT_HAL_PIN_P1,
1010
MICROBIT_HAL_PIN_P2,
11+
MICROBIT_HAL_PIN_P3,
12+
MICROBIT_HAL_PIN_P4,
13+
MICROBIT_HAL_PIN_P5,
14+
MICROBIT_HAL_PIN_P6,
15+
MICROBIT_HAL_PIN_P7,
16+
MICROBIT_HAL_PIN_P8,
17+
MICROBIT_HAL_PIN_P9,
18+
MICROBIT_HAL_PIN_P10,
19+
MICROBIT_HAL_PIN_P11,
20+
MICROBIT_HAL_PIN_P12,
21+
MICROBIT_HAL_PIN_P13,
22+
MICROBIT_HAL_PIN_P14,
23+
MICROBIT_HAL_PIN_P15,
24+
MICROBIT_HAL_PIN_P16,
25+
MICROBIT_HAL_PIN_P19,
26+
MICROBIT_HAL_PIN_P20,
1127
} from "./constants";
1228
import * as conversions from "./conversions";
1329
import { DataLogging } from "./data-logging";
1430
import { Display } from "./display";
1531
import { FileSystem } from "./fs";
1632
import { Microphone } from "./microphone";
17-
import { Pin } from "./pins";
33+
import { Pin, StubPin, TouchPin } from "./pins";
1834
import { Radio } from "./radio";
1935
import { RangeSensor, State } from "./state";
2036
import { ModuleWrapper } from "./wasm";
@@ -121,17 +137,34 @@ export class Board {
121137
),
122138
];
123139
this.pins = Array(33);
124-
this.pins[MICROBIT_HAL_PIN_FACE] = new Pin(
140+
this.pins[MICROBIT_HAL_PIN_FACE] = new TouchPin(
125141
"pinLogo",
126142
{
127143
element: this.svg.querySelector("#Logo")!,
128144
label: () => this.formattedMessage({ id: "touch-logo" }),
129145
},
130146
onChange
131147
);
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);
148+
this.pins[MICROBIT_HAL_PIN_P0] = new TouchPin("pin0", null, onChange);
149+
this.pins[MICROBIT_HAL_PIN_P1] = new TouchPin("pin1", null, onChange);
150+
this.pins[MICROBIT_HAL_PIN_P2] = new TouchPin("pin2", null, onChange);
151+
this.pins[MICROBIT_HAL_PIN_P3] = new StubPin("pin3");
152+
this.pins[MICROBIT_HAL_PIN_P4] = new StubPin("pin4");
153+
this.pins[MICROBIT_HAL_PIN_P5] = new StubPin("pin5");
154+
this.pins[MICROBIT_HAL_PIN_P6] = new StubPin("pin6");
155+
this.pins[MICROBIT_HAL_PIN_P7] = new StubPin("pin7");
156+
this.pins[MICROBIT_HAL_PIN_P8] = new StubPin("pin8");
157+
this.pins[MICROBIT_HAL_PIN_P9] = new StubPin("pin9");
158+
this.pins[MICROBIT_HAL_PIN_P10] = new StubPin("pin10");
159+
this.pins[MICROBIT_HAL_PIN_P11] = new StubPin("pin11");
160+
this.pins[MICROBIT_HAL_PIN_P12] = new StubPin("pin12");
161+
this.pins[MICROBIT_HAL_PIN_P13] = new StubPin("pin13");
162+
this.pins[MICROBIT_HAL_PIN_P14] = new StubPin("pin14");
163+
this.pins[MICROBIT_HAL_PIN_P15] = new StubPin("pin15");
164+
this.pins[MICROBIT_HAL_PIN_P16] = new StubPin("pin16");
165+
this.pins[MICROBIT_HAL_PIN_P19] = new StubPin("pin19");
166+
this.pins[MICROBIT_HAL_PIN_P20] = new StubPin("pin20");
167+
135168
this.audio = new Audio();
136169
this.temperature = new RangeSensor("temperature", -5, 50, 21, "°C");
137170
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+
const initialAnalogPeriodUs = -1;
20+
21+
abstract class BasePin implements Pin {
22+
state: RangeSensor;
23+
24+
// For now we just allow get/set of this value
25+
// but don't do anything with it.
26+
private analogPeriodUs: number = initialAnalogPeriodUs;
27+
28+
constructor(id: string) {
29+
this.state = new RangeSensor(id, 0, 1, 0, undefined);
30+
}
31+
32+
updateTranslations() {}
33+
34+
setValue(value: any): void {
35+
this.state.setValue(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+
this.analogPeriodUs = initialAnalogPeriodUs;
53+
}
54+
}
55+
56+
export class StubPin extends BasePin {}
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 & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -113,37 +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-
117-
/*
118-
// Calling setAnalogPeriodUs requires the pin to be in analog-out mode. So
119-
// test for this mode by first calling getAnalogPeriodUs, and if it fails then
120-
// attempt to configure the pin in analog-out mode by calling setAnalogValue.
121-
if ((ErrorCode)pin_obj[pin]->getAnalogPeriodUs() == DEVICE_NOT_SUPPORTED) {
122-
if (pin_obj[pin]->setAnalogValue(0) != DEVICE_OK) {
123-
return -1;
124-
}
125-
}
126-
127-
// Set the analog period.
128-
if (pin_obj[pin]->setAnalogPeriodUs(period) == DEVICE_OK) {
129-
return 0;
130-
} else {
131-
return -1;
132-
}
133-
*/
134-
return -1;
116+
return mp_js_hal_pin_set_analog_period_us(pin, period);
135117
}
136118

137119
int microbit_hal_pin_get_analog_period_us(int pin) {
138-
/*
139-
int period = pin_obj[pin]->getAnalogPeriodUs();
140-
if (period != DEVICE_NOT_SUPPORTED) {
141-
return period;
142-
} else {
143-
return -1;
144-
}
145-
*/
146-
return -1;
120+
return mp_js_hal_pin_get_analog_period_us(pin);
147121
}
148122

149123
void microbit_hal_pin_set_touch_mode(int pin, int mode) {

0 commit comments

Comments
 (0)