CPU overload when showing sensor data on screen

Hi,

I am running a code on ESP IDE for WT32-SC01 Plus Touch LCD which has a ESP32 S3 MCU.
I am displaying 60 float numbers on a page that is under menu section of my UI. The way I hav implemented it works but it seems super inefficient and I can see my LCD is struggling to update the numbers (CPU usage goes to 98%). How ever, I can not find any other way to do it. Can anyone suggest a better way?

Here is the simplified code:

void Rate_0(void *pvParameter)
{
	           TickType_t xLastWakeTime0;
		    const TickType_t xFrequency = 1 / portTICK_PERIOD_MS; // 1000 Hz
		    xLastWakeTime0 = xTaskGetTickCount();

		    char str[25];

	 while (1) {

	       lv_timer_handler();

	                                      sprintf(str, "Status: %.2f", *(IncomingData485 +0)); 
	       		                      lv_label_set_text(ui_Status, str);
	       		                      sprintf(str, "ArmPos1: %.2f", *(IncomingData485 +1)); 
	       		                      lv_label_set_text(ui_ArmPos1, str);
	       		                      sprintf(str, "ArmPos2: %.2f", *(IncomingData485 +2)); 
	       		                      lv_label_set_text(ui_ArmPos2, str);
.
.
.
	       		                      sprintf(str, "ArmPos58: %.2f", *(IncomingData485 +58)); 
	       		                      lv_label_set_text(ui_ArmPos58, str);
	       		                      sprintf(str, "ArmPos59: %.2f", *(IncomingData485 +59)); 
	       		                      lv_label_set_text(ui_ArmPos59, str);

	    	vTaskDelayUntil(&xLastWakeTime0, xFrequency);
	    }
}

extern "C" void app_main(void)
{
	  /* Initialize model */
   init_lvgl_lgfx();
    ui_init();


	  xTaskCreate(Rate_0, "Rate_0", 10240 , NULL, configMAX_PRIORITIES-1,NULL);

               }

As you can, these numbers are getting updated 1000 times per second in my loop. However, I do not need them to be updated all the time. I only want them updated when I go to that specific page in my menu UI. when I actually try to see them.

How can I make them run only when that page is showing?
Is it even an efficient way to put the number in a string then use “lv_label_set_text” to update it on screen? is there any other way?

PS. The code freaks out if I take the “lv_label_set_text” functions out of “lv_timer_handler” task and put it in another task with a different rate or even if I put them all in an “if” condition that makes it run less frequent. Not sure why that happens. By freaking out I mean it interrupts other features in my code and they wont work properly.

It is recommended to use timer to control the text refresh cycle instead of placing it directly in the lvgl main loop. Reference document:Timers — LVGL documentation
You can refer to the code below:

static void sensor_update_timer(lv_timer_t* timer)
{
	lv_label_set_text_fmt(ui_Status, "Status: %.2f", *(IncomingData485 + 0));
	...
	lv_label_set_text_fmt(ui_ArmPos59, "ArmPos59: %.2f", *(IncomingData485 + 59));
}

void Rate_0(void *pvParameter)
{
    TickType_t xLastWakeTime0;
    const TickType_t xFrequency = 1 / portTICK_PERIOD_MS; // 1000 Hz
    xLastWakeTime0 = xTaskGetTickCount();

    lv_timer_create(sensor_update_timer, 100, NULL);

    while (1) {
        lv_timer_handler();
        vTaskDelayUntil(&xLastWakeTime0, xFrequency);
    }
}

In addition, LVGL’s API cannot be used across threads, which may lead to thread safety issues. Reference document: Operating system and interrupts — LVGL documentation.

1 Like

I think your TickType_t is a int or an int32_t or something. But if you divide 1 / portTICK_PERIOD_MS the result is probably 0. So no delay and high CPU load.

Just do xFrequency = 1

Also if you want to use multi core, you have to put a mutex in front of lv_timer_handler() and lock it when you update the labels.

1 Like

Thank you soo much for your help.
I have been asking this question 3 different ways within the last 3 months. Finally you answered it and saved me a lot of time. My code runs perfectly after your suggestion.
I also did not know about the " lv_label_set_text_fmt " command. much more efficient.