How to get the state of the switch?

What MCU/Processor/Board and compiler are you using?

ESP32 D0WDQ6-V3 (M5Stack Core2) on Arduino IDE

What LVGL version are you using?


What do you want to achieve?

So, I have a simple project that I created with SquareLine studio that calls a function when a switch’s value is changed. In that function I want to print to serial the state of the switch each time it is updated.

Sorry if this is the wrong place to ask, I am a beginner.

What have you tried so far?

Using the code from the documentation

Code to reproduce

void DACChanged(lv_event_t * e)
	Serial.println("DAC Changed! Value= " + lv_switch_get_state(ui_DACSwitch));

Screenshot and/or video



If you have a look at the documentation here:

You should see the following example:

with the following showing how to get the current state:

    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_target(e);
    if(code == LV_EVENT_VALUE_CHANGED) {
        LV_LOG_USER("State: %s\n", lv_obj_has_state(obj, LV_STATE_CHECKED) ? "On" : "Off");


1 Like

Thank you so much. Have a great day

You’re welcome.

Have a good one.

1 Like