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