From 26f77014f22274f906ff5a707dd630848c25d201 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Sat, 5 Oct 2024 14:30:05 +0200 Subject: [PATCH] pybricks.common.System: Add info dictionary. Can be used to indicate various hub states instead of adding a new method for each. See https://github.com/pybricks/support/issues/1496 --- CHANGELOG.md | 2 ++ pybricks/common/pb_type_system.c | 31 +++++++++++++++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe4848c44..6793282d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ coding ([support#1661]). - Added `observe_enable` to the hub `BLE` class to selectively turn observing on and off, just like you can with broadcasting ([support#1806]). +- Added `hub.system.info()` method with hub status flags ([support#1496]). ### Changed @@ -31,6 +32,7 @@ at exact multiples of its animation interval ([support#1295]). [support#1295]: https://github.com/pybricks/support/issues/1295 +[support#1496]: https://github.com/pybricks/support/issues/1496 [support#1623]: https://github.com/pybricks/support/issues/1623 [support#1661]: https://github.com/pybricks/support/issues/1661 [support#1668]: https://github.com/pybricks/support/issues/1668 diff --git a/pybricks/common/pb_type_system.c b/pybricks/common/pb_type_system.c index 3f068512d..065f39543 100644 --- a/pybricks/common/pb_type_system.c +++ b/pybricks/common/pb_type_system.c @@ -8,6 +8,9 @@ #include #include +#include +#include +#include #include #include "py/obj.h" @@ -15,6 +18,7 @@ #include "py/runtime.h" #include +#include #include #include #include @@ -27,8 +31,6 @@ static MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_name_obj, pb_type_System_name); #if PBDRV_CONFIG_RESET -#include - static mp_obj_t pb_type_System_reset_reason(void) { pbdrv_reset_reason_t reason = pbdrv_reset_get_reason(); return MP_OBJ_NEW_SMALL_INT(reason); @@ -37,12 +39,28 @@ static MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_reset_reason_obj, pb_type_System #endif // PBDRV_CONFIG_RESET -#if PBIO_CONFIG_ENABLE_SYS +static mp_obj_t pb_type_System_info(void) { + const char *hub_name = pbdrv_bluetooth_get_hub_name(); -#include -#include + mp_map_elem_t info[] = { + {MP_OBJ_NEW_QSTR(MP_QSTR_name), mp_obj_new_str(hub_name, strlen(hub_name))}, + #if PBDRV_CONFIG_RESET + {MP_OBJ_NEW_QSTR(MP_QSTR_reset_reason), mp_obj_new_int(pbdrv_reset_get_reason())}, + #endif // PBDRV_CONFIG_RESET + {MP_OBJ_NEW_QSTR(MP_QSTR_host_connected_ble), mp_obj_new_bool(pbsys_status_test(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED))}, + }; + mp_obj_t info_dict = mp_obj_new_dict(MP_ARRAY_SIZE(info)); + + for (size_t i = 0; i < MP_ARRAY_SIZE(info); i++) { + mp_map_elem_t *elem = &info[i]; + mp_obj_dict_store(info_dict, elem->key, elem->value); + } -#include + return info_dict; +} +static MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_info_obj, pb_type_System_info); + +#if PBIO_CONFIG_ENABLE_SYS static mp_obj_t pb_type_System_set_stop_button(mp_obj_t buttons_in) { pbio_button_flags_t buttons = 0; @@ -127,6 +145,7 @@ static MP_DEFINE_CONST_FUN_OBJ_KW(pb_type_System_storage_obj, 0, pb_type_System_ // dir(pybricks.common.System) static const mp_rom_map_elem_t common_System_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&pb_type_System_name_obj) }, + { MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&pb_type_System_info_obj) }, #if PBDRV_CONFIG_RESET { MP_ROM_QSTR(MP_QSTR_reset_reason), MP_ROM_PTR(&pb_type_System_reset_reason_obj) }, #endif // PBDRV_CONFIG_RESET