add bezier solver function and combine math and trigo files

This commit is contained in:
Gabor Kiss-Vamosi
2018-06-11 10:30:06 +02:00
parent c858a13aa3
commit df226053cf
6 changed files with 108 additions and 148 deletions

View File

@@ -155,28 +155,25 @@ int32_t lv_anim_path_linear(const lv_anim_t *a)
* @param a pointer to an animation
* @return the current value to set
*/
int32_t lv_anim_path_momentum(const lv_anim_t *a)
int32_t lv_anim_path_ease_in_out(const lv_anim_t *a)
{
/*Calculate the current step*/
int16_t angle;
if(a->time == a->act_time) angle = 180;
else angle = (int32_t)((int32_t)a->act_time * 180) / a->time;
uint32_t t;
if(a->time == a->act_time) t = 1024;
else t = (uint32_t)((uint32_t)a->act_time * 1024) / a->time;
int32_t step = lv_trigo_sin(angle - 90) + LV_TRIGO_SIN_MAX;
int32_t step = lv_bezier3(t, 0, 100, 924, 1024);
// printf("t: %d, v: %d\n", t, step);
int32_t new_value;
new_value = (int32_t) step * (a->end - a->start);
new_value = new_value >> 10;
new_value += a->start;
/* Get the new value which will be proportional to `step`
* and the `start` and `end` values*/
int32_t new_value;
new_value = (int32_t) step * (a->end - a->start);
new_value = new_value >> 16;
new_value += a->start;
// printf("angle: %d, val: %d\n", angle, new_value);
return new_value;
return new_value;
}
/**

View File

@@ -114,7 +114,7 @@ int32_t lv_anim_path_linear(const lv_anim_t *a);
* @param a pointer to an animation
* @return the current value to set
*/
int32_t lv_anim_path_momentum(const lv_anim_t *a);
int32_t lv_anim_path_ease_in_out(const lv_anim_t *a);
/**
* Calculate the current value of an animation applying step characteristic.

View File

@@ -24,6 +24,19 @@
/**********************
* STATIC VARIABLES
**********************/
static int16_t sin0_90_table[] =
{
0, 572, 1144, 1715, 2286, 2856, 3425, 3993, 4560, 5126,
5690, 6252, 6813, 7371, 7927, 8481, 9032, 9580, 10126, 10668,
11207, 11743, 12275, 12803, 13328, 13848, 14364, 14876, 15383, 15886,
16383, 16876, 17364, 17846, 18323, 18794, 19260, 19720, 20173, 20621,
21062, 21497, 21925, 22347, 22762, 23170, 23571, 23964, 24351, 24730,
25101, 25465, 25821, 26169, 26509, 26841, 27165, 27481, 27788, 28087,
28377, 28659, 28932, 29196, 29451, 29697, 29934, 30162, 30381, 30591,
30791, 30982, 31163, 31335, 31498, 31650, 31794, 31927, 32051, 32165,
32269, 32364, 32448, 32523, 32587, 32642, 32687, 32722, 32747, 32762
};
/**********************
* MACROS
@@ -90,6 +103,63 @@ char * lv_math_num_to_str(int32_t num, char * buf)
return buf_ori;
}
/**
* Return with sinus of an angle
* @param angle
* @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
*/
int16_t lv_trigo_sin(int16_t angle)
{
int16_t ret = 0;
angle = angle % 360;
if(angle < 0) angle = 360 + angle;
if(angle < 90){
ret = sin0_90_table[angle];
} else if(angle >= 90 && angle < 180)
{
angle = 179 - angle;
ret = sin0_90_table[angle];
} else if(angle >= 180 && angle < 270)
{
angle = angle - 180;
ret = - sin0_90_table[angle];
} else { /*angle >=270*/
angle = 359 - angle;
ret = - sin0_90_table[angle];
}
return ret;
}
/**
* Calculate a value of a Cubic Bezier function.
* @param t time in range of [0..LV_BEZIER_VAL_MAX]
* @param u0 start values in range of [0..LV_BEZIER_VAL_MAX]
* @param u1 control value 1 values in range of [0..LV_BEZIER_VAL_MAX]
* @param u2 control value 2 in range of [0..LV_BEZIER_VAL_MAX]
* @param u3 end values in range of [0..LV_BEZIER_VAL_MAX]
* @return the value calculated from the given parameters in range of [0..LV_BEZIER_VAL_MAX]
*/
int32_t lv_bezier3(uint32_t t, int32_t u0, int32_t u1, int32_t u2, int32_t u3)
{
uint32_t t_rem = 1024 - t;
uint32_t t_rem2 = (t_rem * t_rem) >> 10;
uint32_t t_rem3 = (t_rem2 * t_rem) >> 10;
uint32_t t2 = (t * t) >> 10;
uint32_t t3 = (t2 * t) >> 10;
uint32_t v1 = ((uint32_t)t_rem3 * u0) >> 10;
uint32_t v2 = ((uint32_t)3 * t_rem2 * t * u1) >> 20;
uint32_t v3 = ((uint32_t)3 * t_rem * t2 * u2) >> 20;
uint32_t v4 = ((uint32_t)t3 * u3) >> 10;
return v1 + v2 + v3 + v4;
}
/**********************
* STATIC FUNCTIONS
**********************/

