1 /**
2 * @file lv_anim.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_anim_private.h"
10
11 #include "../core/lv_global.h"
12 #include "../tick/lv_tick.h"
13 #include "lv_assert.h"
14 #include "lv_timer.h"
15 #include "lv_math.h"
16 #include "../stdlib/lv_mem.h"
17 #include "../stdlib/lv_string.h"
18
19 /*********************
20 * DEFINES
21 *********************/
22
23 /**Perform linear animations in max 1024 steps. Used in `path_cb`s*/
24 #define LV_ANIM_RESOLUTION 1024
25
26 /**log2(LV_ANIM_RESOLUTION)*/
27 #define LV_ANIM_RES_SHIFT 10
28
29 /**In an anim. time this bit indicates that the value is speed, and not time*/
30 #define LV_ANIM_SPEED_MASK 0x80000000
31
32 #define state LV_GLOBAL_DEFAULT()->anim_state
33 #define anim_ll_p &(state.anim_ll)
34
35 /**********************
36 * TYPEDEFS
37 **********************/
38
39 /**********************
40 * STATIC PROTOTYPES
41 **********************/
42 static void anim_timer(lv_timer_t * param);
43 static void anim_mark_list_change(void);
44 static void anim_completed_handler(lv_anim_t * a);
45 static int32_t lv_anim_path_cubic_bezier(const lv_anim_t * a, int32_t x1,
46 int32_t y1, int32_t x2, int32_t y2);
47 static void lv_anim_pause_for_internal(lv_anim_t * a, uint32_t ms);
48 static void resolve_time(lv_anim_t * a);
49 static bool remove_concurrent_anims(lv_anim_t * a_current);
50 static void remove_anim(void * a);
51
52 /**********************
53 * STATIC VARIABLES
54 **********************/
55
56 /**********************
57 * MACROS
58 **********************/
59 #if LV_USE_LOG && LV_LOG_TRACE_ANIM
60 #define LV_TRACE_ANIM(...) LV_LOG_TRACE(__VA_ARGS__)
61 #else
62 #define LV_TRACE_ANIM(...)
63 #endif
64
65 /**********************
66 * GLOBAL FUNCTIONS
67 **********************/
68
lv_anim_core_init(void)69 void lv_anim_core_init(void)
70 {
71 lv_ll_init(anim_ll_p, sizeof(lv_anim_t));
72 state.timer = lv_timer_create(anim_timer, LV_DEF_REFR_PERIOD, NULL);
73 anim_mark_list_change(); /*Turn off the animation timer*/
74 state.anim_list_changed = false;
75 state.anim_run_round = false;
76 }
77
lv_anim_core_deinit(void)78 void lv_anim_core_deinit(void)
79 {
80 lv_anim_delete_all();
81 }
82
lv_anim_init(lv_anim_t * a)83 void lv_anim_init(lv_anim_t * a)
84 {
85 lv_memzero(a, sizeof(lv_anim_t));
86 a->duration = 500;
87 a->start_value = 0;
88 a->end_value = 100;
89 a->repeat_cnt = 1;
90 a->path_cb = lv_anim_path_linear;
91 a->early_apply = 1;
92 }
93
lv_anim_start(const lv_anim_t * a)94 lv_anim_t * lv_anim_start(const lv_anim_t * a)
95 {
96 LV_TRACE_ANIM("begin");
97
98 /*Add the new animation to the animation linked list*/
99 lv_anim_t * new_anim = lv_ll_ins_head(anim_ll_p);
100 LV_ASSERT_MALLOC(new_anim);
101 if(new_anim == NULL) return NULL;
102
103 /*Initialize the animation descriptor*/
104 lv_memcpy(new_anim, a, sizeof(lv_anim_t));
105 if(a->var == a) new_anim->var = new_anim;
106 new_anim->run_round = state.anim_run_round;
107 new_anim->last_timer_run = lv_tick_get();
108 new_anim->is_paused = false;
109
110 /*Set the start value*/
111 if(new_anim->early_apply) {
112 if(new_anim->get_value_cb) {
113 int32_t v_ofs = new_anim->get_value_cb(new_anim);
114 new_anim->start_value += v_ofs;
115 new_anim->end_value += v_ofs;
116
117 }
118
119 resolve_time(new_anim);
120
121 /*Do not let two animations for the same 'var' with the same 'exec_cb'*/
122 if(a->exec_cb || a->custom_exec_cb) remove_concurrent_anims(new_anim);
123
124 if(new_anim->exec_cb) {
125 new_anim->exec_cb(new_anim->var, new_anim->start_value);
126 }
127 if(new_anim->custom_exec_cb) {
128 new_anim->custom_exec_cb(new_anim, new_anim->start_value);
129 }
130 }
131
132 /*Creating an animation changed the linked list.
133 *It's important if it happens in a ready callback. (see `anim_timer`)*/
134 anim_mark_list_change();
135
136 LV_TRACE_ANIM("finished");
137 return new_anim;
138 }
139
lv_anim_get_playtime(const lv_anim_t * a)140 uint32_t lv_anim_get_playtime(const lv_anim_t * a)
141 {
142 if(a->repeat_cnt == LV_ANIM_REPEAT_INFINITE) {
143 return LV_ANIM_PLAYTIME_INFINITE;
144 }
145
146 uint32_t repeat_cnt = a->repeat_cnt;
147 if(repeat_cnt < 1) repeat_cnt = 1;
148
149 uint32_t playtime = a->repeat_delay + a->duration + a->reverse_delay + a->reverse_duration;
150 playtime = playtime * repeat_cnt;
151 return playtime;
152 }
153
lv_anim_delete(void * var,lv_anim_exec_xcb_t exec_cb)154 bool lv_anim_delete(void * var, lv_anim_exec_xcb_t exec_cb)
155 {
156 lv_anim_t * a;
157 bool del_any = false;
158 a = lv_ll_get_head(anim_ll_p);
159 while(a != NULL) {
160 bool del = false;
161 if((a->var == var || var == NULL) && (a->exec_cb == exec_cb || exec_cb == NULL)) {
162 remove_anim(a);
163 anim_mark_list_change(); /*Read by `anim_timer`. It need to know if a delete occurred in
164 the linked list*/
165 del_any = true;
166 del = true;
167 }
168
169 /*Always start from the head on delete, because we don't know
170 *how `anim_ll_p` was changes in `a->deleted_cb` */
171 a = del ? lv_ll_get_head(anim_ll_p) : lv_ll_get_next(anim_ll_p, a);
172 }
173
174 return del_any;
175 }
176
lv_anim_delete_all(void)177 void lv_anim_delete_all(void)
178 {
179 lv_ll_clear_custom(anim_ll_p, remove_anim);
180 anim_mark_list_change();
181 }
182
lv_anim_get(void * var,lv_anim_exec_xcb_t exec_cb)183 lv_anim_t * lv_anim_get(void * var, lv_anim_exec_xcb_t exec_cb)
184 {
185 lv_anim_t * a;
186 LV_LL_READ(anim_ll_p, a) {
187 if(a->var == var && (a->exec_cb == exec_cb || exec_cb == NULL)) {
188 return a;
189 }
190 }
191
192 return NULL;
193 }
194
lv_anim_get_timer(void)195 lv_timer_t * lv_anim_get_timer(void)
196 {
197 return state.timer;
198 }
199
lv_anim_count_running(void)200 uint16_t lv_anim_count_running(void)
201 {
202 uint16_t cnt = 0;
203 lv_anim_t * a;
204 LV_LL_READ(anim_ll_p, a) cnt++;
205
206 return cnt;
207 }
208
lv_anim_speed_clamped(uint32_t speed,uint32_t min_time,uint32_t max_time)209 uint32_t lv_anim_speed_clamped(uint32_t speed, uint32_t min_time, uint32_t max_time)
210 {
211
212 if(speed > 10000) {
213 LV_LOG_WARN("speed is truncated to 10000 (was %"LV_PRIu32")", speed);
214 speed = 10230;
215 }
216 if(min_time > 10000) {
217 LV_LOG_WARN("min_time is truncated to 10000 (was %"LV_PRIu32")", min_time);
218 min_time = 10230;
219 }
220 if(max_time > 10000) {
221 LV_LOG_WARN("max_time is truncated to 10000 (was %"LV_PRIu32")", max_time);
222 max_time = 10230;
223 }
224
225 /*Lower the resolution to fit the 0.1023 range*/
226 speed = (speed + 5) / 10;
227 min_time = (min_time + 5) / 10;
228 max_time = (max_time + 5) / 10;
229
230 return LV_ANIM_SPEED_MASK + (max_time << 20) + (min_time << 10) + speed;
231
232 }
233
lv_anim_speed(uint32_t speed)234 uint32_t lv_anim_speed(uint32_t speed)
235 {
236 return lv_anim_speed_clamped(speed, 0, 10000);
237 }
238
lv_anim_speed_to_time(uint32_t speed,int32_t start,int32_t end)239 uint32_t lv_anim_speed_to_time(uint32_t speed, int32_t start, int32_t end)
240 {
241 uint32_t d = LV_ABS(start - end);
242 uint32_t time = (d * 1000) / speed;
243
244 time = time == 0 ? 1 : time;
245
246 return time;
247 }
248
lv_anim_refr_now(void)249 void lv_anim_refr_now(void)
250 {
251 anim_timer(NULL);
252 }
253
lv_anim_path_linear(const lv_anim_t * a)254 int32_t lv_anim_path_linear(const lv_anim_t * a)
255 {
256 /*Calculate the current step*/
257 int32_t step = lv_map(a->act_time, 0, a->duration, 0, LV_ANIM_RESOLUTION);
258
259 /*Get the new value which will be proportional to `step`
260 *and the `start` and `end` values*/
261 int32_t new_value;
262 new_value = step * (a->end_value - a->start_value);
263 new_value = new_value >> LV_ANIM_RES_SHIFT;
264 new_value += a->start_value;
265
266 return new_value;
267 }
268
lv_anim_path_ease_in(const lv_anim_t * a)269 int32_t lv_anim_path_ease_in(const lv_anim_t * a)
270 {
271 return lv_anim_path_cubic_bezier(a, LV_BEZIER_VAL_FLOAT(0.42), LV_BEZIER_VAL_FLOAT(0),
272 LV_BEZIER_VAL_FLOAT(1), LV_BEZIER_VAL_FLOAT(1));
273 }
274
lv_anim_path_ease_out(const lv_anim_t * a)275 int32_t lv_anim_path_ease_out(const lv_anim_t * a)
276 {
277 return lv_anim_path_cubic_bezier(a, LV_BEZIER_VAL_FLOAT(0), LV_BEZIER_VAL_FLOAT(0),
278 LV_BEZIER_VAL_FLOAT(0.58), LV_BEZIER_VAL_FLOAT(1));
279 }
280
lv_anim_path_ease_in_out(const lv_anim_t * a)281 int32_t lv_anim_path_ease_in_out(const lv_anim_t * a)
282 {
283 return lv_anim_path_cubic_bezier(a, LV_BEZIER_VAL_FLOAT(0.42), LV_BEZIER_VAL_FLOAT(0),
284 LV_BEZIER_VAL_FLOAT(0.58), LV_BEZIER_VAL_FLOAT(1));
285 }
286
lv_anim_path_overshoot(const lv_anim_t * a)287 int32_t lv_anim_path_overshoot(const lv_anim_t * a)
288 {
289 return lv_anim_path_cubic_bezier(a, 341, 0, 683, 1300);
290 }
291
lv_anim_path_bounce(const lv_anim_t * a)292 int32_t lv_anim_path_bounce(const lv_anim_t * a)
293 {
294 /*Calculate the current step*/
295 int32_t t = lv_map(a->act_time, 0, a->duration, 0, LV_BEZIER_VAL_MAX);
296 int32_t diff = (a->end_value - a->start_value);
297
298 /*3 bounces has 5 parts: 3 down and 2 up. One part is t / 5 long*/
299
300 if(t < 408) {
301 /*Go down*/
302 t = (t * 2500) >> LV_BEZIER_VAL_SHIFT; /*[0..1024] range*/
303 t = LV_BEZIER_VAL_MAX - t;
304 }
305 else if(t >= 408 && t < 614) {
306 /*First bounce back*/
307 t -= 408;
308 t = t * 5; /*to [0..1024] range*/
309 diff = diff / 20;
310 }
311 else if(t >= 614 && t < 819) {
312 /*Fall back*/
313 t -= 614;
314 t = t * 5; /*to [0..1024] range*/
315 t = LV_BEZIER_VAL_MAX - t;
316 diff = diff / 20;
317 }
318 else if(t >= 819 && t < 921) {
319 /*Second bounce back*/
320 t -= 819;
321 t = t * 10; /*to [0..1024] range*/
322 diff = diff / 40;
323 }
324 else if(t >= 921 && t <= LV_BEZIER_VAL_MAX) {
325 /*Fall back*/
326 t -= 921;
327 t = t * 10; /*to [0..1024] range*/
328 t = LV_BEZIER_VAL_MAX - t;
329 diff = diff / 40;
330 }
331
332 if(t > LV_BEZIER_VAL_MAX) t = LV_BEZIER_VAL_MAX;
333 if(t < 0) t = 0;
334 int32_t step = lv_bezier3(t, 0, 500, 800, LV_BEZIER_VAL_MAX);
335
336 int32_t new_value;
337 new_value = step * diff;
338 new_value = new_value >> LV_BEZIER_VAL_SHIFT;
339 new_value = a->end_value - new_value;
340
341 return new_value;
342 }
343
lv_anim_path_step(const lv_anim_t * a)344 int32_t lv_anim_path_step(const lv_anim_t * a)
345 {
346 if(a->act_time >= a->duration)
347 return a->end_value;
348 else
349 return a->start_value;
350 }
351
lv_anim_path_custom_bezier3(const lv_anim_t * a)352 int32_t lv_anim_path_custom_bezier3(const lv_anim_t * a)
353 {
354 const lv_anim_bezier3_para_t * para = &a->parameter.bezier3;
355 return lv_anim_path_cubic_bezier(a, para->x1, para->y1, para->x2, para->y2);
356 }
357
lv_anim_set_var(lv_anim_t * a,void * var)358 void lv_anim_set_var(lv_anim_t * a, void * var)
359 {
360 a->var = var;
361 }
362
lv_anim_set_exec_cb(lv_anim_t * a,lv_anim_exec_xcb_t exec_cb)363 void lv_anim_set_exec_cb(lv_anim_t * a, lv_anim_exec_xcb_t exec_cb)
364 {
365 a->exec_cb = exec_cb;
366 }
367
lv_anim_set_duration(lv_anim_t * a,uint32_t duration)368 void lv_anim_set_duration(lv_anim_t * a, uint32_t duration)
369 {
370 a->duration = duration;
371 }
372
lv_anim_set_delay(lv_anim_t * a,uint32_t delay)373 void lv_anim_set_delay(lv_anim_t * a, uint32_t delay)
374 {
375 a->act_time = -(int32_t)(delay);
376 }
377
lv_anim_set_values(lv_anim_t * a,int32_t start,int32_t end)378 void lv_anim_set_values(lv_anim_t * a, int32_t start, int32_t end)
379 {
380 a->start_value = start;
381 a->current_value = INT32_MIN;
382 a->end_value = end;
383 }
384
lv_anim_set_custom_exec_cb(lv_anim_t * a,lv_anim_custom_exec_cb_t exec_cb)385 void lv_anim_set_custom_exec_cb(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb)
386 {
387 a->custom_exec_cb = exec_cb;
388 }
389
lv_anim_set_path_cb(lv_anim_t * a,lv_anim_path_cb_t path_cb)390 void lv_anim_set_path_cb(lv_anim_t * a, lv_anim_path_cb_t path_cb)
391 {
392 a->path_cb = path_cb;
393 }
394
lv_anim_set_start_cb(lv_anim_t * a,lv_anim_start_cb_t start_cb)395 void lv_anim_set_start_cb(lv_anim_t * a, lv_anim_start_cb_t start_cb)
396 {
397 a->start_cb = start_cb;
398 }
399
lv_anim_set_get_value_cb(lv_anim_t * a,lv_anim_get_value_cb_t get_value_cb)400 void lv_anim_set_get_value_cb(lv_anim_t * a, lv_anim_get_value_cb_t get_value_cb)
401 {
402 a->get_value_cb = get_value_cb;
403 }
404
lv_anim_set_completed_cb(lv_anim_t * a,lv_anim_completed_cb_t completed_cb)405 void lv_anim_set_completed_cb(lv_anim_t * a, lv_anim_completed_cb_t completed_cb)
406 {
407 a->completed_cb = completed_cb;
408 }
409
lv_anim_set_deleted_cb(lv_anim_t * a,lv_anim_deleted_cb_t deleted_cb)410 void lv_anim_set_deleted_cb(lv_anim_t * a, lv_anim_deleted_cb_t deleted_cb)
411 {
412 a->deleted_cb = deleted_cb;
413 }
414
lv_anim_set_reverse_duration(lv_anim_t * a,uint32_t duration)415 void lv_anim_set_reverse_duration(lv_anim_t * a, uint32_t duration)
416 {
417 a->reverse_duration = duration;
418 }
419
lv_anim_set_reverse_time(lv_anim_t * a,uint32_t duration)420 void lv_anim_set_reverse_time(lv_anim_t * a, uint32_t duration)
421 {
422 lv_anim_set_reverse_duration(a, duration);
423 }
424
lv_anim_set_reverse_delay(lv_anim_t * a,uint32_t delay)425 void lv_anim_set_reverse_delay(lv_anim_t * a, uint32_t delay)
426 {
427 a->reverse_delay = delay;
428 }
429
lv_anim_set_repeat_count(lv_anim_t * a,uint32_t cnt)430 void lv_anim_set_repeat_count(lv_anim_t * a, uint32_t cnt)
431 {
432 a->repeat_cnt = cnt;
433 }
434
lv_anim_set_repeat_delay(lv_anim_t * a,uint32_t delay)435 void lv_anim_set_repeat_delay(lv_anim_t * a, uint32_t delay)
436 {
437 a->repeat_delay = delay;
438 }
439
lv_anim_set_early_apply(lv_anim_t * a,bool en)440 void lv_anim_set_early_apply(lv_anim_t * a, bool en)
441 {
442 a->early_apply = en;
443 }
444
lv_anim_set_user_data(lv_anim_t * a,void * user_data)445 void lv_anim_set_user_data(lv_anim_t * a, void * user_data)
446 {
447 a->user_data = user_data;
448 }
449
lv_anim_set_bezier3_param(lv_anim_t * a,int16_t x1,int16_t y1,int16_t x2,int16_t y2)450 void lv_anim_set_bezier3_param(lv_anim_t * a, int16_t x1, int16_t y1, int16_t x2, int16_t y2)
451 {
452 lv_anim_bezier3_para_t * para = &a->parameter.bezier3;
453
454 para->x1 = x1;
455 para->x2 = x2;
456 para->y1 = y1;
457 para->y2 = y2;
458 }
459
lv_anim_get_delay(const lv_anim_t * a)460 uint32_t lv_anim_get_delay(const lv_anim_t * a)
461 {
462 return -a->act_time;
463 }
464
lv_anim_get_time(const lv_anim_t * a)465 uint32_t lv_anim_get_time(const lv_anim_t * a)
466 {
467 return a->duration;
468 }
469
lv_anim_get_repeat_count(const lv_anim_t * a)470 uint32_t lv_anim_get_repeat_count(const lv_anim_t * a)
471 {
472 return a->repeat_cnt;
473 }
474
lv_anim_get_user_data(const lv_anim_t * a)475 void * lv_anim_get_user_data(const lv_anim_t * a)
476 {
477 return a->user_data;
478 }
479
lv_anim_custom_delete(lv_anim_t * a,lv_anim_custom_exec_cb_t exec_cb)480 bool lv_anim_custom_delete(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb)
481 {
482 return lv_anim_delete(a ? a->var : NULL, (lv_anim_exec_xcb_t)exec_cb);
483 }
484
lv_anim_custom_get(lv_anim_t * a,lv_anim_custom_exec_cb_t exec_cb)485 lv_anim_t * lv_anim_custom_get(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb)
486 {
487 return lv_anim_get(a ? a->var : NULL, (lv_anim_exec_xcb_t)exec_cb);
488 }
489
lv_anim_resolve_speed(uint32_t speed_or_time,int32_t start,int32_t end)490 uint32_t lv_anim_resolve_speed(uint32_t speed_or_time, int32_t start, int32_t end)
491 {
492 /*It was a simple time*/
493 if((speed_or_time & LV_ANIM_SPEED_MASK) == 0) return speed_or_time;
494
495 uint32_t d = LV_ABS(start - end);
496 uint32_t speed = speed_or_time & 0x3FF;
497 uint32_t time = (d * 100) / speed; /*Speed is in 10 units per sec*/
498 uint32_t max_time = (speed_or_time >> 20) & 0x3FF;
499 uint32_t min_time = (speed_or_time >> 10) & 0x3FF;
500
501 return LV_CLAMP(min_time * 10, time, max_time * 10);
502 }
503
lv_anim_is_paused(lv_anim_t * a)504 bool lv_anim_is_paused(lv_anim_t * a)
505 {
506 LV_ASSERT_NULL(a);
507 return a->is_paused;
508 }
509
lv_anim_pause(lv_anim_t * a)510 void lv_anim_pause(lv_anim_t * a)
511 {
512 LV_ASSERT_NULL(a);
513 lv_anim_pause_for_internal(a, LV_ANIM_PAUSE_FOREVER);
514 }
515
lv_anim_pause_for(lv_anim_t * a,uint32_t ms)516 void lv_anim_pause_for(lv_anim_t * a, uint32_t ms)
517 {
518 LV_ASSERT_NULL(a);
519 lv_anim_pause_for_internal(a, ms);
520 }
521
lv_anim_resume(lv_anim_t * a)522 void lv_anim_resume(lv_anim_t * a)
523 {
524 LV_ASSERT_NULL(a);
525 a->is_paused = false;
526 a->pause_duration = 0;
527 }
528
529
530 /**********************
531 * STATIC FUNCTIONS
532 **********************/
533
534 /**
535 * Periodically handle the animations.
536 * @param param unused
537 */
anim_timer(lv_timer_t * param)538 static void anim_timer(lv_timer_t * param)
539 {
540 LV_UNUSED(param);
541
542 /*Flip the run round*/
543 state.anim_run_round = state.anim_run_round ? false : true;
544
545 lv_anim_t * a = lv_ll_get_head(anim_ll_p);
546
547 while(a != NULL) {
548 uint32_t elaps = lv_tick_elaps(a->last_timer_run);
549
550 if(a->is_paused) {
551 const uint32_t time_paused = lv_tick_elaps(a->pause_time);
552 const bool is_pause_over = a->pause_duration != LV_ANIM_PAUSE_FOREVER && time_paused >= a->pause_duration;
553
554 if(is_pause_over) {
555 const uint32_t pause_overrun = time_paused - a->pause_duration;
556 a->is_paused = false;
557 a->act_time += pause_overrun;
558 }
559 }
560 else {
561 a->act_time += elaps;
562 }
563 a->last_timer_run = lv_tick_get();
564
565 /*It can be set by `lv_anim_delete()` typically in `end_cb`. If set then an animation delete
566 * happened in `anim_completed_handler` which could make this linked list reading corrupt
567 * because the list is changed meanwhile
568 */
569 state.anim_list_changed = false;
570
571 if(!a->is_paused && a->run_round != state.anim_run_round) {
572 a->run_round = state.anim_run_round; /*The list readying might be reset so need to know which anim has run already*/
573
574 /*The animation will run now for the first time. Call `start_cb`*/
575 if(!a->start_cb_called && a->act_time >= 0) {
576
577 if(a->early_apply == 0 && a->get_value_cb) {
578 int32_t v_ofs = a->get_value_cb(a);
579 a->start_value += v_ofs;
580 a->end_value += v_ofs;
581 }
582
583 resolve_time(a);
584
585 if(a->start_cb) a->start_cb(a);
586 a->start_cb_called = 1;
587
588 /*Do not let two animations for the same 'var' with the same 'exec_cb'*/
589 remove_concurrent_anims(a);
590 }
591
592 if(a->act_time >= 0) {
593 int32_t act_time_original = a->act_time; /*The unclipped version is used later to correctly repeat the animation*/
594 if(a->act_time > a->duration) a->act_time = a->duration;
595
596 int32_t act_time_before_exec = a->act_time;
597 int32_t new_value;
598 new_value = a->path_cb(a);
599
600 if(new_value != a->current_value) {
601 a->current_value = new_value;
602 /*Apply the calculated value*/
603 if(a->exec_cb) a->exec_cb(a->var, new_value);
604 if(!state.anim_list_changed && a->custom_exec_cb) a->custom_exec_cb(a, new_value);
605 }
606
607 if(!state.anim_list_changed) {
608 /*Restore the original time to see is there is over time.
609 *Restore only if it wasn't changed in the `exec_cb` for some special reasons.*/
610 if(a->act_time == act_time_before_exec) a->act_time = act_time_original;
611
612 /*If the time is elapsed the animation is ready*/
613 if(a->act_time >= a->duration) {
614 anim_completed_handler(a);
615 }
616 }
617 }
618 }
619
620 /*If the linked list changed due to anim. delete then it's not safe to continue
621 *the reading of the list from here -> start from the head*/
622 if(state.anim_list_changed)
623 a = lv_ll_get_head(anim_ll_p);
624 else
625 a = lv_ll_get_next(anim_ll_p, a);
626 }
627
628 }
629
630 /**
631 * Called when an animation is completed to do the necessary things
632 * e.g. repeat, play in reverse, delete etc.
633 * @param a pointer to an animation descriptor
634 */
anim_completed_handler(lv_anim_t * a)635 static void anim_completed_handler(lv_anim_t * a)
636 {
637 /*In the end of a forward anim decrement repeat cnt.*/
638 if(a->reverse_play_in_progress == 0 && a->repeat_cnt > 0 && a->repeat_cnt != LV_ANIM_REPEAT_INFINITE) {
639 a->repeat_cnt--;
640 }
641
642 /*Delete animation if
643 * - no repeat left and no reverse play scheduled (simple one shot animation); or
644 * - no repeat, reverse play enabled (reverse_duration != 0) and reverse play is completed. */
645 if(a->repeat_cnt == 0 && (a->reverse_duration == 0 || a->reverse_play_in_progress == 1)) {
646
647 /*Delete the animation from the list.
648 * This way the `completed_cb` will see the animations like it's animation is already deleted*/
649 lv_ll_remove(anim_ll_p, a);
650 /*Flag that the list has changed*/
651 anim_mark_list_change();
652
653 /*Call the callback function at the end*/
654 if(a->completed_cb != NULL) a->completed_cb(a);
655 if(a->deleted_cb != NULL) a->deleted_cb(a);
656 lv_free(a);
657 }
658 /*If the animation is not deleted then restart it*/
659 else {
660 /*Restart the animation. If the time is over a little compensate it.*/
661 int32_t over_time = 0;
662 if(a->act_time > a->duration) over_time = a->act_time - a->duration;
663 a->act_time = over_time - (int32_t)(a->repeat_delay);
664 /*Swap start and end values in reverse-play mode*/
665 if(a->reverse_duration != 0) {
666 /*If now now playing in reverse, use the 'reverse_delay'.*/
667 if(a->reverse_play_in_progress == 0) a->act_time = -(int32_t)(a->reverse_delay);
668
669 /*Toggle reverse-play state*/
670 a->reverse_play_in_progress = a->reverse_play_in_progress == 0 ? 1 : 0;
671 /*Swap the start and end values*/
672 int32_t tmp = a->start_value;
673 a->start_value = a->end_value;
674 a->end_value = tmp;
675 /*Swap the time and reverse_duration*/
676 tmp = a->duration;
677 a->duration = a->reverse_duration;
678 a->reverse_duration = tmp;
679 }
680 }
681 }
682
anim_mark_list_change(void)683 static void anim_mark_list_change(void)
684 {
685 state.anim_list_changed = true;
686 if(lv_ll_get_head(anim_ll_p) == NULL)
687 lv_timer_pause(state.timer);
688 else
689 lv_timer_resume(state.timer);
690 }
691
lv_anim_path_cubic_bezier(const lv_anim_t * a,int32_t x1,int32_t y1,int32_t x2,int32_t y2)692 static int32_t lv_anim_path_cubic_bezier(const lv_anim_t * a, int32_t x1, int32_t y1, int32_t x2, int32_t y2)
693 {
694 /*Calculate the current step*/
695 uint32_t t = lv_map(a->act_time, 0, a->duration, 0, LV_BEZIER_VAL_MAX);
696 int32_t step = lv_cubic_bezier(t, x1, y1, x2, y2);
697
698 int32_t new_value;
699 new_value = step * (a->end_value - a->start_value);
700 new_value = new_value >> LV_BEZIER_VAL_SHIFT;
701 new_value += a->start_value;
702
703 return new_value;
704 }
705
lv_anim_pause_for_internal(lv_anim_t * a,uint32_t ms)706 static void lv_anim_pause_for_internal(lv_anim_t * a, uint32_t ms)
707 {
708
709 a->is_paused = true;
710 a->pause_time = lv_tick_get();
711 a->pause_duration = ms;
712 }
713
resolve_time(lv_anim_t * a)714 static void resolve_time(lv_anim_t * a)
715 {
716 a->duration = lv_anim_resolve_speed(a->duration, a->start_value, a->end_value);
717 a->reverse_duration = lv_anim_resolve_speed(a->reverse_duration, a->start_value, a->end_value);
718 a->reverse_delay = lv_anim_resolve_speed(a->reverse_delay, a->start_value, a->end_value);
719 a->repeat_delay = lv_anim_resolve_speed(a->repeat_delay, a->start_value, a->end_value);
720 }
721
722 /**
723 * Remove animations which are animating the same var with the same exec_cb
724 * and they are already running or they have early_apply
725 * @param a_current the current animation, use its var and exec_cb as reference to know what to remove
726 * @return true: at least one animation was delete
727 */
remove_concurrent_anims(lv_anim_t * a_current)728 static bool remove_concurrent_anims(lv_anim_t * a_current)
729 {
730 if(a_current->exec_cb == NULL && a_current->custom_exec_cb == NULL) return false;
731
732 lv_anim_t * a;
733 bool del_any = false;
734 a = lv_ll_get_head(anim_ll_p);
735 while(a != NULL) {
736 bool del = false;
737 /*We can't test for custom_exec_cb equality because in the MicroPython binding
738 *a wrapper callback is used here an the real callback data is stored in the `user_data`.
739 *Therefore equality check would remove all animations.*/
740 if(a != a_current &&
741 (a->act_time >= 0 || a->early_apply) &&
742 (a->var == a_current->var) &&
743 ((a->exec_cb && a->exec_cb == a_current->exec_cb)
744 /*|| (a->custom_exec_cb && a->custom_exec_cb == a_current->custom_exec_cb)*/)) {
745 lv_ll_remove(anim_ll_p, a);
746 if(a->deleted_cb != NULL) a->deleted_cb(a);
747 lv_free(a);
748 /*Read by `anim_timer`. It need to know if a delete occurred in the linked list*/
749 anim_mark_list_change();
750
751 del_any = true;
752 del = true;
753 }
754
755 /*Always start from the head on delete, because we don't know
756 *how `anim_ll_p` was changes in `a->deleted_cb` */
757 a = del ? lv_ll_get_head(anim_ll_p) : lv_ll_get_next(anim_ll_p, a);
758 }
759
760 return del_any;
761 }
762
remove_anim(void * a)763 static void remove_anim(void * a)
764 {
765 lv_anim_t * anim = a;
766 lv_ll_remove(anim_ll_p, a);
767 if(anim->deleted_cb != NULL) anim->deleted_cb(anim);
768 lv_free(a);
769 }
770