LVGL version 9.3.0
ESP32-P4 on a JC1060P470 7" 1024X600 touch screen.
I have painted the screen with many widgets but have got stuck on what I thought would be a simple thing to do. I wanted a checkbox to switch between two options. The call back event worked except that I could not get the ‘tick’ in the box to work. Looking through the many pleas for help that Google found I realised I was not alone. So I decided to use a switch widget instead. That almost works; I can change the bar colour OK when it switches but I cannot get the round button of the switch to move. When I declare the switch I can position the button at either end but cannot get the call back event to change its position. There is no complete example showing how to do it and AI (Artificial Idiot?) was no help at all.
This is my code:
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 (code == LV_EVENT_VALUE_CHANGED)
if(sw == sw_Clockwise)
{
if (lv_obj_has_state(sw, LV_STATE_CHECKED))
ST_dirn = true;
else
ST_dirn = false;
Serial.printf("Clockwise = %i\r\n",ST_dirn);
}
}
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_VALUE_CHANGED, NULL);
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);
Can anyone see what simple thing I am missing? I can’t believe I couldn’t work out what is missing.
Dicky.