From ba1fba1f10e35de81459c32ca469127298f40a21 Mon Sep 17 00:00:00 2001 From: Themba Dube Date: Tue, 6 Aug 2019 09:28:50 -0400 Subject: [PATCH] Add lv_label_set_text_fmt --- src/lv_misc/lv_printf.c | 10 --------- src/lv_objx/lv_label.c | 46 +++++++++++++++++++++++++++++++++++++++++ src/lv_objx/lv_label.h | 8 +++++++ 3 files changed, 54 insertions(+), 10 deletions(-) diff --git a/src/lv_misc/lv_printf.c b/src/lv_misc/lv_printf.c index 8c94449c3..11a39a8ad 100644 --- a/src/lv_misc/lv_printf.c +++ b/src/lv_misc/lv_printf.c @@ -139,16 +139,6 @@ static inline void _out_null(char character, void* buffer, size_t idx, size_t ma } -// internal _putchar wrapper -static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen) -{ - (void)buffer; (void)idx; (void)maxlen; - if (character) { - _putchar(character); - } -} - - // internal output function wrapper static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) { diff --git a/src/lv_objx/lv_label.c b/src/lv_objx/lv_label.c index 9dfa92260..2f95abdc3 100644 --- a/src/lv_objx/lv_label.c +++ b/src/lv_objx/lv_label.c @@ -13,6 +13,7 @@ #include "../lv_core/lv_group.h" #include "../lv_misc/lv_color.h" #include "../lv_misc/lv_math.h" +#include "../lv_misc/lv_printf.h" /********************* * DEFINES @@ -203,6 +204,51 @@ void lv_label_set_text(lv_obj_t * label, const char * text) lv_label_refr_text(label); } +/** + * Set a new formatted text for a label. Memory will be allocated to store the text by the label. + * @param label pointer to a label object + * @param fmt `printf`-like format + */ +void lv_label_set_text_fmt(lv_obj_t * label, const char * fmt, ...) +{ + lv_obj_invalidate(label); + + lv_label_ext_t * ext = lv_obj_get_ext_attr(label); + + /*If text is NULL then refresh */ + if(fmt == NULL) { + lv_label_refr_text(label); + return; + } + + if(ext->text != NULL && ext->static_txt == 0) { + lv_mem_free(ext->text); + ext->text = NULL; + } + + va_list ap, ap2; + va_start(ap, fmt); + va_copy(ap2, ap); + + /*Allocate space for the new text by using trick from C99 standard section 7.19.6.12 */ + uint32_t len = lv_vsnprintf(NULL, 0, fmt, ap); + + va_end(ap); + + + ext->text = lv_mem_alloc(len+1); + lv_mem_assert(ext->text); + if(ext->text == NULL) return; + ext->text[len-1] = 0; /* Ensure NULL termination */ + + lv_vsnprintf(ext->text, len, fmt, ap2); + + va_end(ap2); + ext->static_txt = 0; /*Now the text is dynamically allocated*/ + + lv_label_refr_text(label); +} + /** * Set a new text for a label from a character array. The array don't has to be '\0' terminated. * Memory will be allocated to store the array by the label. diff --git a/src/lv_objx/lv_label.h b/src/lv_objx/lv_label.h index 16b1a6e8f..2804ef700 100644 --- a/src/lv_objx/lv_label.h +++ b/src/lv_objx/lv_label.h @@ -21,6 +21,7 @@ extern "C" { #if LV_USE_LABEL != 0 +#include #include "../lv_core/lv_obj.h" #include "../lv_font/lv_font.h" #include "../lv_font/lv_symbol_def.h" @@ -124,6 +125,13 @@ lv_obj_t * lv_label_create(lv_obj_t * par, const lv_obj_t * copy); */ void lv_label_set_text(lv_obj_t * label, const char * text); +/** + * Set a new formatted text for a label. Memory will be allocated to store the text by the label. + * @param label pointer to a label object + * @param fmt `printf`-like format + */ +void lv_label_set_text_fmt(lv_obj_t * label, const char * fmt, ...); + /** * Set a new text for a label from a character array. The array don't has to be '\0' terminated. * Memory will be allocated to store the array by the label.