How to use LVGL API in pthread

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?

iMX93 evk

What LVGL version are you using?

8.3.5

What do you want to achieve?

I want to display 5 screen after every 2 mints in loop like screen -1,2,3,1,2,3… and read data from ZMQ dealer thread and display on UI.

What have you tried so far?

I have created one thread for ZMQ dealer and another(data_display_thread) for parsing data received from ZMQ and display on . but ZMQ data received thread reciving data correctly but when I call any LVGL APIs they are not working in “data_display_thread”

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

/*You code here*/
In event.c
static void main_event_handler (lv_event_t *e)
{
	lv_event_code_t code = lv_event_get_code(e);

	switch (code) {
	case LV_EVENT_SCREEN_LOADED:
	{
switch_scr=lv_timer_create(switch_screen, 3000, &guider_ui);
	main_init();
		break;
	}
	case LV_EVENT_SCREEN_UNLOAD_START:
	{
	lv_timer_del(switch_scr);
		break;
	}
	default:
		break;
	}
}
///////////////////////////////////////////////////////////////////////////////
In Custom.c
void main_init(void)
{
     printf("main init \n");
     bool status ;
     status = pthread_create(&screen_display, NULL,switch_screen_thread, NULL);
     status = ZMQ_Rec();
     printf("return from ZMQ recive\n");
}


static void *switch_screen_thread(void *arg)
{
    printf("In screen display\n");
    phase_A.Phase_Voltage=10;phase_B.Phase_Voltage=11;phase_C.Phase_Voltage=12;
    while (1)
    {
        phase_A.Phase_Voltage++;phase_B.Phase_Voltage++;phase_C.Phase_Voltage++;
        /* Set the poll item */
        pthread_mutex_lock(&mutex);  // Acquire the mutex
        printf("In Mutex \n");
        usleep(10 * 1000); 
        lv_label_set_text_fmt(guider_ui.Home_PHA_volt, "%d",phase_A.Phase_Voltage);
        usleep(10 * 1000); printf("after volt A\n");
        lv_label_set_text_fmt(guider_ui.Home_PHB_volt, "%d",phase_B.Phase_Voltage); 
        usleep(10 * 1000); printf("after volt B \n");
        lv_label_set_text_fmt(guider_ui.Home_PHC_volt, "%d",phase_C.Phase_Voltage);
        lv_task_handler();
        usleep(10 * 1000); printf("after volt C \n");// Sleep for 10ms
    	//  task_meter1 =lv_timer_create(current_meter_timer_cb, 5000, &guider_ui);
        pthread_mutex_unlock(&mutex);  // Release the mutex
       
    //  printf("after Mutex \n");
    //  lv_timer_del(task_meter1 );
     }
    return 0;
}
int ZMQ_Rec(void)
{
  if( SUCCESS == Display_ZMQInit()){
		pthread_join(tid, NULL);
	}
pthread_join(screen_display, NULL);	
}

In above code all print message are coming but display on screen or change in screen not happing. please help it urgent 
## Screenshot and/or video
If possible, add screenshots and/or videos about the current state.

Hi,

Do you use pthread_mutex_lock(&mutex); around lv_timer_handler too?

custom.c

pthread_mutex_t mutex;

******************************************************************
void custom_init(lv_ui *ui)
{
   main_init(); /* Add your codes here */
}
void main_init(void)
{
bool status ;
status = pthread_create(&screen_display, NULL,switch_screen_thread, NULL);
status = pthread_create(&tid, NULL, ZMQ_receiver,NULL);

}
*****************************************************************

static void *switch_screen_thread()//*arg)
{
 int temp_var=0,temp_var1=0;
 bool temp_flag=1;
 Display.Meter_value=20; // display for meter
 Display.Led=1;             // led indication
 temp_var1 = Display.Meter_value;
 switch_scr1=lv_timer_create(switch_screen_timer_cb, 15000, NULL);//&guider_ui);
   while (1)
    {
      if (counter_scr1==1)
	  {
		if(counter_scr==1)
			{
			pthread_mutex_lock(&mutex);  
			setup_scr_Home(&guider_ui);
			lv_scr_load_anim(guider_ui.Home, LV_SCR_LOAD_ANIM_NONE, 200, 200, true);
			pthread_mutex_unlock(&mutex);  
            } 

		if(counter_scr==2)
			{
			pthread_mutex_lock(&mutex);  
			setup_scr_main(&guider_ui);
			lv_scr_load_anim(guider_ui.main, LV_SCR_LOAD_ANIM_NONE, 200, 200, true);
			pthread_mutex_unlock(&mutex);  
			} 
		if(counter_scr==3)
		   {
		    pthread_mutex_lock(&mutex);  
			setup_scr_Diagnostic(&guider_ui);
			lv_scr_load_anim(guider_ui.Diagnostic, LV_SCR_LOAD_ANIM_NONE, 200, 200, true);
			pthread_mutex_unlock(&mutex);  
			} 
        counter_scr1=0;
      }
 
  lv_meter_set_indicator_value(guider_ui.main_CurrentMeter, guider_ui.main_CurrentMeter_scale_1_ndimg_0,Display.Meter_value);//message.mesg_text[1]);
   }
    return 0;
}
*************************************************************************************
void switch_screen_timer_cb(lv_timer_t *timer)
{
 counter_scr++;counter_scr1=1;
   if (counter_scr > 3)
	{ counter_scr=1;
    }
}

Sorry for very late reply. Yes, above is my code. in which I want to switch between 3 screens. for switching i used timer for 1 minute and in timer handler i just increment the counter. In thread I check for counter and according to value of counter i load the screen . i used mutex also but once the screen changes I got segmentation fault. plz guide where i am wrong

I can’t see any obvious issues with your code. Can you use a debugger to get more info about the segfault?

Please also enable all LV_USE_ASSERT_...s in lv_conf.h and also configure the logging as described here: Logging — LVGL documentation

ok thanks. let me check

Hi Kisvegabor, do you have any example similar to my requirement?

Hi , I tried to debug for seg fault. i am getting seg fault in generated file. please have a look in log

if you get any clue. please guide