How to get a switch's status?

Description

how to get a sw’s status? I can’t find any information about it in lv_sw.h

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

What do you want to achieve?

What have you tried so far?

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

/*You code here*/

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.

sorry, i find it.

I recently ran into the same problem and I used this code to get the state of a switch:

#include "lv_objx/lv_sw.h"

lv_sw_ext_t * ext = (lv_sw_ext_t *)lv_obj_get_ext_attr(obj);
bool sw_is_on = ext->state != 0;

The same workaround applies for getting the state of a led object.

#include "lv_objx/lv_led.h"

lv_led_ext_t * ext = (lv_led_ext_t *)lv_obj_get_ext_attr(obj);
bool led_is_on = ext->bright == 255;

I’m not sure if there is a better way to go about this? It would be nice if there is a lv_sw_is_on() and lv_led_is_on(), similar to the lv_cb_is_checked() function.

Could you share the solution you used?

lv_sw_get_state is in the switch documentation.

1 Like

Accessing members of the ext structure is generally not a wise/portable practice, because the values are meant for internal use by LittlevGL and may not be what you expect. (Also, we’re free to change the ext structure in minor releases, so members may be removed/renamed/added.)

When they exist, it’s better to use the proper getter functions that are documented for each object type.

1 Like

Great, thanks embeddedt. It seemed like a hack and I knew there must be a better way.
I overlooked those functions…

Now I eliminated the use of extended properties using lv_sw_get_state and lv_led_get_bright.