1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00
lvgl/lv_misc/lv_anim.c

278 lines
7.3 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 USE_LV_ANIMATION
2017-11-23 20:42:14 +01:00
#include <stddef.h>
#include <string.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"
/*********************
* DEFINES
*********************/
2018-06-09 08:45:38 +02:00
#define LV_ANIM_RESOLUTION 1024
2017-12-17 01:54:09 +01:00
#define LV_ANIM_RES_SHIFT 10
2017-11-23 20:42:14 +01:00
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
2018-06-19 09:49:58 +02:00
static void anim_task(void * 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
**********************/
2017-11-24 17:48:47 +01:00
static lv_ll_t anim_ll;
2017-11-23 20:42:14 +01:00
static uint32_t last_task_run;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Init. the animation module
*/
2017-11-23 21:28:36 +01:00
void lv_anim_init(void)
2017-11-23 20:42:14 +01:00
{
2018-02-15 13:37:23 +01:00
lv_ll_init(&anim_ll, sizeof(lv_anim_t));
last_task_run = lv_tick_get();
lv_task_create(anim_task, LV_REFR_PERIOD, LV_TASK_PRIO_MID, NULL);
2017-11-23 20:42:14 +01:00
}
/**
* Create an animation
* @param anim_p an initialized 'anim_t' variable. Not required after call.
*/
2017-11-23 21:28:36 +01:00
void lv_anim_create(lv_anim_t * anim_p)
2017-11-23 20:42:14 +01:00
{
/* Do not let two animations for the same 'var' with the same 'fp'*/
2017-11-23 21:28:36 +01:00
if(anim_p->fp != NULL) lv_anim_del(anim_p->var, anim_p->fp); /*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*/
lv_anim_t * new_anim = lv_ll_ins_head(&anim_ll);
lv_mem_assert(new_anim);
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
/*Initialize the animation descriptor*/
anim_p->playback_now = 0;
memcpy(new_anim, anim_p, sizeof(lv_anim_t));
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
/*Set the start value*/
if(new_anim->fp != NULL) new_anim->fp(new_anim->var, new_anim->start);
2017-11-23 20:42:14 +01:00
}
/**
* Delete an animation for a variable with a given animator function
2017-11-23 20:42:14 +01:00
* @param var pointer to variable
* @param fp a function pointer which is animating 'var',
* or NULL to delete all animations of 'var'
* @return true: at least 1 animation is deleted, false: no animation is deleted
*/
2017-11-23 21:28:36 +01:00
bool lv_anim_del(void * var, lv_anim_fp_t fp)
2017-11-23 20:42:14 +01:00
{
2018-02-15 13:37:23 +01:00
bool del = false;
lv_anim_t * a;
lv_anim_t * a_next;
a = lv_ll_get_head(&anim_ll);
while(a != NULL) {
/*'a' might be deleted, so get the next object while 'a' is valid*/
a_next = lv_ll_get_next(&anim_ll, a);
if(a->var == var && (a->fp == fp || fp == NULL)) {
lv_ll_rem(&anim_ll, a);
lv_mem_free(a);
del = true;
}
a = a_next;
}
return del;
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
*/
2017-11-23 21:28:36 +01:00
uint16_t lv_anim_speed_to_time(uint16_t speed, int32_t start, int32_t end)
2017-11-23 20:42:14 +01:00
{
2018-02-15 13:37:23 +01: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
*/
2018-06-19 09:49:58 +02:00
int32_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*/
uint16_t step;
2018-06-09 08:45:38 +02:00
if(a->time == a->act_time) step = LV_ANIM_RESOLUTION; /*Use the last value if the time fully elapsed*/
2017-12-17 01:54:09 +01:00
else step = (a->act_time * LV_ANIM_RESOLUTION) / a->time;
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;
2018-06-19 09:49:58 +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 new_value;
2017-11-23 20:42:14 +01:00
}
2017-12-17 01:54:09 +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
*/
2018-06-19 09:49:58 +02:00
int32_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;
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;
new_value = (int32_t) step * (a->end - a->start);
new_value = new_value >> 10;
new_value += a->start;
2018-06-09 08:45:38 +02:00
2018-06-19 09:49:58 +02:00
return new_value;
2018-06-09 08:45:38 +02: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
*/
2018-06-19 09:49:58 +02:00
int32_t lv_anim_path_step(const lv_anim_t * a)
2017-12-17 01:54:09 +01:00
{
if(a->act_time >= a->time) return a->end;
else return a->start;
}
2017-11-23 20:42:14 +01:00
/**********************
* STATIC FUNCTIONS
**********************/
/**
* Periodically handle the animations.
* @param param unused
*/
2018-06-19 09:49:58 +02:00
static void anim_task(void * 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
volatile uint32_t elaps;
elaps = lv_tick_elaps(last_task_run);
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;
a = lv_ll_get_head(&anim_ll);
while(a != NULL) {
/*'a' might be deleted, so get the next object while 'a' is valid*/
a_next = lv_ll_get_next(&anim_ll, a);
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
a->act_time += elaps;
if(a->act_time >= 0) {
if(a->act_time > a->time) a->act_time = a->time;
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
int32_t new_value;
2017-12-17 01:54:09 +01:00
new_value = a->path(a);
2018-02-15 13:37:23 +01:00
if(a->fp != NULL) a->fp(a->var, new_value); /*Apply the calculated value*/
2017-11-23 20:42:14 +01:00
2018-02-15 13:37:23 +01:00
/*If the time is elapsed the animation is ready*/
if(a->act_time >= a->time) {
anim_ready_handler(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
a = a_next;
}
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
* @return true: animation delete occurred
* */
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
bool invalid = false;
/*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 */
if((a->repeat == 0 && a->playback == 0) ||
(a->repeat == 0 && a->playback == 1 && a->playback_now == 1)) {
2018-06-19 09:49:58 +02:00
void (*cb)(void *) = a->end_cb;
2018-02-15 13:37:23 +01:00
void * p = a->var;
lv_ll_rem(&anim_ll, a);
lv_mem_free(a);
/*Call the callback function at the end*/
/* Check if an animation is deleted in the cb function
* if yes then the caller function has to know this*/
if(cb != NULL) cb(p);
invalid = true;
}
/*If the animation is not deleted then restart it*/
else {
a->act_time = - a->repeat_pause; /*Restart the animation*/
/*Swap the start and end values in play back mode*/
if(a->playback != 0) {
/*If now turning back use the 'playback_pause*/
if(a->playback_now == 0) a->act_time = - a->playback_pause;
/*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;
tmp = a->start;
a->start = a->end;
a->end = tmp;
}
}
return invalid;
2017-11-23 20:42:14 +01:00
}
2017-11-27 17:48:54 +01:00
#endif