From 2a701eeaa7d66a2e1c02bf7b23ca908f4d5853f2 Mon Sep 17 00:00:00 2001 From: gesture1968 Date: Thu, 14 Oct 2021 13:42:25 +0200 Subject: [PATCH] feat(lv_spinbox) support both right-to-left and left-to-right digit steps when clicking encoder button (#2644) * Update lv_spinbox.c * Added support for moving the Spinbox digit position from right-to-left when clicking the button on an encoder. The default behaviour is when clicking the encoder button, the digit is moved from left-to-right (MSB to LSB). * Added a check to see if the spinbox digit-count is just one. In that case it is pointless to check the buttonclick * See also the spinbox.h file * Update lv_spinbox.c * Forgot the implementation of the setter function * forgot a ; * Update lv_spinbox.h Adding Spinbox support for moving the digitposition both from left-to-right and right-to-left when editing a spinbox and clicking the encoder button. The current behaviour is clicking the encoder button only moves the digitposition from right to left (from MSB to LSB) * Update lv_spinbox.c Added brief / comment to new function * Update lv_spinbox.h More clear Brief / Comment * Update lv_spinbox.c nested function replaced by lv_pow fiunction * Update lv_spinbox.h removed spaces * Update lv_spinbox.h Replaced type used for direction of digit step when clicking an encoder with existing LVGL lv_dir_t * Update lv_spinbox.c Replaced type used for direction of digit step when clicking an encoder with existing LVGL lv_dir_t * Update spinbox.md Added comment for the new function 'lv_spinbox_set_digit_step_direction' * Update src/extra/widgets/spinbox/lv_spinbox.h Co-authored-by: embeddedt <42941056+embeddedt@users.noreply.github.com> * Update src/extra/widgets/spinbox/lv_spinbox.h Co-authored-by: embeddedt <42941056+embeddedt@users.noreply.github.com> * Update lv_spinbox.c bug: old definition LV_SPINBOX_DIGIT_DIR_TO_RIGHT changed to LV_DIR_RIGHT * Update lv_spinbox.h Extra linefeed removed Co-authored-by: Gabor Kiss-Vamosi Co-authored-by: embeddedt <42941056+embeddedt@users.noreply.github.com> --- docs/widgets/extra/spinbox.md | 2 ++ src/extra/widgets/spinbox/lv_spinbox.c | 47 +++++++++++++++++++------- src/extra/widgets/spinbox/lv_spinbox.h | 11 +++++- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/docs/widgets/extra/spinbox.md b/docs/widgets/extra/spinbox.md index f39936e73..1b4444e21 100644 --- a/docs/widgets/extra/spinbox.md +++ b/docs/widgets/extra/spinbox.md @@ -22,6 +22,8 @@ The parts of the Spinbox are identical to the [Text area](/widgets/core/textarea `lv_spinbox_set_pos(spinbox, 1)` sets the cursor to a specific digit to change on increment/decrement. For example position '0' sets the cursor to the least significant digit. +If an encoder is used as input device, the selected digit is shifted to the right by default whenever the encoder button is clicked. To change this behaviour to shifting to the left, the `lv_spinbox_set_digit_step_direction(spinbox, LV_DIR_LEFT)` can be used + ### Format `lv_spinbox_set_digit_format(spinbox, digit_count, separator_position)` sets the number format. `digit_count` is the number of digits excluding the decimal separator and the sign. diff --git a/src/extra/widgets/spinbox/lv_spinbox.c b/src/extra/widgets/spinbox/lv_spinbox.c index 83fd2f458..97d00404e 100644 --- a/src/extra/widgets/spinbox/lv_spinbox.c +++ b/src/extra/widgets/spinbox/lv_spinbox.c @@ -169,6 +169,20 @@ void lv_spinbox_set_pos(lv_obj_t * obj, uint8_t pos) lv_spinbox_updatevalue(obj); } + +/** + * Set direction of digit step when clicking an encoder button while in editing mode + * @param spinbox pointer to spinbox + * @param direction the direction (LV_DIR_RIGHT or LV_DIR_LEFT) + */ +void lv_spinbox_set_digit_step_direction(lv_obj_t *obj, lv_dir_t direction) +{ + LV_ASSERT_OBJ(obj, MY_CLASS); + lv_spinbox_t * spinbox = (lv_spinbox_t *)obj; + spinbox->digit_step_dir = direction; + + lv_spinbox_updatevalue(obj); +} /*===================== * Getter functions *====================*/ @@ -318,6 +332,7 @@ static void lv_spinbox_constructor(const lv_obj_class_t * class_p, lv_obj_t * ob spinbox->range_max = 99999; spinbox->range_min = -99999; spinbox->rollover = false; + spinbox->digit_step_dir = LV_DIR_RIGHT; lv_textarea_set_one_line(obj, true); lv_textarea_set_cursor_click_pos(obj, true); @@ -345,19 +360,27 @@ static void lv_spinbox_event(const lv_obj_class_t * class_p, lv_event_t * e) lv_indev_t * indev = lv_indev_get_act(); if(lv_indev_get_type(indev) == LV_INDEV_TYPE_ENCODER) { if(lv_group_get_editing(lv_obj_get_group(obj))) { - if(spinbox->step > 1) { - lv_spinbox_step_next(obj); - } - else { - /*Restart from the MSB*/ - spinbox->step = 1; - uint32_t i; - for(i = 0; i < spinbox->digit_count; i++) { - int32_t new_step = spinbox->step * 10; - if(new_step >= spinbox->range_max) break; - spinbox->step = new_step; + if (spinbox->digit_count > 1) { + if (spinbox->digit_step_dir == LV_DIR_RIGHT) { + if(spinbox->step > 1) { + lv_spinbox_step_next(obj); + } + else { + /*Restart from the MSB*/ + spinbox->step = lv_pow(10, spinbox->digit_count - 2); + lv_spinbox_step_prev(obj); + } + } + else { + if(spinbox->step < lv_pow(10, spinbox->digit_count - 1)) { + lv_spinbox_step_prev(obj); + } + else { + /*Restart from the LSB*/ + spinbox->step = 10; + lv_spinbox_step_next(obj); + } } - lv_spinbox_step_prev(obj); } } } diff --git a/src/extra/widgets/spinbox/lv_spinbox.h b/src/extra/widgets/spinbox/lv_spinbox.h index ce3bce27c..71856c917 100644 --- a/src/extra/widgets/spinbox/lv_spinbox.h +++ b/src/extra/widgets/spinbox/lv_spinbox.h @@ -42,6 +42,7 @@ typedef struct { uint16_t digit_count : 4; uint16_t dec_point_pos : 4; /*if 0, there is no separator and the number is an integer*/ uint16_t rollover : 1; // Set to true for rollover functionality + uint16_t digit_step_dir : 2; // the direction the digit will step on encoder button press when editing } lv_spinbox_t; extern const lv_obj_class_t lv_spinbox_class; @@ -105,6 +106,14 @@ void lv_spinbox_set_range(lv_obj_t * obj, int32_t range_min, int32_t range_max); * @param pos selected position in spinbox */ void lv_spinbox_set_pos(lv_obj_t * obj, uint8_t pos); + +/** + * Set direction of digit step when clicking an encoder button while in editing mode + * @param spinbox pointer to spinbox + * @param direction the direction (LV_DIR_RIGHT or LV_DIR_LEFT) + */ +void lv_spinbox_set_digit_step_direction(lv_obj_t * obj, lv_dir_t direction); + /*===================== * Getter functions *====================*/ @@ -113,7 +122,7 @@ void lv_spinbox_set_pos(lv_obj_t * obj, uint8_t pos); * Get spinbox rollover function status * @param spinbox pointer to spinbox */ -bool lv_spinbox_get_rollover(lv_obj_t * obj); +bool lv_spinbox_get_rollover(lv_obj_t *obj); /** * Get the spinbox numeral value (user has to convert to float according to its digit format)