LVGL v8 Baselabel
lvsf_baselabel is a custom label widget that SiFli wraps around LVGL v8’s native lv_label. On top of an ordinary text label it builds in a data-driven refresh path: after binding a data-source ID, registering a data callback, and creating a refresh timer, the widget periodically pulls data and updates its own text, so the application layer does not need to poll on its own. It also keeps the regular text capabilities of lv_label.
Main header: middleware/lvgl/lvsf/gui_widgets/lvsf_baselabel.h
Data refresh path interfaces: middleware/lvgl/lvsf/gui_widgets/lvsf_obj_ext.h
Dependent widget: LVGL label (
LV_USE_LABEL)Example project: example/multimedia/lvgl/lvgl_v8_baselabel
Features
Inherits from
lv_label_tand supports all regular label capabilities, such asset_text,set_text_fmt, long-text modes, recolor, and text selection and editing.Built-in data refresh path: bind a data-source ID + register a data callback + create a refresh timer to periodically pull data and automatically refresh the text.
Supports custom text (dynamically allocated and copied) and static text (only the pointer is stored, suitable for constant strings).
Supports a static string table (
sfat_str): add, clear, and switch the displayed string by index, suitable for enumerated strings such as weekday/month.Supports text truncation with ellipsis (
...is appended automatically based on the display width).Supports formatted text setting (
lv_baselabel_set_text_fmt).
Use Cases
Real-time numeric readouts, such as runtime duration, heart rate, battery level, and sensor readings that change over time.
Numbers that auto-refresh with a data source on a status bar or watch dial.
Labels that must switch among several predefined strings (weekday, month, status enumerations).
Long text truncated to a fixed width with an ellipsis.
Supported Boards
sf32lb52-lcd_n16r8
sf32lb52-lchspi-ulp
Configuration and Initialization
lvsf_baselabel depends on the LVGL label component. In menuconfig, confirm it is enabled:
CONFIG_LV_USE_LABEL=y
In the BSP, the LVSF_USE_BASELABEL macro controls whether this widget is compiled.
Minimum flow to create an object:
#include "lvsf_baselabel.h"
lv_obj_t *label = lv_baselabel_create(lv_scr_act());
lv_baselabel_set_text(label, "hello");
API Reference
Callback types
Type definition |
Description |
Parameters / Returns |
|---|---|---|
|
Base label refresh callback function type |
|
Creation and refresh
Function |
Description |
Parameters / Returns |
|---|---|---|
|
Creates a base label object |
|
|
Refresh timer callback |
|
Text settings
Function |
Description |
Parameters / Returns |
|---|---|---|
|
Sets custom text |
|
|
Sets the text with a format string |
|
|
Sets the displayed text |
Directly calls the underlying |
|
Sets static text (does not copy content) |
Only stores the pointer, suitable for constant strings; the text lifetime must outlive the label |
|
Sets the long-text display mode |
|
|
Enables/disables recolor |
|
|
Sets text with ellipsis |
|
|
Gets the current text |
Returns a pointer to the text content |
Text selection and character positioning
Function |
Description |
Parameters / Returns |
|---|---|---|
|
Sets the text-selection start position |
|
|
Sets the text-selection end position |
|
|
Gets the text-selection start position |
Returns the start character index |
|
Gets the text-selection end position |
Returns the end character index |
|
Gets the long-text display mode |
Returns the current long-text mode |
|
Gets the recolor state |
Returns |
|
Gets the coordinates of a specific character |
|
|
Gets the character index at the given coordinates |
|
|
Checks whether there is a character at the given coordinates |
Returns |
|
Inserts text at the given position |
|
|
Cuts (deletes) text |
|
Static string table (sfat_str)
Function |
Description |
Parameters / Returns |
|---|---|---|
|
Adds a static string |
|
|
Clears all static strings |
Releases the memory of all added strings and resets the counter |
|
Selects and displays a static string by index |
|
|
Gets the number of static strings |
Returns the count; 0 means none have been added |
|
Gets the index of the currently displayed static string |
Range 0 to |
Typical Usage
Data-driven refresh (recommended)
When the refresh timer expires it calls the data callback; inside the callback, format the current data into the label:
#include "lvsf_baselabel.h"
#include "lvsf_obj_ext.h" /* lv_obj_set_gmdata_cb / set_source_id / create_refresh_timer */
/* Data callback: called when the refresh timer expires */
static int32_t uptime_gmdata_cb(lv_obj_t *label, uint32_t *id_tab, uint8_t id_num)
{
(void)id_tab;
(void)id_num;
static uint32_t secs = 0;
secs++;
char buf[32];
lv_snprintf(buf, sizeof(buf), "uptime %02u:%02u",
(unsigned)((secs / 60) % 100), (unsigned)(secs % 60));
lv_baselabel_set_text(label, buf); /* Build the string yourself + set_text */
return 0;
}
void demo_baselabel_init(void)
{
lv_obj_t *label = lv_baselabel_create(lv_scr_act());
lv_obj_set_style_text_font(label, &lv_font_montserrat_36, 0);
lv_obj_set_style_text_color(label, lv_palette_main(LV_PALETTE_BLUE), 0);
lv_baselabel_set_text(label, "uptime 00:00"); /* Initial text before the first refresh */
lv_obj_center(label);
/* Bind the data source + callback + 1-second refresh timer */
static uint32_t source_id = 0x1105;
lv_obj_set_source_id(label, &source_id, 1);
lv_obj_set_gmdata_cb(label, uptime_gmdata_cb);
lv_obj_create_refresh_timer(label, 1000, lv_baselabel_refresh_timer);
lv_obj_refresh_start(label);
}
Note
To update the text inside the callback, build the string yourself with lv_snprintf() and then call lv_baselabel_set_text(). lv_baselabel_set_text_fmt() is intended for data-binding format strings, not ordinary printf; passing variable arguments directly to it produces incorrect results.
Switching the static string table
lv_baselabel_add_sfat_str(label, "Mon", 3);
lv_baselabel_add_sfat_str(label, "Tue", 3);
lv_baselabel_add_sfat_str(label, "Wed", 3);
lv_baselabel_select_sfat_str(label, 1); /* Show "Tue" */
Demo
Run the lvgl_v8_baselabel example to see the result: a large blue uptime 00:00 is shown in the center of the screen. The refresh timer then triggers the data callback once per second, and the label increments to 00:01, 00:02…… The text change is driven entirely by the baselabel data refresh path.
See Also
Source path:
middleware/lvgl/lvsf/gui_widgets/lvsf_baselabel.hExample path:
example/multimedia/lvgl/lvgl_v8_baselabel