Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_misc/lv_anim.c
1476 views
1
/**
2
* @file anim.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_anim.h"
10
11
#if USE_LV_ANIMATION
12
#include <stddef.h>
13
#include <string.h>
14
#include "../lv_hal/lv_hal_tick.h"
15
#include "lv_task.h"
16
#include "lv_math.h"
17
#include "lv_gc.h"
18
19
#if defined(LV_GC_INCLUDE)
20
# include LV_GC_INCLUDE
21
#endif /* LV_ENABLE_GC */
22
23
24
/*********************
25
* DEFINES
26
*********************/
27
#define LV_ANIM_RESOLUTION 1024
28
#define LV_ANIM_RES_SHIFT 10
29
30
/**********************
31
* TYPEDEFS
32
**********************/
33
34
/**********************
35
* STATIC PROTOTYPES
36
**********************/
37
static void anim_task(void * param);
38
static bool anim_ready_handler(lv_anim_t * a);
39
40
/**********************
41
* STATIC VARIABLES
42
**********************/
43
static uint32_t last_task_run;
44
static bool anim_list_changed;
45
46
/**********************
47
* MACROS
48
**********************/
49
50
/**********************
51
* GLOBAL FUNCTIONS
52
**********************/
53
54
/**
55
* Init. the animation module
56
*/
57
void lv_anim_init(void)
58
{
59
lv_ll_init(&LV_GC_ROOT(_lv_anim_ll), sizeof(lv_anim_t));
60
last_task_run = lv_tick_get();
61
lv_task_create(anim_task, LV_REFR_PERIOD, LV_TASK_PRIO_MID, NULL);
62
}
63
64
/**
65
* Create an animation
66
* @param anim_p an initialized 'anim_t' variable. Not required after call.
67
*/
68
void lv_anim_create(lv_anim_t * anim_p)
69
{
70
LV_LOG_TRACE("animation create started")
71
/* Do not let two animations for the same 'var' with the same 'fp'*/
72
if(anim_p->fp != NULL) lv_anim_del(anim_p->var, anim_p->fp); /*fp == NULL would delete all animations of var*/
73
74
/*Add the new animation to the animation linked list*/
75
lv_anim_t * new_anim = lv_ll_ins_head(&LV_GC_ROOT(_lv_anim_ll));
76
lv_mem_assert(new_anim);
77
if(new_anim == NULL) return;
78
79
/*Initialize the animation descriptor*/
80
anim_p->playback_now = 0;
81
memcpy(new_anim, anim_p, sizeof(lv_anim_t));
82
83
/*Set the start value*/
84
if(new_anim->fp != NULL) new_anim->fp(new_anim->var, new_anim->start);
85
86
/* Creating an animation changed the linked list.
87
* It's important if it happens in a ready callback. (see `anim_task`)*/
88
anim_list_changed = true;
89
90
LV_LOG_TRACE("animation created")
91
}
92
93
/**
94
* Delete an animation for a variable with a given animator function
95
* @param var pointer to variable
96
* @param fp a function pointer which is animating 'var',
97
* or NULL to delete all animations of 'var'
98
* @return true: at least 1 animation is deleted, false: no animation is deleted
99
*/
100
bool lv_anim_del(void * var, lv_anim_fp_t fp)
101
{
102
lv_anim_t * a;
103
lv_anim_t * a_next;
104
bool del = false;
105
a = lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
106
while(a != NULL) {
107
/*'a' might be deleted, so get the next object while 'a' is valid*/
108
a_next = lv_ll_get_next(&LV_GC_ROOT(_lv_anim_ll), a);
109
110
if(a->var == var && (a->fp == fp || fp == NULL)) {
111
lv_ll_rem(&LV_GC_ROOT(_lv_anim_ll), a);
112
lv_mem_free(a);
113
anim_list_changed = true; /*Read by `anim_task`. It need to know if a delete occurred in the linked list*/
114
del = true;
115
}
116
117
a = a_next;
118
}
119
120
return del;
121
}
122
123
/**
124
* Get the number of currently running animations
125
* @return the number of running animations
126
*/
127
uint16_t lv_anim_count_running(void)
128
{
129
uint16_t cnt = 0;
130
lv_anim_t * a;
131
LL_READ(LV_GC_ROOT(_lv_anim_ll), a) cnt++;
132
133
return cnt++;
134
}
135
136
/**
137
* Calculate the time of an animation with a given speed and the start and end values
138
* @param speed speed of animation in unit/sec
139
* @param start start value of the animation
140
* @param end end value of the animation
141
* @return the required time [ms] for the animation with the given parameters
142
*/
143
uint16_t lv_anim_speed_to_time(uint16_t speed, int32_t start, int32_t end)
144
{
145
int32_t d = LV_MATH_ABS((int32_t) start - end);
146
uint32_t time = (int32_t)((int32_t)(d * 1000) / speed);
147
148
if(time > UINT16_MAX) time = UINT16_MAX;
149
150
if(time == 0) {
151
time++;
152
}
153
154
return time;
155
}
156
157
/**
158
* Calculate the current value of an animation applying linear characteristic
159
* @param a pointer to an animation
160
* @return the current value to set
161
*/
162
int32_t lv_anim_path_linear(const lv_anim_t * a)
163
{
164
/*Calculate the current step*/
165
uint16_t step;
166
if(a->time == a->act_time) step = LV_ANIM_RESOLUTION; /*Use the last value if the time fully elapsed*/
167
else step = (a->act_time * LV_ANIM_RESOLUTION) / a->time;
168
169
/* Get the new value which will be proportional to `step`
170
* and the `start` and `end` values*/
171
int32_t new_value;
172
new_value = (int32_t) step * (a->end - a->start);
173
new_value = new_value >> LV_ANIM_RES_SHIFT;
174
new_value += a->start;
175
176
return new_value;
177
}
178
179
/**
180
* Calculate the current value of an animation slowing down the start phase
181
* @param a pointer to an animation
182
* @return the current value to set
183
*/
184
int32_t lv_anim_path_ease_in(const lv_anim_t * a)
185
{
186
/*Calculate the current step*/
187
uint32_t t;
188
if(a->time == a->act_time) t = 1024;
189
else t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
190
191
int32_t step = lv_bezier3(t, 0, 1, 1, 1024);
192
193
int32_t new_value;
194
new_value = (int32_t) step * (a->end - a->start);
195
new_value = new_value >> 10;
196
new_value += a->start;
197
198
199
return new_value;
200
}
201
202
/**
203
* Calculate the current value of an animation slowing down the end phase
204
* @param a pointer to an animation
205
* @return the current value to set
206
*/
207
int32_t lv_anim_path_ease_out(const lv_anim_t * a)
208
{
209
/*Calculate the current step*/
210
211
uint32_t t;
212
if(a->time == a->act_time) t = 1024;
213
else t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
214
215
int32_t step = lv_bezier3(t, 0, 1023, 1023, 1024);
216
217
int32_t new_value;
218
new_value = (int32_t) step * (a->end - a->start);
219
new_value = new_value >> 10;
220
new_value += a->start;
221
222
223
return new_value;
224
}
225
226
/**
227
* Calculate the current value of an animation applying an "S" characteristic (cosine)
228
* @param a pointer to an animation
229
* @return the current value to set
230
*/
231
int32_t lv_anim_path_ease_in_out(const lv_anim_t * a)
232
{
233
/*Calculate the current step*/
234
235
uint32_t t;
236
if(a->time == a->act_time) t = 1024;
237
else t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
238
239
int32_t step = lv_bezier3(t, 0, 100, 924, 1024);
240
241
int32_t new_value;
242
new_value = (int32_t) step * (a->end - a->start);
243
new_value = new_value >> 10;
244
new_value += a->start;
245
246
247
return new_value;
248
}
249
250
/**
251
* Calculate the current value of an animation with overshoot at the end
252
* @param a pointer to an animation
253
* @return the current value to set
254
*/
255
int32_t lv_anim_path_overshoot(const lv_anim_t * a)
256
{
257
/*Calculate the current step*/
258
259
uint32_t t;
260
if(a->time == a->act_time) t = 1024;
261
else t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
262
263
int32_t step = lv_bezier3(t, 0, 600, 1300, 1024);
264
265
int32_t new_value;
266
new_value = (int32_t) step * (a->end - a->start);
267
new_value = new_value >> 10;
268
new_value += a->start;
269
270
271
return new_value;
272
}
273
274
/**
275
* Calculate the current value of an animation with 3 bounces
276
* @param a pointer to an animation
277
* @return the current value to set
278
*/
279
int32_t lv_anim_path_bounce(const lv_anim_t * a)
280
{
281
/*Calculate the current step*/
282
uint32_t t;
283
if(a->time == a->act_time) t = 1024;
284
else t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
285
286
int32_t diff = (a->end - a->start);
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) >> 10; /*[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 = 1024 - t;
299
diff = diff / 6;
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 / 6;
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 = 1024 - t;
312
diff = diff / 16;
313
}
314
else if(t >= 921 && t <= 1024) {
315
/*Fall back*/
316
t -= 921;
317
t = t * 10; /*to [0..1024] range*/
318
diff = diff / 16;
319
}
320
321
if(t > 1024) t = 1024;
322
323
int32_t step = lv_bezier3(t, 1024, 1024, 800, 0);
324
325
int32_t new_value;
326
327
new_value = (int32_t) step * diff;
328
new_value = new_value >> 10;
329
new_value = a->end - new_value;
330
331
332
return new_value;
333
}
334
335
/**
336
* Calculate the current value of an animation applying step characteristic.
337
* (Set end value on the end of the animation)
338
* @param a pointer to an animation
339
* @return the current value to set
340
*/
341
int32_t lv_anim_path_step(const lv_anim_t * a)
342
{
343
if(a->act_time >= a->time) return a->end;
344
else return a->start;
345
}
346
347
/**********************
348
* STATIC FUNCTIONS
349
**********************/
350
351
/**
352
* Periodically handle the animations.
353
* @param param unused
354
*/
355
static void anim_task(void * param)
356
{
357
(void)param;
358
359
lv_anim_t * a;
360
LL_READ(LV_GC_ROOT(_lv_anim_ll), a) {
361
a->has_run = 0;
362
}
363
364
uint32_t elaps = lv_tick_elaps(last_task_run);
365
a = lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
366
367
while(a != NULL) {
368
/*It can be set by `lv_anim_del()` typically in `end_cb`. If set then an animation delete happened in `anim_ready_handler`
369
* which could make this linked list reading corrupt because the list is changed meanwhile
370
*/
371
anim_list_changed = false;
372
373
if(!a->has_run) {
374
a->has_run = 1; /*The list readying might be reseted so need to know which anim has run already*/
375
a->act_time += elaps;
376
if(a->act_time >= 0) {
377
if(a->act_time > a->time) a->act_time = a->time;
378
379
int32_t new_value;
380
new_value = a->path(a);
381
382
if(a->fp != NULL) a->fp(a->var, new_value); /*Apply the calculated value*/
383
384
/*If the time is elapsed the animation is ready*/
385
if(a->act_time >= a->time) {
386
anim_ready_handler(a);
387
}
388
}
389
}
390
391
/* If the linked list changed due to anim. delete then it's not safe to continue
392
* the reading of the list from here -> start from the head*/
393
if(anim_list_changed) a = lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
394
else a = lv_ll_get_next(&LV_GC_ROOT(_lv_anim_ll), a);
395
}
396
397
last_task_run = lv_tick_get();
398
}
399
400
/**
401
* Called when an animation is ready to do the necessary thinks
402
* e.g. repeat, play back, delete etc.
403
* @param a pointer to an animation descriptor
404
* @return true: animation delete occurred nnd the `LV_GC_ROOT(_lv_anim_ll)` has changed
405
* */
406
static bool anim_ready_handler(lv_anim_t * a)
407
{
408
409
/*Delete the animation if
410
* - no repeat and no play back (simple one shot animation)
411
* - no repeat, play back is enabled and play back is ready */
412
if((a->repeat == 0 && a->playback == 0) ||
413
(a->repeat == 0 && a->playback == 1 && a->playback_now == 1)) {
414
void (*cb)(void *) = a->end_cb;
415
void * p = a->var;
416
lv_ll_rem(&LV_GC_ROOT(_lv_anim_ll), a);
417
lv_mem_free(a);
418
anim_list_changed = true;
419
420
/* Call the callback function at the end*/
421
/* Check if an animation is deleted in the cb function
422
* if yes then the caller function has to know this*/
423
if(cb != NULL) cb(p);
424
}
425
/*If the animation is not deleted then restart it*/
426
else {
427
a->act_time = - a->repeat_pause; /*Restart the animation*/
428
/*Swap the start and end values in play back mode*/
429
if(a->playback != 0) {
430
/*If now turning back use the 'playback_pause*/
431
if(a->playback_now == 0) a->act_time = - a->playback_pause;
432
433
/*Toggle the play back state*/
434
a->playback_now = a->playback_now == 0 ? 1 : 0;
435
/*Swap the start and end values*/
436
int32_t tmp;
437
tmp = a->start;
438
a->start = a->end;
439
a->end = tmp;
440
}
441
}
442
443
return anim_list_changed;
444
}
445
#endif
446
447