From 345e9864aac03693eec6cddd09a0dcd47df9c453 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 1 Jun 2019 16:06:25 +1000 Subject: [PATCH] stm32/modpyb: Add pyb.country() function to set the country. To be used for peripherals (like radio) that must be location aware. --- ports/stm32/factoryreset.c | 1 + ports/stm32/modpyb.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/ports/stm32/factoryreset.c b/ports/stm32/factoryreset.c index 6b0c289a78..24e0ff88cf 100644 --- a/ports/stm32/factoryreset.c +++ b/ports/stm32/factoryreset.c @@ -35,6 +35,7 @@ static const char fresh_boot_py[] = "\r\n" "import machine\r\n" "import pyb\r\n" +"pyb.country('US') # ISO 3166-1 Alpha-2 code, eg US, GB, DE, AU\r\n" "#pyb.main('main.py') # main script to run after this one\r\n" #if MICROPY_HW_ENABLE_USB "#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device\r\n" diff --git a/ports/stm32/modpyb.c b/ports/stm32/modpyb.c index 85c6ee1381..c3e60caebb 100644 --- a/ports/stm32/modpyb.c +++ b/ports/stm32/modpyb.c @@ -56,6 +56,8 @@ #include "extmod/vfs.h" #include "extmod/utime_mphal.h" +char pyb_country_code[2]; + STATIC mp_obj_t pyb_fault_debug(mp_obj_t value) { pyb_hard_fault_debug = mp_obj_is_true(value); return mp_const_none; @@ -112,6 +114,22 @@ STATIC mp_obj_t pyb_repl_uart(size_t n_args, const mp_obj_t *args) { } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_repl_uart_obj, 0, 1, pyb_repl_uart); +STATIC mp_obj_t pyb_country(size_t n_args, const mp_obj_t *args) { + if (n_args == 0) { + return mp_obj_new_str(pyb_country_code, 2); + } else { + size_t len; + const char *str = mp_obj_str_get_data(args[0], &len); + if (len != 2) { + mp_raise_ValueError(NULL); + } + pyb_country_code[0] = str[0]; + pyb_country_code[1] = str[1]; + return mp_const_none; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_country_obj, 0, 1, pyb_country); + STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyb) }, @@ -139,6 +157,7 @@ STATIC const mp_rom_map_elem_t pyb_module_globals_table[] = { #endif { MP_ROM_QSTR(MP_QSTR_main), MP_ROM_PTR(&pyb_main_obj) }, { MP_ROM_QSTR(MP_QSTR_repl_uart), MP_ROM_PTR(&pyb_repl_uart_obj) }, + { MP_ROM_QSTR(MP_QSTR_country), MP_ROM_PTR(&pyb_country_obj) }, #if MICROPY_HW_ENABLE_USB { MP_ROM_QSTR(MP_QSTR_usb_mode), MP_ROM_PTR(&pyb_usb_mode_obj) },