1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00
lvgl/src/lv_misc/lv_anim.c

475 lines
13 KiB
C
Raw Normal View History

2017-11-23 20:42:14 +01:00
/**
* @file anim.c
*
*/
/*********************
* INCLUDES
*********************/
2017-11-27 17:48:54 +01:00
#include "lv_anim.h"
#if LV_USE_ANIMATION
2017-11-23 20:42:14 +01:00
#include <stddef.h>
#include <string.h>
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
2017-11-26 23:57:39 +01:00
#include "../lv_hal/lv_hal_tick.h"
2017-11-23 21:28:36 +01:00
#include "lv_task.h"
2017-11-23 20:42:14 +01:00
#include "lv_math.h"
#include "lv_gc.h"
#if defined(LV_GC_INCLUDE)
2019-04-04 07:15:40 +02:00
#include LV_GC_INCLUDE
#endif /* LV_ENABLE_GC */
2017-11-23 20:42:14 +01:00
/*********************
* DEFINES
*********************/
2019-04-04 07:15:40 +02:00
#define LV_ANIM_RESOLUTION 1024
#define LV_ANIM_RES_SHIFT 10
2017-11-23 20:42:14 +01:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static void anim_task(lv_task_t * param);
2017-11-23 21:28:36 +01:00
static bool anim_ready_handler(lv_anim_t * a);
2017-11-23 20:42:14 +01:00
/**********************
* STATIC VARIABLES
**********************/
static uint32_t last_task_run;
static bool anim_list_changed;
2017-11-23 20:42:14 +01:00
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Init. the animation module
*/
2019-05-15 06:15:12 +02:00
void lv_anim_core_init(void)
2017-11-23 20:42:14 +01:00
{
2019-01-18 22:18:56 +02:00
lv_ll_init(&LV_GC_ROOT(_lv_anim_ll), sizeof(lv_anim_t));
2018-02-15 13:37:23 +01:00
last_task_run = lv_tick_get();
lv_task_create(anim_task, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, NULL);
2017-11-23 20:42:14 +01:00
}
/**
* Initialize an animation variable.
* E.g.:
* lv_anim_t a;
* lv_anim_init(&a);
* lv_anim_set_...(&a);
* lv_anim_craete(&a);
* @param a pointer to an `lv_anim_t` variable to initialize
*/
void lv_anim_init(lv_anim_t * a)
{
memset(a, 0, sizeof(lv_anim_t));
2019-06-27 07:16:15 +02:00
a->time = 500;
a->start = 0;
a->end = 100;
2019-06-16 09:16:20 +02:00
a->path_cb = lv_anim_path_linear;
}
2017-11-23 20:42:14 +01:00
/**
* Create an animation
* @param a an initialized 'anim_t' variable. Not required after call.
2017-11-23 20:42:14 +01:00
*/
void lv_anim_create(lv_anim_t * a)
2017-11-23 20:42:14 +01:00
{
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("animation create started")
2017-11-23 20:42:14 +01:00
/* Do not let two animations for the same 'var' with the same 'fp'*/
2019-06-06 06:05:40 +02:00
if(a->exec_cb != NULL) lv_anim_del(a->var, a->exec_cb); /*fp == NULL would delete all animations of var*/
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
/*Add the new animation to the animation linked list*/
2019-01-18 22:18:56 +02:00
lv_anim_t * new_anim = lv_ll_ins_head(&LV_GC_ROOT(_lv_anim_ll));
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(new_anim);
if(new_anim == NULL) return;
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
/*Initialize the animation descriptor*/
a->playback_now = 0;
memcpy(new_anim, a, sizeof(lv_anim_t));
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
/*Set the start value*/
2019-05-27 15:10:15 +02:00
if(new_anim->exec_cb) new_anim->exec_cb(new_anim->var, new_anim->start);
2018-07-08 03:25:07 +02:00
/* Creating an animation changed the linked list.
* It's important if it happens in a ready callback. (see `anim_task`)*/
anim_list_changed = true;
2018-07-25 17:57:08 +02:00
2018-10-05 17:22:49 +02:00
LV_LOG_TRACE("animation created")
2017-11-23 20:42:14 +01:00
}
/**
2019-05-17 07:58:47 +02:00
* Delete an animation of a variable with a given animator function
2017-11-23 20:42:14 +01:00
* @param var pointer to variable
2019-05-17 07:58:47 +02:00
* @param exec_cb a function pointer which is animating 'var',
* or NULL to delete all the animations of 'var'
2017-11-23 20:42:14 +01:00
* @return true: at least 1 animation is deleted, false: no animation is deleted
*/
2019-06-12 23:10:54 +02:00
bool lv_anim_del(void * var, lv_anim_exec_xcb_t exec_cb)
2017-11-23 20:42:14 +01:00
{
2018-02-15 13:37:23 +01:00
lv_anim_t * a;
lv_anim_t * a_next;
bool del = false;
2019-04-04 07:15:40 +02:00
a = lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
2018-02-15 13:37:23 +01:00
while(a != NULL) {
/*'a' might be deleted, so get the next object while 'a' is valid*/
2019-01-18 22:18:56 +02:00
a_next = lv_ll_get_next(&LV_GC_ROOT(_lv_anim_ll), a);
2018-02-15 13:37:23 +01:00
2019-05-17 07:50:00 +02:00
if(a->var == var && (a->exec_cb == exec_cb || exec_cb == NULL)) {
2019-11-15 11:04:46 +01:00
lv_ll_remove(&LV_GC_ROOT(_lv_anim_ll), a);
2018-02-15 13:37:23 +01:00
lv_mem_free(a);
2019-04-04 07:15:40 +02:00
anim_list_changed = true; /*Read by `anim_task`. It need to know if a delete occurred in
the linked list*/
2018-02-15 13:37:23 +01:00
del = true;
}
a = a_next;
}
return del;
2017-11-23 20:42:14 +01:00
}
2018-11-09 12:36:38 +01:00
/**
* Get the number of currently running animations
* @return the number of running animations
*/
uint16_t lv_anim_count_running(void)
{
uint16_t cnt = 0;
lv_anim_t * a;
LV_LL_READ(LV_GC_ROOT(_lv_anim_ll), a) cnt++;
2018-11-09 12:36:38 +01:00
return cnt++;
}
2017-11-23 20:42:14 +01:00
/**
* Calculate the time of an animation with a given speed and the start and end values
* @param speed speed of animation in unit/sec
* @param start start value of the animation
* @param end end value of the animation
* @return the required time [ms] for the animation with the given parameters
*/
uint16_t lv_anim_speed_to_time(uint16_t speed, lv_anim_value_t start, lv_anim_value_t end)
2017-11-23 20:42:14 +01:00
{
2019-04-04 07:15:40 +02:00
int32_t d = LV_MATH_ABS((int32_t)start - end);
2018-04-17 14:11:26 +02:00
uint32_t time = (int32_t)((int32_t)(d * 1000) / speed);
if(time > UINT16_MAX) time = UINT16_MAX;
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
if(time == 0) {
time++;
}
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
return time;
2017-11-23 20:42:14 +01:00
}
/**
2017-12-17 01:54:09 +01:00
* Calculate the current value of an animation applying linear characteristic
* @param a pointer to an animation
* @return the current value to set
2017-11-23 20:42:14 +01:00
*/
lv_anim_value_t lv_anim_path_linear(const lv_anim_t * a)
2017-11-23 20:42:14 +01:00
{
2017-12-17 01:54:09 +01:00
/*Calculate the current step*/
uint32_t step;
if(a->time == a->act_time) {
2019-04-04 07:15:40 +02:00
step = LV_ANIM_RESOLUTION; /*Use the last value if the time fully elapsed*/
2019-06-06 06:05:40 +02:00
} else {
step = ((int32_t)a->act_time * LV_ANIM_RESOLUTION) / a->time;
}
2017-12-17 01:54:09 +01:00
2018-06-09 08:45:38 +02:00
/* Get the new value which will be proportional to `step`
* and the `start` and `end` values*/
2017-12-17 01:54:09 +01:00
int32_t new_value;
2019-04-04 07:15:40 +02:00
new_value = (int32_t)step * (a->end - a->start);
2017-12-17 01:54:09 +01:00
new_value = new_value >> LV_ANIM_RES_SHIFT;
new_value += a->start;
return (lv_anim_value_t)new_value;
2017-11-23 20:42:14 +01:00
}
2017-12-17 01:54:09 +01:00
2019-01-22 15:50:00 +01:00
/**
* Calculate the current value of an animation slowing down the start phase
* @param a pointer to an animation
* @return the current value to set
*/
lv_anim_value_t lv_anim_path_ease_in(const lv_anim_t * a)
2019-01-22 15:50:00 +01:00
{
/*Calculate the current step*/
uint32_t t;
2019-04-04 07:15:40 +02:00
if(a->time == a->act_time)
t = 1024;
else
t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
2019-01-22 15:50:00 +01:00
int32_t step = lv_bezier3(t, 0, 1, 1, 1024);
int32_t new_value;
2019-04-04 07:15:40 +02:00
new_value = (int32_t)step * (a->end - a->start);
2019-01-22 15:50:00 +01:00
new_value = new_value >> 10;
new_value += a->start;
return (lv_anim_value_t)new_value;
2019-01-22 15:50:00 +01:00
}
/**
* Calculate the current value of an animation slowing down the end phase
* @param a pointer to an animation
* @return the current value to set
*/
lv_anim_value_t lv_anim_path_ease_out(const lv_anim_t * a)
2019-01-22 15:50:00 +01:00
{
/*Calculate the current step*/
uint32_t t;
2019-04-04 07:15:40 +02:00
if(a->time == a->act_time)
t = 1024;
else
t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
2019-01-22 15:50:00 +01:00
int32_t step = lv_bezier3(t, 0, 1023, 1023, 1024);
int32_t new_value;
2019-04-04 07:15:40 +02:00
new_value = (int32_t)step * (a->end - a->start);
2019-01-22 15:50:00 +01:00
new_value = new_value >> 10;
new_value += a->start;
return (lv_anim_value_t)new_value;
2019-01-22 15:50:00 +01:00
}
2018-06-09 08:45:38 +02:00
/**
* Calculate the current value of an animation applying an "S" characteristic (cosine)
* @param a pointer to an animation
* @return the current value to set
*/
lv_anim_value_t lv_anim_path_ease_in_out(const lv_anim_t * a)
2018-06-09 08:45:38 +02:00
{
/*Calculate the current step*/
2018-06-19 09:49:58 +02:00
uint32_t t;
2019-04-04 07:15:40 +02:00
if(a->time == a->act_time)
t = 1024;
else
t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
2018-06-09 08:45:38 +02:00
2018-06-19 09:49:58 +02:00
int32_t step = lv_bezier3(t, 0, 100, 924, 1024);
2018-06-09 08:45:38 +02:00
2018-06-19 09:49:58 +02:00
int32_t new_value;
2019-04-04 07:15:40 +02:00
new_value = (int32_t)step * (a->end - a->start);
2018-06-19 09:49:58 +02:00
new_value = new_value >> 10;
new_value += a->start;
2018-06-09 08:45:38 +02:00
return (lv_anim_value_t)new_value;
2018-06-09 08:45:38 +02:00
}
2018-11-20 14:49:16 +01:00
/**
* Calculate the current value of an animation with overshoot at the end
* @param a pointer to an animation
* @return the current value to set
*/
lv_anim_value_t lv_anim_path_overshoot(const lv_anim_t * a)
2018-11-20 14:49:16 +01:00
{
/*Calculate the current step*/
uint32_t t;
2019-04-04 07:15:40 +02:00
if(a->time == a->act_time)
t = 1024;
else
t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
2018-11-20 14:49:16 +01:00
int32_t step = lv_bezier3(t, 0, 600, 1300, 1024);
int32_t new_value;
2019-04-04 07:15:40 +02:00
new_value = (int32_t)step * (a->end - a->start);
2018-11-20 14:49:16 +01:00
new_value = new_value >> 10;
new_value += a->start;
return (lv_anim_value_t)new_value;
2018-11-20 14:49:16 +01:00
}
2018-11-24 07:38:07 +01:00
/**
* Calculate the current value of an animation with 3 bounces
* @param a pointer to an animation
* @return the current value to set
*/
lv_anim_value_t lv_anim_path_bounce(const lv_anim_t * a)
2018-11-24 07:38:07 +01:00
{
/*Calculate the current step*/
uint32_t t;
2019-04-04 07:15:40 +02:00
if(a->time == a->act_time)
t = 1024;
else
t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
2018-11-24 07:38:07 +01:00
int32_t diff = (a->end - a->start);
/*3 bounces has 5 parts: 3 down and 2 up. One part is t / 5 long*/
2019-04-04 07:15:40 +02:00
if(t < 408) {
2018-11-24 07:38:07 +01:00
/*Go down*/
2019-04-04 07:15:40 +02:00
t = (t * 2500) >> 10; /*[0..1024] range*/
} else if(t >= 408 && t < 614) {
2018-11-24 07:38:07 +01:00
/*First bounce back*/
t -= 408;
2019-04-04 07:15:40 +02:00
t = t * 5; /*to [0..1024] range*/
t = 1024 - t;
2018-11-24 07:38:07 +01:00
diff = diff / 6;
2019-04-04 07:15:40 +02:00
} else if(t >= 614 && t < 819) {
2018-11-24 07:38:07 +01:00
/*Fall back*/
t -= 614;
2019-04-04 07:15:40 +02:00
t = t * 5; /*to [0..1024] range*/
2018-11-24 07:38:07 +01:00
diff = diff / 6;
2019-04-04 07:15:40 +02:00
} else if(t >= 819 && t < 921) {
2018-11-24 07:38:07 +01:00
/*Second bounce back*/
t -= 819;
2019-04-04 07:15:40 +02:00
t = t * 10; /*to [0..1024] range*/
t = 1024 - t;
2018-11-24 07:38:07 +01:00
diff = diff / 16;
2019-04-04 07:15:40 +02:00
} else if(t >= 921 && t <= 1024) {
2018-11-24 07:38:07 +01:00
/*Fall back*/
t -= 921;
2019-04-04 07:15:40 +02:00
t = t * 10; /*to [0..1024] range*/
2018-11-24 07:38:07 +01:00
diff = diff / 16;
}
if(t > 1024) t = 1024;
int32_t step = lv_bezier3(t, 1024, 1024, 800, 0);
int32_t new_value;
2019-04-04 07:15:40 +02:00
new_value = (int32_t)step * diff;
2018-11-24 07:38:07 +01:00
new_value = new_value >> 10;
new_value = a->end - new_value;
return (lv_anim_value_t)new_value;
2018-11-24 07:38:07 +01:00
}
2017-12-17 01:54:09 +01:00
/**
* Calculate the current value of an animation applying step characteristic.
* (Set end value on the end of the animation)
* @param a pointer to an animation
* @return the current value to set
*/
lv_anim_value_t lv_anim_path_step(const lv_anim_t * a)
2017-12-17 01:54:09 +01:00
{
2019-04-04 07:15:40 +02:00
if(a->act_time >= a->time)
return a->end;
else
return a->start;
2017-12-17 01:54:09 +01:00
}
2017-11-23 20:42:14 +01:00
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Periodically handle the animations.
* @param param unused
*/
static void anim_task(lv_task_t * param)
2017-11-23 20:42:14 +01:00
{
2017-12-02 20:43:50 +01:00
(void)param;
2018-02-15 13:37:23 +01:00
lv_anim_t * a;
2019-04-04 07:15:40 +02:00
LV_LL_READ(LV_GC_ROOT(_lv_anim_ll), a)
{
2018-10-05 17:22:49 +02:00
a->has_run = 0;
}
2017-11-23 20:42:14 +01:00
uint32_t elaps = lv_tick_elaps(last_task_run);
a = lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
2017-11-23 20:42:14 +01:00
while(a != NULL) {
2019-04-04 07:15:40 +02:00
/*It can be set by `lv_anim_del()` typically in `end_cb`. If set then an animation delete
* happened in `anim_ready_handler` which could make this linked list reading corrupt
* because the list is changed meanwhile
2018-10-05 17:22:49 +02:00
*/
anim_list_changed = false;
if(!a->has_run) {
2019-06-06 06:05:40 +02:00
a->has_run = 1; /*The list readying might be reseted so need to know which anim has run already*/
2018-10-05 17:22:49 +02:00
a->act_time += elaps;
if(a->act_time >= 0) {
if(a->act_time > a->time) a->act_time = a->time;
int32_t new_value;
2019-04-22 08:45:07 +02:00
new_value = a->path_cb(a);
2018-10-05 17:22:49 +02:00
/*Apply the calculated value*/
2019-05-27 15:10:15 +02:00
if(a->exec_cb) a->exec_cb(a->var, new_value);
2018-10-05 17:22:49 +02:00
/*If the time is elapsed the animation is ready*/
if(a->act_time >= a->time) {
anim_ready_handler(a);
}
}
}
/* If the linked list changed due to anim. delete then it's not safe to continue
* the reading of the list from here -> start from the head*/
2019-04-04 07:15:40 +02:00
if(anim_list_changed)
a = lv_ll_get_head(&LV_GC_ROOT(_lv_anim_ll));
else
a = lv_ll_get_next(&LV_GC_ROOT(_lv_anim_ll), a);
2018-02-15 13:37:23 +01:00
}
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
last_task_run = lv_tick_get();
2017-11-23 20:42:14 +01:00
}
/**
* Called when an animation is ready to do the necessary thinks
* e.g. repeat, play back, delete etc.
* @param a pointer to an animation descriptor
2019-01-18 22:18:56 +02:00
* @return true: animation delete occurred nnd the `LV_GC_ROOT(_lv_anim_ll)` has changed
2017-11-23 20:42:14 +01:00
* */
2017-11-23 21:28:36 +01:00
static bool anim_ready_handler(lv_anim_t * a)
2017-11-23 20:42:14 +01:00
{
2018-02-15 13:37:23 +01:00
/*Delete the animation if
* - no repeat and no play back (simple one shot animation)
* - no repeat, play back is enabled and play back is ready */
2019-06-06 06:05:40 +02:00
if((a->repeat == 0 && a->playback == 0) || (a->repeat == 0 && a->playback == 1 && a->playback_now == 1)) {
/*Create copy from the animation and delete the animation from the list.
* This way the `ready_cb` will see the animations like it's animation is ready deleted*/
lv_anim_t a_tmp;
memcpy(&a_tmp, a, sizeof(lv_anim_t));
2019-11-15 11:04:46 +01:00
lv_ll_remove(&LV_GC_ROOT(_lv_anim_ll), a);
2018-02-15 13:37:23 +01:00
lv_mem_free(a);
2018-10-15 19:00:03 +02:00
anim_list_changed = true;
2018-02-15 13:37:23 +01:00
/* Call the callback function at the end*/
if(a_tmp.ready_cb != NULL) a_tmp.ready_cb(&a_tmp);
2018-02-15 13:37:23 +01:00
}
/*If the animation is not deleted then restart it*/
else {
2019-04-04 07:15:40 +02:00
a->act_time = -a->repeat_pause; /*Restart the animation*/
2018-02-15 13:37:23 +01:00
/*Swap the start and end values in play back mode*/
if(a->playback != 0) {
/*If now turning back use the 'playback_pause*/
2019-04-04 07:15:40 +02:00
if(a->playback_now == 0) a->act_time = -a->playback_pause;
2018-02-15 13:37:23 +01:00
/*Toggle the play back state*/
2018-06-19 09:49:58 +02:00
a->playback_now = a->playback_now == 0 ? 1 : 0;
2018-02-15 13:37:23 +01:00
/*Swap the start and end values*/
int32_t tmp;
2019-04-04 07:15:40 +02:00
tmp = a->start;
2018-02-15 13:37:23 +01:00
a->start = a->end;
2019-04-04 07:15:40 +02:00
a->end = tmp;
2018-02-15 13:37:23 +01:00
}
}
return anim_list_changed;
2017-11-23 20:42:14 +01:00
}
2017-11-27 17:48:54 +01:00
#endif