Skip to content

Commit 5f45dda

Browse files
committed
pybricks.common: Pass parent object to keypad and light.
There may be more than one in general, so pass relevant reference.
1 parent 5294ec8 commit 5f45dda

16 files changed

+35
-32
lines changed

pybricks/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ void pb_type_LightMatrix_display_char(pbio_light_matrix_t *light_matrix, mp_obj_
6868

6969
#if PYBRICKS_PY_COMMON_KEYPAD
7070
// pybricks._common.KeyPad()
71-
mp_obj_t pb_type_Keypad_obj_new(pb_type_button_get_pressed_t get_pressed);
71+
mp_obj_t pb_type_Keypad_obj_new(mp_obj_t parent_obj, pb_type_button_get_pressed_t get_pressed);
7272
#endif
7373

7474
// pybricks._common.Battery()

pybricks/common/pb_type_colorlight_external.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
// pybricks._common.ColorLight class object
2121
typedef struct {
2222
mp_obj_base_t base;
23-
void *context;
23+
mp_obj_t parent_obj;
2424
pb_type_ColorLight_on_t on;
2525

2626
} common_ColorLight_external_obj_t;
@@ -32,14 +32,14 @@ static mp_obj_t common_ColorLight_external_on(size_t n_args, const mp_obj_t *pos
3232
common_ColorLight_external_obj_t, self,
3333
PB_ARG_REQUIRED(color));
3434

35-
return self->on(self->context, pb_type_Color_get_hsv(color_in));
35+
return self->on(self->parent_obj, pb_type_Color_get_hsv(color_in));
3636
}
3737
static MP_DEFINE_CONST_FUN_OBJ_KW(common_ColorLight_external_on_obj, 1, common_ColorLight_external_on);
3838

3939
// pybricks._common.ColorLight.off
4040
static mp_obj_t common_ColorLight_external_off(mp_obj_t self_in) {
4141
common_ColorLight_external_obj_t *self = MP_OBJ_TO_PTR(self_in);
42-
return self->on(self->context, &pb_Color_NONE_obj.hsv);
42+
return self->on(self->parent_obj, &pb_Color_NONE_obj.hsv);
4343
}
4444
static MP_DEFINE_CONST_FUN_OBJ_1(common_ColorLight_external_off_obj, common_ColorLight_external_off);
4545

@@ -57,9 +57,9 @@ static MP_DEFINE_CONST_OBJ_TYPE(pb_type_ColorLight_external,
5757
locals_dict, &common_ColorLight_external_locals_dict);
5858

