1 /**
2  * @file lv_sw.h
3  *
4  */
5 
6 #ifndef LV_SW_H
7 #define LV_SW_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #ifdef LV_CONF_INCLUDE_SIMPLE
17 #include "lv_conf.h"
18 #else
19 #include "../../lv_conf.h"
20 #endif
21 
22 #if LV_USE_SW != 0
23 
24 /*Testing of dependencies*/
25 #if LV_USE_SLIDER == 0
26 #error "lv_sw: lv_slider is required. Enable it in lv_conf.h (LV_USE_SLIDER  1)"
27 #endif
28 
29 #include "../lv_core/lv_obj.h"
30 #include "lv_slider.h"
31 
32 /*********************
33  *      DEFINES
34  *********************/
35 #define LV_SW_MAX_VALUE 100
36 
37 /**********************
38  *      TYPEDEFS
39  **********************/
40 /*Data of switch*/
41 typedef struct
42 {
43     lv_slider_ext_t slider; /*Ext. of ancestor*/
44     /*New data for this type */
45     const lv_style_t * style_knob_off; /**< Style of the knob when the switch is OFF*/
46     const lv_style_t * style_knob_on;  /**< Style of the knob when the switch is ON (NULL to use the same as OFF)*/
47     lv_coord_t start_x;
48     uint8_t changed : 1; /*Indicates the switch state explicitly changed by drag*/
49     uint8_t slided : 1;
50 #if LV_USE_ANIMATION
51     uint16_t anim_time; /*switch animation time */
52 #endif
53 } lv_sw_ext_t;
54 
55 /**
56  * Switch styles.
57  */
58 enum {
59     LV_SW_STYLE_BG, /**< Switch background. */
60     LV_SW_STYLE_INDIC, /**< Switch fill area. */
61     LV_SW_STYLE_KNOB_OFF, /**< Switch knob (when off). */
62     LV_SW_STYLE_KNOB_ON, /**< Switch knob (when on). */
63 };
64 typedef uint8_t lv_sw_style_t;
65 
66 /**********************
67  * GLOBAL PROTOTYPES
68  **********************/
69 
70 /**
71  * Create a switch objects
72  * @param par pointer to an object, it will be the parent of the new switch
73  * @param copy pointer to a switch object, if not NULL then the new object will be copied from it
74  * @return pointer to the created switch
75  */
76 lv_obj_t * lv_sw_create(lv_obj_t * par, const lv_obj_t * copy);
77 
78 /*=====================
79  * Setter functions
80  *====================*/
81 
82 /**
83  * Turn ON the switch
84  * @param sw pointer to a switch object
85  * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
86  */
87 void lv_sw_on(lv_obj_t * sw, lv_anim_enable_t anim);
88 
89 /**
90  * Turn OFF the switch
91  * @param sw pointer to a switch object
92  * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
93  */
94 void lv_sw_off(lv_obj_t * sw, lv_anim_enable_t anim);
95 
96 /**
97  * Toggle the position of the switch
98  * @param sw pointer to a switch object
99  * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
100  * @return resulting state of the switch.
101  */
102 bool lv_sw_toggle(lv_obj_t * sw, lv_anim_enable_t anim);
103 
104 /**
105  * Set a style of a switch
106  * @param sw pointer to a switch object
107  * @param type which style should be set
108  * @param style pointer to a style
109  */
110 void lv_sw_set_style(lv_obj_t * sw, lv_sw_style_t type, const lv_style_t * style);
111 
112 /**
113  * Set the animation time of the switch
114  * @param sw pointer to a  switch object
115  * @param anim_time animation time
116  * @return style pointer to a style
117  */
118 void lv_sw_set_anim_time(lv_obj_t * sw, uint16_t anim_time);
119 
120 /*=====================
121  * Getter functions
122  *====================*/
123 
124 /**
125  * Get the state of a switch
126  * @param sw pointer to a switch object
127  * @return false: OFF; true: ON
128  */
lv_sw_get_state(const lv_obj_t * sw)129 static inline bool lv_sw_get_state(const lv_obj_t * sw)
130 {
131     return lv_bar_get_value(sw) < LV_SW_MAX_VALUE / 2 ? false : true;
132 }
133 
134 /**
135  * Get a style of a switch
136  * @param sw pointer to a  switch object
137  * @param type which style should be get
138  * @return style pointer to a style
139  */
140 const lv_style_t * lv_sw_get_style(const lv_obj_t * sw, lv_sw_style_t type);
141 
142 /**
143  * Get the animation time of the switch
144  * @param sw pointer to a  switch object
145  * @return style pointer to a style
146  */
147 uint16_t lv_sw_get_anim_time(const lv_obj_t * sw);
148 
149 /**********************
150  *      MACROS
151  **********************/
152 
153 #endif /*LV_USE_SW*/
154 
155 #ifdef __cplusplus
156 } /* extern "C" */
157 #endif
158 
159 #endif /*LV_SW_H*/
160