From c2404c307c569b4bdc0b9de1174988c799e5ec84 Mon Sep 17 00:00:00 2001 From: Wielebny666 Date: Wed, 29 Apr 2020 13:24:07 +0200 Subject: [PATCH] Adjusting min and max values to the number format --- src/lv_misc/lv_math.c | 20 ++++++++++++++++++++ src/lv_misc/lv_math.h | 8 ++++++++ src/lv_objx/lv_spinbox.c | 7 +++++++ src/lv_objx/lv_spinbox.h | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/lv_misc/lv_math.c b/src/lv_misc/lv_math.c index f015456e4..74ddbe49c 100644 --- a/src/lv_misc/lv_math.c +++ b/src/lv_misc/lv_math.c @@ -198,6 +198,26 @@ uint32_t lv_sqrt(uint32_t num) return root; } +/** + * Calculate the integer exponents. + * @param base + * @param power + * @return base raised to the power exponent + */ +int64_t lv_pow(int64_t base, int8_t exp) +{ + int64_t result = 1; + while (exp) + { + if (exp & 1) + result *= base; + exp >>= 1; + base *= base; + } + + return result; +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/lv_misc/lv_math.h b/src/lv_misc/lv_math.h index 0f93a7c6a..7fadc8590 100644 --- a/src/lv_misc/lv_math.h +++ b/src/lv_misc/lv_math.h @@ -74,6 +74,14 @@ uint16_t lv_atan2(int x, int y); */ uint32_t lv_sqrt(uint32_t num); +/** + * Calculate the integer exponents. + * @param base + * @param power + * @return base raised to the power exponent + */ +int64_t lv_pow(int64_t base, int8_t exp); + /********************** * MACROS **********************/ diff --git a/src/lv_objx/lv_spinbox.c b/src/lv_objx/lv_spinbox.c index 21a19205d..e89570856 100644 --- a/src/lv_objx/lv_spinbox.c +++ b/src/lv_objx/lv_spinbox.c @@ -154,6 +154,13 @@ void lv_spinbox_set_digit_format(lv_obj_t * spinbox, uint8_t digit_count, uint8_ if(separator_position > LV_SPINBOX_MAX_DIGIT_COUNT) separator_position = LV_SPINBOX_MAX_DIGIT_COUNT; + if(digit_count < LV_SPINBOX_MAX_DIGIT_COUNT) + { + uint64_t max_val = lv_pow(10, digit_count); + if(ext->range_max > max_val - 1) ext->range_max = max_val - 1; + if(ext->range_min < - max_val + 1) ext->range_min = - max_val + 1; + } + ext->digit_count = digit_count; ext->dec_point_pos = separator_position; diff --git a/src/lv_objx/lv_spinbox.h b/src/lv_objx/lv_spinbox.h index 396159590..5e7f3c17d 100644 --- a/src/lv_objx/lv_spinbox.h +++ b/src/lv_objx/lv_spinbox.h @@ -32,7 +32,7 @@ extern "C" { /********************* * DEFINES *********************/ -#define LV_SPINBOX_MAX_DIGIT_COUNT 16 +#define LV_SPINBOX_MAX_DIGIT_COUNT 10 /********************** * TYPEDEFS