Skip to content

Upgrade MicroPython #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ Otherwise, the user will need to use <code>radio.receive_bytes</code> or <code>r

</table>

## Upgrading micropython-microbit-v2

1. Update the lib/micropython-microbit-v2 to the relevant hash. Make sure that its lib/micropython submodule is updated (see checkout instructions above).
2. Review the full diff for micropython-microbit-v2. In particular, note changes to:
1. main.c, src/Makefile and mpconfigport.h all which have simulator versions that may need updates
2. the HAL, which may require implementing in the simulator
3. the filesystem, which has a JavaScript implementation.

## Web Assembly debugging

Steps for WASM debugging in Chrome:
Expand Down
2 changes: 2 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
microbit_pinaudio.c \
microbit_pinmode.c \
microbit_sound.c \
microbit_soundeffect.c \
microbit_soundevent.c \
microbit_speaker.c \
microbit_spi.c \
Expand All @@ -91,6 +92,7 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
modmusic.c \
modmusictunes.c \
modos.c \
modpower.c \
modradio.c \
modspeech.c \
modthis.c \
Expand Down
4 changes: 2 additions & 2 deletions src/board/audio/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export class Audio {
);
}

playSoundExpression(expression: string) {
const soundEffects = parseSoundEffects(replaceBuiltinSound(expression));
playSoundExpression(expr: string) {
const soundEffects = parseSoundEffects(replaceBuiltinSound(expr));
const onDone = () => {
this.stopSoundExpression();
};
Expand Down
9 changes: 7 additions & 2 deletions src/board/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ export const MICROBIT_HAL_ACCELEROMETER_EVT_8G = 10;
export const MICROBIT_HAL_ACCELEROMETER_EVT_SHAKE = 11;
export const MICROBIT_HAL_ACCELEROMETER_EVT_2G = 12;

export const MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW = 1;
export const MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH = 2;
// Microphone events, passed to microbit_hal_level_detector_callback().
export const MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW = 1;
export const MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH = 2;

// Threshold kind, passed to microbit_hal_microphone_set_threshold().
export const MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW = 0;
export const MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH = 1;

export const MICROBIT_HAL_LOG_TIMESTAMP_NONE = 0;
export const MICROBIT_HAL_LOG_TIMESTAMP_MILLISECONDS = 1;
Expand Down
25 changes: 9 additions & 16 deletions src/board/conversions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_LEFT,
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_RIGHT,
MICROBIT_HAL_ACCELEROMETER_EVT_TILT_UP,
MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH,
MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW,
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH,
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW,
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH,
MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW,
} from "./constants";

export function convertAudioBuffer(source: number, target: AudioBuffer) {
Expand All @@ -26,22 +28,13 @@ export function convertAudioBuffer(source: number, target: AudioBuffer) {
return target;
}

export function convertSoundEventStringToNumber(value: "low" | "high"): number {
export function convertSoundThresholdNumberToString(
value: number
): "low" | "high" {
switch (value) {
case "low":
return MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW;
case "high":
return MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH;
default:
throw new Error(`Invalid value ${value}`);
}
}

export function convertSoundEventNumberToString(value: number): "low" | "high" {
switch (value) {
case MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_LOW:
case MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_LOW:
return "low";
case MICROBIT_HAL_MICROPHONE_LEVEL_THRESHOLD_HIGH:
case MICROBIT_HAL_MICROPHONE_SET_THRESHOLD_HIGH:
return "high";
default:
throw new Error(`Invalid value ${value}`);
Expand Down
9 changes: 6 additions & 3 deletions src/board/microphone.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { convertSoundEventStringToNumber } from "./conversions";
import {
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH,
MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW,
} from "./constants";
import { RangeSensor, State } from "./state";

type SoundLevelCallback = (v: number) => void;
Expand Down Expand Up @@ -48,9 +51,9 @@ export class Microphone {
const low = this.soundLevel.lowThreshold!;
const high = this.soundLevel.highThreshold!;
if (prev > low && curr <= low) {
this.soundLevelCallback!(convertSoundEventStringToNumber("low"));
this.soundLevelCallback!(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_LOW);
} else if (prev < high && curr >= high!) {
this.soundLevelCallback!(convertSoundEventStringToNumber("high"));
this.soundLevelCallback!(MICROBIT_HAL_MICROPHONE_EVT_THRESHOLD_HIGH);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
<option value="pin_logo">Pin logo</option>
<option value="radio">Radio</option>
<option value="sensors">Sensors</option>
<option value="sound_effects">Sound effects</option>
<option value="sound_effects_builtin">Sound effects (builtin)</option>
<option value="sound_effects_user">Sound effects (user)</option>
<option value="speech">Speech</option>
<option value="volume">Volume</option>
</select>
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions src/examples/sound_effects_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from microbit import *

# Create a Sound Effect and immediately play it
audio.play(
audio.SoundEffect(
freq_start=400,
freq_end=2000,
duration=500,
vol_start=100,
vol_end=255,
wave=audio.SoundEffect.WAVE_TRIANGLE,
fx=audio.SoundEffect.FX_VIBRATO,
shape=audio.SoundEffect.SHAPE_LOG,
)
)
2 changes: 1 addition & 1 deletion src/jshal.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void mp_js_hal_audio_speech_init(uint32_t sample_rate);
void mp_js_hal_audio_speech_write_data(const uint8_t *buf, size_t num_samples);
void mp_js_hal_audio_period_us(int period);
void mp_js_hal_audio_amplitude_u10(int amplitude);
void mp_js_hal_audio_play_expression_by_name(const char *name);
void mp_js_hal_audio_play_expression(const char *name);
void mp_js_hal_audio_stop_expression(void);
bool mp_js_hal_audio_is_expression_active(void);

Expand Down
7 changes: 3 additions & 4 deletions src/jshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ mergeInto(LibraryManager.library, {

mp_js_hal_microphone_set_threshold: function (kind, value) {
board.microphone.setThreshold(
// `+ 1` is temporary, see https://github.com/microbit-foundation/micropython-microbit-v2/pull/109
conversions.convertSoundEventNumberToString(kind + 1),
conversions.convertSoundThresholdNumberToString(kind),
value
);
},
Expand All @@ -217,8 +216,8 @@ mergeInto(LibraryManager.library, {
return board.microphone.soundLevel.value;
},

mp_js_hal_audio_play_expression_by_name: function (name) {
return board.audio.playSoundExpression(UTF8ToString(name));
mp_js_hal_audio_play_expression: function (expr) {
return board.audio.playSoundExpression(UTF8ToString(expr));
},

mp_js_hal_audio_stop_expression: function () {
Expand Down
24 changes: 22 additions & 2 deletions src/microbithal_js.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@ int microbit_hal_temperature(void) {
return mp_js_hal_temperature();
}

void microbit_hal_power_clear_wake_sources(void) {
// Stub, unsupported.
}

void microbit_hal_power_wake_on_button(int button, bool wake_on_active) {
// Stub, unsupported.
}

void microbit_hal_power_wake_on_pin(int pin, bool wake_on_active) {
// Stub, unsupported.
}

void microbit_hal_power_off(void) {
// Stub, unsupported.
}

void microbit_hal_power_deep_sleep(bool wake_on_ms, uint32_t ms) {
// Stub, unsupported.
}

void microbit_hal_pin_set_pull(int pin, int pull) {
//pin_obj[pin]->setPull(pin_pull_mode_mapping[pull]);
//pin_pull_state[pin] = pull;
Expand Down Expand Up @@ -431,8 +451,8 @@ bool microbit_hal_audio_is_expression_active(void) {
return mp_js_hal_audio_is_expression_active();
}

void microbit_hal_audio_play_expression_by_name(const char *name) {
mp_js_hal_audio_play_expression_by_name(name);
void microbit_hal_audio_play_expression(const char *expr) {
mp_js_hal_audio_play_expression(expr);
}

void microbit_hal_audio_stop_expression(void) {
Expand Down
2 changes: 2 additions & 0 deletions src/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ extern const struct _mp_obj_module_t machine_module;
extern const struct _mp_obj_module_t microbit_module;
extern const struct _mp_obj_module_t music_module;
extern const struct _mp_obj_module_t os_module;
extern const struct _mp_obj_module_t power_module;
extern const struct _mp_obj_module_t radio_module;
extern const struct _mp_obj_module_t speech_module;
extern const struct _mp_obj_module_t this_module;
Expand All @@ -129,6 +130,7 @@ extern const struct _mp_obj_module_t utime_module;
{ MP_ROM_QSTR(MP_QSTR_microbit), MP_ROM_PTR(&microbit_module) }, \
{ MP_ROM_QSTR(MP_QSTR_music), MP_ROM_PTR(&music_module) }, \
{ MP_ROM_QSTR(MP_QSTR_os), MP_ROM_PTR(&os_module) }, \
{ MP_ROM_QSTR(MP_QSTR_power), MP_ROM_PTR(&power_module) }, \
{ MP_ROM_QSTR(MP_QSTR_radio), MP_ROM_PTR(&radio_module) }, \
{ MP_ROM_QSTR(MP_QSTR_speech), MP_ROM_PTR(&speech_module) }, \
{ MP_ROM_QSTR(MP_QSTR_this), MP_ROM_PTR(&this_module) }, \
Expand Down