1 /**
2 * @file lv_math.h
3 *
4 */
5
6 #ifndef LV_MATH_H
7 #define LV_MATH_H
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 /*********************
14 * INCLUDES
15 *********************/
16 #include "../lv_conf_internal.h"
17 #include <stdint.h>
18
19 /*********************
20 * DEFINES
21 *********************/
22 #define LV_TRIGO_SIN_MAX 32767
23 #define LV_TRIGO_SHIFT 15 /**< >> LV_TRIGO_SHIFT to normalize*/
24
25 #define LV_BEZIER_VAL_MAX 1024 /**< Max time in Bezier functions (not [0..1] to use integers)*/
26 #define LV_BEZIER_VAL_SHIFT 10 /**< log2(LV_BEZIER_VAL_MAX): used to normalize up scaled values*/
27
28 /**********************
29 * TYPEDEFS
30 **********************/
31
32 typedef struct {
33 uint16_t i;
34 uint16_t f;
35 } lv_sqrt_res_t;
36
37 /**********************
38 * GLOBAL PROTOTYPES
39 **********************/
40
41 //! @cond Doxygen_Suppress
42 /**
43 * Return with sinus of an angle
44 * @param angle
45 * @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
46 */
47 LV_ATTRIBUTE_FAST_MEM int16_t lv_trigo_sin(int16_t angle);
48
lv_trigo_cos(int16_t angle)49 static inline LV_ATTRIBUTE_FAST_MEM int16_t lv_trigo_cos(int16_t angle)
50 {
51 return lv_trigo_sin(angle + 90);
52 }
53
54 //! @endcond
55
56 /**
57 * Calculate a value of a Cubic Bezier function.
58 * @param t time in range of [0..LV_BEZIER_VAL_MAX]
59 * @param u0 start values in range of [0..LV_BEZIER_VAL_MAX]
60 * @param u1 control value 1 values in range of [0..LV_BEZIER_VAL_MAX]
61 * @param u2 control value 2 in range of [0..LV_BEZIER_VAL_MAX]
62 * @param u3 end values in range of [0..LV_BEZIER_VAL_MAX]
63 * @return the value calculated from the given parameters in range of [0..LV_BEZIER_VAL_MAX]
64 */
65 uint32_t lv_bezier3(uint32_t t, uint32_t u0, uint32_t u1, uint32_t u2, uint32_t u3);
66
67 /**
68 * Calculate the atan2 of a vector.
69 * @param x
70 * @param y
71 * @return the angle in degree calculated from the given parameters in range of [0..360]
72 */
73 uint16_t lv_atan2(int x, int y);
74
75 //! @cond Doxygen_Suppress
76
77 /**
78 * Get the square root of a number
79 * @param x integer which square root should be calculated
80 * @param q store the result here. q->i: integer part, q->f: fractional part in 1/256 unit
81 * @param mask optional to skip some iterations if the magnitude of the root is known.
82 * Set to 0x8000 by default.
83 * If root < 16: mask = 0x80
84 * If root < 256: mask = 0x800
85 * Else: mask = 0x8000
86 */
87 LV_ATTRIBUTE_FAST_MEM void lv_sqrt(uint32_t x, lv_sqrt_res_t * q, uint32_t mask);
88
89 //! @endcond
90
91 /**
92 * Calculate the integer exponents.
93 * @param base
94 * @param power
95 * @return base raised to the power exponent
96 */
97 int64_t lv_pow(int64_t base, int8_t exp);
98
99 /**
100 * Get the mapped of a number given an input and output range
101 * @param x integer which mapped value should be calculated
102 * @param min_in min input range
103 * @param max_in max input range
104 * @param min_out max output range
105 * @param max_out max output range
106 * @return the mapped number
107 */
108 int32_t lv_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min_out, int32_t max_out);
109
110 /**
111 * Get a pseudo random number in the given range
112 * @param min the minimum value
113 * @param max the maximum value
114 * @return return the random number. min <= return_value <= max
115 */
116 uint32_t lv_rand(uint32_t min, uint32_t max);
117
118 /**********************
119 * MACROS
120 **********************/
121 #define LV_MIN(a, b) ((a) < (b) ? (a) : (b))
122 #define LV_MIN3(a, b, c) (LV_MIN(LV_MIN(a,b), c))
123 #define LV_MIN4(a, b, c, d) (LV_MIN(LV_MIN(a,b), LV_MIN(c,d)))
124
125 #define LV_MAX(a, b) ((a) > (b) ? (a) : (b))
126 #define LV_MAX3(a, b, c) (LV_MAX(LV_MAX(a,b), c))
127 #define LV_MAX4(a, b, c, d) (LV_MAX(LV_MAX(a,b), LV_MAX(c,d)))
128
129 #define LV_CLAMP(min, val, max) (LV_MAX(min, (LV_MIN(val, max))))
130
131 #define LV_ABS(x) ((x) > 0 ? (x) : (-(x)))
132 #define LV_UDIV255(x) (((x) * 0x8081U) >> 0x17)
133
134 #define LV_IS_SIGNED(t) (((t)(-1)) < ((t)0))
135 #define LV_UMAX_OF(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0xFULL << ((sizeof(t) * 8ULL) - 4ULL)))
136 #define LV_SMAX_OF(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0x7ULL << ((sizeof(t) * 8ULL) - 4ULL)))
137 #define LV_MAX_OF(t) ((unsigned long)(LV_IS_SIGNED(t) ? LV_SMAX_OF(t) : LV_UMAX_OF(t)))
138
139 #ifdef __cplusplus
140 } /*extern "C"*/
141 #endif
142
143 #endif
144