1 /**
2 * @file lv_spinner.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_spinner.h"
10 #if LV_USE_SPINNER != 0
11
12 #include "../lv_misc/lv_debug.h"
13 #include "../lv_misc/lv_math.h"
14 #include "../lv_draw/lv_draw_rect.h"
15 #include "../lv_draw/lv_draw_arc.h"
16 #include "../lv_themes/lv_theme.h"
17
18 /*********************
19 * DEFINES
20 *********************/
21 #define LV_OBJX_NAME "lv_spinner"
22
23 #ifndef LV_SPINNER_DEF_ARC_LENGTH
24 #define LV_SPINNER_DEF_ARC_LENGTH 60 /*[deg]*/
25 #endif
26
27 #ifndef LV_SPINNER_DEF_SPIN_TIME
28 #define LV_SPINNER_DEF_SPIN_TIME 1000 /*[ms]*/
29 #endif
30
31 #ifndef LV_SPINNER_DEF_ANIM
32 #define LV_SPINNER_DEF_ANIM LV_SPINNER_TYPE_SPINNING_ARC /*animation type*/
33 #endif
34
35 /**********************
36 * TYPEDEFS
37 **********************/
38
39 /**********************
40 * STATIC PROTOTYPES
41 **********************/
42 static lv_res_t lv_spinner_signal(lv_obj_t * spinner, lv_signal_t sign, void * param);
43
44 /**********************
45 * STATIC VARIABLES
46 **********************/
47 static lv_signal_cb_t ancestor_signal;
48 static lv_design_cb_t ancestor_design;
49
50 /**********************
51 * MACROS
52 **********************/
53
54 /**********************
55 * GLOBAL FUNCTIONS
56 **********************/
57
58 /**
59 * Create a spinner object
60 * @param par pointer to an object, it will be the parent of the new spinner
61 * @param copy pointer to a spinner object, if not NULL then the new object will be copied from
62 * it
63 * @return pointer to the created spinner
64 */
lv_spinner_create(lv_obj_t * par,const lv_obj_t * copy)65 lv_obj_t * lv_spinner_create(lv_obj_t * par, const lv_obj_t * copy)
66 {
67 LV_LOG_TRACE("spinner create started");
68
69 /*Create the ancestor of spinner*/
70 lv_obj_t * spinner = lv_arc_create(par, copy);
71 LV_ASSERT_MEM(spinner);
72 if(spinner == NULL) return NULL;
73
74 /*Allocate the spinner type specific extended data*/
75 lv_spinner_ext_t * ext = lv_obj_allocate_ext_attr(spinner, sizeof(lv_spinner_ext_t));
76 LV_ASSERT_MEM(ext);
77 if(ext == NULL) {
78 lv_obj_del(spinner);
79 return NULL;
80 }
81
82 if(ancestor_signal == NULL) ancestor_signal = lv_obj_get_signal_cb(spinner);
83 if(ancestor_design == NULL) ancestor_design = lv_obj_get_design_cb(spinner);
84
85 /*Initialize the allocated 'ext' */
86 ext->arc_length = LV_SPINNER_DEF_ARC_LENGTH;
87 ext->anim_type = LV_SPINNER_DEF_ANIM;
88 ext->anim_dir = LV_SPINNER_DIR_FORWARD;
89 ext->time = LV_SPINNER_DEF_SPIN_TIME;
90
91 /*The signal and design functions are not copied so set them here*/
92 lv_obj_set_signal_cb(spinner, lv_spinner_signal);
93
94 /*Init the new spinner spinner*/
95 if(copy == NULL) {
96 ext->arc.bg_angle_start = 0;
97 ext->arc.bg_angle_end = 360;
98 lv_obj_set_size(spinner, LV_DPI, LV_DPI);
99 lv_theme_apply(spinner, LV_THEME_SPINNER);
100
101 }
102 /*Copy an existing spinner*/
103 else {
104 lv_spinner_ext_t * copy_ext = lv_obj_get_ext_attr(copy);
105 ext->arc_length = copy_ext->arc_length;
106 ext->time = copy_ext->time;
107 ext->anim_dir = copy_ext->anim_dir;
108 /*Refresh the style with new signal function*/
109 lv_obj_refresh_style(spinner, LV_OBJ_PART_ALL, LV_STYLE_PROP_ALL);
110 }
111
112 lv_spinner_set_type(spinner, ext->anim_type);
113
114 LV_LOG_INFO("spinner created");
115
116 return spinner;
117 }
118
119 /*======================
120 * Add/remove functions
121 *=====================*/
122
123 /**
124 * Set the length of the spinning arc in degrees
125 * @param spinner pointer to a spinner object
126 * @param deg length of the arc
127 */
lv_spinner_set_arc_length(lv_obj_t * spinner,lv_anim_value_t deg)128 void lv_spinner_set_arc_length(lv_obj_t * spinner, lv_anim_value_t deg)
129 {
130 LV_ASSERT_OBJ(spinner, LV_OBJX_NAME);
131
132 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
133
134 ext->arc_length = deg;
135 }
136
137 /**
138 * Set the spin time of the arc
139 * @param spinner pointer to a spinner object
140 * @param time time of one round in milliseconds
141 */
lv_spinner_set_spin_time(lv_obj_t * spinner,uint16_t time)142 void lv_spinner_set_spin_time(lv_obj_t * spinner, uint16_t time)
143 {
144 LV_ASSERT_OBJ(spinner, LV_OBJX_NAME);
145
146 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
147
148 ext->time = time;
149 lv_spinner_set_type(spinner, ext->anim_type);
150 }
151 /*=====================
152 * Setter functions
153 *====================*/
154
155 /**
156 * Set the animation type of a spinner.
157 * @param spinner pointer to spinner object
158 * @param type animation type of the spinner
159 * */
lv_spinner_set_type(lv_obj_t * spinner,lv_spinner_type_t type)160 void lv_spinner_set_type(lv_obj_t * spinner, lv_spinner_type_t type)
161 {
162 LV_ASSERT_OBJ(spinner, LV_OBJX_NAME);
163
164 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
165
166 /*delete previous animation*/
167 lv_anim_del(spinner, NULL);
168 switch(type) {
169 case LV_SPINNER_TYPE_FILLSPIN_ARC: {
170 ext->anim_type = LV_SPINNER_TYPE_FILLSPIN_ARC;
171 lv_anim_path_t path;
172 lv_anim_path_init(&path);
173 lv_anim_path_set_cb(&path, lv_anim_path_ease_in_out);
174
175 lv_anim_t a;
176 lv_anim_init(&a);
177 lv_anim_set_var(&a, spinner);
178 lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_spinner_anim_cb);
179 lv_anim_set_path(&a, &path);
180 lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
181 lv_anim_set_time(&a, ext->time);
182 if(ext->anim_dir == LV_SPINNER_DIR_FORWARD) lv_anim_set_values(&a, 0, 360);
183 else lv_anim_set_values(&a, 360, 0);
184 lv_anim_start(&a);
185
186 lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_spinner_set_arc_length);
187 if(ext->anim_dir == LV_SPINNER_DIR_FORWARD) lv_anim_set_values(&a, ext->arc_length, 360 - ext->arc_length);
188 else lv_anim_set_values(&a, 360 - ext->arc_length, ext->arc_length);
189
190 lv_anim_set_playback_time(&a, ext->time);
191 lv_anim_start(&a);
192 break;
193 }
194 case LV_SPINNER_TYPE_CONSTANT_ARC:
195 case LV_SPINNER_TYPE_SPINNING_ARC:
196 default: {
197 ext->anim_type = type;
198
199 lv_anim_path_t path;
200 lv_anim_path_init(&path);
201 lv_anim_path_set_cb(&path, (LV_SPINNER_TYPE_CONSTANT_ARC == type ? lv_anim_path_linear : lv_anim_path_ease_in_out));
202
203 lv_anim_t a;
204 lv_anim_init(&a);
205 lv_anim_set_var(&a, spinner);
206 lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_spinner_anim_cb);
207 lv_anim_set_time(&a, ext->time);
208 lv_anim_set_path(&a, &path);
209 lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
210 if(ext->anim_dir == LV_SPINNER_DIR_FORWARD) lv_anim_set_values(&a, 0, 360);
211 else lv_anim_set_values(&a, 360, 0);
212 lv_anim_start(&a);
213 break;
214 }
215 }
216 }
217
lv_spinner_set_dir(lv_obj_t * spinner,lv_spinner_dir_t dir)218 void lv_spinner_set_dir(lv_obj_t * spinner, lv_spinner_dir_t dir)
219 {
220 LV_ASSERT_OBJ(spinner, LV_OBJX_NAME);
221
222 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
223
224 ext->anim_dir = dir;
225 lv_spinner_set_type(spinner, ext->anim_type);
226 }
227
228 /*=====================
229 * Getter functions
230 *====================*/
231
232 /**
233 * Get the arc length [degree] of the a spinner
234 * @param spinner pointer to a spinner object
235 */
lv_spinner_get_arc_length(const lv_obj_t * spinner)236 lv_anim_value_t lv_spinner_get_arc_length(const lv_obj_t * spinner)
237 {
238 LV_ASSERT_OBJ(spinner, LV_OBJX_NAME);
239
240 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
241 return ext->arc_length;
242 }
243
244 /**
245 * Get the spin time of the arc
246 * @param spinner pointer to a spinner object [milliseconds]
247 */
lv_spinner_get_spin_time(const lv_obj_t * spinner)248 uint16_t lv_spinner_get_spin_time(const lv_obj_t * spinner)
249 {
250 LV_ASSERT_OBJ(spinner, LV_OBJX_NAME);
251
252 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
253 return ext->time;
254 }
255
256 /**
257 * Get the animation type of a spinner.
258 * @param spinner pointer to spinner object
259 * @return animation type
260 * */
lv_spinner_get_type(lv_obj_t * spinner)261 lv_spinner_type_t lv_spinner_get_type(lv_obj_t * spinner)
262 {
263 LV_ASSERT_OBJ(spinner, LV_OBJX_NAME);
264
265 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
266 return ext->anim_type;
267 }
268
lv_spinner_get_dir(lv_obj_t * spinner)269 lv_spinner_dir_t lv_spinner_get_dir(lv_obj_t * spinner)
270 {
271 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
272 return ext->anim_dir;
273 }
274
275 /*=====================
276 * Other functions
277 *====================*/
278
279 /**
280 * Animator function (exec_cb) to rotate the arc of spinner.
281 * @param ptr pointer to spinner
282 * @param val the current desired value [0..360]
283 */
lv_spinner_anim_cb(void * ptr,lv_anim_value_t val)284 void lv_spinner_anim_cb(void * ptr, lv_anim_value_t val)
285 {
286 lv_obj_t * spinner = ptr;
287 lv_spinner_ext_t * ext = lv_obj_get_ext_attr(spinner);
288
289 int16_t angle_start = val - ext->arc_length / 2 - 90;
290 if(angle_start < 0) angle_start += 360;
291 int16_t angle_end = angle_start + ext->arc_length;
292
293 angle_start = angle_start % 360;
294 angle_end = angle_end % 360;
295
296 lv_arc_set_angles(spinner, angle_start, angle_end);
297 }
298
299 /**********************
300 * STATIC FUNCTIONS
301 **********************/
302
303 /**
304 * Signal function of the spinner
305 * @param spinner pointer to a spinner object
306 * @param sign a signal type from lv_signal_t enum
307 * @param param pointer to a signal specific variable
308 * @return LV_RES_OK: the object is not deleted in the function; LV_RES_INV: the object is deleted
309 */
lv_spinner_signal(lv_obj_t * spinner,lv_signal_t sign,void * param)310 static lv_res_t lv_spinner_signal(lv_obj_t * spinner, lv_signal_t sign, void * param)
311 {
312 lv_res_t res;
313
314 /* Include the ancient signal function */
315 res = ancestor_signal(spinner, sign, param);
316 if(res != LV_RES_OK) return res;
317 if(sign == LV_SIGNAL_GET_TYPE) return lv_obj_handle_get_type_signal(param, LV_OBJX_NAME);
318
319 if(sign == LV_SIGNAL_CLEANUP) {
320 /*Nothing to cleanup. (No dynamically allocated memory in 'ext')*/
321 }
322
323 return res;
324 }
325
326 #endif
327