Skip to content

Commit f0fe982

Browse files
committed
codal_port/modpower: Merge pins/buttons argument to wake_on.
Signed-off-by: Damien George <[email protected]>
1 parent 8364035 commit f0fe982

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

src/codal_port/modpower.c

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,10 @@ STATIC mp_obj_t power_off(void) {
5151
STATIC MP_DEFINE_CONST_FUN_OBJ_0(power_off_obj, power_off);
5252

5353
STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
54-
enum { ARG_ms, ARG_pins, ARG_buttons, ARG_run_every };
54+
enum { ARG_ms, ARG_wake_on, ARG_run_every };
5555
static const mp_arg_t allowed_args[] = {
5656
{ MP_QSTR_ms, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
57-
{ MP_QSTR_pins, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
58-
{ MP_QSTR_buttons, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
57+
{ MP_QSTR_wake_on, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
5958
{ MP_QSTR_run_every, MP_ARG_BOOL, {.u_bool = false} },
6059
};
6160

@@ -64,25 +63,17 @@ STATIC mp_obj_t power_deep_sleep(size_t n_args, const mp_obj_t *pos_args, mp_map
6463

6564
microbit_hal_power_clear_wake_sources();
6665

67-
if (args[ARG_pins].u_obj != mp_const_none) {
68-
mp_obj_t *items;
69-
size_t len = get_array(&args[ARG_pins].u_obj, &items);
70-
for (size_t i = 0; i < len; ++i) {
66+
// Configure wake-up sources.
67+
mp_obj_t *items;
68+
size_t len = get_array(&args[ARG_wake_on].u_obj, &items);
69+
for (size_t i = 0; i < len; ++i) {
70+
const mp_obj_type_t *type = mp_obj_get_type(items[i]);
71+
if (microbit_obj_type_is_button(type)) {
72+
microbit_hal_power_wake_on_button(microbit_obj_get_button_id(items[i]), true);
73+
} else if (microbit_obj_type_is_pin(type)) {
7174
microbit_hal_power_wake_on_pin(microbit_obj_get_pin_name(items[i]), true);
72-
}
73-
}
74-
75-
if (args[ARG_buttons].u_obj != mp_const_none) {
76-
mp_obj_t *items;
77-
size_t len = get_array(&args[ARG_buttons].u_obj, &items);
78-
for (size_t i = 0; i < len; ++i) {
79-
if (items[i] == MP_OBJ_FROM_PTR(&microbit_button_a_obj)) {
80-
microbit_hal_power_wake_on_button(0, true);
81-
} else if (items[i] == MP_OBJ_FROM_PTR(&microbit_button_b_obj)) {
82-
microbit_hal_power_wake_on_button(1, true);
83-
} else {
84-
mp_raise_ValueError(MP_ERROR_TEXT("expecting a button"));
85-
}
75+
} else {
76+
mp_raise_ValueError(MP_ERROR_TEXT("expecting a pin or button"));
8677
}
8778
}
8879

0 commit comments

Comments
 (0)