I have just tried to use the lvgl library to paint a screen on an LCD touch screen driven by the latest version of ESP32 boards manager.All the widgets I need worked AOK. Except the checkbox and the switch widgets. The checkbox would never show the ‘tick’ in the box no matter what I tried so I abandoned it and tried the switch. I could get the switch to show the slider button either to the left or right when creating the switch in setup.
static lv_style_t style_switch_checked;
static lv_style_t style_switch_default;
lv_style_init(&style_switch_checked);
lv_style_set_bg_color(&style_switch_checked, lv_color_make(0x00, 0xFF, 0x00)); // Green for checked
lv_style_init(&style_switch_default);
lv_style_set_bg_color(&style_switch_default, lv_color_make(0x80, 0x80, 0x80)); // Grey for default
sw_Clockwise = lv_switch_create(ST_panel);
lv_obj_add_event_cb(sw_Clockwise, sw_event_cb, LV_EVENT_PRESSED, NULL);
//lv_obj_add_state(sw_Clockwise, LV_STATE_CHECKED);//On
lv_obj_add_style(sw_Clockwise, &style_switch_default, (int)LV_PART_MAIN | (int)LV_STATE_DEFAULT);
lv_obj_add_style(sw_Clockwise, &style_switch_checked, (int)LV_PART_MAIN | (int)LV_STATE_CHECKED);
lv_obj_set_pos(sw_Clockwise, 190, 200);
lv_obj_t * lblsw_clck = lv_label_create(ST_panel);
lv_label_set_text(lblsw_clck, "Clockwise");
lv_obj_align_to(lblsw_clck, sw_Clockwise, LV_ALIGN_LEFT_MID , -100, 0);
But the button doesn’t toggle when I tap on the switch although the background does change colour.
This is my callback function:
static void sw_event_cb(lv_event_t * e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * sw = lv_event_get_target_obj(e);
if(sw == sw_Clockwise)
{
if (lv_obj_has_state(sw, LV_STATE_CHECKED))
{
Serial.println("Was checked");
//lv_obj_remove_state(sw, LV_STATE_CHECKED);
}
else
{
Serial.println("Was not checked");
//lv_obj_add_state(sw, LV_STATE_CHECKED);
}
ST_dirn = !ST_dirn;
Serial.printf("Clockwise = %i\r\n",ST_dirn);
}
}
If I un-comment the two state change commands the button will move to the right but never back again.
I have tried every combination I can think of to no avail. Has anyone used this library and successfully toggled the switch?
Hopefully someone will see the obvious stupid error I’ve searched for in vain.
Dicky.





