Skip to content

Commit

Permalink
Update MicroPython
Browse files Browse the repository at this point in the history
Adjust build and samples for record/playback
No HAL changes but the API has changed
  • Loading branch information
microbit-matt-hillsdon committed Aug 19, 2024
1 parent 34e45b3 commit 7d84c58
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/micropython-microbit-v2
Submodule micropython-microbit-v2 updated 43 files
+1 −1 lib/micropython
+1 −1 src/codal_app/codal.json
+0 −3 src/codal_app/main.cpp
+3 −0 src/codal_port/Makefile
+2 −2 src/codal_port/drv_display.c
+1 −1 src/codal_port/drv_image.c
+1 −1 src/codal_port/drv_softtimer.c
+1 −1 src/codal_port/iters.c
+1 −1 src/codal_port/main.c
+29 −29 src/codal_port/microbit_accelerometer.c
+128 −0 src/codal_port/microbit_audiorecording.c
+167 −0 src/codal_port/microbit_audiotrack.c
+2 −2 src/codal_port/microbit_button.c
+19 −19 src/codal_port/microbit_compass.c
+5 −5 src/codal_port/microbit_display.c
+7 −7 src/codal_port/microbit_i2c.c
+16 −16 src/codal_port/microbit_image.c
+41 −45 src/codal_port/microbit_microphone.c
+8 −8 src/codal_port/microbit_pin.c
+1 −1 src/codal_port/microbit_pinaudio.c
+4 −4 src/codal_port/microbit_sound.c
+15 −15 src/codal_port/microbit_soundeffect.c
+3 −3 src/codal_port/microbit_soundevent.c
+7 −7 src/codal_port/microbit_speaker.c
+9 −9 src/codal_port/microbit_spi.c
+10 −10 src/codal_port/microbit_uart.c
+46 −46 src/codal_port/microbitfs.c
+7 −7 src/codal_port/modantigravity.c
+60 −36 src/codal_port/modaudio.c
+17 −1 src/codal_port/modaudio.h
+12 −12 src/codal_port/modlog.c
+6 −6 src/codal_port/modlove.c
+10 −10 src/codal_port/modmachine.c
+21 −21 src/codal_port/modmicrobit.c
+15 −15 src/codal_port/modmusic.c
+13 −13 src/codal_port/modos.c
+7 −7 src/codal_port/modpower.c
+16 −16 src/codal_port/modradio.c
+13 −13 src/codal_port/modspeech.c
+6 −6 src/codal_port/modthis.c
+35 −0 src/codal_port/utils.c
+33 −0 src/codal_port/utils.h
+9 −5 src/test_record.py
3 changes: 3 additions & 0 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
help.c \
iters.c \
microbit_accelerometer.c \
microbit_audiorecording.c \
microbit_audiotrack.c \
microbit_button.c \
microbit_compass.c \
microbit_display.c \
Expand Down Expand Up @@ -109,6 +111,7 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
modspeech.c \
modthis.c \
mphalport.c \
utils.c \
)

SRC_C += \
Expand Down
7 changes: 6 additions & 1 deletion src/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,18 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
<option value="display">Display</option>
<option value="inline_assembler">Inline assembler</option>
<option value="microphone">Microphone</option>
<option value="microphone_sensitivity">Microphone sensitivity</option>
<option value="microphone_sensitivity">
Microphone sensitivity
</option>
<option value="music">Music</option>
<option value="pin_logo">Pin logo</option>
<option value="pin_touches">Pin touches</option>
<option value="radio">Radio</option>
<option value="random">Random</option>
<option value="record">Record</option>
<option value="test_record">
Record (MicroPython test file)
</option>
<option value="sensors">Sensors</option>
<option value="sound_effects_builtin">
Sound effects (builtin)
Expand Down
6 changes: 3 additions & 3 deletions src/examples/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
rate_index = 0

print("Recording...")
frame = microphone.record(3000)
my_track = microphone.record(3000)
print("Button A to play")
while True:
if button_a.was_pressed():
audio.play(frame, wait=False)
audio.play(my_track, wait=False)
print("Rate playing", rates[rate_index])

if button_b.was_pressed():
rate_index = (rate_index + 1) % len(rates)
print("Rate change to", rates[rate_index])
frame.set_rate(rates[rate_index])
my_track.set_rate(rates[rate_index])
57 changes: 57 additions & 0 deletions src/examples/test_record.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from microbit import *

mouth_open = Image(
"09090:"
"00000:"
"09990:"
"90009:"
"09990"
)
mouth_closed = Image(
"09090:"
"00000:"
"00000:"
"99999:"
"00000"
)
play = Image(
"00000:"
"04740:"
"07970:"
"04740:"
"00000"
)

