Toggle button programmatically change state

Description

toggle button, programmatically change it’s state from LV_STATE_CHECKED to LV_STATE_DEFAULT

What MCU/Processor/Board and compiler are you using? ESP32, CYD

What LVGL version are you using? 9.1

What do you want to achieve? The toggle button is labeled to “start” a count down. After toggled the toggle label changes to “stop”… If the countdown finishes successfully I want to programmatically uncheck the button and reset the label to “start”… I could reset the label manually, but I need/want to reset the toggle, which will also reset the toggle label for me.

What have you tried so far?

is this the correct way to check and uncheck a toggle button? I don’t have the hardware with me at the moment and am looking for confirmation. These commands don’t seem really well documented, or at least I couldn’t find them associated much with the button.

lv_obj_add_state(tgl_btn, LV_STATE_CHECKED);
lv_obj_clear_state(tgl_btn, LV_STATE_DEFAULT);

If this is correct I am just looking for confirmation from someone with more experience than I have (just beginning with lvgl)

if you want to clear the state,you need use lv_obj_clear_state(tgl_btn, LV_STATE_CHECKED);

I don’t quite understand your needs, I guess you want to implement a countdown function?

lv_timer_t* label_count_timer;
lv_obj_t* btn;
lv_obj_t* label;
uint8_t cnt = 5;

static void label_count_evnet_cb(lv_timer_t* timer)
{
lv_obj_t* label = timer->user_data;

cnt--;
lv_label_set_text_fmt(label, "%d", cnt);
if (cnt == 0)
{
    lv_obj_clear_state(btn, LV_STATE_CHECKED);
    lv_timer_del(label_count_timer);
    label_count_timer = NULL;      
}

}

static void btn_click_event_handler(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t* label = (lv_obj_t*)e->user_data;

if (code == LV_EVENT_VALUE_CHANGED) {
    if (label_count_timer == NULL)
    {
        cnt = 5;
        lv_label_set_text_fmt(label, "%d", cnt);
        label_count_timer = lv_timer_create(label_count_evnet_cb, 1000, label);
    }        
}

}

int app_obj_2(lv_obj_t *parent)
{
btn = lv_btn_create(parent);

lv_obj_center(btn);
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
lv_obj_set_size(btn, 200, 60);

label = lv_label_create(btn);
lv_label_set_text_fmt(label, "%d", cnt);

lv_obj_center(label);

lv_obj_add_event_cb(btn, btn_click_event_handler, LV_EVENT_ALL, label);
return 0;

}

This is an example, I hope it will be helpful to you.