From e7b4bef8acdbbaa744dc77b5551f3c478e2f7012 Mon Sep 17 00:00:00 2001 From: Themba Dube Date: Wed, 20 Mar 2019 18:33:33 -0400 Subject: [PATCH 1/3] Add lv_btnm_set_one_toggle/lv_btnm_get_one_toggle functions --- src/lv_objx/lv_btnm.c | 46 +++++++++++++++++++++++++++++++++++++++++++ src/lv_objx/lv_btnm.h | 14 +++++++++++++ 2 files changed, 60 insertions(+) diff --git a/src/lv_objx/lv_btnm.c b/src/lv_objx/lv_btnm.c index a37eea9f9..541f04152 100644 --- a/src/lv_objx/lv_btnm.c +++ b/src/lv_objx/lv_btnm.c @@ -39,6 +39,7 @@ static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p); static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map); static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx); static bool maps_are_identical(const char ** map1, const char ** map2); +static void make_one_button_toggled(const lv_obj_t *btnm, uint16_t btn_idx); /********************** * STATIC VARIABLES @@ -87,6 +88,7 @@ lv_obj_t * lv_btnm_create(lv_obj_t * par, const lv_obj_t * copy) ext->ctrl_bits = NULL; ext->map_p = NULL; ext->recolor = 0; + ext->one_toggle = 0; ext->styles_btn[LV_BTN_STATE_REL] = &lv_style_btn_rel; ext->styles_btn[LV_BTN_STATE_PR] = &lv_style_btn_pr; ext->styles_btn[LV_BTN_STATE_TGL_REL] = &lv_style_btn_tgl_rel; @@ -401,6 +403,19 @@ void lv_btnm_set_btn_width(const lv_obj_t * btnm, uint16_t btn_id, uint8_t width lv_btnm_set_map(btnm, ext->map_p); } +/** + * Make the button matrix like a selector widget (only one button may be toggled at a time). + * @param btnm Button matrix object + * @param one_toggle Whether "one toggle" mode is enabled + */ +void lv_btnm_set_one_toggle(const lv_obj_t *btnm, bool one_toggle) { + lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm); + ext->one_toggle = one_toggle; + + /*If more than one button is toggled only the first one should be*/ + make_one_button_toggled(btnm, 0); +} + /*===================== * Getter functions *====================*/ @@ -549,6 +564,17 @@ lv_style_t * lv_btnm_get_style(const lv_obj_t * btnm, lv_btnm_style_t type) return style; } +/** + * Find whether "one toggle" mode is enabled. + * @param btnm Button matrix object + * @return whether "one toggle" mode is enabled + */ +bool lv_btnm_get_one_toggle(const lv_obj_t *btnm) { + lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm); + + return ext->one_toggle; +} + /********************** * STATIC FUNCTIONS **********************/ @@ -739,6 +765,7 @@ static lv_res_t lv_btnm_signal(lv_obj_t * btnm, lv_signal_t sign, void * param) } else { ext->ctrl_bits[ext->btn_id_pr] |= LV_BTNM_CTRL_TGL_STATE; } + make_one_button_toggled(btnm, ext->btn_id_pr); } /*Invalidate to old pressed area*/; @@ -1034,5 +1061,24 @@ static bool maps_are_identical(const char ** map1, const char ** map2) return map1[i][0] == '\0' && map2[i][0] == '\0'; } +/** + * Enforces a single button being toggled on the button matrix. + * It simply clears the toggle flag on other buttons. + * @param btnm Button matrix object + * @param btn_idx Button that should remain toggled + */ +static void make_one_button_toggled(const lv_obj_t *btnm, uint16_t btn_idx) +{ + lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm); + + uint16_t i = 0; + + for(i = 0; i < ext->btn_cnt; i++) { + if(i != btn_idx) { + ext->ctrl_bits[i] &= (~LV_BTNM_CTRL_TGL_STATE); + invalidate_button_area(btnm, i); + } + } +} #endif diff --git a/src/lv_objx/lv_btnm.h b/src/lv_objx/lv_btnm.h index 6cdc8f3fd..3b98db5ea 100644 --- a/src/lv_objx/lv_btnm.h +++ b/src/lv_objx/lv_btnm.h @@ -60,6 +60,7 @@ typedef struct uint16_t btn_id_pr; /*Index of the currently pressed button or LV_BTNM_BTN_NONE*/ uint16_t btn_id_act; /*Index of the active button (being pressed/released etc) or LV_BTNM_BTN_NONE */ uint8_t recolor :1; /*Enable button recoloring*/ + uint8_t one_toggle :1; /*Single button toggled at once*/ } lv_btnm_ext_t; enum { @@ -166,6 +167,12 @@ void lv_btnm_set_btn_ctrl_all(lv_obj_t * btnm, lv_btnm_ctrl_t ctrl, bool en); */ void lv_btnm_set_btn_width(const lv_obj_t * btnm, uint16_t btn_id, uint8_t width); +/** + * Make the button matrix like a selector widget (only one button may be toggled at a time). + * @param btnm Button matrix object + * @param one_toggle Whether "one toggle" mode is enabled + */ +void lv_btnm_set_one_toggle(const lv_obj_t *btnm, bool one_toggle); /*===================== * Getter functions @@ -233,6 +240,13 @@ bool lv_btnm_get_btn_ctrl(lv_obj_t * btnm, uint16_t btn_id, lv_btnm_ctrl_t ctrl) * @return style pointer to a style */ lv_style_t * lv_btnm_get_style(const lv_obj_t * btnm, lv_btnm_style_t type); + +/** + * Find whether "one toggle" mode is enabled. + * @param btnm Button matrix object + * @return whether "one toggle" mode is enabled + */ +bool lv_btnm_get_one_toggle(const lv_obj_t *btnm); /********************** * MACROS **********************/ From f5aa26f7e177ecac175662a54cded38548d65536 Mon Sep 17 00:00:00 2001 From: Themba Dube Date: Thu, 21 Mar 2019 13:42:03 -0400 Subject: [PATCH 2/3] Update one toggle feature based on review comments --- src/lv_objx/lv_btnm.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/lv_objx/lv_btnm.c b/src/lv_objx/lv_btnm.c index 541f04152..53bbf0782 100644 --- a/src/lv_objx/lv_btnm.c +++ b/src/lv_objx/lv_btnm.c @@ -765,7 +765,8 @@ static lv_res_t lv_btnm_signal(lv_obj_t * btnm, lv_signal_t sign, void * param) } else { ext->ctrl_bits[ext->btn_id_pr] |= LV_BTNM_CTRL_TGL_STATE; } - make_one_button_toggled(btnm, ext->btn_id_pr); + if(ext->one_toggle) + make_one_button_toggled(btnm, ext->btn_id_pr); } /*Invalidate to old pressed area*/; @@ -1069,16 +1070,13 @@ static bool maps_are_identical(const char ** map1, const char ** map2) */ static void make_one_button_toggled(const lv_obj_t *btnm, uint16_t btn_idx) { - lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm); + /*Save whether the button was toggled*/ + bool was_toggled = lv_btnm_get_btn_ctrl(btnm, btn_idx, LV_BTNM_CTRL_TGL_STATE); - uint16_t i = 0; + lv_btnm_set_btn_ctrl_all(btnm, LV_BTNM_CTRL_TGL_STATE, false); - for(i = 0; i < ext->btn_cnt; i++) { - if(i != btn_idx) { - ext->ctrl_bits[i] &= (~LV_BTNM_CTRL_TGL_STATE); - invalidate_button_area(btnm, i); - } - } + if(was_toggled) + lv_btnm_set_btn_ctrl(btnm, btn_idx, LV_BTNM_CTRL_TGL_STATE, true); } #endif From de04b39165f2b5020bccb19b3e695d0013d5ba18 Mon Sep 17 00:00:00 2001 From: Themba Dube Date: Fri, 22 Mar 2019 19:11:37 -0400 Subject: [PATCH 3/3] Remove use of 'const' in `lv_btnm_set_one_toggle` --- src/lv_objx/lv_btnm.c | 6 +++--- src/lv_objx/lv_btnm.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lv_objx/lv_btnm.c b/src/lv_objx/lv_btnm.c index 53bbf0782..1594cf781 100644 --- a/src/lv_objx/lv_btnm.c +++ b/src/lv_objx/lv_btnm.c @@ -39,7 +39,7 @@ static uint16_t get_button_from_point(lv_obj_t * btnm, lv_point_t * p); static void allocate_btn_areas_and_controls(const lv_obj_t * btnm, const char ** map); static void invalidate_button_area(const lv_obj_t * btnm, uint16_t btn_idx); static bool maps_are_identical(const char ** map1, const char ** map2); -static void make_one_button_toggled(const lv_obj_t *btnm, uint16_t btn_idx); +static void make_one_button_toggled(lv_obj_t *btnm, uint16_t btn_idx); /********************** * STATIC VARIABLES @@ -408,7 +408,7 @@ void lv_btnm_set_btn_width(const lv_obj_t * btnm, uint16_t btn_id, uint8_t width * @param btnm Button matrix object * @param one_toggle Whether "one toggle" mode is enabled */ -void lv_btnm_set_one_toggle(const lv_obj_t *btnm, bool one_toggle) { +void lv_btnm_set_one_toggle(lv_obj_t *btnm, bool one_toggle) { lv_btnm_ext_t * ext = lv_obj_get_ext_attr(btnm); ext->one_toggle = one_toggle; @@ -1068,7 +1068,7 @@ static bool maps_are_identical(const char ** map1, const char ** map2) * @param btnm Button matrix object * @param btn_idx Button that should remain toggled */ -static void make_one_button_toggled(const lv_obj_t *btnm, uint16_t btn_idx) +static void make_one_button_toggled(lv_obj_t *btnm, uint16_t btn_idx) { /*Save whether the button was toggled*/ bool was_toggled = lv_btnm_get_btn_ctrl(btnm, btn_idx, LV_BTNM_CTRL_TGL_STATE); diff --git a/src/lv_objx/lv_btnm.h b/src/lv_objx/lv_btnm.h index 3b98db5ea..edadc03c7 100644 --- a/src/lv_objx/lv_btnm.h +++ b/src/lv_objx/lv_btnm.h @@ -172,7 +172,7 @@ void lv_btnm_set_btn_width(const lv_obj_t * btnm, uint16_t btn_id, uint8_t width * @param btnm Button matrix object * @param one_toggle Whether "one toggle" mode is enabled */ -void lv_btnm_set_one_toggle(const lv_obj_t *btnm, bool one_toggle); +void lv_btnm_set_one_toggle(lv_obj_t *btnm, bool one_toggle); /*===================== * Getter functions