1 /**
2  * @file lv_anim.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_anim.h"
10 
11 #include "../hal/lv_hal_tick.h"
12 #include "lv_assert.h"
13 #include "lv_timer.h"
14 #include "lv_math.h"
15 #include "lv_mem.h"
16 #include "lv_gc.h"
17 
18 /*********************
19  *      DEFINES
20  *********************/
21 #define LV_ANIM_RESOLUTION 1024
22 #define LV_ANIM_RES_SHIFT 10
23 
24 /**********************
25  *      TYPEDEFS
26  **********************/
27 
28 /**********************
29  *  STATIC PROTOTYPES
30  **********************/
31 static void anim_timer(lv_timer_t * param);
32 static void anim_mark_list_change(void);
33 static void anim_ready_handler(lv_anim_t * a);
34 
35 /**********************
36  *  STATIC VARIABLES
37  **********************/
38 static uint32_t last_timer_run;
39 static bool anim_list_changed;
40 static bool anim_run_round;
41 static lv_timer_t * _lv_anim_tmr;
42 
43 /**********************
44  *      MACROS
45  **********************/
46 #if LV_LOG_TRACE_ANIM
47     #define TRACE_ANIM(...) LV_LOG_TRACE(__VA_ARGS__)
48 #else
49     #define TRACE_ANIM(...)
50 #endif
51 
52 
53 /**********************
54  *   GLOBAL FUNCTIONS
55  **********************/
56 
_lv_anim_core_init(void)57 void _lv_anim_core_init(void)
58 {
59     _lv_ll_init(&LV_GC_ROOT(_lv_anim_ll), sizeof(lv_anim_t));
60     _lv_anim_tmr = lv_timer_create(anim_timer, LV_DISP_DEF_REFR_PERIOD, NULL);
61     anim_mark_list_change(); /*Turn off the animation timer*/
62     anim_list_changed = false;
63 }
64 
lv_anim_init(lv_anim_t * a)65 void lv_anim_init(lv_anim_t * a)
66 {
67     lv_memset_00(a, sizeof(lv_anim_t));
68     a->time = 500;
69     a->start_value = 0;
70     a->end_value = 100;
71     a->repeat_cnt = 1;
72     a->path_cb = lv_anim_path_linear;
73     a->early_apply = 1;
74 }
75 
lv_anim_start(const lv_anim_t * a)76 lv_anim_t * lv_anim_start(const lv_anim_t * a)
77 {
78     TRACE_ANIM("begin");
79 
80     /*Do not let two animations for the same 'var' with the same 'exec_cb'*/
81     if(a->exec_cb != NULL) lv_anim_del(a->var, a->exec_cb); /*exec_cb == NULL would delete all animations of var*/
82 
83     /*If the list is empty the anim timer was suspended and it's last run measure is invalid*/
84     if(_lv_ll_is_empty(&LV_GC_ROOT(_lv_anim_ll))) {
85         last_timer_run = lv_tick_get();
86     }
87 
88     /*Add the new animation to the animation linked list*/
89     lv_anim_t * new_anim = _lv_ll_ins_head(&LV_GC_ROOT(_lv_anim_ll));
90     LV_ASSERT_MALLOC(new_anim);
91     if(new_anim == NULL) return NULL;
92 
93     /*Initialize the animation descriptor*/
94     lv_memcpy(new_anim, a, sizeof(lv_anim_t));
95     if(a->var == a) new_anim->var = new_anim;
96     new_anim->run_round = anim_run_round;
97 
98     /*Set the start value*/
99     if(new_anim->early_apply) {
100         if(new_anim->get_value_cb) {
101             int32_t v_ofs = new_anim->get_value_cb(new_anim);
102             new_anim->start_value += v_ofs;
103             new_anim->end_value += v_ofs;
104         }
105 
106         if(new_anim->exec_cb && new_anim->var) new_anim->exec_cb(new_anim->var, new_anim->start_value);
107     }
108 
109     /*Creating an animation changed the linked list.
110      *It's important if it happens in a ready callback. (see `anim_timer`)*/
111     anim_mark_list_change();
112 
113     TRACE_ANIM("finished");
114     return new_anim;
115 }
116 
lv_anim_get_playtime(lv_anim_t * a)117 uint32_t lv_anim_get_playtime(lv_anim_t * a)
118 {
119     uint32_t playtime = LV_ANIM_PLAYTIME_INFINITE;
120 
121     if(a->repeat_cnt == LV_ANIM_REPEAT_INFINITE)
122         return playtime;
123 
124     playtime = a->time - a->act_time;
125     if(a->playback_now == 0)
126         playtime += a->playback_delay + a->playback_time;
127 
128     if(a->repeat_cnt <= 1)
129         return playtime;
130 
131     playtime += (a->repeat_delay + a->time +
132                  a->playback_delay + a->playback_time) *
133                 (a->repeat_cnt - 1);
134 
135     return playtime;
136 }
137 
lv_anim_del(void * var,lv_anim_exec_xcb_t exec_cb)138 bool lv_anim_del(void * var, lv_anim_exec_xcb_t exec_cb)
139 {
140     lv_anim_t * a;
141     lv_anim_t * a_next;
142     bool del = false;
143     a        = _lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
144     while(a != NULL) {
145         /*'a' might be deleted, so get the next object while 'a' is valid*/
146         a_next = _lv_ll_get_next(&LV_GC_ROOT(_lv_anim_ll), a);
147 
148         if((a->var == var || var == NULL) && (a->exec_cb == exec_cb || exec_cb == NULL)) {
149             _lv_ll_remove(&LV_GC_ROOT(_lv_anim_ll), a);
150             if(a->deleted_cb != NULL) a->deleted_cb(a);
151             lv_mem_free(a);
152             anim_mark_list_change(); /*Read by `anim_timer`. It need to know if a delete occurred in
153                                        the linked list*/
154             del = true;
155         }
156 
157         a = a_next;
158     }
159 
160     return del;
161 }
162 
lv_anim_del_all(void)163 void lv_anim_del_all(void)
164 {
165     _lv_ll_clear(&LV_GC_ROOT(_lv_anim_ll));
166     anim_mark_list_change();
167 }
168 
lv_anim_get(void * var,lv_anim_exec_xcb_t exec_cb)169 lv_anim_t * lv_anim_get(void * var, lv_anim_exec_xcb_t exec_cb)
170 {
171     lv_anim_t * a;
172     _LV_LL_READ(&LV_GC_ROOT(_lv_anim_ll), a) {
173         if(a->var == var && (a->exec_cb == exec_cb || exec_cb == NULL)) {
174             return a;
175         }
176     }
177 
178     return NULL;
179 }
180 
lv_anim_get_timer(void)181 struct _lv_timer_t * lv_anim_get_timer(void)
182 {
183     return _lv_anim_tmr;
184 }
185 
lv_anim_count_running(void)186 uint16_t lv_anim_count_running(void)
187 {
188     uint16_t cnt = 0;
189     lv_anim_t * a;
190     _LV_LL_READ(&LV_GC_ROOT(_lv_anim_ll), a) cnt++;
191 
192     return cnt;
193 }
194 
lv_anim_speed_to_time(uint32_t speed,int32_t start,int32_t end)195 uint32_t lv_anim_speed_to_time(uint32_t speed, int32_t start, int32_t end)
196 {
197     uint32_t d    = LV_ABS(start - end);
198     uint32_t time = (d * 1000) / speed;
199 
200     if(time == 0) {
201         time++;
202     }
203 
204     return time;
205 }
206 
lv_anim_refr_now(void)207 void lv_anim_refr_now(void)
208 {
209     anim_timer(NULL);
210 }
211 
lv_anim_path_linear(const lv_anim_t * a)212 int32_t lv_anim_path_linear(const lv_anim_t * a)
213 {
214     /*Calculate the current step*/
215     int32_t step = lv_map(a->act_time, 0, a->time, 0, LV_ANIM_RESOLUTION);
216 
217     /*Get the new value which will be proportional to `step`
218      *and the `start` and `end` values*/
219     int32_t new_value;
220     new_value = step * (a->end_value - a->start_value);
221     new_value = new_value >> LV_ANIM_RES_SHIFT;
222     new_value += a->start_value;
223 
224     return new_value;
225 }
226 
lv_anim_path_ease_in(const lv_anim_t * a)227 int32_t lv_anim_path_ease_in(const lv_anim_t * a)
228 {
229     /*Calculate the current step*/
230     uint32_t t = lv_map(a->act_time, 0, a->time, 0, LV_BEZIER_VAL_MAX);
231     int32_t step = lv_bezier3(t, 0, 50, 100, LV_BEZIER_VAL_MAX);
232 
233     int32_t new_value;
234     new_value = step * (a->end_value - a->start_value);
235     new_value = new_value >> LV_BEZIER_VAL_SHIFT;
236     new_value += a->start_value;
237 
238     return new_value;
239 }
240 
lv_anim_path_ease_out(const lv_anim_t * a)241 int32_t lv_anim_path_ease_out(const lv_anim_t * a)
242 {
243     /*Calculate the current step*/
244     uint32_t t = lv_map(a->act_time, 0, a->time, 0, LV_BEZIER_VAL_MAX);
245     int32_t step = lv_bezier3(t, 0, 900, 950, LV_BEZIER_VAL_MAX);
246 
247     int32_t new_value;
248     new_value = step * (a->end_value - a->start_value);
249     new_value = new_value >> LV_BEZIER_VAL_SHIFT;
250     new_value += a->start_value;
251 
252     return new_value;
253 }
254 
lv_anim_path_ease_in_out(const lv_anim_t * a)255 int32_t lv_anim_path_ease_in_out(const lv_anim_t * a)
256 {
257     /*Calculate the current step*/
258     uint32_t t = lv_map(a->act_time, 0, a->time, 0, LV_BEZIER_VAL_MAX);
259     int32_t step = lv_bezier3(t, 0, 50, 952, LV_BEZIER_VAL_MAX);
260 
261     int32_t new_value;
262     new_value = step * (a->end_value - a->start_value);
263     new_value = new_value >> LV_BEZIER_VAL_SHIFT;
264     new_value += a->start_value;
265 
266     return new_value;
267 }
268 
lv_anim_path_overshoot(const lv_anim_t * a)269 int32_t lv_anim_path_overshoot(const lv_anim_t * a)
270 {
271     /*Calculate the current step*/
272     uint32_t t = lv_map(a->act_time, 0, a->time, 0, LV_BEZIER_VAL_MAX);
273     int32_t step = lv_bezier3(t, 0, 1000, 1300, LV_BEZIER_VAL_MAX);
274 
275     int32_t new_value;
276     new_value = step * (a->end_value - a->start_value);
277     new_value = new_value >> LV_BEZIER_VAL_SHIFT;
278     new_value += a->start_value;
279 
280     return new_value;
281 }
282 
lv_anim_path_bounce(const lv_anim_t * a)283 int32_t lv_anim_path_bounce(const lv_anim_t * a)
284 {
285     /*Calculate the current step*/
286     int32_t t = lv_map(a->act_time, 0, a->time, 0, LV_BEZIER_VAL_MAX);
287     int32_t diff = (a->end_value - a->start_value);
288 
289     /*3 bounces has 5 parts: 3 down and 2 up. One part is t / 5 long*/
290 
291     if(t < 408) {
292         /*Go down*/
293         t = (t * 2500) >> LV_BEZIER_VAL_SHIFT; /*[0..1024] range*/
294     }
295     else if(t >= 408 && t < 614) {
296         /*First bounce back*/
297         t -= 408;
298         t    = t * 5; /*to [0..1024] range*/
299         t    = LV_BEZIER_VAL_MAX - t;
300         diff = diff / 20;
301     }
302     else if(t >= 614 && t < 819) {
303         /*Fall back*/
304         t -= 614;
305         t    = t * 5; /*to [0..1024] range*/
306         diff = diff / 20;
307     }
308     else if(t >= 819 && t < 921) {
309         /*Second bounce back*/
310         t -= 819;
311         t    = t * 10; /*to [0..1024] range*/
312         t    = LV_BEZIER_VAL_MAX - t;
313         diff = diff / 40;
314     }
315     else if(t >= 921 && t <= LV_BEZIER_VAL_MAX) {
316         /*Fall back*/
317         t -= 921;
318         t    = t * 10; /*to [0..1024] range*/
319         diff = diff / 40;
320     }
321 
322     if(t > LV_BEZIER_VAL_MAX) t = LV_BEZIER_VAL_MAX;
323     if(t < 0) t = 0;
324     int32_t step = lv_bezier3(t, LV_BEZIER_VAL_MAX, 800, 500, 0);
325 
326     int32_t new_value;
327     new_value = step * diff;
328     new_value = new_value >> LV_BEZIER_VAL_SHIFT;
329     new_value = a->end_value - new_value;
330 
331     return new_value;
332 }
333 
lv_anim_path_step(const lv_anim_t * a)334 int32_t lv_anim_path_step(const lv_anim_t * a)
335 {
336     if(a->act_time >= a->time)
337         return a->end_value;
338     else
339         return a->start_value;
340 }
341 
342 /**********************
343  *   STATIC FUNCTIONS
344  **********************/
345 
346 /**
347  * Periodically handle the animations.
348  * @param param unused
349  */
anim_timer(lv_timer_t * param)350 static void anim_timer(lv_timer_t * param)
351 {
352     LV_UNUSED(param);
353 
354     uint32_t elaps = lv_tick_elaps(last_timer_run);
355 
356     /*Flip the run round*/
357     anim_run_round = anim_run_round ? false : true;
358 
359     lv_anim_t * a = _lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
360 
361     while(a != NULL) {
362         /*It can be set by `lv_anim_del()` typically in `end_cb`. If set then an animation delete
363          * happened in `anim_ready_handler` which could make this linked list reading corrupt
364          * because the list is changed meanwhile
365          */
366         anim_list_changed = false;
367 
368         if(a->run_round != anim_run_round) {
369             a->run_round = anim_run_round; /*The list readying might be reset so need to know which anim has run already*/
370 
371             /*The animation will run now for the first time. Call `start_cb`*/
372             int32_t new_act_time = a->act_time + elaps;
373             if(!a->start_cb_called && a->act_time <= 0 && new_act_time >= 0) {
374                 if(a->early_apply == 0 && a->get_value_cb) {
375                     int32_t v_ofs = a->get_value_cb(a);
376                     a->start_value += v_ofs;
377                     a->end_value += v_ofs;
378                 }
379                 if(a->start_cb) a->start_cb(a);
380                 a->start_cb_called = 1;
381             }
382             a->act_time += elaps;
383             if(a->act_time >= 0) {
384                 if(a->act_time > a->time) a->act_time = a->time;
385 
386                 int32_t new_value;
387                 new_value = a->path_cb(a);
388 
389                 if(new_value != a->current_value) {
390                     a->current_value = new_value;
391                     /*Apply the calculated value*/
392                     if(a->exec_cb) a->exec_cb(a->var, new_value);
393                 }
394 
395                 /*If the time is elapsed the animation is ready*/
396                 if(a->act_time >= a->time) {
397                     anim_ready_handler(a);
398                 }
399             }
400         }
401 
402         /*If the linked list changed due to anim. delete then it's not safe to continue
403          *the reading of the list from here -> start from the head*/
404         if(anim_list_changed)
405             a = _lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
406         else
407             a = _lv_ll_get_next(&LV_GC_ROOT(_lv_anim_ll), a);
408     }
409 
410     last_timer_run = lv_tick_get();
411 }
412 
413 /**
414  * Called when an animation is ready to do the necessary thinks
415  * e.g. repeat, play back, delete etc.
416  * @param a pointer to an animation descriptor
417  */
anim_ready_handler(lv_anim_t * a)418 static void anim_ready_handler(lv_anim_t * a)
419 {
420     /*In the end of a forward anim decrement repeat cnt.*/
421     if(a->playback_now == 0 && a->repeat_cnt > 0 && a->repeat_cnt != LV_ANIM_REPEAT_INFINITE) {
422         a->repeat_cnt--;
423     }
424 
425     /*Delete the animation if
426      * - no repeat left and no play back (simple one shot animation)
427      * - no repeat, play back is enabled and play back is ready*/
428     if(a->repeat_cnt == 0 && (a->playback_time == 0 || a->playback_now == 1)) {
429 
430         /*Delete the animation from the list.
431          * This way the `ready_cb` will see the animations like it's animation is ready deleted*/
432         _lv_ll_remove(&LV_GC_ROOT(_lv_anim_ll), a);
433         /*Flag that the list has changed*/
434         anim_mark_list_change();
435 
436         /*Call the callback function at the end*/
437         if(a->ready_cb != NULL) a->ready_cb(a);
438         if(a->deleted_cb != NULL) a->deleted_cb(a);
439         lv_mem_free(a);
440     }
441     /*If the animation is not deleted then restart it*/
442     else {
443         a->act_time = -(int32_t)(a->repeat_delay); /*Restart the animation*/
444         /*Swap the start and end values in play back mode*/
445         if(a->playback_time != 0) {
446             /*If now turning back use the 'playback_pause*/
447             if(a->playback_now == 0) a->act_time = -(int32_t)(a->playback_delay);
448 
449             /*Toggle the play back state*/
450             a->playback_now = a->playback_now == 0 ? 1 : 0;
451             /*Swap the start and end values*/
452             int32_t tmp    = a->start_value;
453             a->start_value = a->end_value;
454             a->end_value   = tmp;
455             /*Swap the time and playback_time*/
456             tmp = a->time;
457             a->time = a->playback_time;
458             a->playback_time = tmp;
459         }
460     }
461 }
462 
anim_mark_list_change(void)463 static void anim_mark_list_change(void)
464 {
465     anim_list_changed = true;
466     if(_lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll)) == NULL)
467         lv_timer_pause(_lv_anim_tmr);
468     else
469         lv_timer_resume(_lv_anim_tmr);
470 }
471