1 /**
2 * @file lv_math.c
3 *
4 */
5
6 /*********************
7 * INCLUDES
8 *********************/
9 #include "lv_math.h"
10 #include <stdbool.h>
11 #include <stdlib.h>
12
13 /*********************
14 * DEFINES
15 *********************/
16
17 /**********************
18 * TYPEDEFS
19 **********************/
20
21 /**********************
22 * STATIC PROTOTYPES
23 **********************/
24
25 /**********************
26 * STATIC VARIABLES
27 **********************/
28 static int16_t sin0_90_table[] = {
29 0, 572, 1144, 1715, 2286, 2856, 3425, 3993, 4560, 5126, 5690, 6252, 6813, 7371, 7927, 8481,
30 9032, 9580, 10126, 10668, 11207, 11743, 12275, 12803, 13328, 13848, 14364, 14876, 15383, 15886, 16383, 16876,
31 17364, 17846, 18323, 18794, 19260, 19720, 20173, 20621, 21062, 21497, 21925, 22347, 22762, 23170, 23571, 23964,
32 24351, 24730, 25101, 25465, 25821, 26169, 26509, 26841, 27165, 27481, 27788, 28087, 28377, 28659, 28932, 29196,
33 29451, 29697, 29934, 30162, 30381, 30591, 30791, 30982, 31163, 31335, 31498, 31650, 31794, 31927, 32051, 32165,
34 32269, 32364, 32448, 32523, 32587, 32642, 32687, 32722, 32747, 32762, 32767};
35
36 /**********************
37 * MACROS
38 **********************/
39
40 /**********************
41 * GLOBAL FUNCTIONS
42 **********************/
43
44 /**
45 * Return with sinus of an angle
46 * @param angle
47 * @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
48 */
lv_trigo_sin(int16_t angle)49 int16_t lv_trigo_sin(int16_t angle)
50 {
51 int16_t ret = 0;
52 angle = angle % 360;
53
54 if(angle < 0) angle = 360 + angle;
55
56 if(angle < 90) {
57 ret = sin0_90_table[angle];
58 } else if(angle >= 90 && angle < 180) {
59 angle = 180 - angle;
60 ret = sin0_90_table[angle];
61 } else if(angle >= 180 && angle < 270) {
62 angle = angle - 180;
63 ret = -sin0_90_table[angle];
64 } else { /*angle >=270*/
65 angle = 360 - angle;
66 ret = -sin0_90_table[angle];
67 }
68
69 return ret;
70 }
71
72 /**
73 * Calculate a value of a Cubic Bezier function.
74 * @param t time in range of [0..LV_BEZIER_VAL_MAX]
75 * @param u0 start values in range of [0..LV_BEZIER_VAL_MAX]
76 * @param u1 control value 1 values in range of [0..LV_BEZIER_VAL_MAX]
77 * @param u2 control value 2 in range of [0..LV_BEZIER_VAL_MAX]
78 * @param u3 end values in range of [0..LV_BEZIER_VAL_MAX]
79 * @return the value calculated from the given parameters in range of [0..LV_BEZIER_VAL_MAX]
80 */
lv_bezier3(uint32_t t,int32_t u0,int32_t u1,int32_t u2,int32_t u3)81 int32_t lv_bezier3(uint32_t t, int32_t u0, int32_t u1, int32_t u2, int32_t u3)
82 {
83 uint32_t t_rem = 1024 - t;
84 uint32_t t_rem2 = (t_rem * t_rem) >> 10;
85 uint32_t t_rem3 = (t_rem2 * t_rem) >> 10;
86 uint32_t t2 = (t * t) >> 10;
87 uint32_t t3 = (t2 * t) >> 10;
88
89 uint32_t v1 = ((uint32_t)t_rem3 * u0) >> 10;
90 uint32_t v2 = ((uint32_t)3 * t_rem2 * t * u1) >> 20;
91 uint32_t v3 = ((uint32_t)3 * t_rem * t2 * u2) >> 20;
92 uint32_t v4 = ((uint32_t)t3 * u3) >> 10;
93
94 return v1 + v2 + v3 + v4;
95 }
96
97 /**********************
98 * STATIC FUNCTIONS
99 **********************/
100