Skip to content

Commit

Permalink
pybricks.hubs: Add system instance attribute.
Browse files Browse the repository at this point in the history
This will group various system functions such as resetting and system data such as bluetooth address: pybricks/support#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.
  • Loading branch information
laurensvalk committed May 26, 2021
1 parent 1450ee0 commit 547e81e
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions bricks/cityhub/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions bricks/ev3dev/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions bricks/ev3dev/brickconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions bricks/movehub/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions bricks/nxt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions bricks/nxt/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions bricks/primehub/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions bricks/stm32/stm32.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions bricks/technichub/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions pybricks/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 37 additions & 0 deletions pybricks/common/pb_type_system.c
Original file line number Diff line number Diff line change
@@ -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 <pybricks/common.h>

#include <pybricks/util_pb/pb_error.h>

// 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
1 change: 1 addition & 0 deletions pybricks/hubs/pb_type_cityhub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
};
Expand Down
1 change: 1 addition & 0 deletions pybricks/hubs/pb_type_ev3brick.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
5 changes: 4 additions & 1 deletion pybricks/hubs/pb_type_movehub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
};
Expand Down
1 change: 1 addition & 0 deletions pybricks/hubs/pb_type_nxtbrick.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions pybricks/hubs/pb_type_primehub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
};
Expand Down
1 change: 1 addition & 0 deletions pybricks/hubs/pb_type_technichub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
};
Expand Down

0 comments on commit 547e81e

Please sign in to comment.