From ced8c2132aee682f62e4d481cc8234c84b6d3375 Mon Sep 17 00:00:00 2001 From: Rahix <rahix@rahix.de> Date: Sat, 9 Nov 2019 16:24:26 +0100 Subject: [PATCH] feat(buttons): Allow calling buttons.read() without args Allow calling buttons.read() without an argument. This is equivalent to a mask with all bits set (that is, buttons.read(255)). Additionally, add a check that the mask is within the limits (< 256). Signed-off-by: Rahix <rahix@rahix.de> --- pycardium/modules/buttons.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pycardium/modules/buttons.c b/pycardium/modules/buttons.c index ab8dabd7..ace61e72 100644 --- a/pycardium/modules/buttons.c +++ b/pycardium/modules/buttons.c @@ -5,13 +5,24 @@ #include "epicardium.h" -static mp_obj_t mp_buttons_read(mp_obj_t mask_in) +static mp_obj_t mp_buttons_read(size_t n_args, const mp_obj_t *args) { - uint8_t mask = mp_obj_get_int(mask_in); + uint8_t mask; + if (n_args == 1) { + mp_int_t mask_int = mp_obj_get_int(args[0]); + if (mask_int > 255) { + mp_raise_ValueError("mask must be less than 256"); + } + mask = (uint8_t)mask_int; + } else { + mask = 0xff; + } uint8_t button_states = epic_buttons_read(mask); return MP_OBJ_NEW_SMALL_INT(button_states); } -static MP_DEFINE_CONST_FUN_OBJ_1(buttons_read_obj, mp_buttons_read); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN( + buttons_read_obj, 0, 1, mp_buttons_read +); static const mp_rom_map_elem_t buttons_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_buttons) }, -- GitLab