my_recording = audio.AudioRecording(duration=5000)

while True:
if button_a.is_pressed():
my_track = microphone.record_into(my_recording, wait=False)
display.show([mouth_open, mouth_closed], loop=True, wait=False, delay=150)
while button_a.is_pressed() and microphone.is_recording():
sleep(50)
microphone.stop_recording()
display.show(mouth_closed)
while button_a.is_pressed():
sleep(50)
display.clear()
# amplify volume
GAIN = 2
#my_recording *= GAIN
for i in range(len(my_track)):
my_track[i] = max(0, min(128 + GAIN * (my_track[i] - 128), 255))
if button_b.is_pressed():
audio.play(my_track, wait=False)
level = 0
while audio.is_playing():
l = audio.sound_level()
if l > level:
level = l
else:
level *= 0.95
display.show(play * min(1, level / 100))
x = accelerometer.get_x()
my_track.set_rate(max(2250, scale(x, (-1000, 1000), (2250, 13374))))
sleep(5)
display.clear()
sleep(100)
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ void mp_js_main(int heap_size) {
}
}

STATIC void microbit_display_exception(mp_obj_t exc_in) {
static void microbit_display_exception(mp_obj_t exc_in) {
// Construct the message string ready for display.
mp_uint_t n, *values;
mp_obj_exception_get_traceback(exc_in, &n, &values);
Expand Down Expand Up @@ -184,7 +184,7 @@ void nlr_jump_fail(void *val) {
exit(1);
}

STATIC void gc_scan_func(void *begin, void *end) {
static void gc_scan_func(void *begin, void *end) {
gc_collect_root((void **)begin, (void **)end - (void **)begin + 1);
}

Expand Down
44 changes: 22 additions & 22 deletions src/microbitfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
/******************************************************************************/
// os-level functions

STATIC mp_obj_t os_mbfs_listdir(void) {
static mp_obj_t os_mbfs_listdir(void) {
mp_obj_t res = mp_obj_new_list(0, NULL);
char buf[MAX_FILENAME_LENGTH];
for (size_t i = 0;; ++i) {
Expand All @@ -65,7 +65,7 @@ typedef struct {
uint8_t idx;
} os_mbfs_ilistdir_it_t;

STATIC mp_obj_t os_mbfs_ilistdir_it_iternext(mp_obj_t self_in) {
static mp_obj_t os_mbfs_ilistdir_it_iternext(mp_obj_t self_in) {
os_mbfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
for (;;) {
char buf[MAX_FILENAME_LENGTH];
Expand All @@ -85,15 +85,15 @@ STATIC mp_obj_t os_mbfs_ilistdir_it_iternext(mp_obj_t self_in) {
}
}

STATIC mp_obj_t os_mbfs_ilistdir(void) {
static mp_obj_t os_mbfs_ilistdir(void) {
os_mbfs_ilistdir_it_t *iter = mp_obj_malloc(os_mbfs_ilistdir_it_t, &mp_type_polymorph_iter);
iter->iternext = os_mbfs_ilistdir_it_iternext;
iter->idx = 0;
return MP_OBJ_FROM_PTR(iter);
}
MP_DEFINE_CONST_FUN_OBJ_0(os_mbfs_ilistdir_obj, os_mbfs_ilistdir);

STATIC mp_obj_t os_mbfs_remove(mp_obj_t filename_in) {
static mp_obj_t os_mbfs_remove(mp_obj_t filename_in) {
size_t name_len;
const char *name = mp_obj_str_get_data(filename_in, &name_len);
int idx = mp_js_hal_filesystem_find(name, name_len);
Expand All @@ -105,7 +105,7 @@ STATIC mp_obj_t os_mbfs_remove(mp_obj_t filename_in) {
}
MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_remove_obj, os_mbfs_remove);

STATIC mp_obj_t os_mbfs_stat(mp_obj_t filename_in) {
static mp_obj_t os_mbfs_stat(mp_obj_t filename_in) {
size_t name_len;
const char *name = mp_obj_str_get_data(filename_in, &name_len);
int idx = mp_js_hal_filesystem_find(name, name_len);
Expand Down Expand Up @@ -141,26 +141,26 @@ typedef struct _mbfs_file_obj_t {
bool binary;
} mbfs_file_obj_t;

STATIC mp_obj_t os_mbfs_file___exit__(size_t n_args, const mp_obj_t *args) {
static mp_obj_t os_mbfs_file___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
return mp_stream_close(args[0]);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_mbfs_file___exit___obj, 4, 4, os_mbfs_file___exit__);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_mbfs_file___exit___obj, 4, 4, os_mbfs_file___exit__);

STATIC mp_obj_t os_mbfs_file_name(mp_obj_t self_in) {
static mp_obj_t os_mbfs_file_name(mp_obj_t self_in) {
mbfs_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
char buf[MAX_FILENAME_LENGTH];
int len = mp_js_hal_filesystem_name(self->idx, buf);
return mp_obj_new_str(buf, len);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_file_name_obj, os_mbfs_file_name);
static MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_file_name_obj, os_mbfs_file_name);

STATIC mp_obj_t microbit_file_writable(mp_obj_t self) {
static mp_obj_t microbit_file_writable(mp_obj_t self) {
return mp_obj_new_bool(((mbfs_file_obj_t *)MP_OBJ_TO_PTR(self))->writable);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_writable_obj, microbit_file_writable);
static MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_writable_obj, microbit_file_writable);

STATIC const mp_rom_map_elem_t os_mbfs_file_locals_dict_table[] = {
static const mp_rom_map_elem_t os_mbfs_file_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&os_mbfs_file___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&os_mbfs_file_name_obj) },
Expand All @@ -172,15 +172,15 @@ STATIC const mp_rom_map_elem_t os_mbfs_file_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
};
STATIC MP_DEFINE_CONST_DICT(os_mbfs_file_locals_dict, os_mbfs_file_locals_dict_table);
static MP_DEFINE_CONST_DICT(os_mbfs_file_locals_dict, os_mbfs_file_locals_dict_table);

STATIC void check_file_open(mbfs_file_obj_t *self) {
static void check_file_open(mbfs_file_obj_t *self) {
if (!self->open) {
mp_raise_ValueError(MP_ERROR_TEXT("I/O operation on closed file"));
}
}

STATIC mp_uint_t microbit_file_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
static mp_uint_t microbit_file_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
mbfs_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_file_open(self);
if (self->writable) {
Expand All @@ -201,7 +201,7 @@ STATIC mp_uint_t microbit_file_read(mp_obj_t self_in, void *buf_in, mp_uint_t si
return bytes_read;
}

STATIC mp_uint_t microbit_file_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
static mp_uint_t microbit_file_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
mbfs_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
check_file_open(self);
if (!self->writable) {
Expand All @@ -216,7 +216,7 @@ STATIC mp_uint_t microbit_file_write(mp_obj_t self_in, const void *buf, mp_uint_
return size;
}

STATIC mp_uint_t microbit_file_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
static mp_uint_t microbit_file_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
mbfs_file_obj_t *self = MP_OBJ_TO_PTR(self_in);

if (request == MP_STREAM_CLOSE) {
Expand All @@ -228,7 +228,7 @@ STATIC mp_uint_t microbit_file_ioctl(mp_obj_t self_in, mp_uint_t request, uintpt
}
}

STATIC const mp_stream_p_t textio_stream_p = {
static const mp_stream_p_t textio_stream_p = {
.read = microbit_file_read,
.write = microbit_file_write,
.ioctl = microbit_file_ioctl,
Expand All @@ -244,7 +244,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
);


STATIC const mp_stream_p_t fileio_stream_p = {
static const mp_stream_p_t fileio_stream_p = {
.read = microbit_file_read,
.write = microbit_file_write,
.ioctl = microbit_file_ioctl,
Expand All @@ -258,7 +258,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
locals_dict, &os_mbfs_file_locals_dict
);

STATIC mbfs_file_obj_t *microbit_file_open(const char *name, size_t name_len, bool write, bool binary) {
static mbfs_file_obj_t *microbit_file_open(const char *name, size_t name_len, bool write, bool binary) {
if (name_len > MAX_FILENAME_LENGTH) {
return NULL;
}
Expand Down Expand Up @@ -298,7 +298,7 @@ mp_import_stat_t mp_import_stat(const char *path) {
}
}

STATIC mp_uint_t file_readbyte(void *self_in) {
static mp_uint_t file_readbyte(void *self_in) {
mbfs_file_obj_t *self = self_in;
int chr = mp_js_hal_filesystem_readbyte(self->idx, self->offset);
if (chr < 0) {
Expand All @@ -308,7 +308,7 @@ STATIC mp_uint_t file_readbyte(void *self_in) {
return chr;
}

STATIC void file_close(void *self_in) {
static void file_close(void *self_in) {
mbfs_file_obj_t *self = self_in;
self->open = false;
}
Expand Down

0 comments on commit 7d84c58

Please sign in to comment.