From 547e81e009ce5baaa57c07461d154e7277601718 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Wed, 26 May 2021 11:22:44 +0200 Subject: [PATCH] pybricks.hubs: Add system instance attribute. This will group various system functions such as resetting and system data such as bluetooth address: https://github.com/pybricks/support/issues/321 For now, the system attribute will be constant and read-only, so we can use a constant instance to save some code size and RAM. --- bricks/cityhub/mpconfigport.h | 1 + bricks/ev3dev/Makefile | 1 + bricks/ev3dev/brickconfig.h | 1 + bricks/movehub/mpconfigport.h | 1 + bricks/nxt/Makefile | 1 + bricks/nxt/mpconfigport.h | 1 + bricks/primehub/mpconfigport.h | 1 + bricks/stm32/stm32.mk | 1 + bricks/technichub/mpconfigport.h | 1 + pybricks/common.h | 7 ++++++ pybricks/common/pb_type_system.c | 37 ++++++++++++++++++++++++++++++ pybricks/hubs/pb_type_cityhub.c | 1 + pybricks/hubs/pb_type_ev3brick.c | 1 + pybricks/hubs/pb_type_movehub.c | 5 +++- pybricks/hubs/pb_type_nxtbrick.c | 1 + pybricks/hubs/pb_type_primehub.c | 1 + pybricks/hubs/pb_type_technichub.c | 1 + 17 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 pybricks/common/pb_type_system.c diff --git a/bricks/cityhub/mpconfigport.h b/bricks/cityhub/mpconfigport.h index de99cfcf0..500305855 100644 --- a/bricks/cityhub/mpconfigport.h +++ b/bricks/cityhub/mpconfigport.h @@ -18,6 +18,7 @@ #define PYBRICKS_PY_COMMON_KEYPAD (0) #define PYBRICKS_PY_COMMON_LIGHT_MATRIX (0) #define PYBRICKS_PY_COMMON_MOTORS (1) +#define PYBRICKS_PY_COMMON_SYSTEM (1) #define PYBRICKS_PY_EXPERIMENTAL (1) #define PYBRICKS_PY_GEOMETRY (1) #define PYBRICKS_PY_HUBS (1) diff --git a/bricks/ev3dev/Makefile b/bricks/ev3dev/Makefile index cdaf1d62a..e83487af6 100644 --- a/bricks/ev3dev/Makefile +++ b/bricks/ev3dev/Makefile @@ -247,6 +247,7 @@ PYBRICKS_PYBRICKS_SRC_C = $(addprefix pybricks/,\ common/pb_type_lightarray.c \ common/pb_type_logger.c \ common/pb_type_motor.c \ + common/pb_type_system.c \ experimental/pb_module_experimental.c \ ev3devices/pb_module_ev3devices.c \ ev3devices/pb_type_ev3devices_colorsensor.c \ diff --git a/bricks/ev3dev/brickconfig.h b/bricks/ev3dev/brickconfig.h index d19581c52..3612072f4 100644 --- a/bricks/ev3dev/brickconfig.h +++ b/bricks/ev3dev/brickconfig.h @@ -17,6 +17,7 @@ #define PYBRICKS_PY_COMMON_KEYPAD (1) #define PYBRICKS_PY_COMMON_LIGHT_MATRIX (0) #define PYBRICKS_PY_COMMON_MOTORS (1) +#define PYBRICKS_PY_COMMON_SYSTEM (1) #define PYBRICKS_PY_EV3DEVICES (1) #define PYBRICKS_PY_EXPERIMENTAL (1) #define PYBRICKS_PY_GEOMETRY (1) diff --git a/bricks/movehub/mpconfigport.h b/bricks/movehub/mpconfigport.h index 2b0a95fca..0f2d20d6e 100644 --- a/bricks/movehub/mpconfigport.h +++ b/bricks/movehub/mpconfigport.h @@ -18,6 +18,7 @@ #define PYBRICKS_PY_COMMON_KEYPAD (0) #define PYBRICKS_PY_COMMON_LIGHT_MATRIX (0) #define PYBRICKS_PY_COMMON_MOTORS (1) +#define PYBRICKS_PY_COMMON_SYSTEM (1) #define PYBRICKS_PY_EXPERIMENTAL (0) #define PYBRICKS_PY_GEOMETRY (0) #define PYBRICKS_PY_HUBS (1) diff --git a/bricks/nxt/Makefile b/bricks/nxt/Makefile index 3f5f4417a..f73df1122 100644 --- a/bricks/nxt/Makefile +++ b/bricks/nxt/Makefile @@ -144,6 +144,7 @@ PYBRICKS_PYBRICKS_SRC_C = $(addprefix pybricks/,\ common/pb_type_dcmotor.c \ common/pb_type_logger.c \ common/pb_type_motor.c \ + common/pb_type_system.c \ experimental/pb_module_experimental.c \ geometry/pb_module_geometry.c \ geometry/pb_type_matrix.c \ diff --git a/bricks/nxt/mpconfigport.h b/bricks/nxt/mpconfigport.h index 661a17b1d..78f4980e6 100644 --- a/bricks/nxt/mpconfigport.h +++ b/bricks/nxt/mpconfigport.h @@ -16,6 +16,7 @@ #define PYBRICKS_PY_COMMON_KEYPAD (1) #define PYBRICKS_PY_COMMON_LIGHT_MATRIX (0) #define PYBRICKS_PY_COMMON_MOTORS (1) +#define PYBRICKS_PY_COMMON_SYSTEM (1) #define PYBRICKS_PY_EXPERIMENTAL (0) #define PYBRICKS_PY_GEOMETRY (0) #define PYBRICKS_PY_HUBS (1) diff --git a/bricks/primehub/mpconfigport.h b/bricks/primehub/mpconfigport.h index 177e87e59..d1641fab5 100644 --- a/bricks/primehub/mpconfigport.h +++ b/bricks/primehub/mpconfigport.h @@ -19,6 +19,7 @@ #define PYBRICKS_PY_COMMON_KEYPAD (1) #define PYBRICKS_PY_COMMON_LIGHT_MATRIX (1) #define PYBRICKS_PY_COMMON_MOTORS (1) +#define PYBRICKS_PY_COMMON_SYSTEM (1) #define PYBRICKS_PY_COMMON_SPEAKER (1) #define PYBRICKS_PY_EXPERIMENTAL (1) #define PYBRICKS_PY_GEOMETRY (1) diff --git a/bricks/stm32/stm32.mk b/bricks/stm32/stm32.mk index 633af7b72..455aa2fc5 100644 --- a/bricks/stm32/stm32.mk +++ b/bricks/stm32/stm32.mk @@ -204,6 +204,7 @@ PYBRICKS_PYBRICKS_SRC_C = $(addprefix pybricks/,\ common/pb_type_logger.c \ common/pb_type_motor.c \ common/pb_type_speaker.c \ + common/pb_type_system.c \ experimental/pb_module_experimental.c \ geometry/pb_module_geometry.c \ geometry/pb_type_matrix.c \ diff --git a/bricks/technichub/mpconfigport.h b/bricks/technichub/mpconfigport.h index a0fdb3343..a5b996c5c 100644 --- a/bricks/technichub/mpconfigport.h +++ b/bricks/technichub/mpconfigport.h @@ -18,6 +18,7 @@ #define PYBRICKS_PY_COMMON_KEYPAD (0) #define PYBRICKS_PY_COMMON_LIGHT_MATRIX (0) #define PYBRICKS_PY_COMMON_MOTORS (1) +#define PYBRICKS_PY_COMMON_SYSTEM (1) #define PYBRICKS_PY_EXPERIMENTAL (1) #define PYBRICKS_PY_GEOMETRY (1) #define PYBRICKS_PY_HUBS (1) diff --git a/pybricks/common.h b/pybricks/common.h index 791ba29d4..f25557915 100644 --- a/pybricks/common.h +++ b/pybricks/common.h @@ -94,6 +94,13 @@ mp_obj_t pb_type_IMU_obj_new(mp_obj_t top_side_axis, mp_obj_t front_side_axis); #endif // PYBRICKS_PY_COMMON_IMU + +#if PYBRICKS_PY_COMMON_SYSTEM + +extern const mp_obj_base_t pb_type_System_obj; + +#endif // PYBRICKS_PY_COMMON_SYSTEM + #endif // PYBRICKS_PY_COMMON #endif // PYBRICKS_INCLUDED_PYBRICKS_COMMON_H diff --git a/pybricks/common/pb_type_system.c b/pybricks/common/pb_type_system.c new file mode 100644 index 000000000..01376b36c --- /dev/null +++ b/pybricks/common/pb_type_system.c @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2018-2020 The Pybricks Authors + +#include "py/mpconfig.h" + +#if PYBRICKS_PY_COMMON && PYBRICKS_PY_COMMON_SYSTEM + +#include "py/obj.h" + +#include + +#include + +// pybricks.common.System class object +typedef struct _pb_type_System_obj_t { + mp_obj_base_t base; +} pb_type_System_obj_t; + +// dir(pybricks.common.System) +STATIC const mp_rom_map_elem_t common_System_locals_dict_table[] = { +}; +STATIC MP_DEFINE_CONST_DICT(common_System_locals_dict, common_System_locals_dict_table); + +// type(pybricks.common.System) +STATIC const mp_obj_type_t pb_type_System = { + { &mp_type_type }, + .name = MP_QSTR_System, + .locals_dict = (mp_obj_dict_t *)&common_System_locals_dict, +}; + +// Preinstantiated constant singleton +const mp_obj_base_t pb_type_System_obj = { + &pb_type_System +}; + + +#endif // PYBRICKS_PY_COMMON && PYBRICKS_PY_COMMON_SYSTEM diff --git a/pybricks/hubs/pb_type_cityhub.c b/pybricks/hubs/pb_type_cityhub.c index 32e180bc6..7f155029c 100644 --- a/pybricks/hubs/pb_type_cityhub.c +++ b/pybricks/hubs/pb_type_cityhub.c @@ -26,6 +26,7 @@ STATIC mp_obj_t hubs_CityHub_make_new(const mp_obj_type_t *type, size_t n_args, STATIC const mp_rom_map_elem_t hubs_CityHub_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_battery), MP_ROM_PTR(&pb_module_battery) }, { MP_ROM_QSTR(MP_QSTR_light), MP_ROM_ATTRIBUTE_OFFSET(hubs_CityHub_obj_t, light) }, + { MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&pb_type_System_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&pb_hubs_Hub_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_reason),MP_ROM_PTR(&pb_hubs_Hub_reset_reason_obj) }, }; diff --git a/pybricks/hubs/pb_type_ev3brick.c b/pybricks/hubs/pb_type_ev3brick.c index 326b2674a..7b0e2aac9 100644 --- a/pybricks/hubs/pb_type_ev3brick.c +++ b/pybricks/hubs/pb_type_ev3brick.c @@ -52,6 +52,7 @@ STATIC const mp_rom_map_elem_t hubs_EV3Brick_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_light), MP_ROM_ATTRIBUTE_OFFSET(hubs_EV3Brick_obj_t, light) }, { MP_ROM_QSTR(MP_QSTR_screen), MP_ROM_ATTRIBUTE_OFFSET(hubs_EV3Brick_obj_t, screen) }, { MP_ROM_QSTR(MP_QSTR_speaker), MP_ROM_ATTRIBUTE_OFFSET(hubs_EV3Brick_obj_t, speaker) }, + { MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&pb_type_System_obj) }, }; STATIC MP_DEFINE_CONST_DICT(hubs_EV3Brick_locals_dict, hubs_EV3Brick_locals_dict_table); diff --git a/pybricks/hubs/pb_type_movehub.c b/pybricks/hubs/pb_type_movehub.c index 01ec56970..59a539f5c 100644 --- a/pybricks/hubs/pb_type_movehub.c +++ b/pybricks/hubs/pb_type_movehub.c @@ -261,8 +261,11 @@ STATIC mp_obj_t hubs_MoveHub_make_new(const mp_obj_type_t *type, size_t n_args, STATIC const mp_rom_map_elem_t hubs_MoveHub_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_battery), MP_ROM_PTR(&pb_module_battery) }, - { MP_ROM_QSTR(MP_QSTR_imu), MP_ROM_ATTRIBUTE_OFFSET(hubs_MoveHub_obj_t, imu) }, + { MP_ROM_QSTR(MP_QSTR_imu), MP_ROM_ATTRIBUTE_OFFSET(hubs_MoveHub_obj_t, imu) }, { MP_ROM_QSTR(MP_QSTR_light), MP_ROM_ATTRIBUTE_OFFSET(hubs_MoveHub_obj_t, light) }, + #if PYBRICKS_PY_COMMON_SYSTEM + { MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&pb_type_System_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&pb_hubs_Hub_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_reason),MP_ROM_PTR(&pb_hubs_Hub_reset_reason_obj) }, }; diff --git a/pybricks/hubs/pb_type_nxtbrick.c b/pybricks/hubs/pb_type_nxtbrick.c index 0c454ca52..7b923dafa 100644 --- a/pybricks/hubs/pb_type_nxtbrick.c +++ b/pybricks/hubs/pb_type_nxtbrick.c @@ -32,6 +32,7 @@ STATIC const mp_rom_map_elem_t hubs_NXTBrick_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_buttons), MP_ROM_ATTRIBUTE_OFFSET(hubs_NXTBrick_obj_t, buttons) }, { MP_ROM_QSTR(MP_QSTR_battery), MP_ROM_PTR(&pb_module_battery) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&pb_hubs_Hub_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&pb_type_System_obj) }, }; STATIC MP_DEFINE_CONST_DICT(hubs_NXTBrick_locals_dict, hubs_NXTBrick_locals_dict_table); diff --git a/pybricks/hubs/pb_type_primehub.c b/pybricks/hubs/pb_type_primehub.c index a7b3a9bb8..743bbc8f0 100644 --- a/pybricks/hubs/pb_type_primehub.c +++ b/pybricks/hubs/pb_type_primehub.c @@ -59,6 +59,7 @@ STATIC const mp_rom_map_elem_t hubs_PrimeHub_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_imu), MP_ROM_ATTRIBUTE_OFFSET(hubs_PrimeHub_obj_t, imu) }, { MP_ROM_QSTR(MP_QSTR_light), MP_ROM_ATTRIBUTE_OFFSET(hubs_PrimeHub_obj_t, light) }, { MP_ROM_QSTR(MP_QSTR_speaker), MP_ROM_ATTRIBUTE_OFFSET(hubs_PrimeHub_obj_t, speaker) }, + { MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&pb_type_System_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&pb_hubs_Hub_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_reason),MP_ROM_PTR(&pb_hubs_Hub_reset_reason_obj) }, }; diff --git a/pybricks/hubs/pb_type_technichub.c b/pybricks/hubs/pb_type_technichub.c index bf4caa04f..c88d0fa24 100644 --- a/pybricks/hubs/pb_type_technichub.c +++ b/pybricks/hubs/pb_type_technichub.c @@ -36,6 +36,7 @@ STATIC const mp_rom_map_elem_t hubs_TechnicHub_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_battery), MP_ROM_PTR(&pb_module_battery) }, { MP_ROM_QSTR(MP_QSTR_imu), MP_ROM_ATTRIBUTE_OFFSET(hubs_TechnicHub_obj_t, imu) }, { MP_ROM_QSTR(MP_QSTR_light), MP_ROM_ATTRIBUTE_OFFSET(hubs_TechnicHub_obj_t, light) }, + { MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&pb_type_System_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&pb_hubs_Hub_reset_obj) }, { MP_ROM_QSTR(MP_QSTR_reset_reason),MP_ROM_PTR(&pb_hubs_Hub_reset_reason_obj) }, };