1 /**
2  * @file anim.h
3  *
4  */
5 
6 #ifndef ANIM_H
7 #define ANIM_H
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 /*********************
14  *      INCLUDES
15  *********************/
16 #ifdef LV_CONF_INCLUDE_SIMPLE
17 #include "lv_conf.h"
18 #else
19 #include "../../lv_conf.h"
20 #endif
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include <string.h>
25 
26 /*********************
27  *      DEFINES
28  *********************/
29 
30 /**********************
31  *      TYPEDEFS
32  **********************/
33 
34 /** Can be used to indicate if animations are enabled or disabled in a case*/
35 enum {
36     LV_ANIM_OFF,
37     LV_ANIM_ON,
38 };
39 
40 typedef uint8_t lv_anim_enable_t;
41 
42 /** Type of the animated value*/
43 typedef lv_coord_t lv_anim_value_t;
44 
45 #if LV_USE_ANIMATION
46 
47 struct _lv_anim_t;
48 
49 /** Generic prototype of "animator" functions.
50  * First parameter is the variable to animate.
51  * Second parameter is the value to set.
52  * Compatible with `lv_xxx_set_yyy(obj, value)` functions
53  * The `x` in `_xcb_t` means its not a fully generic prototype because
54  * it doesn't receive `lv_anim_t *` as its first argument*/
55 typedef void (*lv_anim_exec_xcb_t)(void *, lv_anim_value_t);
56 
57 /** Same as `lv_anim_exec_xcb_t` but receives `lv_anim_t *` as the first parameter.
58  * It's more consistent but less convenient. Might be used by binding generator functions.*/
59 typedef void (*lv_anim_custom_exec_cb_t)(struct _lv_anim_t *, lv_anim_value_t);
60 
61 /** Get the current value during an animation*/
62 typedef lv_anim_value_t (*lv_anim_path_cb_t)(const struct _lv_anim_t *);
63 
64 /** Callback to call when the animation is ready*/
65 typedef void (*lv_anim_ready_cb_t)(struct _lv_anim_t *);
66 
67 /** Describes an animation*/
68 typedef struct _lv_anim_t
69 {
70     void * var;                  /**<Variable to animate*/
71     lv_anim_exec_xcb_t exec_cb;   /**< Function to execute to animate*/
72     lv_anim_path_cb_t path_cb;   /**< Function to get the steps of animations*/
73     lv_anim_ready_cb_t ready_cb; /**< Call it when the animation is ready*/
74     int32_t start;               /**< Start value*/
75     int32_t end;                 /**< End value*/
76     uint16_t time;               /**< Animation time in ms*/
77     int16_t act_time;            /**< Current time in animation. Set to negative to make delay.*/
78     uint16_t playback_pause;     /**< Wait before play back*/
79     uint16_t repeat_pause;       /**< Wait before repeat*/
80 #if LV_USE_USER_DATA
81     lv_anim_user_data_t user_data; /**< Custom user data*/
82 #endif
83 
84     uint8_t playback : 1; /**< When the animation is ready play it back*/
85     uint8_t repeat : 1;   /**< Repeat the animation infinitely*/
86     /*Animation system use these - user shouldn't set*/
87     uint8_t playback_now : 1; /**< Play back is in progress*/
88     uint32_t has_run : 1;     /**< Indicates the animation has run in this round*/
89 } lv_anim_t;
90 
91 
92 /**********************
93  * GLOBAL PROTOTYPES
94  **********************/
95 
96 /**
97  * Init. the animation module
98  */
99 void lv_anim_core_init(void);
100 
101 /**
102  * Initialize an animation variable.
103  * E.g.:
104  * lv_anim_t a;
105  * lv_anim_init(&a);
106  * lv_anim_set_...(&a);
107  * lv_anim_create(&a);
108  * @param a pointer to an `lv_anim_t` variable to initialize
109  */
110 void lv_anim_init(lv_anim_t * a);
111 
112 /**
113  * Set a variable to animate function to execute on `var`
114  * @param a pointer to an initialized `lv_anim_t` variable
115  * @param var pointer to a variable to animate
116  * @param exec_cb a function to execute.
117  *                LittelvGL's built-in functions can be used.
118  *                E.g. lv_obj_set_x
119  */
lv_anim_set_exec_cb(lv_anim_t * a,void * var,lv_anim_exec_xcb_t exec_cb)120 static inline void lv_anim_set_exec_cb(lv_anim_t * a, void * var, lv_anim_exec_xcb_t exec_cb)
121 {
122     a->var     = var;
123     a->exec_cb = exec_cb;
124 }
125 
126 /**
127  * Set the duration and delay of an animation
128  * @param a pointer to an initialized `lv_anim_t` variable
129  * @param duration duration of the animation in milliseconds
130  * @param delay delay before the animation in milliseconds
131  */
lv_anim_set_time(lv_anim_t * a,uint16_t duration,uint16_t delay)132 static inline void lv_anim_set_time(lv_anim_t * a, uint16_t duration, uint16_t delay)
133 {
134     a->time     = duration;
135     a->act_time = -delay;
136 }
137 
138 /**
139  * Set the start and end values of an animation
140  * @param a pointer to an initialized `lv_anim_t` variable
141  * @param start the start value
142  * @param end the end value
143  */
lv_anim_set_values(lv_anim_t * a,lv_anim_value_t start,lv_anim_value_t end)144 static inline void lv_anim_set_values(lv_anim_t * a, lv_anim_value_t start, lv_anim_value_t end)
145 {
146     a->start = start;
147     a->end   = end;
148 }
149 
150 /**
151  * Similar to `lv_anim_set_var_and_cb` but `lv_anim_custom_exec_cb_t` receives
152  * `lv_anim_t * ` as its first parameter instead of `void *`.
153  * This function might be used when LittlevGL is binded to other languages because
154  * it's more consistent to have `lv_anim_t *` as first parameter.
155  * @param a pointer to an initialized `lv_anim_t` variable
156  * @param exec_cb a function to execute.
157  */
lv_anim_set_custom_exec_cb(lv_anim_t * a,lv_anim_custom_exec_cb_t exec_cb)158 static inline void lv_anim_set_custom_exec_cb(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb)
159 {
160     a->var     = a;
161     a->exec_cb = (lv_anim_exec_xcb_t)exec_cb;
162 }
163 
164 /**
165  * Set the path (curve) of the animation.
166  * @param a pointer to an initialized `lv_anim_t` variable
167  * @param path_cb a function the get the current value of the animation.
168  *                The built in functions starts with `lv_anim_path_...`
169  */
lv_anim_set_path_cb(lv_anim_t * a,lv_anim_path_cb_t path_cb)170 static inline void lv_anim_set_path_cb(lv_anim_t * a, lv_anim_path_cb_t path_cb)
171 {
172     a->path_cb = path_cb;
173 }
174 
175 /**
176  * Set a function call when the animation is ready
177  * @param a pointer to an initialized `lv_anim_t` variable
178  * @param ready_cb a function call when the animation is ready
179  */
lv_anim_set_ready_cb(lv_anim_t * a,lv_anim_ready_cb_t ready_cb)180 static inline void lv_anim_set_ready_cb(lv_anim_t * a, lv_anim_ready_cb_t ready_cb)
181 {
182     a->ready_cb = ready_cb;
183 }
184 
185 /**
186  * Make the animation to play back to when the forward direction is ready
187  * @param a pointer to an initialized `lv_anim_t` variable
188  * @param wait_time time in milliseconds to wait before starting the back direction
189  */
lv_anim_set_playback(lv_anim_t * a,uint16_t wait_time)190 static inline void lv_anim_set_playback(lv_anim_t * a, uint16_t wait_time)
191 {
192     a->playback       = 1;
193     a->playback_pause = wait_time;
194 }
195 
196 /**
197  * Disable playback. (Disabled after `lv_anim_init()`)
198  * @param a pointer to an initialized `lv_anim_t` variable
199  */
lv_anim_clear_playback(lv_anim_t * a)200 static inline void lv_anim_clear_playback(lv_anim_t * a)
201 {
202     a->playback = 0;
203 }
204 
205 /**
206  * Make the animation to start again when ready.
207  * @param a pointer to an initialized `lv_anim_t` variable
208  * @param wait_time time in milliseconds to wait before starting the animation again
209  */
lv_anim_set_repeat(lv_anim_t * a,uint16_t wait_time)210 static inline void lv_anim_set_repeat(lv_anim_t * a, uint16_t wait_time)
211 {
212     a->repeat       = 1;
213     a->repeat_pause = wait_time;
214 }
215 
216 /**
217  * Disable repeat. (Disabled after `lv_anim_init()`)
218  * @param a pointer to an initialized `lv_anim_t` variable
219  */
lv_anim_clear_repeat(lv_anim_t * a)220 static inline void lv_anim_clear_repeat(lv_anim_t * a)
221 {
222     a->repeat = 0;
223 }
224 
225 /**
226  * Create an animation
227  * @param a an initialized 'anim_t' variable. Not required after call.
228  */
229 void lv_anim_create(lv_anim_t * a);
230 
231 /**
232  * Delete an animation of a variable with a given animator function
233  * @param var pointer to variable
234  * @param exec_cb a function pointer which is animating 'var',
235  *           or NULL to ignore it and delete all the animations of 'var
236  * @return true: at least 1 animation is deleted, false: no animation is deleted
237  */
238 bool lv_anim_del(void * var, lv_anim_exec_xcb_t exec_cb);
239 
240 /**
241  * Delete an aniamation by getting the animated variable from `a`.
242  * Only animations with `exec_cb` will be deleted.
243  * This function exist becasue it's logical that all anim functions receives an
244  * `lv_anim_t` as their first parameter. It's not practical in C but might makes
245  * the API more conequent and makes easier to genrate bindings.
246  * @param a pointer to an animation.
247  * @param exec_cb a function pointer which is animating 'var',
248  *           or NULL to ignore it and delete all the animations of 'var
249  * @return true: at least 1 animation is deleted, false: no animation is deleted
250  */
lv_anim_custom_del(lv_anim_t * a,lv_anim_custom_exec_cb_t exec_cb)251 static inline bool lv_anim_custom_del(lv_anim_t * a, lv_anim_custom_exec_cb_t exec_cb)
252 {
253     return lv_anim_del(a->var, (lv_anim_exec_xcb_t)exec_cb);
254 }
255 
256 /**
257  * Get the number of currently running animations
258  * @return the number of running animations
259  */
260 uint16_t lv_anim_count_running(void);
261 
262 /**
263  * Calculate the time of an animation with a given speed and the start and end values
264  * @param speed speed of animation in unit/sec
265  * @param start start value of the animation
266  * @param end end value of the animation
267  * @return the required time [ms] for the animation with the given parameters
268  */
269 uint16_t lv_anim_speed_to_time(uint16_t speed, lv_anim_value_t start, lv_anim_value_t end);
270 
271 /**
272  * Calculate the current value of an animation applying linear characteristic
273  * @param a pointer to an animation
274  * @return the current value to set
275  */
276 lv_anim_value_t lv_anim_path_linear(const lv_anim_t * a);
277 
278 /**
279  * Calculate the current value of an animation slowing down the start phase
280  * @param a pointer to an animation
281  * @return the current value to set
282  */
283 lv_anim_value_t lv_anim_path_ease_in(const lv_anim_t * a);
284 
285 /**
286  * Calculate the current value of an animation slowing down the end phase
287  * @param a pointer to an animation
288  * @return the current value to set
289  */
290 lv_anim_value_t lv_anim_path_ease_out(const lv_anim_t * a);
291 
292 /**
293  * Calculate the current value of an animation applying an "S" characteristic (cosine)
294  * @param a pointer to an animation
295  * @return the current value to set
296  */
297 lv_anim_value_t lv_anim_path_ease_in_out(const lv_anim_t * a);
298 
299 /**
300  * Calculate the current value of an animation with overshoot at the end
301  * @param a pointer to an animation
302  * @return the current value to set
303  */
304 lv_anim_value_t lv_anim_path_overshoot(const lv_anim_t * a);
305 
306 /**
307  * Calculate the current value of an animation with 3 bounces
308  * @param a pointer to an animation
309  * @return the current value to set
310  */
311 lv_anim_value_t lv_anim_path_bounce(const lv_anim_t * a);
312 
313 /**
314  * Calculate the current value of an animation applying step characteristic.
315  * (Set end value on the end of the animation)
316  * @param a pointer to an animation
317  * @return the current value to set
318  */
319 lv_anim_value_t lv_anim_path_step(const lv_anim_t * a);
320 
321 /**********************
322  *      MACROS
323  **********************/
324 
325 #endif /*LV_USE_ANIMATION == 0*/
326 
327 #ifdef __cplusplus
328 } /* extern "C" */
329 #endif
330 
331 #endif /*LV_ANIM_H*/
332