From 404c52e3c29b9a81f66741cce8df3a02b10447ed Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Thu, 1 Nov 2018 18:48:13 +0100 Subject: [PATCH 01/11] create spinbox object, derivating lv_ta --- lv_objx/lv_spinbox.c | 511 +++++++++++++++++++++++++++++++++++++++++++ lv_objx/lv_spinbox.h | 139 ++++++++++++ 2 files changed, 650 insertions(+) create mode 100644 lv_objx/lv_spinbox.c create mode 100644 lv_objx/lv_spinbox.h diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c new file mode 100644 index 000000000..a2ca5e882 --- /dev/null +++ b/lv_objx/lv_spinbox.c @@ -0,0 +1,511 @@ +/** + * @file lv_templ.c + * + */ + +/* TODO Remove these instructions + * Search an replace: spinbox -> object normal name with lower case (e.g. button, label etc.) + * templ -> object short name with lower case(e.g. btn, label etc) + * TEMPL -> object short name with upper case (e.g. BTN, LABEL etc.) + * + */ + +/********************* + * INCLUDES + *********************/ +#include "lv_spinbox.h" + +#if USE_LV_SPINBOX != 0 + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ +static bool lv_spinbox_design(lv_obj_t * templ, const lv_area_t * mask, lv_design_mode_t mode); +static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * param); +static void lv_spinbox_updatevalue(lv_obj_t * spinbox); +static int32_t lv_spinbox_double_to_int(lv_obj_t * spinbox, double d); +static double lv_spinbox_int_to_double(lv_obj_t * spinbox, int32_t i); + +/********************** + * STATIC VARIABLES + **********************/ +static lv_signal_func_t ancestor_signal; +static lv_design_func_t ancestor_design; + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +/** + * Create a spinbox object + * @param par pointer to an object, it will be the parent of the new spinbox + * @param copy pointer to a spinbox object, if not NULL then the new object will be copied from it + * @return pointer to the created spinbox + */ +lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy) +{ + LV_LOG_TRACE("spinbox create started"); + + /*Create the ancestor of spinbox*/ + /*TODO modify it to the ancestor create function */ + lv_obj_t * new_spinbox = lv_ta_create(par, copy); + lv_mem_assert(new_spinbox); + if(new_spinbox == NULL) return NULL; + + /*Allocate the spinbox type specific extended data*/ + lv_spinbox_ext_t * ext = lv_obj_allocate_ext_attr(new_spinbox, sizeof(lv_spinbox_ext_t)); + lv_mem_assert(ext); + if(ext == NULL) return NULL; + if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_spinbox); + if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_spinbox); + + /*Initialize the allocated 'ext' */ + ext->ta.one_line = 1; + ext->ta.pwd_mode = 0; + ext->ta.accapted_chars = "1234567890+-."; + + ext->value = 0.0; + ext->valueDigit = 0; + ext->decPointPos = 2; + ext->digitCount = 5; + ext->step = 100; + ext->rangeMax = 99999; + ext->rangeMin = -99999; + + lv_ta_set_cursor_type(new_spinbox, LV_CURSOR_BLOCK | LV_CURSOR_HIDDEN); //hidden by default + lv_ta_set_cursor_pos(new_spinbox, 4); + + + /*The signal and design functions are not copied so set them here*/ + lv_obj_set_signal_func(new_spinbox, lv_spinbox_signal); + lv_obj_set_design_func(new_spinbox, lv_spinbox_design); + + /*Init the new spinbox spinbox*/ + if(copy == NULL) { + + } + /*Copy an existing spinbox*/ + else { + lv_spinbox_ext_t * copy_ext = lv_obj_get_ext_attr(copy); + + /*Refresh the style with new signal function*/ + lv_obj_refresh_style(new_spinbox); + } + + lv_spinbox_updatevalue(new_spinbox); + + LV_LOG_INFO("spinbox created"); + + return new_spinbox; +} + + +void lv_spinbox_step_next(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + + if((ext->step / 10) < ext->rangeMax && (ext->step / 10) > ext->rangeMin && (ext->step / 10) > 0) + { + ext->step /= 10; + } + + lv_spinbox_updatevalue(spinbox); +} + +void lv_spinbox_step_previous(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + + if((ext->step * 10) <= ext->rangeMax && (ext->step * 10) > ext->rangeMin && (ext->step * 10) > 0) + { + ext->step *= 10; + } + + lv_spinbox_updatevalue(spinbox); +} + +/*====================== + * Add/remove functions + *=====================*/ + +/* + * New object specific "add" or "remove" functions come here + */ + + +/*===================== + * Setter functions + *====================*/ + +/* + * New object specific "set" functions come here + */ +void lv_spinbox_set_double(const lv_obj_t * spinbox, double d) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + if(ext == NULL) + return; +} + +void lv_spinbox_set_int(const lv_obj_t * spinbox, int32_t i) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + if(ext == NULL) + return; + + if(i > ext->rangeMax) + i = ext->rangeMax; + if(i < ext->rangeMin) + i = ext->rangeMin; + + ext->valueDigit = i; +} + +void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + if(ext == NULL) + return; + + if(digit_count > LV_SPINBOX_MAX_DIGIT_COUNT) + digit_count = LV_SPINBOX_MAX_DIGIT_COUNT; + + if(separator_position < LV_SPINBOX_MAX_DIGIT_COUNT) + separator_position = LV_SPINBOX_MAX_DIGIT_COUNT; + + ext->digitCount = digit_count; + ext->decPointPos = separator_position; + + lv_spinbox_updatevalue(spinbox); +} + +void lv_spinbox_set_range_int(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + if(ext == NULL) + return; + + ext->rangeMax = rangeMax; + ext->rangeMin = rangeMin; + + if(ext->valueDigit > ext->rangeMax) + { + ext->valueDigit = ext->rangeMax; + lv_obj_invalidate(spinbox); + } + if(ext->valueDigit < ext->rangeMin) + { + ext->valueDigit = ext->rangeMin; + lv_obj_invalidate(spinbox); + } +} + +void lv_spinbox_set_range_double(const lv_obj_t * spinbox, double rangeMin, double rangeMax) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + if(ext == NULL) + return; + + double rMinDouble = lv_spinbox_double_to_int(spinbox, rangeMin); + double rMaxDouble = lv_spinbox_double_to_int(spinbox, rangeMax); + + lv_spinbox_set_range_int(spinbox, rMinDouble, rMaxDouble); +} + + +/** + * Set a style of a spinbox. + * @param templ pointer to spinbox object + * @param type which style should be set + * @param style pointer to a style + */ +void lv_spinbox_set_style(lv_obj_t * spinbox, lv_spinbox_style_t type, lv_style_t * style) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + switch(type) { + case LV_SPINBOX_STYLE_BG: + lv_page_set_style(spinbox, LV_PAGE_STYLE_BG, style); + break; + case LV_SPINBOX_STYLE_SB: + lv_page_set_style(spinbox, LV_PAGE_STYLE_SB, style); + break; + case LV_SPINBOX_STYLE_CURSOR: + ext->ta.cursor.style = style; + lv_obj_refresh_ext_size(lv_page_get_scrl(spinbox)); /*Refresh ext. size because of cursor drawing*/ + break; + } +} + +/*===================== + * Getter functions + *====================*/ + +/* + * New object specific "get" functions come here + */ + +/** + * Get style of a spinbox. + * @param templ pointer to spinbox object + * @param type which style should be get + * @return style pointer to the style + */ +lv_style_t * lv_spinbox_get_style(const lv_obj_t * templ, lv_spinbox_style_t type) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(templ); + + switch(type) { + default: + return NULL; + } + + /*To avoid warning*/ + return NULL; +} + +/*===================== + * Other functions + *====================*/ + +/* + * New object specific "other" functions come here + */ + +/********************** + * STATIC FUNCTIONS + **********************/ + +/** + * Handle the drawing related tasks of the spinboxs + * @param templ pointer to an object + * @param mask the object will be drawn only in this area + * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area + * (return 'true' if yes) + * LV_DESIGN_DRAW: draw the object (always return 'true') + * LV_DESIGN_DRAW_POST: drawing after every children are drawn + * @param return true/false, depends on 'mode' + */ +static bool lv_spinbox_design(lv_obj_t * templ, const lv_area_t * mask, lv_design_mode_t mode) +{ + /*Return false if the object is not covers the mask_p area*/ + if(mode == LV_DESIGN_COVER_CHK) { + return false; + } + /*Draw the object*/ + else if(mode == LV_DESIGN_DRAW_MAIN) { + + } + /*Post draw when the children are drawn*/ + else if(mode == LV_DESIGN_DRAW_POST) { + + } + + return true; +} + +/** + * Signal function of the spinbox + * @param templ pointer to a spinbox object + * @param sign a signal type from lv_signal_t enum + * @param param pointer to a signal specific variable + * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted + */ +static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * param) +{ + + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + lv_res_t res; + + /* Include the ancient signal function */ + if(sign != LV_SIGNAL_CONTROLL) + { + res = ancestor_signal(spinbox, sign, param); + if(res != LV_RES_OK) return res; + } + + + if(sign == LV_SIGNAL_CLEANUP) { + /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/ + } else if(sign == LV_SIGNAL_GET_TYPE) + { + lv_obj_type_t * buf = param; + uint8_t i; + for(i = 0; i < LV_MAX_ANCESTOR_NUM - 1; i++) + { /*Find the last set data*/ + if(buf->type[i] == NULL) break; + } + buf->type[i] = "lv_spinbox"; + }else if(sign == LV_SIGNAL_CONTROLL) + { + uint32_t c = *((uint32_t *)param); /*uint32_t because can be UTF-8*/ + if(c == LV_GROUP_KEY_RIGHT) + { + if(ext->valueDigit - ext->step >= ext->rangeMin) + { + //Special mode when zero crossing + if((ext->valueDigit - ext->step) < 0 && ext->valueDigit > 0) + { + ext->valueDigit = -ext->valueDigit; + }//end special mode + + ext->valueDigit -= ext->step; + } + + lv_spinbox_updatevalue(spinbox); + lv_label_set_text(ext->ta.label, (char*)ext->digits); + } + else if(c == LV_GROUP_KEY_LEFT) + { + if(ext->valueDigit + ext->step <= ext->rangeMax) + { + //Special mode when zero crossing + if((ext->valueDigit + ext->step) > 0 && ext->valueDigit < 0) + { + ext->valueDigit = -ext->valueDigit; + }//end special mode + ext->valueDigit += ext->step; + } + lv_spinbox_updatevalue(spinbox); + lv_label_set_text(ext->ta.label, (char*)ext->digits); + } + else if(c == LV_GROUP_KEY_UP) + { + lv_ta_cursor_up(spinbox); + } + else if(c == LV_GROUP_KEY_DOWN) + { + lv_ta_cursor_down(spinbox); + } + else + { + if(c == '\n') + { + int p = lv_ta_get_cursor_pos(spinbox); + if(p == ext->digitCount + 1) + { + for(int i = 0; i < ext->digitCount; i++) + lv_spinbox_step_previous(spinbox); + } else + { + lv_spinbox_step_next(spinbox); + } + + + lv_spinbox_updatevalue(spinbox); + lv_label_set_text(ext->ta.label, (char*)ext->digits); + } + else + { + lv_ta_add_char(spinbox, c); + } + } + } + + + return res; +} + +static void lv_spinbox_updatevalue(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + int32_t v = ext->valueDigit; + int32_t intDigits, decDigits; + uint8_t dc = ext->digitCount; + + intDigits = (ext->decPointPos==0)?ext->digitCount:ext->decPointPos; + decDigits = ext->digitCount - intDigits; + + ext->digits[0] = v>=0?'+':'-'; + + int i = 0; + uint8_t digits[16]; + + if(v < 0) + v = -v; + for(i = 0; i < dc; i++) + { + digits[i] = v%10; + v = v/10; + } + + int k; + for(k = 0; k < intDigits; k++) + { + ext->digits[1 + k] = '0' + digits[--i]; + } + + ext->digits[1 + intDigits] = '.'; + + int d; + + for(d = 0; d < decDigits; d++) + { + ext->digits[1 + intDigits + 1 + d] = '0' + digits[--i]; + } + + ext->digits[1 + intDigits + 1 + decDigits] = '\0'; + + lv_label_set_text(ext->ta.label, (char*)ext->digits); + + int32_t step = ext->step; + uint8_t cPos = ext->digitCount; + while(step >= 10) + { + step /= 10; + cPos--; + } + if(cPos > intDigits) + { + cPos ++; + } + + lv_ta_set_cursor_pos(spinbox, cPos); +} + +static double lv_spinbox_int_to_double(lv_obj_t * spinbox, int32_t i) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + /* + if(i > ext->rangeMax) + i = ext->rangeMax; + if(i < ext->rangeMin) + i = ext->rangeMin; + */ + + return (double)( i / (10 * (ext->digitCount - ext->decPointPos))); +} + +static int32_t lv_spinbox_double_to_int(lv_obj_t * spinbox, double d) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + int32_t i = (int32_t)(d * (10 * (ext->digitCount - ext->decPointPos))); + + /* + if(i > ext->rangeMax) + i = ext->rangeMax; + if(i < ext->rangeMin) + i = ext->rangeMin; + */ + + return i; +} + +#endif diff --git a/lv_objx/lv_spinbox.h b/lv_objx/lv_spinbox.h new file mode 100644 index 000000000..0701bde69 --- /dev/null +++ b/lv_objx/lv_spinbox.h @@ -0,0 +1,139 @@ +/** + * @file lv_templ.h + * + */ + + +/* TODO Remove these instructions + * Search an replace: template -> object normal name with lower case (e.g. button, label etc.) + * templ -> object short name with lower case(e.g. btn, label etc) + * TEMPL -> object short name with upper case (e.g. BTN, LABEL etc.) + * + */ + +#ifndef LV_SPINBOX_H +#define LV_SPINBOX_H + +#ifdef __cplusplus +extern "C" { +#endif + +/********************* + * INCLUDES + *********************/ +#ifdef LV_CONF_INCLUDE_SIMPLE +#include "lv_conf.h" +#else +#include "../../lv_conf.h" +#endif + +#if USE_LV_SPINBOX != 0 + +#include "../lv_core/lv_obj.h" +#include "../lv_objx/lv_ta.h" + +/********************* + * DEFINES + *********************/ +#define LV_SPINBOX_MAX_DIGIT_COUNT 16 + +/********************** + * TYPEDEFS + **********************/ +/*Data of template*/ +typedef struct { + lv_ta_ext_t ta; /*Ext. of ancestor*/ + /*New data for this type */ + double value; + int32_t valueDigit; + int32_t rangeMax; + int32_t rangeMin; + int32_t step; + uint8_t digitCount:4; + uint8_t decPointPos:4; //if 0, there is no separator and the number is an integer + uint8_t digits[1+1+16]; //1 sign, 1 point, 16 num digits + +} lv_spinbox_ext_t; + + +/*Styles*/ +enum { + LV_SPINBOX_STYLE_BG, + LV_SPINBOX_STYLE_SB, + LV_SPINBOX_STYLE_CURSOR, +}; +typedef uint8_t lv_spinbox_style_t; + + +/********************** + * GLOBAL PROTOTYPES + **********************/ + +/** + * Create a template objects + * @param par pointer to an object, it will be the parent of the new template + * @param copy pointer to a template object, if not NULL then the new object will be copied from it + * @return pointer to the created template + */ +lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy); + +void lv_spinbox_step_next(lv_obj_t * spinbox); +void lv_spinbox_step_previous(lv_obj_t * spinbox); + + +/*====================== + * Add/remove functions + *=====================*/ + + +/*===================== + * Setter functions + *====================*/ + +/** + * Set a style of a template. + * @param templ pointer to template object + * @param type which style should be set + * @param style pointer to a style + */ +void lv_spinbox_set_style(lv_obj_t * templ, lv_spinbox_style_t type, lv_style_t *style); + + +void lv_spinbox_set_double(const lv_obj_t * spinbox, double d); +void lv_spinbox_set_int(const lv_obj_t * spinbox, int32_t i); +void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position); +void lv_spinbox_set_range_int(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax); +void lv_spinbox_set_range_double(const lv_obj_t * spinbox, double rangeMin, double rangeMax); + + +/*===================== + * Getter functions + *====================*/ + +/** + * Get style of a template. + * @param templ pointer to template object + * @param type which style should be get + * @return style pointer to the style + */ +lv_style_t * lv_spinbox_get_style(const lv_obj_t * templ, lv_spinbox_style_t type); + + +double lv_spinbox_get_double(const lv_obj_t * spinbox); +int32_t lv_spinbox_get_int(const lv_obj_t * spinbox); + +/*===================== + * Other functions + *====================*/ + +/********************** + * MACROS + **********************/ + +#endif /*USE_LV_TEMPL*/ + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /*LV_SPINBOX_H*/ From 0fceadc529f238cdea91933c7b817f1e4aec8798 Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Thu, 1 Nov 2018 19:45:48 +0100 Subject: [PATCH 02/11] add spinbox object to include list in lvgl.h --- lvgl.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lvgl.h b/lvgl.h index 6e74d53fa..400e61ab7 100644 --- a/lvgl.h +++ b/lvgl.h @@ -55,6 +55,7 @@ extern "C" { #include "lv_objx/lv_arc.h" #include "lv_objx/lv_preload.h" #include "lv_objx/lv_calendar.h" +#include "lv_objx/lv_spinbox.h" /********************* * DEFINES From 612c618240516d267170c69cd975f4db678718a1 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Fri, 2 Nov 2018 14:21:10 +0100 Subject: [PATCH 03/11] minor fixes --- lv_objx/lv_spinbox.c | 22 +++++++--------------- lv_objx/lv_spinbox.h | 11 ++--------- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c index a2ca5e882..780b27c5d 100644 --- a/lv_objx/lv_spinbox.c +++ b/lv_objx/lv_spinbox.c @@ -3,13 +3,6 @@ * */ -/* TODO Remove these instructions - * Search an replace: spinbox -> object normal name with lower case (e.g. button, label etc.) - * templ -> object short name with lower case(e.g. btn, label etc) - * TEMPL -> object short name with upper case (e.g. BTN, LABEL etc.) - * - */ - /********************* * INCLUDES *********************/ @@ -59,7 +52,6 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy) LV_LOG_TRACE("spinbox create started"); /*Create the ancestor of spinbox*/ - /*TODO modify it to the ancestor create function */ lv_obj_t * new_spinbox = lv_ta_create(par, copy); lv_mem_assert(new_spinbox); if(new_spinbox == NULL) return NULL; @@ -71,7 +63,7 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy) if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_func(new_spinbox); if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_func(new_spinbox); - /*Initialize the allocated 'ext' */ + /*Initialize the allocated 'ext'*/ ext->ta.one_line = 1; ext->ta.pwd_mode = 0; ext->ta.accapted_chars = "1234567890+-."; @@ -84,7 +76,7 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy) ext->rangeMax = 99999; ext->rangeMin = -99999; - lv_ta_set_cursor_type(new_spinbox, LV_CURSOR_BLOCK | LV_CURSOR_HIDDEN); //hidden by default + lv_ta_set_cursor_type(new_spinbox, LV_CURSOR_BLOCK | LV_CURSOR_HIDDEN); /*hidden by default*/ lv_ta_set_cursor_pos(new_spinbox, 4); @@ -358,11 +350,11 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p { if(ext->valueDigit - ext->step >= ext->rangeMin) { - //Special mode when zero crossing + /*Special mode when zero crossing*/ if((ext->valueDigit - ext->step) < 0 && ext->valueDigit > 0) { ext->valueDigit = -ext->valueDigit; - }//end special mode + }/*end special mode*/ ext->valueDigit -= ext->step; } @@ -374,11 +366,11 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p { if(ext->valueDigit + ext->step <= ext->rangeMax) { - //Special mode when zero crossing + /*Special mode when zero crossing*/ if((ext->valueDigit + ext->step) > 0 && ext->valueDigit < 0) { ext->valueDigit = -ext->valueDigit; - }//end special mode + }/*end special mode*/ ext->valueDigit += ext->step; } lv_spinbox_updatevalue(spinbox); @@ -394,7 +386,7 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p } else { - if(c == '\n') + if(c == LV_GROUP_KEY_ENTER) { int p = lv_ta_get_cursor_pos(spinbox); if(p == ext->digitCount + 1) diff --git a/lv_objx/lv_spinbox.h b/lv_objx/lv_spinbox.h index 0701bde69..d9df3eb15 100644 --- a/lv_objx/lv_spinbox.h +++ b/lv_objx/lv_spinbox.h @@ -4,13 +4,6 @@ */ -/* TODO Remove these instructions - * Search an replace: template -> object normal name with lower case (e.g. button, label etc.) - * templ -> object short name with lower case(e.g. btn, label etc) - * TEMPL -> object short name with upper case (e.g. BTN, LABEL etc.) - * - */ - #ifndef LV_SPINBOX_H #define LV_SPINBOX_H @@ -50,8 +43,8 @@ typedef struct { int32_t rangeMin; int32_t step; uint8_t digitCount:4; - uint8_t decPointPos:4; //if 0, there is no separator and the number is an integer - uint8_t digits[1+1+16]; //1 sign, 1 point, 16 num digits + uint8_t decPointPos:4; /*if 0, there is no separator and the number is an integer*/ + uint8_t digits[1+1+LV_SPINBOX_MAX_DIGIT_COUNT]; /*1 sign, 1 point, 16 num digits*/ } lv_spinbox_ext_t; From 46309f7333184118e7de8895dfdaad3f2d5f77db Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Sat, 3 Nov 2018 18:07:25 +0100 Subject: [PATCH 04/11] update spinbox object when int value change --- lv_objx/lv_spinbox.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c index 780b27c5d..34f3eddf8 100644 --- a/lv_objx/lv_spinbox.c +++ b/lv_objx/lv_spinbox.c @@ -165,6 +165,8 @@ void lv_spinbox_set_int(const lv_obj_t * spinbox, int32_t i) i = ext->rangeMin; ext->valueDigit = i; + + lv_spinbox_updatevalue(spinbox); } void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position) From 8cafd07e0ac2105ca08d767d97ac1d10d458af3e Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Sat, 3 Nov 2018 18:26:59 +0100 Subject: [PATCH 05/11] Remove double api, add increment/decrement api --- lv_objx/lv_spinbox.c | 140 ++++++++++++++++--------------------------- lv_objx/lv_spinbox.h | 16 ++--- 2 files changed, 58 insertions(+), 98 deletions(-) diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c index 34f3eddf8..5df85b7b0 100644 --- a/lv_objx/lv_spinbox.c +++ b/lv_objx/lv_spinbox.c @@ -24,8 +24,6 @@ static bool lv_spinbox_design(lv_obj_t * templ, const lv_area_t * mask, lv_design_mode_t mode); static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * param); static void lv_spinbox_updatevalue(lv_obj_t * spinbox); -static int32_t lv_spinbox_double_to_int(lv_obj_t * spinbox, double d); -static double lv_spinbox_int_to_double(lv_obj_t * spinbox, int32_t i); /********************** * STATIC VARIABLES @@ -68,8 +66,7 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy) ext->ta.pwd_mode = 0; ext->ta.accapted_chars = "1234567890+-."; - ext->value = 0.0; - ext->valueDigit = 0; + ext->value = 0; ext->decPointPos = 2; ext->digitCount = 5; ext->step = 100; @@ -130,6 +127,40 @@ void lv_spinbox_step_previous(lv_obj_t * spinbox) lv_spinbox_updatevalue(spinbox); } + +void lv_spinbox_increment(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + if(ext->value + ext->step <= ext->rangeMax) + { + /*Special mode when zero crossing*/ + if((ext->value + ext->step) > 0 && ext->value < 0) + { + ext->value = -ext->value; + }/*end special mode*/ + ext->value += ext->step; + } + lv_spinbox_updatevalue(spinbox); +} + +void lv_spinbox_decrement(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + if(ext->value - ext->step >= ext->rangeMin) + { + /*Special mode when zero crossing*/ + if((ext->value - ext->step) < 0 && ext->value > 0) + { + ext->value = -ext->value; + }/*end special mode*/ + ext->value -= ext->step; + } + lv_spinbox_updatevalue(spinbox); +} + + /*====================== * Add/remove functions *=====================*/ @@ -143,17 +174,7 @@ void lv_spinbox_step_previous(lv_obj_t * spinbox) * Setter functions *====================*/ -/* - * New object specific "set" functions come here - */ -void lv_spinbox_set_double(const lv_obj_t * spinbox, double d) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - if(ext == NULL) - return; -} - -void lv_spinbox_set_int(const lv_obj_t * spinbox, int32_t i) +void lv_spinbox_set_value(const lv_obj_t * spinbox, int32_t i) { lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); if(ext == NULL) @@ -164,7 +185,7 @@ void lv_spinbox_set_int(const lv_obj_t * spinbox, int32_t i) if(i < ext->rangeMin) i = ext->rangeMin; - ext->valueDigit = i; + ext->value = i; lv_spinbox_updatevalue(spinbox); } @@ -187,7 +208,7 @@ void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, lv_spinbox_updatevalue(spinbox); } -void lv_spinbox_set_range_int(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax) +void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax) { lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); if(ext == NULL) @@ -196,30 +217,18 @@ void lv_spinbox_set_range_int(const lv_obj_t * spinbox, int32_t rangeMin, int32_ ext->rangeMax = rangeMax; ext->rangeMin = rangeMin; - if(ext->valueDigit > ext->rangeMax) + if(ext->value > ext->rangeMax) { - ext->valueDigit = ext->rangeMax; + ext->value = ext->rangeMax; lv_obj_invalidate(spinbox); } - if(ext->valueDigit < ext->rangeMin) + if(ext->value < ext->rangeMin) { - ext->valueDigit = ext->rangeMin; + ext->value = ext->rangeMin; lv_obj_invalidate(spinbox); } } -void lv_spinbox_set_range_double(const lv_obj_t * spinbox, double rangeMin, double rangeMax) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - if(ext == NULL) - return; - - double rMinDouble = lv_spinbox_double_to_int(spinbox, rangeMin); - double rMaxDouble = lv_spinbox_double_to_int(spinbox, rangeMax); - - lv_spinbox_set_range_int(spinbox, rMinDouble, rMaxDouble); -} - /** * Set a style of a spinbox. @@ -272,6 +281,13 @@ lv_style_t * lv_spinbox_get_style(const lv_obj_t * templ, lv_spinbox_style_t typ return NULL; } + +int32_t lv_spinbox_get_value(const lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + return ext->value; +} /*===================== * Other functions *====================*/ @@ -350,33 +366,11 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p uint32_t c = *((uint32_t *)param); /*uint32_t because can be UTF-8*/ if(c == LV_GROUP_KEY_RIGHT) { - if(ext->valueDigit - ext->step >= ext->rangeMin) - { - /*Special mode when zero crossing*/ - if((ext->valueDigit - ext->step) < 0 && ext->valueDigit > 0) - { - ext->valueDigit = -ext->valueDigit; - }/*end special mode*/ - - ext->valueDigit -= ext->step; - } - - lv_spinbox_updatevalue(spinbox); - lv_label_set_text(ext->ta.label, (char*)ext->digits); + lv_spinbox_decrement(spinbox); } else if(c == LV_GROUP_KEY_LEFT) { - if(ext->valueDigit + ext->step <= ext->rangeMax) - { - /*Special mode when zero crossing*/ - if((ext->valueDigit + ext->step) > 0 && ext->valueDigit < 0) - { - ext->valueDigit = -ext->valueDigit; - }/*end special mode*/ - ext->valueDigit += ext->step; - } - lv_spinbox_updatevalue(spinbox); - lv_label_set_text(ext->ta.label, (char*)ext->digits); + lv_spinbox_increment(spinbox); } else if(c == LV_GROUP_KEY_UP) { @@ -418,7 +412,7 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p static void lv_spinbox_updatevalue(lv_obj_t * spinbox) { lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - int32_t v = ext->valueDigit; + int32_t v = ext->value; int32_t intDigits, decDigits; uint8_t dc = ext->digitCount; @@ -472,34 +466,4 @@ static void lv_spinbox_updatevalue(lv_obj_t * spinbox) lv_ta_set_cursor_pos(spinbox, cPos); } -static double lv_spinbox_int_to_double(lv_obj_t * spinbox, int32_t i) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - - /* - if(i > ext->rangeMax) - i = ext->rangeMax; - if(i < ext->rangeMin) - i = ext->rangeMin; - */ - - return (double)( i / (10 * (ext->digitCount - ext->decPointPos))); -} - -static int32_t lv_spinbox_double_to_int(lv_obj_t * spinbox, double d) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - - int32_t i = (int32_t)(d * (10 * (ext->digitCount - ext->decPointPos))); - - /* - if(i > ext->rangeMax) - i = ext->rangeMax; - if(i < ext->rangeMin) - i = ext->rangeMin; - */ - - return i; -} - #endif diff --git a/lv_objx/lv_spinbox.h b/lv_objx/lv_spinbox.h index d9df3eb15..1b4dc3548 100644 --- a/lv_objx/lv_spinbox.h +++ b/lv_objx/lv_spinbox.h @@ -37,8 +37,7 @@ extern "C" { typedef struct { lv_ta_ext_t ta; /*Ext. of ancestor*/ /*New data for this type */ - double value; - int32_t valueDigit; + int32_t value; int32_t rangeMax; int32_t rangeMin; int32_t step; @@ -72,6 +71,8 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy); void lv_spinbox_step_next(lv_obj_t * spinbox); void lv_spinbox_step_previous(lv_obj_t * spinbox); +void lv_spinbox_increment(lv_obj_t * spinbox); +void lv_spinbox_decrement(lv_obj_t * spinbox); /*====================== @@ -91,12 +92,9 @@ void lv_spinbox_step_previous(lv_obj_t * spinbox); */ void lv_spinbox_set_style(lv_obj_t * templ, lv_spinbox_style_t type, lv_style_t *style); - -void lv_spinbox_set_double(const lv_obj_t * spinbox, double d); -void lv_spinbox_set_int(const lv_obj_t * spinbox, int32_t i); +void lv_spinbox_set_value(const lv_obj_t * spinbox, int32_t i); void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position); -void lv_spinbox_set_range_int(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax); -void lv_spinbox_set_range_double(const lv_obj_t * spinbox, double rangeMin, double rangeMax); +void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax); /*===================== @@ -111,9 +109,7 @@ void lv_spinbox_set_range_double(const lv_obj_t * spinbox, double rangeMin, doub */ lv_style_t * lv_spinbox_get_style(const lv_obj_t * templ, lv_spinbox_style_t type); - -double lv_spinbox_get_double(const lv_obj_t * spinbox); -int32_t lv_spinbox_get_int(const lv_obj_t * spinbox); +int32_t lv_spinbox_get_value(const lv_obj_t * spinbox); /*===================== * Other functions From a88ad62af34492dc284a596913160de449f99940 Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Sat, 3 Nov 2018 18:41:06 +0100 Subject: [PATCH 06/11] Add/fix comment --- lv_objx/lv_spinbox.c | 18 ++++++------ lv_objx/lv_spinbox.h | 65 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 63 insertions(+), 20 deletions(-) diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c index 5df85b7b0..00d926aeb 100644 --- a/lv_objx/lv_spinbox.c +++ b/lv_objx/lv_spinbox.c @@ -1,5 +1,5 @@ /** - * @file lv_templ.c + * @file lv_spinbox.c * */ @@ -21,7 +21,7 @@ /********************** * STATIC PROTOTYPES **********************/ -static bool lv_spinbox_design(lv_obj_t * templ, const lv_area_t * mask, lv_design_mode_t mode); +static bool lv_spinbox_design(lv_obj_t * spinbox, const lv_area_t * mask, lv_design_mode_t mode); static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * param); static void lv_spinbox_updatevalue(lv_obj_t * spinbox); @@ -232,7 +232,7 @@ void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t rangeMin, int32_t ra /** * Set a style of a spinbox. - * @param templ pointer to spinbox object + * @param spinbox pointer to spinbox object * @param type which style should be set * @param style pointer to a style */ @@ -264,13 +264,13 @@ void lv_spinbox_set_style(lv_obj_t * spinbox, lv_spinbox_style_t type, lv_style_ /** * Get style of a spinbox. - * @param templ pointer to spinbox object + * @param spinbox pointer to spinbox object * @param type which style should be get * @return style pointer to the style */ -lv_style_t * lv_spinbox_get_style(const lv_obj_t * templ, lv_spinbox_style_t type) +lv_style_t * lv_spinbox_get_style(const lv_obj_t * spinbox, lv_spinbox_style_t type) { - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(templ); + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); switch(type) { default: @@ -302,7 +302,7 @@ int32_t lv_spinbox_get_value(const lv_obj_t * spinbox) /** * Handle the drawing related tasks of the spinboxs - * @param templ pointer to an object + * @param spinbox pointer to an object * @param mask the object will be drawn only in this area * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area * (return 'true' if yes) @@ -310,7 +310,7 @@ int32_t lv_spinbox_get_value(const lv_obj_t * spinbox) * LV_DESIGN_DRAW_POST: drawing after every children are drawn * @param return true/false, depends on 'mode' */ -static bool lv_spinbox_design(lv_obj_t * templ, const lv_area_t * mask, lv_design_mode_t mode) +static bool lv_spinbox_design(lv_obj_t * spinbox, const lv_area_t * mask, lv_design_mode_t mode) { /*Return false if the object is not covers the mask_p area*/ if(mode == LV_DESIGN_COVER_CHK) { @@ -330,7 +330,7 @@ static bool lv_spinbox_design(lv_obj_t * templ, const lv_area_t * mask, lv_desig /** * Signal function of the spinbox - * @param templ pointer to a spinbox object + * @param spinbox pointer to a spinbox object * @param sign a signal type from lv_signal_t enum * @param param pointer to a signal specific variable * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted diff --git a/lv_objx/lv_spinbox.h b/lv_objx/lv_spinbox.h index 1b4dc3548..9809bfd92 100644 --- a/lv_objx/lv_spinbox.h +++ b/lv_objx/lv_spinbox.h @@ -1,5 +1,5 @@ /** - * @file lv_templ.h + * @file lv_spinbox.h * */ @@ -33,7 +33,7 @@ extern "C" { /********************** * TYPEDEFS **********************/ -/*Data of template*/ +/*Data of spinbox*/ typedef struct { lv_ta_ext_t ta; /*Ext. of ancestor*/ /*New data for this type */ @@ -62,16 +62,35 @@ typedef uint8_t lv_spinbox_style_t; **********************/ /** - * Create a template objects - * @param par pointer to an object, it will be the parent of the new template - * @param copy pointer to a template object, if not NULL then the new object will be copied from it - * @return pointer to the created template + * Create a spinbox objects + * @param par pointer to an object, it will be the parent of the new spinbox + * @param copy pointer to a spinbox object, if not NULL then the new object will be copied from it + * @return pointer to the created spinbox */ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy); +/** + * Select next lower digit for edition by dividing the step by 10 + * @param spinbox pointer to spinbox + */ void lv_spinbox_step_next(lv_obj_t * spinbox); + +/** + * Select next higher digit for edition by multiplying the step by 10 + * @param spinbox pointer to spinbox + */ void lv_spinbox_step_previous(lv_obj_t * spinbox); + +/** + * Increment spinbox value by one step + * @param spinbox pointer to spinbox + */ void lv_spinbox_increment(lv_obj_t * spinbox); + +/** + * Decrement spinbox value by one step + * @param spinbox pointer to spinbox + */ void lv_spinbox_decrement(lv_obj_t * spinbox); @@ -85,15 +104,34 @@ void lv_spinbox_decrement(lv_obj_t * spinbox); *====================*/ /** - * Set a style of a template. + * Set a style of a spinbox. * @param templ pointer to template object * @param type which style should be set * @param style pointer to a style */ -void lv_spinbox_set_style(lv_obj_t * templ, lv_spinbox_style_t type, lv_style_t *style); +void lv_spinbox_set_style(lv_obj_t * spinbox, lv_spinbox_style_t type, lv_style_t *style); +/** + * Set spinbox value + * @param spinbox pointer to spinbox + * @param i value to be set + */ void lv_spinbox_set_value(const lv_obj_t * spinbox, int32_t i); + +/** + * Set spinbox digit format (digit count and decimal format) + * @param spinbox pointer to spinbox + * @param digit_count number of digit excluding the decimal separator and the sign + * @param separator_position number of digit before the decimal point. If 0, decimal point is not shown + */ void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position); + +/** + * Set spinbox value range + * @param spinbox pointer to spinbox + * @param i rangeMin maximum value, inclusive + * @param i rangeMax minimum value, inclusive + */ void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax); @@ -102,13 +140,18 @@ void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t rangeMin, int32_t ra *====================*/ /** - * Get style of a template. + * Get style of a spinbox. * @param templ pointer to template object * @param type which style should be get * @return style pointer to the style */ -lv_style_t * lv_spinbox_get_style(const lv_obj_t * templ, lv_spinbox_style_t type); +lv_style_t * lv_spinbox_get_style(const lv_obj_t * spinbox, lv_spinbox_style_t type); +/** + * Get the spinbox numeral value (user has to convert to float according to its digit format) + * @param spinbox pointer to spinbox + * @return value integer value of the spinbox + */ int32_t lv_spinbox_get_value(const lv_obj_t * spinbox); /*===================== @@ -119,7 +162,7 @@ int32_t lv_spinbox_get_value(const lv_obj_t * spinbox); * MACROS **********************/ -#endif /*USE_LV_TEMPL*/ +#endif /*USE_LV_SPINBOX*/ #ifdef __cplusplus } /* extern "C" */ From e5464f5be715b9e6328a415f3f0d767d3b46a2db Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Sun, 4 Nov 2018 20:21:51 +0100 Subject: [PATCH 07/11] spinbox: formatting --- lv_objx/lv_spinbox.c | 314 +++++++++++++++++++------------------------ lv_objx/lv_spinbox.h | 143 ++++++++++---------- 2 files changed, 214 insertions(+), 243 deletions(-) diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c index 00d926aeb..bcec2f27e 100644 --- a/lv_objx/lv_spinbox.c +++ b/lv_objx/lv_spinbox.c @@ -21,7 +21,6 @@ /********************** * STATIC PROTOTYPES **********************/ -static bool lv_spinbox_design(lv_obj_t * spinbox, const lv_area_t * mask, lv_design_mode_t mode); static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * param); static void lv_spinbox_updatevalue(lv_obj_t * spinbox); @@ -67,11 +66,11 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy) ext->ta.accapted_chars = "1234567890+-."; ext->value = 0; - ext->decPointPos = 2; - ext->digitCount = 5; + ext->dec_point_pos = 2; + ext->digit_count = 5; ext->step = 100; - ext->rangeMax = 99999; - ext->rangeMin = -99999; + ext->range_max = 99999; + ext->range_min = -99999; lv_ta_set_cursor_type(new_spinbox, LV_CURSOR_BLOCK | LV_CURSOR_HIDDEN); /*hidden by default*/ lv_ta_set_cursor_pos(new_spinbox, 4); @@ -79,16 +78,21 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy) /*The signal and design functions are not copied so set them here*/ lv_obj_set_signal_func(new_spinbox, lv_spinbox_signal); - lv_obj_set_design_func(new_spinbox, lv_spinbox_design); + lv_obj_set_design_func(new_spinbox, ancestor_design); /*Leave the Text area's design function*/ /*Init the new spinbox spinbox*/ if(copy == NULL) { - + /*Already inited above*/ } /*Copy an existing spinbox*/ else { lv_spinbox_ext_t * copy_ext = lv_obj_get_ext_attr(copy); + lv_spinbox_set_value(new_spinbox, copy_ext->value); + lv_spinbox_set_digit_format(new_spinbox, copy_ext->digit_count, copy_ext->dec_point_pos); + lv_spinbox_set_range(new_spinbox, copy_ext->range_min, copy_ext->range_max); + lv_spinbox_set_step(new_spinbox, copy_ext->step); + /*Refresh the style with new signal function*/ lv_obj_refresh_style(new_spinbox); } @@ -101,95 +105,37 @@ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy) } -void lv_spinbox_step_next(lv_obj_t * spinbox) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - - - if((ext->step / 10) < ext->rangeMax && (ext->step / 10) > ext->rangeMin && (ext->step / 10) > 0) - { - ext->step /= 10; - } - - lv_spinbox_updatevalue(spinbox); -} - -void lv_spinbox_step_previous(lv_obj_t * spinbox) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - - - if((ext->step * 10) <= ext->rangeMax && (ext->step * 10) > ext->rangeMin && (ext->step * 10) > 0) - { - ext->step *= 10; - } - - lv_spinbox_updatevalue(spinbox); -} - - -void lv_spinbox_increment(lv_obj_t * spinbox) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - - if(ext->value + ext->step <= ext->rangeMax) - { - /*Special mode when zero crossing*/ - if((ext->value + ext->step) > 0 && ext->value < 0) - { - ext->value = -ext->value; - }/*end special mode*/ - ext->value += ext->step; - } - lv_spinbox_updatevalue(spinbox); -} - -void lv_spinbox_decrement(lv_obj_t * spinbox) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - - if(ext->value - ext->step >= ext->rangeMin) - { - /*Special mode when zero crossing*/ - if((ext->value - ext->step) < 0 && ext->value > 0) - { - ext->value = -ext->value; - }/*end special mode*/ - ext->value -= ext->step; - } - lv_spinbox_updatevalue(spinbox); -} - - -/*====================== - * Add/remove functions - *=====================*/ - -/* - * New object specific "add" or "remove" functions come here - */ - - /*===================== * Setter functions *====================*/ +/** + * Set spinbox value + * @param spinbox pointer to spinbox + * @param i value to be set + */ void lv_spinbox_set_value(const lv_obj_t * spinbox, int32_t i) { lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); if(ext == NULL) return; - if(i > ext->rangeMax) - i = ext->rangeMax; - if(i < ext->rangeMin) - i = ext->rangeMin; + if(i > ext->range_max) + i = ext->range_max; + if(i < ext->range_min) + i = ext->range_min; ext->value = i; lv_spinbox_updatevalue(spinbox); } +/** + * Set spinbox digit format (digit count and decimal format) + * @param spinbox pointer to spinbox + * @param digit_count number of digit excluding the decimal separator and the sign + * @param separator_position number of digit before the decimal point. If 0, decimal point is not shown + */ void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position) { lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); @@ -202,55 +148,48 @@ void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, if(separator_position < LV_SPINBOX_MAX_DIGIT_COUNT) separator_position = LV_SPINBOX_MAX_DIGIT_COUNT; - ext->digitCount = digit_count; - ext->decPointPos = separator_position; + ext->digit_count = digit_count; + ext->dec_point_pos = separator_position; lv_spinbox_updatevalue(spinbox); } -void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax) +/** + * Set spinbox step + * @param spinbox pointer to spinbox + * @param step steps on increment/decrement + */ +void lv_spinbox_set_step(const lv_obj_t * spinbox, uint32_t step) { - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - if(ext == NULL) - return; + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + if(ext == NULL) return; - ext->rangeMax = rangeMax; - ext->rangeMin = rangeMin; - - if(ext->value > ext->rangeMax) - { - ext->value = ext->rangeMax; - lv_obj_invalidate(spinbox); - } - if(ext->value < ext->rangeMin) - { - ext->value = ext->rangeMin; - lv_obj_invalidate(spinbox); - } + ext->step = step; } - /** - * Set a style of a spinbox. - * @param spinbox pointer to spinbox object - * @param type which style should be set - * @param style pointer to a style + * Set spinbox value range + * @param spinbox pointer to spinbox + * @param range_min maximum value, inclusive + * @param range_max minimum value, inclusive */ -void lv_spinbox_set_style(lv_obj_t * spinbox, lv_spinbox_style_t type, lv_style_t * style) +void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t range_min, int32_t range_max) { lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + if(ext == NULL) return; - switch(type) { - case LV_SPINBOX_STYLE_BG: - lv_page_set_style(spinbox, LV_PAGE_STYLE_BG, style); - break; - case LV_SPINBOX_STYLE_SB: - lv_page_set_style(spinbox, LV_PAGE_STYLE_SB, style); - break; - case LV_SPINBOX_STYLE_CURSOR: - ext->ta.cursor.style = style; - lv_obj_refresh_ext_size(lv_page_get_scrl(spinbox)); /*Refresh ext. size because of cursor drawing*/ - break; + ext->range_max = range_max; + ext->range_min = range_min; + + if(ext->value > ext->range_max) + { + ext->value = ext->range_max; + lv_obj_invalidate(spinbox); + } + if(ext->value < ext->range_min) + { + ext->value = ext->range_min; + lv_obj_invalidate(spinbox); } } @@ -258,76 +197,102 @@ void lv_spinbox_set_style(lv_obj_t * spinbox, lv_spinbox_style_t type, lv_style_ * Getter functions *====================*/ -/* - * New object specific "get" functions come here - */ - /** - * Get style of a spinbox. - * @param spinbox pointer to spinbox object - * @param type which style should be get - * @return style pointer to the style + * Get the spinbox numeral value (user has to convert to float according to its digit format) + * @param spinbox pointer to spinbox + * @return value integer value of the spinbox */ -lv_style_t * lv_spinbox_get_style(const lv_obj_t * spinbox, lv_spinbox_style_t type) -{ - lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); - - switch(type) { - default: - return NULL; - } - - /*To avoid warning*/ - return NULL; -} - - int32_t lv_spinbox_get_value(const lv_obj_t * spinbox) { lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); return ext->value; } + /*===================== * Other functions *====================*/ -/* - * New object specific "other" functions come here +/** + * Select next lower digit for edition by dividing the step by 10 + * @param spinbox pointer to spinbox */ +void lv_spinbox_step_next(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + + if((ext->step / 10) < ext->range_max && (ext->step / 10) > ext->range_min && (ext->step / 10) > 0) + { + ext->step /= 10; + } + + lv_spinbox_updatevalue(spinbox); +} + +/** + * Select next higher digit for edition by multiplying the step by 10 + * @param spinbox pointer to spinbox + */ +void lv_spinbox_step_previous(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + + if((ext->step * 10) <= ext->range_max && (ext->step * 10) > ext->range_min && (ext->step * 10) > 0) + { + ext->step *= 10; + } + + lv_spinbox_updatevalue(spinbox); +} + +/** + * Increment spinbox value by one step + * @param spinbox pointer to spinbox + */ +void lv_spinbox_increment(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + if(ext->value + ext->step <= ext->range_max) + { + /*Special mode when zero crossing*/ + if((ext->value + ext->step) > 0 && ext->value < 0) + { + ext->value = -ext->value; + }/*end special mode*/ + ext->value += ext->step; + } + lv_spinbox_updatevalue(spinbox); +} + +/** + * Decrement spinbox value by one step + * @param spinbox pointer to spinbox + */ +void lv_spinbox_decrement(lv_obj_t * spinbox) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + + if(ext->value - ext->step >= ext->range_min) + { + /*Special mode when zero crossing*/ + if((ext->value - ext->step) < 0 && ext->value > 0) + { + ext->value = -ext->value; + }/*end special mode*/ + ext->value -= ext->step; + } + lv_spinbox_updatevalue(spinbox); +} + + /********************** * STATIC FUNCTIONS **********************/ -/** - * Handle the drawing related tasks of the spinboxs - * @param spinbox pointer to an object - * @param mask the object will be drawn only in this area - * @param mode LV_DESIGN_COVER_CHK: only check if the object fully covers the 'mask_p' area - * (return 'true' if yes) - * LV_DESIGN_DRAW: draw the object (always return 'true') - * LV_DESIGN_DRAW_POST: drawing after every children are drawn - * @param return true/false, depends on 'mode' - */ -static bool lv_spinbox_design(lv_obj_t * spinbox, const lv_area_t * mask, lv_design_mode_t mode) -{ - /*Return false if the object is not covers the mask_p area*/ - if(mode == LV_DESIGN_COVER_CHK) { - return false; - } - /*Draw the object*/ - else if(mode == LV_DESIGN_DRAW_MAIN) { - - } - /*Post draw when the children are drawn*/ - else if(mode == LV_DESIGN_DRAW_POST) { - - } - - return true; -} - /** * Signal function of the spinbox * @param spinbox pointer to a spinbox object @@ -349,7 +314,6 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p if(res != LV_RES_OK) return res; } - if(sign == LV_SIGNAL_CLEANUP) { /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/ } else if(sign == LV_SIGNAL_GET_TYPE) @@ -385,9 +349,9 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p if(c == LV_GROUP_KEY_ENTER) { int p = lv_ta_get_cursor_pos(spinbox); - if(p == ext->digitCount + 1) + if(p == ext->digit_count + 1) { - for(int i = 0; i < ext->digitCount; i++) + for(int i = 0; i < ext->digit_count; i++) lv_spinbox_step_previous(spinbox); } else { @@ -414,10 +378,10 @@ static void lv_spinbox_updatevalue(lv_obj_t * spinbox) lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); int32_t v = ext->value; int32_t intDigits, decDigits; - uint8_t dc = ext->digitCount; + uint8_t dc = ext->digit_count; - intDigits = (ext->decPointPos==0)?ext->digitCount:ext->decPointPos; - decDigits = ext->digitCount - intDigits; + intDigits = (ext->dec_point_pos==0)?ext->digit_count:ext->dec_point_pos; + decDigits = ext->digit_count - intDigits; ext->digits[0] = v>=0?'+':'-'; @@ -452,7 +416,7 @@ static void lv_spinbox_updatevalue(lv_obj_t * spinbox) lv_label_set_text(ext->ta.label, (char*)ext->digits); int32_t step = ext->step; - uint8_t cPos = ext->digitCount; + uint8_t cPos = ext->digit_count; while(step >= 10) { step /= 10; diff --git a/lv_objx/lv_spinbox.h b/lv_objx/lv_spinbox.h index 9809bfd92..d0bee6422 100644 --- a/lv_objx/lv_spinbox.h +++ b/lv_objx/lv_spinbox.h @@ -38,11 +38,11 @@ typedef struct { lv_ta_ext_t ta; /*Ext. of ancestor*/ /*New data for this type */ int32_t value; - int32_t rangeMax; - int32_t rangeMin; + int32_t range_max; + int32_t range_min; int32_t step; - uint8_t digitCount:4; - uint8_t decPointPos:4; /*if 0, there is no separator and the number is an integer*/ + uint8_t digit_count:4; + uint8_t dec_point_pos:4; /*if 0, there is no separator and the number is an integer*/ uint8_t digits[1+1+LV_SPINBOX_MAX_DIGIT_COUNT]; /*1 sign, 1 point, 16 num digits*/ } lv_spinbox_ext_t; @@ -69,6 +69,77 @@ typedef uint8_t lv_spinbox_style_t; */ lv_obj_t * lv_spinbox_create(lv_obj_t * par, const lv_obj_t * copy); +/*===================== + * Setter functions + *====================*/ + +/** + * Set a style of a spinbox. + * @param templ pointer to template object + * @param type which style should be set + * @param style pointer to a style + */ +static inline void lv_spinbox_set_style(lv_obj_t * spinbox, lv_spinbox_style_t type, lv_style_t *style) +{ + lv_ta_set_style(spinbox, type, style); +} + +/** + * Set spinbox value + * @param spinbox pointer to spinbox + * @param i value to be set + */ +void lv_spinbox_set_value(const lv_obj_t * spinbox, int32_t i); + +/** + * Set spinbox digit format (digit count and decimal format) + * @param spinbox pointer to spinbox + * @param digit_count number of digit excluding the decimal separator and the sign + * @param separator_position number of digit before the decimal point. If 0, decimal point is not shown + */ +void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position); + +/** + * Set spinbox step + * @param spinbox pointer to spinbox + * @param step steps on increment/decrement + */ +void lv_spinbox_set_step(const lv_obj_t * spinbox, uint32_t step); + +/** + * Set spinbox value range + * @param spinbox pointer to spinbox + * @param range_min maximum value, inclusive + * @param range_max minimum value, inclusive + */ +void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t range_min, int32_t range_max); + +/*===================== + * Getter functions + *====================*/ + +/** + * Get style of a spinbox. + * @param templ pointer to template object + * @param type which style should be get + * @return style pointer to the style + */ +static inline lv_style_t * lv_spinbox_get_style(const lv_obj_t * spinbox, lv_spinbox_style_t type) +{ + return lv_ta_get_style(spinbox, type); +} + +/** + * Get the spinbox numeral value (user has to convert to float according to its digit format) + * @param spinbox pointer to spinbox + * @return value integer value of the spinbox + */ +int32_t lv_spinbox_get_value(const lv_obj_t * spinbox); + +/*===================== + * Other functions + *====================*/ + /** * Select next lower digit for edition by dividing the step by 10 * @param spinbox pointer to spinbox @@ -94,70 +165,6 @@ void lv_spinbox_increment(lv_obj_t * spinbox); void lv_spinbox_decrement(lv_obj_t * spinbox); -/*====================== - * Add/remove functions - *=====================*/ - - -/*===================== - * Setter functions - *====================*/ - -/** - * Set a style of a spinbox. - * @param templ pointer to template object - * @param type which style should be set - * @param style pointer to a style - */ -void lv_spinbox_set_style(lv_obj_t * spinbox, lv_spinbox_style_t type, lv_style_t *style); - -/** - * Set spinbox value - * @param spinbox pointer to spinbox - * @param i value to be set - */ -void lv_spinbox_set_value(const lv_obj_t * spinbox, int32_t i); - -/** - * Set spinbox digit format (digit count and decimal format) - * @param spinbox pointer to spinbox - * @param digit_count number of digit excluding the decimal separator and the sign - * @param separator_position number of digit before the decimal point. If 0, decimal point is not shown - */ -void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, uint8_t separator_position); - -/** - * Set spinbox value range - * @param spinbox pointer to spinbox - * @param i rangeMin maximum value, inclusive - * @param i rangeMax minimum value, inclusive - */ -void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t rangeMin, int32_t rangeMax); - - -/*===================== - * Getter functions - *====================*/ - -/** - * Get style of a spinbox. - * @param templ pointer to template object - * @param type which style should be get - * @return style pointer to the style - */ -lv_style_t * lv_spinbox_get_style(const lv_obj_t * spinbox, lv_spinbox_style_t type); - -/** - * Get the spinbox numeral value (user has to convert to float according to its digit format) - * @param spinbox pointer to spinbox - * @return value integer value of the spinbox - */ -int32_t lv_spinbox_get_value(const lv_obj_t * spinbox); - -/*===================== - * Other functions - *====================*/ - /********************** * MACROS **********************/ From 6e116d9e59d1a4b6613392780f54401022919b80 Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Mon, 5 Nov 2018 11:52:21 +0100 Subject: [PATCH 08/11] Add digit padding feature [WIP] --- lv_objx/lv_spinbox.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lv_objx/lv_spinbox.h b/lv_objx/lv_spinbox.h index 9809bfd92..3ce36c3f3 100644 --- a/lv_objx/lv_spinbox.h +++ b/lv_objx/lv_spinbox.h @@ -43,6 +43,8 @@ typedef struct { int32_t step; uint8_t digitCount:4; uint8_t decPointPos:4; /*if 0, there is no separator and the number is an integer*/ + uint8_t digitPaddingLeft:4; + uint8_t digitPaddingRight:4; uint8_t digits[1+1+LV_SPINBOX_MAX_DIGIT_COUNT]; /*1 sign, 1 point, 16 num digits*/ } lv_spinbox_ext_t; From d5747a037977d23ac4fda190304fa8be670e3514 Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Mon, 5 Nov 2018 14:47:30 +0100 Subject: [PATCH 09/11] fix set_digit_format --- lv_objx/lv_spinbox.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c index 468d9d7c8..a5a78c2c1 100644 --- a/lv_objx/lv_spinbox.c +++ b/lv_objx/lv_spinbox.c @@ -146,7 +146,7 @@ void lv_spinbox_set_digit_format(const lv_obj_t * spinbox, uint8_t digit_count, if(digit_count > LV_SPINBOX_MAX_DIGIT_COUNT) digit_count = LV_SPINBOX_MAX_DIGIT_COUNT; - if(separator_position < LV_SPINBOX_MAX_DIGIT_COUNT) + if(separator_position > LV_SPINBOX_MAX_DIGIT_COUNT) separator_position = LV_SPINBOX_MAX_DIGIT_COUNT; ext->digit_count = digit_count; From 2c7b51ca56b7acc1a33e11467cc3d00307e205e1 Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Mon, 5 Nov 2018 14:48:33 +0100 Subject: [PATCH 10/11] Add left digit padding handling --- lv_objx/lv_spinbox.c | 33 ++++++++++++++++++++++++++------- lv_objx/lv_spinbox.h | 6 ++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c index a5a78c2c1..9ec5766a5 100644 --- a/lv_objx/lv_spinbox.c +++ b/lv_objx/lv_spinbox.c @@ -205,6 +205,18 @@ void lv_spinbox_set_value_changed_cb(const lv_obj_t * spinbox, lv_spinbox_value_ ext->value_changed_cb = cb; } +/** + * Set spinbox left padding in digits count (added between sign and first digit) + * @param spinbox pointer to spinbox + * @param cb Callback function called on value change event + */ +void lv_spinbox_set_padding_left(const lv_obj_t * spinbox, uint8_t padding) +{ + lv_spinbox_ext_t * ext = lv_obj_get_ext_attr(spinbox); + ext->digit_padding_left = padding; + lv_spinbox_updatevalue(spinbox); +} + /*===================== * Getter functions *====================*/ @@ -387,7 +399,7 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p if(c == LV_GROUP_KEY_ENTER) { int p = lv_ta_get_cursor_pos(spinbox); - if(p == ext->digit_count + 1) + if(p == (1 + ext->digit_padding_left + ext->digit_count)) { for(int i = 0; i < ext->digit_count; i++) lv_spinbox_step_previous(spinbox); @@ -422,6 +434,12 @@ static void lv_spinbox_updatevalue(lv_obj_t * spinbox) ext->digits[0] = v>=0 ? '+' : '-'; + int pl; /*padding left*/ + for(pl = 0; pl < ext->digit_padding_left; pl++) + { + ext->digits[1 + pl] = ' '; + } + int i = 0; uint8_t digits[16]; @@ -436,30 +454,31 @@ static void lv_spinbox_updatevalue(lv_obj_t * spinbox) int k; for(k = 0; k < intDigits; k++) { - ext->digits[1 + k] = '0' + digits[--i]; + ext->digits[1 + pl + k] = '0' + digits[--i]; } - ext->digits[1 + intDigits] = '.'; + ext->digits[1 + pl + intDigits] = '.'; int d; for(d = 0; d < decDigits; d++) { - ext->digits[1 + intDigits + 1 + d] = '0' + digits[--i]; + ext->digits[1 + pl + intDigits + 1 + d] = '0' + digits[--i]; } - ext->digits[1 + intDigits + 1 + decDigits] = '\0'; + ext->digits[1 + pl + intDigits + 1 + decDigits] = '\0'; lv_label_set_text(ext->ta.label, (char*)ext->digits); int32_t step = ext->step; - uint8_t cPos = ext->digit_count; + uint8_t cPos = ext->digit_count + pl; while(step >= 10) { step /= 10; cPos--; } - if(cPos > intDigits) + + if(cPos > pl + intDigits ) { cPos ++; } diff --git a/lv_objx/lv_spinbox.h b/lv_objx/lv_spinbox.h index 25dfbde82..628e5dfae 100644 --- a/lv_objx/lv_spinbox.h +++ b/lv_objx/lv_spinbox.h @@ -127,6 +127,12 @@ void lv_spinbox_set_range(const lv_obj_t * spinbox, int32_t range_min, int32_t r */ void lv_spinbox_set_value_changed_cb(const lv_obj_t * spinbox, lv_spinbox_value_changed_cb_t cb); +/** + * Set spinbox left padding in digits count (added between sign and first digit) + * @param spinbox pointer to spinbox + * @param cb Callback function called on value change event + */ +void lv_spinbox_set_padding_left(const lv_obj_t * spinbox, uint8_t padding); /*===================== * Getter functions From d4fb9b13434ec667cdace0718540098ec8ca66b8 Mon Sep 17 00:00:00 2001 From: AloyseTech Date: Tue, 6 Nov 2018 17:55:11 +0100 Subject: [PATCH 11/11] reverse encoder direction (CCW==decrement, CW==increment) --- lv_objx/lv_spinbox.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lv_objx/lv_spinbox.c b/lv_objx/lv_spinbox.c index 9ec5766a5..1209c0d15 100644 --- a/lv_objx/lv_spinbox.c +++ b/lv_objx/lv_spinbox.c @@ -368,7 +368,7 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p { if(indev_type == LV_INDEV_TYPE_ENCODER) { - lv_spinbox_decrement(spinbox); + lv_spinbox_increment(spinbox); } else { @@ -379,7 +379,7 @@ static lv_res_t lv_spinbox_signal(lv_obj_t * spinbox, lv_signal_t sign, void * p { if(indev_type == LV_INDEV_TYPE_ENCODER) { - lv_spinbox_increment(spinbox); + lv_spinbox_decrement(spinbox); } else {