diff --git a/epicardium/epicardium.h b/epicardium/epicardium.h index 54fd254ae97ba9e8b870ddc1a53ac2e81b45b61d..3d5eb7682306be7d605756f6b0d45823f493e4e8 100644 --- a/epicardium/epicardium.h +++ b/epicardium/epicardium.h @@ -86,11 +86,6 @@ typedef _Bool bool; #define API_LIGHT_SENSOR_STOP 0x82 #define API_BUTTONS_READ 0x90 -#define API_BUTTONS_UPDATE 0x91 -#define API_BUTTONS_READ_BOTTOM_LEFT_FROMCACHE 0x92 -#define API_BUTTONS_READ_BOTTOM_RIGHT_FROMCACHE 0x93 -#define API_BUTTONS_READ_TOP_RIGHT_FROMCACHE 0x94 -#define API_BUTTONS_READ_RESET_FROMCACHE 0x95 /* clang-format on */ typedef uint32_t api_int_id_t; @@ -232,12 +227,14 @@ API_ISR(EPIC_INT_CTRL_C, epic_isr_ctrl_c); * */ +/** + * Reads buttons. Returns nonzero value if unmasked buttons are pushed. + * + * Always reads all buttons, use mask=15 for not wasting slow-ish API calls and extract values in pycardium. Note: The reset button cannot be unmapped from reset functionality afaik. + * + * :param uint8_t mask: The first 4 LSBs enable readout from corresponding button. The zeroeth bit corresponds to bottom left pin, increasing counterclockwise. If buttons module is imported in micropython also the designators BOTTOM_LEFT, BOTTOM_RIGHT, TOP_RIGHT, TOP_LEFT and RESET can be used with the latter two being identical. + */ API(API_BUTTONS_READ, uint8_t epic_buttons_read(uint8_t mask)); -API(API_BUTTONS_UPDATE, void epic_buttons_update(void)); -API(API_BUTTONS_READ_BOTTOM_LEFT_FROMCACHE, uint8_t epic_buttons_read_bottom_left_fromcache(void)); -API(API_BUTTONS_READ_BOTTOM_RIGHT_FROMCACHE, uint8_t epic_buttons_read_bottom_right_fromcache(void)); -API(API_BUTTONS_READ_TOP_RIGHT_FROMCACHE, uint8_t epic_buttons_read_top_right_fromcache(void)); -API(API_BUTTONS_READ_RESET_FROMCACHE, uint8_t epic_buttons_read_reset_fromcache(void)); /** * LEDs diff --git a/epicardium/modules/buttons.c b/epicardium/modules/buttons.c index 4b77b0b2409cb756d79c3a58580c52ec46b1f494..d5b7400693978af327e5fc72df85875ffcc86c2e 100644 --- a/epicardium/modules/buttons.c +++ b/epicardium/modules/buttons.c @@ -1,4 +1,3 @@ -//#include "pb.h" #include "portexpander.h" #include "MAX77650-Arduino-Library.h" #include <stdint.h> @@ -9,19 +8,8 @@ static const uint8_t ButtonPin[] = {BOTTOM_LEFT,BOTTOM_RIGHT,TOP_RIGHT}; static uint8_t button_states[4]; //as defined by button pin, 3->reset button -#if 0 -uint8_t epic_buttons_poll(uint8_t mask){ //legacy af - uint8_t button_status=0; - for(int i=0; i<4; i++){ - if((mask>>i)&1){ - button_status += PB_Get(i+1)<<i; - } - } - return button_status; -} -#endif -void epic_buttons_update(){ +static void epic_buttons_update(){ button_states[3] = MAX77650_getDebounceStatusnEN0(); if(portexpander_detected()){ uint8_t button_status = portexpander_get(); @@ -42,18 +30,3 @@ uint8_t epic_buttons_read(uint8_t mask){ return button_status; } -uint8_t epic_buttons_read_bottom_left_fromcache(){ - return button_states[0]; -} - -uint8_t epic_buttons_read_bottom_right_fromcache(){ - return button_states[1]; -} - -uint8_t epic_buttons_read_top_right_fromcache(){ - return button_states[2]; -} - -uint8_t epic_buttons_read_reset_fromcache(){ - return button_states[3]; -} diff --git a/pycardium/modules/buttons.c b/pycardium/modules/buttons.c index 7e11235eee202b5c150aba432aceff94c85b367e..ad412d9faac14010d550f926404ac4c0ad988550 100644 --- a/pycardium/modules/buttons.c +++ b/pycardium/modules/buttons.c @@ -5,73 +5,6 @@ #include "epicardium.h" -static mp_obj_t mp_buttons_update() -{ - epic_buttons_update(); - return mp_const_none; -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_update_obj, mp_buttons_update); - -static mp_obj_t mp_buttons_read_bottom_left() -{ - epic_buttons_update(); - uint8_t retval = epic_buttons_read_bottom_left_fromcache(); - return MP_OBJ_NEW_SMALL_INT(retval); -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_read_bottom_left_obj, mp_buttons_read_bottom_left); - -static mp_obj_t mp_buttons_read_bottom_right() -{ - epic_buttons_update(); - uint8_t retval = epic_buttons_read_bottom_right_fromcache(); - return MP_OBJ_NEW_SMALL_INT(retval); -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_read_bottom_right_obj, mp_buttons_read_bottom_right); - -static mp_obj_t mp_buttons_read_top_right() -{ - epic_buttons_update(); - uint8_t retval = epic_buttons_read_top_right_fromcache(); - return MP_OBJ_NEW_SMALL_INT(retval); -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_read_top_right_obj, mp_buttons_read_top_right); - -static mp_obj_t mp_buttons_read_reset() -{ - epic_buttons_update(); - uint8_t retval = epic_buttons_read_reset_fromcache(); - return MP_OBJ_NEW_SMALL_INT(retval); -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_read_reset_obj, mp_buttons_read_reset); - -static mp_obj_t mp_buttons_read_bottom_left_fromcache() -{ - uint8_t retval = epic_buttons_read_bottom_left_fromcache(); - return MP_OBJ_NEW_SMALL_INT(retval); -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_read_bottom_left_fromcache_obj, mp_buttons_read_bottom_left_fromcache); - -static mp_obj_t mp_buttons_read_bottom_right_fromcache() -{ - uint8_t retval = epic_buttons_read_bottom_right_fromcache(); - return MP_OBJ_NEW_SMALL_INT(retval); -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_read_bottom_right_fromcache_obj, mp_buttons_read_bottom_right_fromcache); - -static mp_obj_t mp_buttons_read_top_right_fromcache() -{ - uint8_t retval = epic_buttons_read_top_right_fromcache(); - return MP_OBJ_NEW_SMALL_INT(retval); -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_read_top_right_fromcache_obj, mp_buttons_read_top_right_fromcache); - -static mp_obj_t mp_buttons_read_reset_fromcache() -{ - uint8_t retval = epic_buttons_read_reset_fromcache(); - return MP_OBJ_NEW_SMALL_INT(retval); -} -static MP_DEFINE_CONST_FUN_OBJ_0(buttons_read_reset_fromcache_obj, mp_buttons_read_reset_fromcache); - static mp_obj_t mp_buttons_read(mp_obj_t mask_in) { uint8_t mask = mp_obj_get_int(mask_in); @@ -83,15 +16,11 @@ static MP_DEFINE_CONST_FUN_OBJ_1(buttons_read_obj, 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) }, { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&buttons_read_obj) }, - { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&buttons_update_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_bottom_left), MP_ROM_PTR(&buttons_read_bottom_left_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_bottom_right), MP_ROM_PTR(&buttons_read_bottom_right_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_top_right), MP_ROM_PTR(&buttons_read_top_right_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_reset), MP_ROM_PTR(&buttons_read_reset_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_bottom_left_fromcache), MP_ROM_PTR(&buttons_read_bottom_left_fromcache_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_bottom_right_fromcache), MP_ROM_PTR(&buttons_read_bottom_right_fromcache_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_top_right_fromcache), MP_ROM_PTR(&buttons_read_top_right_fromcache_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_reset_fromcache), MP_ROM_PTR(&buttons_read_reset_fromcache_obj) }, + { MP_ROM_QSTR(MP_QSTR_BOTTOM_LEFT), MP_OBJ_NEW_SMALL_INT(0) }, + { MP_ROM_QSTR(MP_QSTR_BOTTOM_RIGHT), MP_OBJ_NEW_SMALL_INT(1) }, + { MP_ROM_QSTR(MP_QSTR_TOP_RIGHT), MP_OBJ_NEW_SMALL_INT(2) }, + { MP_ROM_QSTR(MP_QSTR_TOP_LEFT), MP_OBJ_NEW_SMALL_INT(3) }, + { MP_ROM_QSTR(MP_QSTR_RESET), MP_OBJ_NEW_SMALL_INT(3) }, }; static MP_DEFINE_CONST_DICT(buttons_module_globals, buttons_module_globals_table); diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index cf199510d6eb2a7bb7ed39c38db9fa61e9b310ff..2affb9a0ee47e1c0a0025b94bdd6717435b183e0 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -27,14 +27,11 @@ Q(TOP_RIGHT) /* buttons */ Q(buttons) Q(read) -Q(read_bottom_left) -Q(read_bottom_right) -Q(read_top_right) -Q(read_reset) -Q(read_bottom_left_fromcache) -Q(read_bottom_right_fromcache) -Q(read_top_right_fromcache) -Q(read_reset_fromcache) +Q(BOTTOM_LEFT) +Q(TOP_LEFT) +Q(BOTTOM_RIGHT) +Q(TOP_RIGHT) +Q(RESET) /* utime */ Q(utime)