Skip to content

Commit fdbbcf1

Browse files
First pass updates.
I've skipped to latest master as it has changes that require source level updates here that I expect to be in the 2.1 release. - Add new sound source files - Update conversions to use new HAL constants - Update sound expression HAL for function name change Largely untested at this point but it builds.
1 parent 56fb38b commit fdbbcf1

File tree

8 files changed

+34
-29
lines changed

8 files changed

+34
-29
lines changed

src/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ SRC_C += \
5959
main.c \
6060
mphalport.c \
6161

62+
# Upgrading?
63+
# Compare to lib/micropython-microbit-v2/src/codal_port/Makefile
6264
SRC_C += $(addprefix $(CODAL_PORT)/, \
6365
drv_display.c \
6466
drv_image.c \
@@ -78,6 +80,7 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
7880
microbit_pinaudio.c \
7981
microbit_pinmode.c \
8082
microbit_sound.c \
83+
microbit_soundeffect.c \
8184
microbit_soundevent.c \
8285
microbit_speaker.c \
8386
microbit_spi.c \
@@ -91,6 +94,7 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
9194
modmusic.c \
9295
modmusictunes.c \
9396
modos.c \
97+
modpower.c \
9498
modradio.c \
9599
modspeech.c \
96100
modthis.c \

src/board/constants.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ export const MICROBIT_HAL_ACCELEROMETER_EVT_8G = 10;
5353
export const MICROBIT_HAL_ACCELEROMETER_EVT_SHAKE = 11;
5454
export const MICROBIT_HAL_ACCELEROMETER_EVT_2G = 12;
5555

56-
export const MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW = 1;
57-
export const MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH = 2;
56+
// Microphone events, passed to microbit_hal_level_detector_callback().
57+
export const MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW = 1;
58+
export const MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH = 2;
59+
60+
// Threshold kind, passed to microbit_hal_microphone_set_threshold().
61+
export const MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW = 0;
62+
export const MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH = 1;
5863

5964
export const MICROBIT_HAL_LOG_TIMESTAMP_NONE = 0;
6065
export const MICROBIT_HAL_LOG_TIMESTAMP_MILLISECONDS = 1;

src/board/conversions.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {
1212
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_LEFT,
1313
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_RIGHT,
1414
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_UP,
15-
MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH,
16-
MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW,
15+
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH,
16+
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW,
17+
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH,
18+
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW,
1719
} from "./constants";
1820

1921
export function convertAudioBuffer(source: number, target: AudioBuffer) {
@@ -26,23 +28,14 @@ export function convertAudioBuffer(source: number, target: AudioBuffer) {
2628
return target;
2729
}
2830

29-
export function convertSoundEventStringToNumber(value: "low" | "high"): number {
31+
export function convertStringToSoundThresoldNumber(
32+
value: "low" | "high"
33+
): number {
3034
switch (value) {
3135
case "low":
32-
return MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW;
36+
return MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW;
3337
case "high":
34-
return MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH;
35-
default:
36-
throw new Error(`Invalid value ${value}`);
37-
}
38-
}
39-
40-
export function convertSoundEventNumberToString(value: number): "low" | "high" {
41-
switch (value) {
42-
case MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW:
43-
return "low";
44-
case MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH:
45-
return "high";
38+
return MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH;
4639
default:
4740
throw new Error(`Invalid value ${value}`);
4841
}

src/board/microphone.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { convertSoundEventStringToNumber } from "./conversions";
1+
import {
2+
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH,
3+
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW,
4+
} from "./constants";
5+
import { convertStringToSoundEventNumber } from "./conversions";
26
import { RangeSensor, State } from "./state";
37

48
type SoundLevelCallback = (v: number) => void;
@@ -48,9 +52,9 @@ export class Microphone {
4852
const low = this.soundLevel.lowThreshold!;
4953
const high = this.soundLevel.highThreshold!;
5054
if (prev > low && curr <= low) {
51-
this.soundLevelCallback!(convertSoundEventStringToNumber("low"));
55+
this.soundLevelCallback!(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW);
5256
} else if (prev < high && curr >= high!) {
53-
this.soundLevelCallback!(convertSoundEventStringToNumber("high"));
57+
this.soundLevelCallback!(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH);
5458
}
5559
}
5660

src/jshal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void mp_js_hal_audio_speech_init(uint32_t sample_rate);
7373
void mp_js_hal_audio_speech_write_data(const uint8_t *buf, size_t num_samples);
7474
void mp_js_hal_audio_period_us(int period);
7575
void mp_js_hal_audio_amplitude_u10(int amplitude);
76-
void mp_js_hal_audio_play_expression_by_name(const char *name);
76+
void mp_js_hal_audio_play_expression(const char *name);
7777
void mp_js_hal_audio_stop_expression(void);
7878
bool mp_js_hal_audio_is_expression_active(void);
7979

src/jshal.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ mergeInto(LibraryManager.library, {
207207

208208
mp_js_hal_microphone_set_threshold: function (kind, value) {
209209
board.microphone.setThreshold(
210-
// `+ 1` is temporary, see https://github.com/microbit-foundation/micropython-microbit-v2/pull/109
211-
conversions.convertSoundEventNumberToString(kind + 1),
210+
conversions.convertStringToSoundThresoldNumber(kind),
212211
value
213212
);
214213
},
@@ -217,8 +216,8 @@ mergeInto(LibraryManager.library, {
217216
return board.microphone.soundLevel.value;
218217
},
219218

220-
mp_js_hal_audio_play_expression_by_name: function (name) {
221-
return board.audio.playSoundExpression(UTF8ToString(name));
219+
mp_js_hal_audio_play_expression: function (data) {
220+
return board.audio.playSoundExpression(UTF8ToString(data));
222221
},
223222

224223
mp_js_hal_audio_stop_expression: function () {

src/microbithal_js.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ bool microbit_hal_audio_is_expression_active(void) {
431431
return mp_js_hal_audio_is_expression_active();
432432
}
433433

434-
void microbit_hal_audio_play_expression_by_name(const char *name) {
435-
mp_js_hal_audio_play_expression_by_name(name);
434+
void microbit_hal_audio_play_expression(const char *name) {
435+
mp_js_hal_audio_play_expression(name);
436436
}
437437

438438
void microbit_hal_audio_stop_expression(void) {

0 commit comments

Comments
 (0)