Switch (lv_switch)

Overview

The Switch can be used to turn on/off something. It looks like a little slider.

Parts and Styles

The Switch uses the the following parts:

  • LV_SWITCH_PART_BG: main part

  • LV_SWITCH_PART_INDIC: the indicator (virtual part)

  • LV_SWITCH_PART_KNOB: the knob (virtual part)

The parts and style works the same as in case of Slider. Read its documentation for a details description.

##Usage

Change state

The state of the Switch can be changed by clicking on it or by lv_switch_on(switch, LV_ANIM_ON/OFF), lv_switch_off(switch, LV_ANIM_ON/OFF) or lv_switch_toggle(switch, LV_ANOM_ON/OFF) functions

Animation time

The time of animations, when the switch changes state, can be adjusted with lv_switch_set_anim_time(switch, anim_time).

Events

Besides the Generic events the following Special events are sent by the Switch:

  • LV_EVENT_VALUE_CHANGED Sent when the switch changes state.

Keys

  • LV_KEY_UP, LV_KEY_RIGHT Turn on the slider

  • LV_KEY_DOWN, LV_KEY_LEFT Turn off the slider

Learn more about Keys.

Example

C

Simple Switch

Switch example in LittlevGL

code

#include "../../../lv_examples.h"
#include <stdio.h>
#if LV_USE_SWITCH

static void event_handler(lv_obj_t * obj, lv_event_t event)
{
    if(event == LV_EVENT_VALUE_CHANGED) {
        printf("State: %s\n", lv_switch_get_state(obj) ? "On" : "Off");
    }
}

void lv_ex_switch_1(void)
{
    /*Create a switch and apply the styles*/
    lv_obj_t *sw1 = lv_switch_create(lv_scr_act(), NULL);
    lv_obj_align(sw1, NULL, LV_ALIGN_CENTER, 0, -50);
    lv_obj_set_event_cb(sw1, event_handler);

    /*Copy the first switch and turn it ON*/
    lv_obj_t *sw2 = lv_switch_create(lv_scr_act(), sw1);
    lv_switch_on(sw2, LV_ANIM_ON);
    lv_obj_align(sw2, NULL, LV_ALIGN_CENTER, 0, 50);
}

#endif

MicroPython

No examples yet.

API

Typedefs

typedef uint8_t lv_switch_part_t

Enums

enum [anonymous]

Switch parts.

Values:

enumerator LV_SWITCH_PART_BG = LV_BAR_PART_BG

Switch background.

enumerator LV_SWITCH_PART_INDIC = LV_BAR_PART_INDIC

Switch fill area.

enumerator LV_SWITCH_PART_KNOB = _LV_BAR_PART_VIRTUAL_LAST

Switch knob.

enumerator _LV_SWITCH_PART_VIRTUAL_LAST

Functions

lv_obj_t *lv_switch_create(lv_obj_t *par, const lv_obj_t *copy)

Create a switch objects

Return

pointer to the created switch

Parameters
  • par: pointer to an object, it will be the parent of the new switch

  • copy: pointer to a switch object, if not NULL then the new object will be copied from it

void lv_switch_on(lv_obj_t *sw, lv_anim_enable_t anim)

Turn ON the switch

Parameters
  • sw: pointer to a switch object

  • anim: LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately

void lv_switch_off(lv_obj_t *sw, lv_anim_enable_t anim)

Turn OFF the switch

Parameters
  • sw: pointer to a switch object

  • anim: LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately

bool lv_switch_toggle(lv_obj_t *sw, lv_anim_enable_t anim)

Toggle the position of the switch

Return

resulting state of the switch.

Parameters
  • sw: pointer to a switch object

  • anim: LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately

void lv_switch_set_anim_time(lv_obj_t *sw, uint16_t anim_time)

Set the animation time of the switch

Return

style pointer to a style

Parameters
  • sw: pointer to a switch object

  • anim_time: animation time

bool lv_switch_get_state(const lv_obj_t *sw)

Get the state of a switch

Return

false: OFF; true: ON

Parameters
  • sw: pointer to a switch object

uint16_t lv_switch_get_anim_time(const lv_obj_t *sw)

Get the animation time of the switch

Return

style pointer to a style

Parameters
  • sw: pointer to a switch object

struct lv_switch_ext_t

Public Members

lv_bar_ext_t bar
lv_style_list_t style_knob
uint8_t state