5959
// pybricks._common.ColorLight.__init__
60-
mp_obj_t pb_type_ColorLight_external_obj_new(void *context, pb_type_ColorLight_on_t on) {
60+
mp_obj_t pb_type_ColorLight_external_obj_new(mp_obj_t parent_obj, pb_type_ColorLight_on_t on) {
6161
common_ColorLight_external_obj_t *light = mp_obj_malloc(common_ColorLight_external_obj_t, &pb_type_ColorLight_external);
62-
light->context = context;
62+
light->parent_obj = parent_obj;
6363
light->on = on;
6464
return MP_OBJ_FROM_PTR(light);
6565
}

pybricks/common/pb_type_keypad.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
// pybricks._common.Keypad class object
1818
typedef struct _common_Keypad_obj_t {
1919
mp_obj_base_t base;
20+
mp_obj_t parent_obj;
2021
pb_type_button_get_pressed_t get_pressed;
2122
} common_Keypad_obj_t;
2223

2324
// pybricks._common.Keypad.pressed
2425
static mp_obj_t common_Keypad_pressed(mp_obj_t self_in) {
2526
common_Keypad_obj_t *self = MP_OBJ_TO_PTR(self_in);
26-
return self->get_pressed();
27+
return self->get_pressed(self->parent_obj);
2728
}
2829
MP_DEFINE_CONST_FUN_OBJ_1(common_Keypad_pressed_obj, common_Keypad_pressed);
2930

@@ -40,9 +41,10 @@ static MP_DEFINE_CONST_OBJ_TYPE(pb_type_Keypad,
4041
locals_dict, &common_Keypad_locals_dict);
4142

4243
// pybricks._common.Keypad.__init__
43-
mp_obj_t pb_type_Keypad_obj_new(pb_type_button_get_pressed_t get_pressed) {
44+
mp_obj_t pb_type_Keypad_obj_new(mp_obj_t parent_obj, pb_type_button_get_pressed_t get_pressed) {
4445
common_Keypad_obj_t *self = mp_obj_malloc(common_Keypad_obj_t, &pb_type_Keypad);
4546
self->get_pressed = get_pressed;
47+
self->parent_obj = parent_obj;
4648
return MP_OBJ_FROM_PTR(self);
4749
}
4850

pybricks/hubs/pb_type_cityhub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static mp_obj_t hubs_CityHub_make_new(const mp_obj_type_t *type, size_t n_args,
3737
#if PYBRICKS_PY_COMMON_BLE
3838
self->ble = pb_type_BLE_new(broadcast_channel_in, observe_channels_in);
3939
#endif
40-
self->button = pb_type_Keypad_obj_new(pb_type_button_pressed_hub_single_button);
40+
self->button = pb_type_Keypad_obj_new(MP_OBJ_FROM_PTR(self), pb_type_button_pressed_hub_single_button);
4141
self->light = common_ColorLight_internal_obj_new(pbsys_status_light_main);
4242
self->system = MP_OBJ_FROM_PTR(&pb_type_System);
4343
return MP_OBJ_FROM_PTR(self);

pybricks/hubs/pb_type_essentialhub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static mp_obj_t hubs_EssentialHub_make_new(const mp_obj_type_t *type, size_t n_a
5252
#if PYBRICKS_PY_COMMON_BLE
5353
self->ble = pb_type_BLE_new(broadcast_channel_in, observe_channels_in);
5454
#endif
55-
self->buttons = pb_type_Keypad_obj_new(pb_type_button_pressed_hub_single_button);
55+
self->buttons = pb_type_Keypad_obj_new(MP_OBJ_FROM_PTR(self), pb_type_button_pressed_hub_single_button);
5656
self->charger = pb_type_Charger_obj_new();
5757
self->imu = pb_type_IMU_obj_new(MP_OBJ_FROM_PTR(self), top_side_in, front_side_in);
5858
self->light = common_ColorLight_internal_obj_new(pbsys_status_light_main);

pybricks/hubs/pb_type_ev3brick.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef struct _hubs_EV3Brick_obj_t {
2424
mp_obj_t system;
2525
} hubs_EV3Brick_obj_t;
2626

27-
static mp_obj_t pb_type_ev3brick_button_pressed(void) {
27+
static mp_obj_t pb_type_ev3brick_button_pressed(mp_obj_t parent_obj) {
2828
pbio_button_flags_t flags = pbdrv_button_get_pressed();
2929
mp_obj_t pressed[5];
3030
size_t num = 0;
@@ -50,7 +50,7 @@ static mp_obj_t hubs_EV3Brick_make_new(const mp_obj_type_t *type, size_t n_args,
5050
hubs_EV3Brick_obj_t *self = mp_obj_malloc(hubs_EV3Brick_obj_t, type);
5151

5252
self->battery = MP_OBJ_FROM_PTR(&pb_module_battery);
53-
self->buttons = pb_type_Keypad_obj_new(pb_type_ev3brick_button_pressed);
53+
self->buttons = pb_type_Keypad_obj_new(MP_OBJ_FROM_PTR(self), pb_type_ev3brick_button_pressed);
5454
self->light = common_ColorLight_internal_obj_new(pbsys_status_light_main);
5555
self->screen = pb_type_Image_display_obj_new();
5656
self->speaker = mp_call_function_0(MP_OBJ_FROM_PTR(&pb_type_Speaker));

pybricks/hubs/pb_type_movehub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ static mp_obj_t hubs_MoveHub_make_new(const mp_obj_type_t *type, size_t n_args,
329329
#if PYBRICKS_PY_COMMON_BLE
330330
self->ble = pb_type_BLE_new(broadcast_channel_in, observe_channels_in);
331331
#endif
332-
self->button = pb_type_Keypad_obj_new(pb_type_button_pressed_hub_single_button);
332+
self->button = pb_type_Keypad_obj_new(MP_OBJ_FROM_PTR(self), pb_type_button_pressed_hub_single_button);
333333
self->imu = hubs_MoveHub_IMU_make_new(top_side_in, front_side_in);
334334
self->light = common_ColorLight_internal_obj_new(pbsys_status_light_main);
335335
self->system = MP_OBJ_FROM_PTR(&pb_type_System);

pybricks/hubs/pb_type_nxtbrick.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ typedef struct _hubs_NXTBrick_obj_t {
2525
mp_obj_t system;
2626
} hubs_NXTBrick_obj_t;
2727

28-
static mp_obj_t pb_type_nxtbrick_button_pressed(void) {
28+
static mp_obj_t pb_type_nxtbrick_button_pressed(mp_obj_t parent_obj) {
2929
pbio_button_flags_t flags = pbdrv_button_get_pressed();
3030
mp_obj_t pressed[4];
3131
size_t num = 0;
@@ -47,7 +47,7 @@ static mp_obj_t pb_type_nxtbrick_button_pressed(void) {
4747
static mp_obj_t hubs_NXTBrick_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
4848
hubs_NXTBrick_obj_t *self = mp_obj_malloc(hubs_NXTBrick_obj_t, type);
4949
self->battery = MP_OBJ_FROM_PTR(&pb_module_battery);
50-
self->buttons = pb_type_Keypad_obj_new(pb_type_nxtbrick_button_pressed);
50+
self->buttons = pb_type_Keypad_obj_new(MP_OBJ_FROM_PTR(self), pb_type_nxtbrick_button_pressed);
5151
self->speaker = mp_call_function_0(MP_OBJ_FROM_PTR(&pb_type_Speaker));
5252
self->system = MP_OBJ_FROM_PTR(&pb_type_System);
5353
return MP_OBJ_FROM_PTR(self);

pybricks/hubs/pb_type_primehub.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct _hubs_PrimeHub_obj_t {
4141
mp_obj_t system;
4242
} hubs_PrimeHub_obj_t;
4343

44-
static mp_obj_t pb_type_primehub_button_pressed(void) {
44+
static mp_obj_t pb_type_primehub_button_pressed(mp_obj_t parent_obj) {
4545
pbio_button_flags_t flags = pbdrv_button_get_pressed();
4646
mp_obj_t pressed[4];
4747
size_t num = 0;
@@ -75,7 +75,7 @@ static mp_obj_t hubs_PrimeHub_make_new(const mp_obj_type_t *type, size_t n_args,
7575
#if PYBRICKS_PY_COMMON_BLE
7676
self->ble = pb_type_BLE_new(broadcast_channel_in, observe_channels_in);
7777
#endif
78-
self->buttons = pb_type_Keypad_obj_new(pb_type_primehub_button_pressed);
78+
self->buttons = pb_type_Keypad_obj_new(MP_OBJ_FROM_PTR(self), pb_type_primehub_button_pressed);
7979
self->charger = pb_type_Charger_obj_new();
8080
self->display = pb_type_LightMatrix_obj_new(pbsys_hub_light_matrix);
8181
self->imu = pb_type_IMU_obj_new(MP_OBJ_FROM_PTR(self), top_side_in, front_side_in);

pybricks/hubs/pb_type_technichub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static mp_obj_t hubs_TechnicHub_make_new(const mp_obj_type_t *type, size_t n_arg
4242
#if PYBRICKS_PY_COMMON_BLE
4343
self->ble = pb_type_BLE_new(broadcast_channel_in, observe_channels_in);
4444
#endif
45-
self->button = pb_type_Keypad_obj_new(pb_type_button_pressed_hub_single_button);
45+
self->button = pb_type_Keypad_obj_new(MP_OBJ_FROM_PTR(self), pb_type_button_pressed_hub_single_button);
4646
self->imu = pb_type_IMU_obj_new(MP_OBJ_FROM_PTR(self), top_side_in, front_side_in);
4747
self->light = common_ColorLight_internal_obj_new(pbsys_status_light_main);
4848
self->system = MP_OBJ_FROM_PTR(&pb_type_System);

0 commit comments

Comments
 (0)