Skip to content

Commit 9480205

Browse files
Clean up initialization.
It's now just about callback updates when the module changes.
1 parent 6a9f051 commit 9480205

File tree

6 files changed

+19
-18
lines changed

6 files changed

+19
-18
lines changed

src/board/accelerometer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export class Accelerometer {
4848

4949
setValue(id: StateKeys, value: any) {
5050
this.state[id].setValue(value);
51-
if (id === "gesture") {
52-
this.gestureCallback!(
51+
if (id === "gesture" && this.gestureCallback) {
52+
this.gestureCallback(
5353
convertAccelerometerStringToNumber(this.state.gesture.value)
5454
);
5555
}
@@ -71,7 +71,7 @@ export class Accelerometer {
7171
});
7272
}
7373

74-
initialize(gestureCallback: GestureCallback) {
74+
initializeCallbacks(gestureCallback: GestureCallback) {
7575
this.gestureCallback = gestureCallback;
7676
}
7777

src/board/audio/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export class Audio {
2323

2424
constructor() {}
2525

26-
initialize({ defaultAudioCallback, speechAudioCallback }: AudioOptions) {
26+
initializeCallbacks({
27+
defaultAudioCallback,
28+
speechAudioCallback,
29+
}: AudioOptions) {
2730
this.context = new AudioContext({
2831
// The highest rate is the sound expression synth.
2932
sampleRate: 44100,

src/board/buttons.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ export class Button {
115115
return result;
116116
}
117117

118-
initialize() {}
119-
120118
dispose() {
121119
this._presses = 0;
122120
}

src/board/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ export class Board {
169169
noInitialRun: true,
170170
});
171171
const module = new ModuleWrapper(wrapped);
172-
this.audio.initialize({
172+
this.audio.initializeCallbacks({
173173
defaultAudioCallback: wrapped._microbit_hal_audio_ready_callback,
174174
speechAudioCallback: wrapped._microbit_hal_audio_speech_ready_callback,
175175
});
176-
this.buttons.forEach((b) => b.initialize());
177-
this.pins.forEach((p) => p.initialize());
178-
this.accelerometer.initialize(wrapped._microbit_hal_gesture_callback);
179-
this.microphone.initialize(
176+
this.accelerometer.initializeCallbacks(
177+
wrapped._microbit_hal_gesture_callback
178+
);
179+
this.microphone.initializeCallbacks(
180180
wrapped._microbit_hal_audio_speech_ready_callback
181181
);
182182
return module;

src/board/microphone.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,16 @@ export class Microphone {
5050

5151
const low = this.soundLevel.lowThreshold!;
5252
const high = this.soundLevel.highThreshold!;
53-
if (prev > low && curr <= low) {
54-
this.soundLevelCallback!(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW);
55-
} else if (prev < high && curr >= high!) {
56-
this.soundLevelCallback!(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH);
53+
if (this.soundLevelCallback) {
54+
if (prev > low && curr <= low) {
55+
this.soundLevelCallback(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW);
56+
} else if (prev < high && curr >= high!) {
57+
this.soundLevelCallback(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH);
58+
}
5759
}
5860
}
5961

60-
initialize(soundLevelCallback: (v: number) => void) {
62+
initializeCallbacks(soundLevelCallback: (v: number) => void) {
6163
this.soundLevelCallback = soundLevelCallback;
6264
}
6365

src/board/pins.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,5 @@ export class Pin {
113113
}
114114
}
115115

116-
initialize() {}
117-
118116
dispose() {}
119117
}

0 commit comments

Comments
 (0)