Thank’s @embeddedt, but it still didn’t work. The switch widget “sw” is not changing state when the screen is in steady state, only when the screen is loaded changes the switch state.
Button widget btn1 is working ok.
I’m using lvgl version 8.0.0.
CURRENT VERSION OF LVGL
#define LVGL_VERSION_MAJOR 8
#define LVGL_VERSION_MINOR 0
#define LVGL_VERSION_PATCH 0
#define LVGL_VERSION_INFO “dev”
#define semaphore_block_time 20 // set max block time to get "lvgl_mutex", in ms.
static SemaphoreHandle_t lvgl_mutex = NULL;
static lv_obj_t* scr1;
static lv_obj_t* scr2;
static lv_obj_t* prev_scr;
static bool a = 0;
static lv_style_t style_screen1;
static lv_style_t style_screen2;
static lv_obj_t* sw;
static lv_obj_t* btn1;
static uint32_t counter = 0;
void task( void* pvParameters )
{
TickType_t* time = (TickType_t*) pvParameters;
static TickType_t xDelay;
xDelay = pdMS_TO_TICKS( *time );
while(1)
{
if( xSemaphoreTake( lvgl_mutex, ( TickType_t ) semaphore_block_time ) == pdTRUE )
{
prev_scr = lv_scr_act(); /* save previously active screen */
scr1 = lv_obj_create(NULL);
lv_style_init(&style_screen1);
lv_style_set_bg_color(&style_screen1, lv_color_white());
lv_obj_add_style(scr1, &style_screen1, LV_PART_MAIN ); //turn the screen white
lv_obj_t* label_scr1 = lv_label_create(scr1);
lv_obj_set_style_text_align(label_scr1, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(label_scr1, LV_ALIGN_CENTER, 0, -100);
lv_label_set_text_fmt(label_scr1, "SCR1: %p", scr1);
lv_obj_t* label_scr1a = lv_label_create(scr1);
lv_obj_set_style_text_align(label_scr1a, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(label_scr1a, LV_ALIGN_CENTER, 0, -80);
lv_label_set_text_fmt(label_scr1a, "&SCR1: %p", &scr1);
sw = lv_switch_create(scr1);
lv_obj_set_size(sw, 200, 100);
lv_obj_align(sw, LV_ALIGN_CENTER, 0, 0);
lv_scr_load(scr1);
if( prev_scr != NULL )
lv_obj_del(prev_scr); /* prev is now not the active screen, so it can safely be deleted */
a = !a;
if( a )
lv_obj_add_state(sw, LV_STATE_CHECKED);
else
lv_obj_clear_state(sw, LV_STATE_CHECKED);
xSemaphoreGive( lvgl_mutex );
}
else
{
// printf("Bug in xxxx, dont take the mutex after: %u ms\n", semaphore_block_time);
}
vTaskDelay( xDelay );
if( xSemaphoreTake( lvgl_mutex, ( TickType_t ) semaphore_block_time ) == pdTRUE )
{
if( !a )
lv_obj_add_state(sw, LV_STATE_CHECKED);
else
lv_obj_clear_state(sw, LV_STATE_CHECKED);
xSemaphoreGive( lvgl_mutex );
}
else
{
// printf("Bug in xxxx, dont take the mutex after: %u ms\n", semaphore_block_time);
}
vTaskDelay( xDelay );
if( xSemaphoreTake( lvgl_mutex, ( TickType_t ) semaphore_block_time ) == pdTRUE )
{
prev_scr = lv_scr_act(); // save previously active screen
scr2 = lv_obj_create(NULL);
lv_style_init(&style_screen2);
lv_style_set_bg_color(&style_screen2, lv_color_white());
lv_obj_add_style(scr2, &style_screen2, LV_PART_MAIN ); //turn the screen black
lv_obj_t* label_scr2 = lv_label_create(scr2);
lv_obj_set_style_text_align(label_scr2, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(label_scr2, LV_ALIGN_CENTER, 0, -100);
lv_label_set_text_fmt(label_scr2, "SCR2: %p", scr2);
lv_obj_t* label_scr2a = lv_label_create(scr2);
lv_obj_set_style_text_align(label_scr2a, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(label_scr2a, LV_ALIGN_CENTER, 0, -80);
lv_label_set_text_fmt(label_scr2a, "&SCR2: %p", &scr2);
counter++;
lv_obj_t* label_counter = lv_label_create(scr2);
lv_obj_set_style_text_align(label_counter, LV_TEXT_ALIGN_CENTER, 0);
lv_obj_align(label_counter, LV_ALIGN_CENTER, 0, 100);
lv_label_set_text_fmt(label_counter, "Counter: %u", counter);
btn1 = lv_btn_create(scr2);
lv_obj_set_size(btn1, 200, 100);
lv_obj_align(btn1, LV_ALIGN_CENTER, 0, 0);
lv_obj_t* label = lv_label_create(btn1);
lv_label_set_text(label, "Button");
lv_obj_center(label);
lv_scr_load(scr2);
if( prev_scr != NULL )
lv_obj_del(prev_scr); // prev is now not the active screen, so it can safely be deleted
if( a )
lv_obj_add_state(btn1, LV_STATE_PRESSED);
else
lv_obj_clear_state(btn1, LV_STATE_PRESSED);
xSemaphoreGive( lvgl_mutex );
}
else
{
// printf("Bug in xxxx, dont take the mutex after: %u ms\n", semaphore_block_time);
}
vTaskDelay( xDelay );
if( xSemaphoreTake( lvgl_mutex, ( TickType_t ) semaphore_block_time ) == pdTRUE )
{
if( !a )
lv_obj_add_state(btn1, LV_STATE_PRESSED);
else
lv_obj_clear_state(btn1, LV_STATE_PRESSED);
xSemaphoreGive( lvgl_mutex );
}
else
{
// printf("Bug in xxxx, dont take the mutex after: %u ms\n", semaphore_block_time);
}
vTaskDelay( xDelay );
}
}
void lvgl_task_handler( void* param )
{
(void) param;
const TickType_t WakeTime = pdMS_TO_TICKS( 5 ); // The macro pdMS_TO_TICKS() can be used to convert milliseconds into ticks.
while(1) // execulta a cada 5 ms.
{
my_led_toggle_3();
if( xSemaphoreTake( lvgl_mutex, ( TickType_t ) semaphore_block_time ) == pdTRUE )
{
lv_task_handler();
xSemaphoreGive( lvgl_mutex );
}
else
{
// printf("Bug in xxxx, dont take the mutex after: %u ms\n", semaphore_block_time);
}
// Wait for the next cycle.
vTaskDelay( WakeTime );
}
}
int main( void )
{
SYS_Initialize( NULL ); // Initialize all modules
my_modules_Initialize();
////////////////////////////////////////////////////////////////////////////////
lvgl_mutex = xSemaphoreCreateMutex();
if( lvgl_mutex == NULL )
{
//printf("ERROR: lvgl_mutex NO MEMORY");
my_led_set_1();
return 0;
}
TickType_t tempo = 5000;
xTaskCreate( task,
"task",
4096, //512, // words(4 bytes). 512 words * 4 = 2048 bytes.
&tempo,
1,
(TaskHandle_t*) NULL
);
xTaskCreate( lvgl_task_handler,
"littleVgl",
4096, //512,
(void*) NULL,
2,
(TaskHandle_t*) NULL
);
vTaskStartScheduler();
}