Skip to content

Commit 1f50b09

Browse files
committed
pybricks.common.Motor: Share code with DCMotor.
We can save on code size by sharing everything and setting a reduced static table size for the DCMotor. This also works well conceptually since Motor is inherited from DMotor. Further savings are obtained by using a single obj type, and a common make_new implementation that returns early for the DCMotor. Also fixes the missing close() method on Motor(). Fixes pybricks/support#904
1 parent 6905609 commit 1f50b09

File tree

8 files changed

+232
-325
lines changed

8 files changed

+232
-325
lines changed

bricks/_common/sources.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ PYBRICKS_PYBRICKS_SRC_C = $(addprefix pybricks/,\
2828
common/pb_type_colorlight_external.c \
2929
common/pb_type_colorlight_internal.c \
3030
common/pb_type_control.c \
31-
common/pb_type_dcmotor.c \
3231
common/pb_type_device.c \
3332
common/pb_type_imu.c \
3433
common/pb_type_keypad.c \

pybricks/common.h

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <pybricks/pupdevices.h>
2424
#include <pybricks/tools.h>
2525
#include <pybricks/tools/pb_type_awaitable.h>
26+
#include <pybricks/common/pb_type_device.h>
2627

2728
void pb_package_pybricks_init(bool import_all);
2829
void pb_package_pybricks_deinit(void);
@@ -99,12 +100,11 @@ mp_obj_t pb_type_MotorModel_obj_make_new(pbio_observer_t *observer);
99100
mp_obj_t common_Logger_obj_make_new(pbio_log_t *log, uint8_t num_values);
100101
#endif
101102

102-
typedef struct _common_Motor_obj_t common_Motor_obj_t;
103-
104-
// pybricks._common.Motor()
105-
struct _common_Motor_obj_t {
106-
mp_obj_base_t base;
103+
// pybricks.common.DCMotor and pybricks.common.Motor
104+
typedef struct {
105+
pb_type_device_obj_base_t device_base;
107106
pbio_servo_t *srv;
107+
pbio_port_id_t port;
108108
#if PYBRICKS_PY_COMMON_MOTOR_MODEL
109109
mp_obj_t model;
110110
#endif
@@ -114,28 +114,11 @@ struct _common_Motor_obj_t {
114114
#if PYBRICKS_PY_COMMON_LOGGER
115115
mp_obj_t logger;
116116
#endif
117-
pbio_port_id_t port;
118-
mp_obj_t awaitables;
119-
};
117+
} pb_type_Motor_obj_t;
120118

121119
extern const mp_obj_type_t pb_type_Motor;
122-
123-
// pybricks._common.DCMotor()
124-
typedef struct _common_DCMotor_obj_t {
125-
mp_obj_base_t base;
126-
pbio_dcmotor_t *dcmotor;
127-
pbio_port_id_t port;
128-
} common_DCMotor_obj_t;
129-
130120
extern const mp_obj_type_t pb_type_DCMotor;
131121

132-
// Nonstatic objects shared between Motor and DCMotor
133-
void common_DCMotor_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
134-
MP_DECLARE_CONST_FUN_OBJ_KW(common_DCMotor_duty_obj);
135-
MP_DECLARE_CONST_FUN_OBJ_1(common_DCMotor_stop_obj);
136-
MP_DECLARE_CONST_FUN_OBJ_1(common_DCMotor_brake_obj);
137-
MP_DECLARE_CONST_FUN_OBJ_KW(common_DCMotor_dc_settings_obj);
138-
139122
#endif // PYBRICKS_PY_COMMON_MOTORS
140123

141124
#if PYBRICKS_PY_COMMON_SPEAKER

pybricks/common/pb_type_dcmotor.c

Lines changed: 0 additions & 158 deletions
This file was deleted.

pybricks/common/pb_type_device.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,19 @@ mp_obj_t pb_type_device_set_data(pb_type_device_obj_base_t *sensor, uint8_t mode
104104
PB_TYPE_AWAITABLE_OPT_RAISE_ON_BUSY);
105105
}
106106

107-
void pb_type_device_init_class(pb_type_device_obj_base_t *self, mp_obj_t port_in, pbdrv_legodev_type_id_t valid_id) {
107+
pbdrv_legodev_type_id_t pb_type_device_init_class(pb_type_device_obj_base_t *self, mp_obj_t port_in, pbdrv_legodev_type_id_t valid_id) {
108108

109109
pb_module_tools_assert_blocking();
110110

111111
pbio_port_id_t port = pb_type_enum_get_value(port_in, &pb_enum_type_Port);
112112
pbio_error_t err;
113-
while ((err = pbdrv_legodev_get_device(port, &valid_id, &self->legodev)) == PBIO_ERROR_AGAIN) {
113+
pbdrv_legodev_type_id_t actual_id = valid_id;
114+
while ((err = pbdrv_legodev_get_device(port, &actual_id, &self->legodev)) == PBIO_ERROR_AGAIN) {
114115
mp_hal_delay_ms(50);
115116
}
116117
pb_assert(err);
117-
118118
self->awaitables = mp_obj_new_list(0, NULL);
119+
return actual_id;
119120
}
120121

121122
#endif // PYBRICKS_PY_PUPDEVICES

pybricks/common/pb_type_device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ extern const mp_obj_type_t pb_type_device_method;
4242

4343
mp_obj_t pb_type_device_method_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
4444
mp_obj_t pb_type_pupdevices_method(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args);
45-
void pb_type_device_init_class(pb_type_device_obj_base_t *self, mp_obj_t port_in, pbdrv_legodev_type_id_t valid_id);
45+
pbdrv_legodev_type_id_t pb_type_device_init_class(pb_type_device_obj_base_t *self, mp_obj_t port_in, pbdrv_legodev_type_id_t valid_id);
4646
mp_obj_t pb_type_device_set_data(pb_type_device_obj_base_t *sensor, uint8_t mode, const void *data, uint8_t size);
4747
void *pb_type_device_get_data(mp_obj_t self_in, uint8_t mode);
4848

0 commit comments

Comments
 (0)