How to Display a simple input value to a label text value

Important: unclear posts may not receive useful answers.

Before posting

  • Get familiar with Markdown to format and structure your post
  • Be sure to update lvgl from the latest version from the master branch.
  • Be sure you have checked the FAQ and read the relevant part of the documentation.
  • If applicable use the Simulator to eliminate hardware related issues.

Delete this section if you read and applied the mentioned points.

Description

What MCU/Processor/Board and compiler are you using?

What LVGL version are you using? 8

What do you want to achieve? Create and update/refresh a label text value from an input (RTC).

What have you tried so far? Adding code to ‘ui_Screen1.c’.

Code to reproduce

Hi, I have successfully managed to add a internal RTC time value to a simple text label, the problem is this value doesn’t seem to update how can I do this? Do I create some kind of event or do I need to refresh the label text value? Thanks

Here is current code ‘ui_Screen1.c’:-

extern RTC_HandleTypeDef hrtc;
RTC_TimeTypeDef sTime = {0};
RTC_DateTypeDef sDate = {0};

void ui_Screen1_screen_init(void)
{
ui_Screen1 = lv_obj_create(NULL);
lv_obj_clear_flag( ui_Screen1, LV_OBJ_FLAG_SCROLLABLE ); /// Flags

ui_Label1 = lv_label_create(ui_Screen1);
lv_obj_set_width( ui_Label1, LV_SIZE_CONTENT); /// 1
lv_obj_set_height( ui_Label1, LV_SIZE_CONTENT); /// 1
lv_obj_set_x( ui_Label1, -94 );
lv_obj_set_y( ui_Label1, -99 );
lv_obj_set_align( ui_Label1, LV_ALIGN_CENTER );

HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
*lv_label_set_text_fmt(ui_Label1, “%02d:%02d:%02d”, sTime.Hours,sTime.Minutes,sTime.Seconds);
*}

Create lv_timer and refresh every sec.

Hi Marian, is this done in the ‘main.c’ file ‘while (1)’ loop? If so are there any instructions for this? Thanks

In setup

lv_timer_t * timer = lv_timer_create(callbackperiod1s, 1000,  NULL);

and callback

void callbackperiod1s(lv_timer_t * timer)
{
...get time struct my example use esp32 getlocaltime
  strftime(timeStringBuff, sizeof(timeStringBuff), "%H:%M:%S", &tv_now);
lv_label_set_text(ui_LabelTime,timeStringBuff);
}
1 Like

Hi, the closest I came to your advice loads the time but it stop refreshing after 1s and freezes? I Show below:-

Blockquote
int main(void)
{

/* USER CODE BEGIN 1 */


void callbackperiod1s(lv_timer_t * timer)
	  {

     HAL_StatusTypeDef res;
     RTC_TimeTypeDef time;


	      res = HAL_RTC_GetTime(&hrtc, &time, RTC_FORMAT_BIN);
	      if(res != HAL_OK) {

	          return;
	      }

	    char tmp[50];
	    snprintf(tmp, sizeof(tmp), "%02d:%02d:%02d", time.Hours, time.Minutes, time.Seconds);
	  	lv_label_set_text_fmt(ui_Label1,tmp);
	  }

/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init /
MX_DMA_Init();
/
USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals /
MX_GPIO_Init();
MX_DMA_Init();
MX_CRC_Init();
MX_SPI4_Init();
MX_RTC_Init();
/
USER CODE BEGIN 2 */
//lv_init();
//display_init(3);

/* USER CODE END 2 */

/* Infinite loop /
/
USER CODE BEGIN WHILE */
lv_init();
Display_init(3);
// ST7789_Init();
HAL_Delay (500);

ui_init();
lv_timer_t * timer = lv_timer_create(callbackperiod1s, 500, NULL);
lv_timer_set_repeat_count(timer, -1);

while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

  HAL_Delay(1);
  lv_task_handler();
  lv_tick_inc(1);
  lv_timer_handler();
  HAL_Delay(5);

}
/* USER CODE END 3 */
}

I mean your while 1 is ugly. Too i dont understand placement void inside main
… read lvgl docu for implement it. Set up a project — LVGL documentation

too remove _fmt from

lv_label_set_text_fmt(ui_Label1,tmp);

Hi Marian, Thanks for the advice I changed the ‘lv_label_set_text_fmt(ui_Label1,tmp);’ to ‘lv_label_set_text(ui_Label1,tmp);’ and it still doesn’t refresh the value on the TFT screen? Am I missing something? if anyone has any example code of a variable text value updating on the screen it would be appreciated!

your while is trouble no update func. Read docu and do what is descripted.
Debug your while is block or running