1 /**
2 * @file lv_sw.h
3 *
4 */
5
6 #ifndef LV_SWITCH_H
7 #define LV_SWITCH_H
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 /*********************
14 * INCLUDES
15 *********************/
16 #include "../lv_conf_internal.h"
17
18 #if LV_USE_SWITCH != 0
19
20 /*Testing of dependencies*/
21 #if LV_USE_SLIDER == 0
22 #error "lv_sw: lv_slider is required. Enable it in lv_conf.h (LV_USE_SLIDER 1)"
23 #endif
24
25 #include "../lv_core/lv_obj.h"
26 #include "lv_bar.h"
27
28 /*********************
29 * DEFINES
30 *********************/
31
32 /**********************
33 * TYPEDEFS
34 **********************/
35 /*Data of switch*/
36 typedef struct {
37 lv_bar_ext_t bar; /*Ext. of ancestor*/
38 /*New data for this type */
39 lv_style_list_t style_knob; /*Style of the knob*/
40 } lv_switch_ext_t;
41
42 /**
43 * Switch parts.
44 */
45 enum {
46 LV_SWITCH_PART_BG = LV_BAR_PART_BG, /**< Switch background. */
47 LV_SWITCH_PART_INDIC = LV_BAR_PART_INDIC, /**< Switch fill area. */
48 LV_SWITCH_PART_KNOB = _LV_BAR_PART_VIRTUAL_LAST, /**< Switch knob. */
49 _LV_SWITCH_PART_VIRTUAL_LAST
50 };
51
52 typedef uint8_t lv_switch_part_t;
53
54 /**********************
55 * GLOBAL PROTOTYPES
56 **********************/
57
58 /**
59 * Create a switch objects
60 * @param par pointer to an object, it will be the parent of the new switch
61 * @param copy pointer to a switch object, if not NULL then the new object will be copied from it
62 * @return pointer to the created switch
63 */
64 lv_obj_t * lv_switch_create(lv_obj_t * par, const lv_obj_t * copy);
65
66 /*=====================
67 * Setter functions
68 *====================*/
69
70 /**
71 * Turn ON the switch
72 * @param sw pointer to a switch object
73 * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
74 */
75 void lv_switch_on(lv_obj_t * sw, lv_anim_enable_t anim);
76
77 /**
78 * Turn OFF the switch
79 * @param sw pointer to a switch object
80 * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
81 */
82 void lv_switch_off(lv_obj_t * sw, lv_anim_enable_t anim);
83
84 /**
85 * Toggle the position of the switch
86 * @param sw pointer to a switch object
87 * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
88 * @return resulting state of the switch.
89 */
90 bool lv_switch_toggle(lv_obj_t * sw, lv_anim_enable_t anim);
91
92 /**
93 * Set the animation time of the switch
94 * @param sw pointer to a switch object
95 * @param anim_time animation time
96 * @return style pointer to a style
97 */
lv_switch_set_anim_time(lv_obj_t * sw,uint16_t anim_time)98 static inline void lv_switch_set_anim_time(lv_obj_t * sw, uint16_t anim_time)
99 {
100 lv_bar_set_anim_time(sw, anim_time);
101 }
102
103 /*=====================
104 * Getter functions
105 *====================*/
106
107 /**
108 * Get the state of a switch
109 * @param sw pointer to a switch object
110 * @return false: OFF; true: ON
111 */
lv_switch_get_state(const lv_obj_t * sw)112 static inline bool lv_switch_get_state(const lv_obj_t * sw)
113 {
114 return lv_bar_get_value(sw) == 1 ? true : false;
115 }
116
117 /**
118 * Get the animation time of the switch
119 * @param sw pointer to a switch object
120 * @return style pointer to a style
121 */
lv_switch_get_anim_time(const lv_obj_t * sw)122 static inline uint16_t lv_switch_get_anim_time(const lv_obj_t * sw)
123 {
124 return lv_bar_get_anim_time(sw);
125 }
126
127 /**********************
128 * MACROS
129 **********************/
130
131 #endif /*LV_USE_SWITCH*/
132
133 #ifdef __cplusplus
134 } /* extern "C" */
135 #endif
136
137 #endif /*LV_SWITCH_H*/
138