Skip to content

Commit 61ed74a

Browse files
Upgrade MicroPython (#47)
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 and power source files - Update conversions to use new HAL constants - Update sound expression HAL for function name change - Stub the power HAL for now (all void returns) - Update config to add power module
1 parent 56fb38b commit 61ed74a

File tree

14 files changed

+80
-32
lines changed

14 files changed

+80
-32
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ Otherwise, the user will need to use <code>radio.receive_bytes</code> or <code>r
268268

269269
</table>
270270

271+
## Upgrading micropython-microbit-v2
272+
273+
1. Update the lib/micropython-microbit-v2 to the relevant hash. Make sure that its lib/micropython submodule is updated (see checkout instructions above).
274+
2. Review the full diff for micropython-microbit-v2. In particular, note changes to:
275+
1. main.c, src/Makefile and mpconfigport.h all which have simulator versions that may need updates
276+
2. the HAL, which may require implementing in the simulator
277+
3. the filesystem, which has a JavaScript implementation.
278+
271279
## Web Assembly debugging
272280

273281
Steps for WASM debugging in Chrome:

src/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
7878
microbit_pinaudio.c \
7979
microbit_pinmode.c \
8080
microbit_sound.c \
81+
microbit_soundeffect.c \
8182
microbit_soundevent.c \
8283
microbit_speaker.c \
8384
microbit_spi.c \
@@ -91,6 +92,7 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
9192
modmusic.c \
9293
modmusictunes.c \
9394
modos.c \
95+
modpower.c \
9496
modradio.c \
9597
modspeech.c \
9698
modthis.c \

src/board/audio/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export class Audio {
5959
);
6060
}
6161

62-
playSoundExpression(expression: string) {
63-
const soundEffects = parseSoundEffects(replaceBuiltinSound(expression));
62+
playSoundExpression(expr: string) {
63+
const soundEffects = parseSoundEffects(replaceBuiltinSound(expr));
6464
const onDone = () => {
6565
this.stopSoundExpression();
6666
};

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,22 +28,13 @@ export function convertAudioBuffer(source: number, target: AudioBuffer) {
2628
return target;
2729
}
2830

29-
export function convertSoundEventStringToNumber(value: "low" | "high"): number {
31+
export function convertSoundThresholdNumberToString(
32+
value: number
33+
): "low" | "high" {
3034
switch (value) {
31-
case "low":
32-
return MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW;
33-
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:
35+
case MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW:
4336
return "low";
44-
case MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH:
37+
case MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH:
4538
return "high";
4639
default:
4740
throw new Error(`Invalid value ${value}`);

src/board/microphone.ts

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

47
type SoundLevelCallback = (v: number) => void;
@@ -48,9 +51,9 @@ export class Microphone {
4851
const low = this.soundLevel.lowThreshold!;
4952
const high = this.soundLevel.highThreshold!;
5053
if (prev > low && curr <= low) {
51-
this.soundLevelCallback!(convertSoundEventStringToNumber("low"));
54+
this.soundLevelCallback!(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW);
5255
} else if (prev < high && curr >= high!) {
53-
this.soundLevelCallback!(convertSoundEventStringToNumber("high"));
56+
this.soundLevelCallback!(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH);
5457
}
5558
}
5659

src/demo.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
9292
<option value="pin_logo">Pin logo</option>
9393
<option value="radio">Radio</option>
9494
<option value="sensors">Sensors</option>
95-
<option value="sound_effects">Sound effects</option>
95+
<option value="sound_effects_builtin">Sound effects (builtin)</option>
96+
<option value="sound_effects_user">Sound effects (user)</option>
9697
<option value="speech">Speech</option>
9798
<option value="volume">Volume</option>
9899
</select>
File renamed without changes.

src/examples/sound_effects_user.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from microbit import *
2+
3+
# Create a Sound Effect and immediately play it
4+
audio.play(
5+
audio.SoundEffect(
6+
freq_start=400,
7+
freq_end=2000,
8+
duration=500,
9+
vol_start=100,
10+
vol_end=255,
11+
wave=audio.SoundEffect.WAVE_TRIANGLE,
12+
fx=audio.SoundEffect.FX_VIBRATO,
13+
shape=audio.SoundEffect.SHAPE_LOG,
14+
)
15+
)

0 commit comments

Comments
 (0)