Skip to content

Commit ca108e7

Browse files
committed
codal_port/modradio: Turn the radio on when importing radio module.
In all cases radio.on() is necessary to start using the radio, otherwise functions like radio.send() or radio.receive() will throw an exception "ValueError: radio is not enabled". The majority of the micro:bit features, especially those that can be turned off like the display, are "on" by default. So make the radio consistent in that regard. Addresses issue #97. Signed-off-by: Damien George <[email protected]>
1 parent abad747 commit ca108e7

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/codal_port/modradio.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,21 @@
3434

3535
STATIC microbit_radio_config_t radio_config;
3636

37+
STATIC mp_obj_t mod_radio_reset(void);
38+
3739
STATIC void ensure_enabled(void) {
3840
if (MP_STATE_PORT(radio_buf) == NULL) {
3941
mp_raise_ValueError(MP_ERROR_TEXT("radio is not enabled"));
4042
}
4143
}
4244

45+
STATIC mp_obj_t mod_radio___init__(void) {
46+
mod_radio_reset();
47+
microbit_radio_enable(&radio_config);
48+
return mp_const_none;
49+
}
50+
MP_DEFINE_CONST_FUN_OBJ_0(mod_radio___init___obj, mod_radio___init__);
51+
4352
STATIC mp_obj_t mod_radio_reset(void) {
4453
radio_config.max_payload = MICROBIT_RADIO_DEFAULT_MAX_PAYLOAD;
4554
radio_config.queue_len = MICROBIT_RADIO_DEFAULT_QUEUE_LEN;
@@ -253,7 +262,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(mod_radio_receive_full_obj, mod_radio_receive_full);
253262

254263
STATIC const mp_map_elem_t radio_module_globals_table[] = {
255264
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_radio) },
256-
{ MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&mod_radio_reset_obj },
265+
{ MP_OBJ_NEW_QSTR(MP_QSTR___init__), (mp_obj_t)&mod_radio___init___obj },
257266

258267
{ MP_OBJ_NEW_QSTR(MP_QSTR_reset), (mp_obj_t)&mod_radio_reset_obj },
259268
{ MP_OBJ_NEW_QSTR(MP_QSTR_config), (mp_obj_t)&mod_radio_config_obj },

0 commit comments

Comments
 (0)