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

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+
)

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.convertSoundThresholdNumberToString(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 (expr) {
220+
return board.audio.playSoundExpression(UTF8ToString(expr));
222221
},
223222

224223
mp_js_hal_audio_stop_expression: function () {

src/microbithal_js.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@ int microbit_hal_temperature(void) {
7676
return mp_js_hal_temperature();
7777
}
7878

79+
void microbit_hal_power_clear_wake_sources(void) {
80+
// Stub, unsupported.
81+
}
82+
83+
void microbit_hal_power_wake_on_button(int button, bool wake_on_active) {
84+
// Stub, unsupported.
85+
}
86+
87+
void microbit_hal_power_wake_on_pin(int pin, bool wake_on_active) {
88+
// Stub, unsupported.
89+
}
90+
91+
void microbit_hal_power_off(void) {
92+
// Stub, unsupported.
93+
}
94+
95+
void microbit_hal_power_deep_sleep(bool wake_on_ms, uint32_t ms) {
96+
// Stub, unsupported.
97+
}
98+
7999
void microbit_hal_pin_set_pull(int pin, int pull) {
80100
//pin_obj[pin]->setPull(pin_pull_mode_mapping[pull]);
81101
//pin_pull_state[pin] = pull;
@@ -431,8 +451,8 @@ bool microbit_hal_audio_is_expression_active(void) {
431451
return mp_js_hal_audio_is_expression_active();
432452
}
433453

434-
void microbit_hal_audio_play_expression_by_name(const char *name) {
435-
mp_js_hal_audio_play_expression_by_name(name);
454+
void microbit_hal_audio_play_expression(const char *expr) {
455+
mp_js_hal_audio_play_expression(expr);
436456
}
437457

438458
void microbit_hal_audio_stop_expression(void) {

src/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ extern const struct _mp_obj_module_t machine_module;
115115
extern const struct _mp_obj_module_t microbit_module;
116116
extern const struct _mp_obj_module_t music_module;
117117
extern const struct _mp_obj_module_t os_module;
118+
extern const struct _mp_obj_module_t power_module;
118119
extern const struct _mp_obj_module_t radio_module;
119120
extern const struct _mp_obj_module_t speech_module;
120121
extern const struct _mp_obj_module_t this_module;
@@ -129,6 +130,7 @@ extern const struct _mp_obj_module_t utime_module;
129130
{ MP_ROM_QSTR(MP_QSTR_microbit), MP_ROM_PTR(&microbit_module) }, \
130131
{ MP_ROM_QSTR(MP_QSTR_music), MP_ROM_PTR(&music_module) }, \
131132
{ MP_ROM_QSTR(MP_QSTR_os), MP_ROM_PTR(&os_module) }, \
133+
{ MP_ROM_QSTR(MP_QSTR_power), MP_ROM_PTR(&power_module) }, \
132134
{ MP_ROM_QSTR(MP_QSTR_radio), MP_ROM_PTR(&radio_module) }, \
133135
{ MP_ROM_QSTR(MP_QSTR_speech), MP_ROM_PTR(&speech_module) }, \
134136
{ MP_ROM_QSTR(MP_QSTR_this), MP_ROM_PTR(&this_module) }, \

0 commit comments

Comments
 (0)