View File

@@ -19,11 +19,16 @@ extern "C" {
/*********************
* DEFINES
*********************/
#define LV_MATH_MIN(a,b) (a<b?a:b)
#define LV_MATH_MAX(a,b) (a>b?a:b)
#define LV_MATH_ABS(x) ((x)>0?(x):(-(x)))
#define LV_TRIGO_SIN_MAX 32767
#define LV_TRIGO_SHIFT 15 /* >> LV_TRIGO_SHIFT to normalize*/
#define LV_BEZIER_VAL_MAX 1024 /*Max time in Bezier functions (not [0..1] to use integers) */
#define LV_BEZIER_VAL_SHIFT 10 /*log2(LV_BEZIER_VAL_MAX): used to normalize up scaled values*/
/**********************
* TYPEDEFS
**********************/
@@ -39,6 +44,24 @@ extern "C" {
*/
char * lv_math_num_to_str(int32_t num, char * buf);
/**
* Return with sinus of an angle
* @param angle
* @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
*/
int16_t lv_trigo_sin(int16_t angle);
/**
* Calculate a value of a Cubic Bezier function.
* @param t time in range of [0..LV_BEZIER_VAL_MAX]
* @param u0 start values in range of [0..LV_BEZIER_VAL_MAX]
* @param u1 control value 1 values in range of [0..LV_BEZIER_VAL_MAX]
* @param u2 control value 2 in range of [0..LV_BEZIER_VAL_MAX]
* @param u3 end values in range of [0..LV_BEZIER_VAL_MAX]
* @return the value calculated from the given parameters in range of [0..LV_BEZIER_VAL_MAX]
*/
int32_t lv_bezier3(uint32_t t, int32_t u0, int32_t u1, int32_t u2, int32_t u3);
/**********************
* MACROS
**********************/

View File

@@ -1,82 +0,0 @@
/**
* @file lv_trigo.c
* Basic trigonometric integer functions
*/
/*********************
* INCLUDES
*********************/
#include "lv_trigo.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
/**********************
* STATIC VARIABLES
**********************/
static int16_t sin0_90_table[] =
{
0, 572, 1144, 1715, 2286, 2856, 3425, 3993, 4560, 5126,
5690, 6252, 6813, 7371, 7927, 8481, 9032, 9580, 10126, 10668,
11207, 11743, 12275, 12803, 13328, 13848, 14364, 14876, 15383, 15886,
16383, 16876, 17364, 17846, 18323, 18794, 19260, 19720, 20173, 20621,
21062, 21497, 21925, 22347, 22762, 23170, 23571, 23964, 24351, 24730,
25101, 25465, 25821, 26169, 26509, 26841, 27165, 27481, 27788, 28087,
28377, 28659, 28932, 29196, 29451, 29697, 29934, 30162, 30381, 30591,
30791, 30982, 31163, 31335, 31498, 31650, 31794, 31927, 32051, 32165,
32269, 32364, 32448, 32523, 32587, 32642, 32687, 32722, 32747, 32762
};
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Return with sinus of an angle
* @param angle
* @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
*/
int16_t lv_trigo_sin(int16_t angle)
{
int16_t ret = 0;
angle = angle % 360;
if(angle < 0) angle = 360 + angle;
if(angle < 90){
ret = sin0_90_table[angle];
} else if(angle >= 90 && angle < 180)
{
angle = 179 - angle;
ret = sin0_90_table[angle];
} else if(angle >= 180 && angle < 270)
{
angle = angle - 180;
ret = - sin0_90_table[angle];
} else { /*angle >=270*/
angle = 359 - angle;
ret = - sin0_90_table[angle];
}
return ret;
}
/**********************
* STATIC FUNCTIONS
**********************/

View File

@@ -1,48 +0,0 @@
/**
* @file lv_trig.h
* Basic trigonometric integer functions
*/
#ifndef LV_TRIGO_H
#define LV_TRIGO_H
#ifdef __cplusplus
extern "C" {
#endif
/*********************
* INCLUDES
*********************/
#include <stdint.h>
/*********************
* DEFINES
*********************/
#define LV_TRIGO_SIN_MAX 32767
#define LV_TRIGO_SHIFT 15 /* >> LV_TRIGO_SHIFT to normalize*/
/**********************
* TYPEDEFS
**********************/
/**********************
* GLOBAL PROTOTYPES
**********************/
/**
* Return with sinus of an angle
* @param angle
* @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
*/
int16_t lv_trigo_sin(int16_t angle);
/**********************
* MACROS
**********************/
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif