Skip to content

Commit 7d84c58

Browse files
Update MicroPython
Adjust build and samples for record/playback No HAL changes but the API has changed
1 parent 34e45b3 commit 7d84c58

File tree

7 files changed

+94
-29
lines changed

7 files changed

+94
-29
lines changed

lib/micropython-microbit-v2

src/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
7979
help.c \
8080
iters.c \
8181
microbit_accelerometer.c \
82+
microbit_audiorecording.c \
83+
microbit_audiotrack.c \
8284
microbit_button.c \
8385
microbit_compass.c \
8486
microbit_display.c \
@@ -109,6 +111,7 @@ SRC_C += $(addprefix $(CODAL_PORT)/, \
109111
modspeech.c \
110112
modthis.c \
111113
mphalport.c \
114+
utils.c \
112115
)
113116

114117
SRC_C += \

src/demo.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,18 @@ <h1>MicroPython-micro:bit simulator example embedding</h1>
9090
<option value="display">Display</option>
9191
<option value="inline_assembler">Inline assembler</option>
9292
<option value="microphone">Microphone</option>
93-
<option value="microphone_sensitivity">Microphone sensitivity</option>
93+
<option value="microphone_sensitivity">
94+
Microphone sensitivity
95+
</option>
9496
<option value="music">Music</option>
9597
<option value="pin_logo">Pin logo</option>
9698
<option value="pin_touches">Pin touches</option>
9799
<option value="radio">Radio</option>
98100
<option value="random">Random</option>
99101
<option value="record">Record</option>
102+
<option value="test_record">
103+
Record (MicroPython test file)
104+
</option>
100105
<option value="sensors">Sensors</option>
101106
<option value="sound_effects_builtin">
102107
Sound effects (builtin)

src/examples/record.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
rate_index = 0
55

66
print("Recording...")
7-
frame = microphone.record(3000)
7+
my_track = microphone.record(3000)
88
print("Button A to play")
99
while True:
1010
if button_a.was_pressed():
11-
audio.play(frame, wait=False)
11+
audio.play(my_track, wait=False)
1212
print("Rate playing", rates[rate_index])
1313

1414
if button_b.was_pressed():
1515
rate_index = (rate_index + 1) % len(rates)
1616
print("Rate change to", rates[rate_index])
17-
frame.set_rate(rates[rate_index])
17+
my_track.set_rate(rates[rate_index])

src/examples/test_record.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from microbit import *
2+
3+
mouth_open = Image(
4+
"09090:"
5+
"00000:"
6+
"09990:"
7+
"90009:"
8+
"09990"
9+
)
10+
mouth_closed = Image(
11+
"09090:"
12+
"00000:"
13+
"00000:"
14+
"99999:"
15+
"00000"
16+
)
17+
play = Image(
18+
"00000:"
19+
"04740:"
20+
"07970:"
21+
"04740:"
22+
"00000"
23+
)
24+
25+
my_recording = audio.AudioRecording(duration=5000)
26+
27+
while True:
28+
if button_a.is_pressed():
29+
my_track = microphone.record_into(my_recording, wait=False)
30+
display.show([mouth_open, mouth_closed], loop=True, wait=False, delay=150)
31+
while button_a.is_pressed() and microphone.is_recording():
32+
sleep(50)
33+
microphone.stop_recording()
34+
display.show(mouth_closed)
35+
while button_a.is_pressed():
36+
sleep(50)
37+
display.clear()
38+
# amplify volume
39+
GAIN = 2
40+
#my_recording *= GAIN
41+
for i in range(len(my_track)):
42+
my_track[i] = max(0, min(128 + GAIN * (my_track[i] - 128), 255))
43+
if button_b.is_pressed():
44+
audio.play(my_track, wait=False)
45+
level = 0
46+
while audio.is_playing():
47+
l = audio.sound_level()
48+
if l > level:
49+
level = l
50+
else:
51+
level *= 0.95
52+
display.show(play * min(1, level / 100))
53+
x = accelerometer.get_x()
54+
my_track.set_rate(max(2250, scale(x, (-1000, 1000), (2250, 13374))))
55+
sleep(5)
56+
display.clear()
57+
sleep(100)

src/main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void mp_js_main(int heap_size) {
110110
}
111111
}
112112

113-
STATIC void microbit_display_exception(mp_obj_t exc_in) {
113+
static void microbit_display_exception(mp_obj_t exc_in) {
114114
// Construct the message string ready for display.
115115
mp_uint_t n, *values;
116116
mp_obj_exception_get_traceback(exc_in, &n, &values);
@@ -184,7 +184,7 @@ void nlr_jump_fail(void *val) {
184184
exit(1);
185185
}
186186

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

src/microbitfs.c

+22-22
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
/******************************************************************************/
4343
// os-level functions
4444

45-
STATIC mp_obj_t os_mbfs_listdir(void) {
45+
static mp_obj_t os_mbfs_listdir(void) {
4646
mp_obj_t res = mp_obj_new_list(0, NULL);
4747
char buf[MAX_FILENAME_LENGTH];
4848
for (size_t i = 0;; ++i) {
@@ -65,7 +65,7 @@ typedef struct {
6565
uint8_t idx;
6666
} os_mbfs_ilistdir_it_t;
6767

68-
STATIC mp_obj_t os_mbfs_ilistdir_it_iternext(mp_obj_t self_in) {
68+
static mp_obj_t os_mbfs_ilistdir_it_iternext(mp_obj_t self_in) {
6969
os_mbfs_ilistdir_it_t *self = MP_OBJ_TO_PTR(self_in);
7070
for (;;) {
7171
char buf[MAX_FILENAME_LENGTH];
@@ -85,15 +85,15 @@ STATIC mp_obj_t os_mbfs_ilistdir_it_iternext(mp_obj_t self_in) {
8585
}
8686
}
8787

88-
STATIC mp_obj_t os_mbfs_ilistdir(void) {
88+
static mp_obj_t os_mbfs_ilistdir(void) {
8989
os_mbfs_ilistdir_it_t *iter = mp_obj_malloc(os_mbfs_ilistdir_it_t, &mp_type_polymorph_iter);
9090
iter->iternext = os_mbfs_ilistdir_it_iternext;
9191
iter->idx = 0;
9292
return MP_OBJ_FROM_PTR(iter);
9393
}
9494
MP_DEFINE_CONST_FUN_OBJ_0(os_mbfs_ilistdir_obj, os_mbfs_ilistdir);
9595

96-
STATIC mp_obj_t os_mbfs_remove(mp_obj_t filename_in) {
96+
static mp_obj_t os_mbfs_remove(mp_obj_t filename_in) {
9797
size_t name_len;
9898
const char *name = mp_obj_str_get_data(filename_in, &name_len);
9999
int idx = mp_js_hal_filesystem_find(name, name_len);
@@ -105,7 +105,7 @@ STATIC mp_obj_t os_mbfs_remove(mp_obj_t filename_in) {
105105
}
106106
MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_remove_obj, os_mbfs_remove);
107107

108-
STATIC mp_obj_t os_mbfs_stat(mp_obj_t filename_in) {
108+
static mp_obj_t os_mbfs_stat(mp_obj_t filename_in) {
109109
size_t name_len;
110110
const char *name = mp_obj_str_get_data(filename_in, &name_len);
111111
int idx = mp_js_hal_filesystem_find(name, name_len);
@@ -141,26 +141,26 @@ typedef struct _mbfs_file_obj_t {
141141
bool binary;
142142
} mbfs_file_obj_t;
143143

144-
STATIC mp_obj_t os_mbfs_file___exit__(size_t n_args, const mp_obj_t *args) {
144+
static mp_obj_t os_mbfs_file___exit__(size_t n_args, const mp_obj_t *args) {
145145
(void)n_args;
146146
return mp_stream_close(args[0]);
147147
}
148-
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_mbfs_file___exit___obj, 4, 4, os_mbfs_file___exit__);
148+
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(os_mbfs_file___exit___obj, 4, 4, os_mbfs_file___exit__);
149149

150-
STATIC mp_obj_t os_mbfs_file_name(mp_obj_t self_in) {
150+
static mp_obj_t os_mbfs_file_name(mp_obj_t self_in) {
151151
mbfs_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
152152
char buf[MAX_FILENAME_LENGTH];
153153
int len = mp_js_hal_filesystem_name(self->idx, buf);
154154
return mp_obj_new_str(buf, len);
155155
}
156-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_file_name_obj, os_mbfs_file_name);
156+
static MP_DEFINE_CONST_FUN_OBJ_1(os_mbfs_file_name_obj, os_mbfs_file_name);
157157

158-
STATIC mp_obj_t microbit_file_writable(mp_obj_t self) {
158+
static mp_obj_t microbit_file_writable(mp_obj_t self) {
159159
return mp_obj_new_bool(((mbfs_file_obj_t *)MP_OBJ_TO_PTR(self))->writable);
160160
}
161-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_writable_obj, microbit_file_writable);
161+
static MP_DEFINE_CONST_FUN_OBJ_1(microbit_file_writable_obj, microbit_file_writable);
162162

163-
STATIC const mp_rom_map_elem_t os_mbfs_file_locals_dict_table[] = {
163+
static const mp_rom_map_elem_t os_mbfs_file_locals_dict_table[] = {
164164
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
165165
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&os_mbfs_file___exit___obj) },
166166
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&os_mbfs_file_name_obj) },
@@ -172,15 +172,15 @@ STATIC const mp_rom_map_elem_t os_mbfs_file_locals_dict_table[] = {
172172
{ MP_ROM_QSTR(MP_QSTR_readline), MP_ROM_PTR(&mp_stream_unbuffered_readline_obj) },
173173
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_stream_write_obj) },
174174
};
175-
STATIC MP_DEFINE_CONST_DICT(os_mbfs_file_locals_dict, os_mbfs_file_locals_dict_table);
175+
static MP_DEFINE_CONST_DICT(os_mbfs_file_locals_dict, os_mbfs_file_locals_dict_table);
176176

177-
STATIC void check_file_open(mbfs_file_obj_t *self) {
177+
static void check_file_open(mbfs_file_obj_t *self) {
178178
if (!self->open) {
179179
mp_raise_ValueError(MP_ERROR_TEXT("I/O operation on closed file"));
180180
}
181181
}
182182

183-
STATIC mp_uint_t microbit_file_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
183+
static mp_uint_t microbit_file_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
184184
mbfs_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
185185
check_file_open(self);
186186
if (self->writable) {
@@ -201,7 +201,7 @@ STATIC mp_uint_t microbit_file_read(mp_obj_t self_in, void *buf_in, mp_uint_t si
201201
return bytes_read;
202202
}
203203

204-
STATIC mp_uint_t microbit_file_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
204+
static mp_uint_t microbit_file_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
205205
mbfs_file_obj_t *self = MP_OBJ_TO_PTR(self_in);
206206
check_file_open(self);
207207
if (!self->writable) {
@@ -216,7 +216,7 @@ STATIC mp_uint_t microbit_file_write(mp_obj_t self_in, const void *buf, mp_uint_
216216
return size;
217217
}
218218

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

222222
if (request == MP_STREAM_CLOSE) {
@@ -228,7 +228,7 @@ STATIC mp_uint_t microbit_file_ioctl(mp_obj_t self_in, mp_uint_t request, uintpt
228228
}
229229
}
230230

231-
STATIC const mp_stream_p_t textio_stream_p = {
231+
static const mp_stream_p_t textio_stream_p = {
232232
.read = microbit_file_read,
233233
.write = microbit_file_write,
234234
.ioctl = microbit_file_ioctl,
@@ -244,7 +244,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
244244
);
245245

246246

247-
STATIC const mp_stream_p_t fileio_stream_p = {
247+
static const mp_stream_p_t fileio_stream_p = {
248248
.read = microbit_file_read,
249249
.write = microbit_file_write,
250250
.ioctl = microbit_file_ioctl,
@@ -258,7 +258,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
258258
locals_dict, &os_mbfs_file_locals_dict
259259
);
260260

261-
STATIC mbfs_file_obj_t *microbit_file_open(const char *name, size_t name_len, bool write, bool binary) {
261+
static mbfs_file_obj_t *microbit_file_open(const char *name, size_t name_len, bool write, bool binary) {
262262
if (name_len > MAX_FILENAME_LENGTH) {
263263
return NULL;
264264
}
@@ -298,7 +298,7 @@ mp_import_stat_t mp_import_stat(const char *path) {
298298
}
299299
}
300300

301-
STATIC mp_uint_t file_readbyte(void *self_in) {
301+
static mp_uint_t file_readbyte(void *self_in) {
302302
mbfs_file_obj_t *self = self_in;
303303
int chr = mp_js_hal_filesystem_readbyte(self->idx, self->offset);
304304
if (chr < 0) {
@@ -308,7 +308,7 @@ STATIC mp_uint_t file_readbyte(void *self_in) {
308308
return chr;
309309
}
310310

311-
STATIC void file_close(void *self_in) {
311+
static void file_close(void *self_in) {
312312
mbfs_file_obj_t *self = self_in;
313313
self->open = false;
314314
}

0 commit comments

Comments
 (0)