1 /**
2  * @file lv_slider.h
3  *
4  */
5 
6 #ifndef LV_SLIDER_H
7 #define LV_SLIDER_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_SLIDER != 0
19 
20 /*Testing of dependencies*/
21 #if LV_USE_BAR == 0
22 #error "lv_slider: lv_bar is required. Enable it in lv_conf.h (LV_USE_BAR  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 
36 enum {
37     LV_SLIDER_TYPE_NORMAL,
38     LV_SLIDER_TYPE_SYMMETRICAL,
39     LV_SLIDER_TYPE_RANGE
40 };
41 typedef uint8_t lv_slider_type_t;
42 
43 /*Data of slider*/
44 typedef struct {
45     lv_bar_ext_t bar; /*Ext. of ancestor*/
46     /*New data for this type */
47     lv_style_list_t style_knob; /*Style of the knob*/
48     lv_area_t left_knob_area;
49     lv_area_t right_knob_area;
50     int16_t * value_to_set; /* Which bar value to set */
51     uint8_t dragging : 1;       /*1: the slider is being dragged*/
52 } lv_slider_ext_t;
53 
54 /** Built-in styles of slider*/
55 enum {
56     LV_SLIDER_PART_BG, /** Slider background style. */
57     LV_SLIDER_PART_INDIC, /** Slider indicator (filled area) style. */
58     LV_SLIDER_PART_KNOB, /** Slider knob style. */
59 };
60 
61 /**********************
62  * GLOBAL PROTOTYPES
63  **********************/
64 
65 /**
66  * Create a slider objects
67  * @param par pointer to an object, it will be the parent of the new slider
68  * @param copy pointer to a slider object, if not NULL then the new object will be copied from it
69  * @return pointer to the created slider
70  */
71 lv_obj_t * lv_slider_create(lv_obj_t * par, const lv_obj_t * copy);
72 
73 /*=====================
74  * Setter functions
75  *====================*/
76 
77 /**
78  * Set a new value on the slider
79  * @param slider pointer to a slider object
80  * @param value new value
81  * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
82  */
lv_slider_set_value(lv_obj_t * slider,int16_t value,lv_anim_enable_t anim)83 static inline void lv_slider_set_value(lv_obj_t * slider, int16_t value, lv_anim_enable_t anim)
84 {
85     lv_bar_set_value(slider, value, anim);
86 }
87 
88 /**
89  * Set a new value for the left knob of a slider
90  * @param slider pointer to a slider object
91  * @param left_value new value
92  * @param anim LV_ANIM_ON: set the value with an animation; LV_ANIM_OFF: change the value immediately
93  */
lv_slider_set_left_value(lv_obj_t * slider,int16_t left_value,lv_anim_enable_t anim)94 static inline void lv_slider_set_left_value(lv_obj_t * slider, int16_t left_value, lv_anim_enable_t anim)
95 {
96     lv_bar_set_start_value(slider, left_value, anim);
97 }
98 
99 /**
100  * Set minimum and the maximum values of a bar
101  * @param slider pointer to the slider object
102  * @param min minimum value
103  * @param max maximum value
104  */
lv_slider_set_range(lv_obj_t * slider,int16_t min,int16_t max)105 static inline void lv_slider_set_range(lv_obj_t * slider, int16_t min, int16_t max)
106 {
107     lv_bar_set_range(slider, min, max);
108 }
109 
110 /**
111  * Set the animation time of the slider
112  * @param slider pointer to a bar object
113  * @param anim_time the animation time in milliseconds.
114  */
lv_slider_set_anim_time(lv_obj_t * slider,uint16_t anim_time)115 static inline void lv_slider_set_anim_time(lv_obj_t * slider, uint16_t anim_time)
116 {
117     lv_bar_set_anim_time(slider, anim_time);
118 }
119 
120 /**
121  * Make the slider symmetric to zero. The indicator will grow from zero instead of the minimum
122  * position.
123  * @param slider pointer to a slider object
124  * @param en true: enable disable symmetric behavior; false: disable
125  */
lv_slider_set_type(lv_obj_t * slider,lv_slider_type_t type)126 static inline void lv_slider_set_type(lv_obj_t * slider, lv_slider_type_t type)
127 {
128     if(type == LV_SLIDER_TYPE_NORMAL)
129         lv_bar_set_type(slider, LV_BAR_TYPE_NORMAL);
130     else if(type == LV_SLIDER_TYPE_SYMMETRICAL)
131         lv_bar_set_type(slider, LV_BAR_TYPE_SYMMETRICAL);
132     else if(type == LV_SLIDER_TYPE_RANGE)
133         lv_bar_set_type(slider, LV_BAR_TYPE_CUSTOM);
134 }
135 
136 /*=====================
137  * Getter functions
138  *====================*/
139 
140 /**
141  * Get the value of the main knob of a slider
142  * @param slider pointer to a slider object
143  * @return the value of the main knob of the slider
144  */
145 int16_t lv_slider_get_value(const lv_obj_t * slider);
146 
147 /**
148  * Get the value of the left knob of a slider
149  * @param slider pointer to a slider object
150  * @return the value of the left knob of the slider
151  */
lv_slider_get_left_value(const lv_obj_t * slider)152 static inline int16_t lv_slider_get_left_value(const lv_obj_t * slider)
153 {
154     return lv_bar_get_start_value(slider);
155 }
156 
157 /**
158  * Get the minimum value of a slider
159  * @param slider pointer to a slider object
160  * @return the minimum value of the slider
161  */
lv_slider_get_min_value(const lv_obj_t * slider)162 static inline int16_t lv_slider_get_min_value(const lv_obj_t * slider)
163 {
164     return lv_bar_get_min_value(slider);
165 }
166 
167 /**
168  * Get the maximum value of a slider
169  * @param slider pointer to a slider object
170  * @return the maximum value of the slider
171  */
lv_slider_get_max_value(const lv_obj_t * slider)172 static inline int16_t lv_slider_get_max_value(const lv_obj_t * slider)
173 {
174     return lv_bar_get_max_value(slider);
175 }
176 
177 /**
178  * Give the slider is being dragged or not
179  * @param slider pointer to a slider object
180  * @return true: drag in progress false: not dragged
181  */
182 bool lv_slider_is_dragged(const lv_obj_t * slider);
183 
184 /**
185  * Get the animation time of the slider
186  * @param slider pointer to a slider object
187  * @return the animation time in milliseconds.
188  */
lv_slider_get_anim_time(lv_obj_t * slider)189 static inline uint16_t lv_slider_get_anim_time(lv_obj_t * slider)
190 {
191     return lv_bar_get_anim_time(slider);
192 }
193 
194 /**
195  * Get whether the slider is symmetric or not.
196  * @param slider pointer to a bar object
197  * @return true: symmetric is enabled; false: disable
198  */
lv_slider_get_type(lv_obj_t * slider)199 static inline lv_slider_type_t lv_slider_get_type(lv_obj_t * slider)
200 {
201     lv_bar_type_t type = lv_bar_get_type(slider);
202     if(type == LV_BAR_TYPE_SYMMETRICAL)
203         return LV_SLIDER_TYPE_SYMMETRICAL;
204     else if(type == LV_BAR_TYPE_CUSTOM)
205         return LV_SLIDER_TYPE_RANGE;
206     else
207         return LV_SLIDER_TYPE_NORMAL;
208 }
209 
210 /**********************
211  *      MACROS
212  **********************/
213 
214 #endif /*LV_USE_SLIDER*/
215 
216 #ifdef __cplusplus
217 } /* extern "C" */
218 #endif
219 
220 #endif /*LV_SLIDER_H*/
221