Skip to content

Commit 26f7701

Browse files
committed
pybricks.common.System: Add info dictionary.
Can be used to indicate various hub states instead of adding a new method for each. See pybricks/support#1496
1 parent ad111f3 commit 26f7701

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
coding ([support#1661]).
1212
- Added `observe_enable` to the hub `BLE` class to selectively turn observing
1313
on and off, just like you can with broadcasting ([support#1806]).
14+
- Added `hub.system.info()` method with hub status flags ([support#1496]).
1415

1516
### Changed
1617

@@ -31,6 +32,7 @@
3132
at exact multiples of its animation interval ([support#1295]).
3233

3334
[support#1295]: https://github.com/pybricks/support/issues/1295
35+
[support#1496]: https://github.com/pybricks/support/issues/1496
3436
[support#1623]: https://github.com/pybricks/support/issues/1623
3537
[support#1661]: https://github.com/pybricks/support/issues/1661
3638
[support#1668]: https://github.com/pybricks/support/issues/1668

pybricks/common/pb_type_system.c

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
#include <string.h>
99

1010
#include <pbdrv/bluetooth.h>
11+
#include <pbdrv/reset.h>
12+
#include <pbsys/program_stop.h>
13+
#include <pbsys/status.h>
1114
#include <pbsys/storage.h>
1215

1316
#include "py/obj.h"
1417
#include "py/objstr.h"
1518
#include "py/runtime.h"
1619

1720
#include <pybricks/common.h>
21+
#include <pybricks/parameters.h>
1822
#include <pybricks/util_pb/pb_error.h>
1923
#include <pybricks/util_mp/pb_kwarg_helper.h>
2024
#include <pybricks/util_mp/pb_obj_helper.h>
@@ -27,8 +31,6 @@ static MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_name_obj, pb_type_System_name);
2731

2832
#if PBDRV_CONFIG_RESET
2933

30-
#include <pbdrv/reset.h>
31-
3234
static mp_obj_t pb_type_System_reset_reason(void) {
3335
pbdrv_reset_reason_t reason = pbdrv_reset_get_reason();
3436
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
3739

3840
#endif // PBDRV_CONFIG_RESET
3941

40-
#if PBIO_CONFIG_ENABLE_SYS
42+
static mp_obj_t pb_type_System_info(void) {
43+
const char *hub_name = pbdrv_bluetooth_get_hub_name();
4144

42-
#include <pbsys/status.h>
43-
#include <pbsys/program_stop.h>
45+
mp_map_elem_t info[] = {
46+
{MP_OBJ_NEW_QSTR(MP_QSTR_name), mp_obj_new_str(hub_name, strlen(hub_name))},
47+
#if PBDRV_CONFIG_RESET
48+
{MP_OBJ_NEW_QSTR(MP_QSTR_reset_reason), mp_obj_new_int(pbdrv_reset_get_reason())},
49+
#endif // PBDRV_CONFIG_RESET
50+
{MP_OBJ_NEW_QSTR(MP_QSTR_host_connected_ble), mp_obj_new_bool(pbsys_status_test(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED))},
51+
};
52+
mp_obj_t info_dict = mp_obj_new_dict(MP_ARRAY_SIZE(info));
53+
54+
for (size_t i = 0; i < MP_ARRAY_SIZE(info); i++) {
55+
mp_map_elem_t *elem = &info[i];
56+
mp_obj_dict_store(info_dict, elem->key, elem->value);
57+
}
4458

45-
#include <pybricks/parameters.h>
59+
return info_dict;
60+
}
61+
static MP_DEFINE_CONST_FUN_OBJ_0(pb_type_System_info_obj, pb_type_System_info);
62+
63+
#if PBIO_CONFIG_ENABLE_SYS
4664

4765
static mp_obj_t pb_type_System_set_stop_button(mp_obj_t buttons_in) {
4866
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_
127145
// dir(pybricks.common.System)
128146
static const mp_rom_map_elem_t common_System_locals_dict_table[] = {
129147
{ MP_ROM_QSTR(MP_QSTR_name), MP_ROM_PTR(&pb_type_System_name_obj) },
148+
{ MP_ROM_QSTR(MP_QSTR_info), MP_ROM_PTR(&pb_type_System_info_obj) },
130149
#if PBDRV_CONFIG_RESET
131150
{ MP_ROM_QSTR(MP_QSTR_reset_reason), MP_ROM_PTR(&pb_type_System_reset_reason_obj) },
132151
#endif // PBDRV_CONFIG_RESET

0 commit comments

Comments
 (0)