From eb376899ce429b6f4deac6260076fb82b4ee1520 Mon Sep 17 00:00:00 2001 From: Gabor Kiss-Vamosi Date: Sun, 4 Mar 2018 17:51:41 +0100 Subject: [PATCH] remove sprintf from lv_gauge the save ROM (custom BCD converter added to lv_math.c) --- lv_misc/lv_math.c | 96 ++++++++++++++++++++++++++++++++++++++++++++++ lv_misc/lv_math.h | 2 + lv_objx/lv_gauge.c | 3 +- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 lv_misc/lv_math.c diff --git a/lv_misc/lv_math.c b/lv_misc/lv_math.c new file mode 100644 index 000000000..128b32c9c --- /dev/null +++ b/lv_misc/lv_math.c @@ -0,0 +1,96 @@ +/** + * @file lv_math.c + * + */ + +/********************* + * INCLUDES + *********************/ +#include "lv_math.h" +#include + +/********************* + * DEFINES + *********************/ + +/********************** + * TYPEDEFS + **********************/ + +/********************** + * STATIC PROTOTYPES + **********************/ + +/********************** + * STATIC VARIABLES + **********************/ + +/********************** + * MACROS + **********************/ + +/********************** + * GLOBAL FUNCTIONS + **********************/ + +/** + * Convert a number to string + * @param num a number + * @param buf pointer to a `char` buffer. The result will be stored here (max 10 elements) + */ +void lv_math_num_to_str(int32_t num, char * buf) +{ + char * buf_ori = buf; + if(num == 0) { + buf[0] = '0'; + buf[1] = '\0'; + return; + } else if(num < 0) { + (*buf) = '-'; + buf++; + num = LV_MATH_ABS(num); + } + uint32_t output = 0; + int8_t i; + + for(i = 31; i >= 0; i--){ + if((output & 0xF) >= 5) + output += 3; + if(((output & 0xF0) >> 4) >= 5) + output += (3 << 4); + if(((output & 0xF00) >> 8) >= 5) + output += (3 << 8); + if(((output & 0xF000) >> 12) >= 5) + output += (3 << 12); + if(((output & 0xF0000) >> 16) >= 5) + output += (3 << 16); + if(((output & 0xF00000) >> 20) >= 5) + output += (3 << 20); + if(((output & 0xF000000) >> 24) >= 5) + output += (3 << 24); + if(((output & 0xF0000000) >> 28) >= 5) + output += (3 << 28); + output = (output << 1) | ((num >> i) & 1); + } + + uint8_t digit; + bool leading_zero_ready = false; + for(i = 28; i >= 0; i -= 4) { + digit = ((output >> i) & 0xF) + '0'; + if(digit == '0' && leading_zero_ready == false) continue; + + leading_zero_ready = true; + (*buf) = digit; + buf++; + } + + (*buf) = '\0'; + + printf("Input decimal or binary: %d\nOutput BCD: %X\nOutput str: %s\n", num, output, buf_ori); +} + +/********************** + * STATIC FUNCTIONS + **********************/ + + diff --git a/lv_misc/lv_math.h b/lv_misc/lv_math.h index d537f7de8..458f26d38 100644 --- a/lv_misc/lv_math.h +++ b/lv_misc/lv_math.h @@ -14,6 +14,7 @@ extern "C" { /********************* * INCLUDES *********************/ +#include /********************* * DEFINES @@ -30,6 +31,7 @@ extern "C" { /********************** * GLOBAL PROTOTYPES **********************/ +void lv_math_num_to_str(int32_t num, char * buf); /********************** * MACROS diff --git a/lv_objx/lv_gauge.c b/lv_objx/lv_gauge.c index 03f9519d7..e5468b243 100644 --- a/lv_objx/lv_gauge.c +++ b/lv_objx/lv_gauge.c @@ -353,7 +353,8 @@ static void lv_gauge_draw_scale(lv_obj_t * gauge, const lv_area_t * mask) int16_t scale_act = (int32_t)((int32_t)(max - min) * i) / (label_num - 1); scale_act += min; - sprintf(scale_txt, "%d", scale_act); + lv_math_num_to_str(scale_act, scale_txt); +// sprintf(scale_txt, "%d", scale_act); lv_area_t label_cord; lv_point_t label_size;