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 "lv_types.h"
18
19 /*********************
20 * DEFINES
21 *********************/
22 #define LV_TRIGO_SIN_MAX 32768
23 #define LV_TRIGO_SHIFT 15 /**< >> LV_TRIGO_SHIFT to normalize*/
24
25 #define LV_BEZIER_VAL_SHIFT 10 /**< log2(LV_BEZIER_VAL_MAX): used to normalize up scaled values*/
26 #define LV_BEZIER_VAL_MAX (1L << LV_BEZIER_VAL_SHIFT) /**< Max time in Bezier functions (not [0..1] to use integers)*/
27 #define LV_BEZIER_VAL_FLOAT(f) ((int32_t)((f) * LV_BEZIER_VAL_MAX)) /**< Convert const float number cubic-bezier values to fix-point value*/
28
29 /** Align up value x to align, align must be a power of two */
30 #define LV_ALIGN_UP(x, align) (((x) + ((align) - 1)) & ~((align) - 1))
31
32 /** Round up value x to round, round can be any integer number */
33 #define LV_ROUND_UP(x, round) ((((x) + ((round) - 1)) / (round)) * (round))
34
35 /**********************
36 * TYPEDEFS
37 **********************/
38
39 typedef struct {
40 uint16_t i;
41 uint16_t f;
42 } lv_sqrt_res_t;
43
44 /**********************
45 * GLOBAL PROTOTYPES
46 **********************/
47
48 //! @cond Doxygen_Suppress
49 /**
50 * Return with sinus of an angle
51 * @param angle
52 * @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
53 */
54 int32_t /* LV_ATTRIBUTE_FAST_MEM */ lv_trigo_sin(int16_t angle);
55
56 int32_t LV_ATTRIBUTE_FAST_MEM lv_trigo_cos(int16_t angle);
57
58 //! @endcond
59
60 /**
61 * Calculate the y value of cubic-bezier(x1, y1, x2, y2) function as specified x.
62 * @param x time in range of [0..LV_BEZIER_VAL_MAX]
63 * @param x1 x of control point 1 in range of [0..LV_BEZIER_VAL_MAX]
64 * @param y1 y of control point 1 in range of [0..LV_BEZIER_VAL_MAX]
65 * @param x2 x of control point 2 in range of [0..LV_BEZIER_VAL_MAX]
66 * @param y2 y of control point 2 in range of [0..LV_BEZIER_VAL_MAX]
67 * @return the value calculated
68 */
69 int32_t lv_cubic_bezier(int32_t x, int32_t x1, int32_t y1, int32_t x2, int32_t y2);
70
71 /**
72 * Calculate a value of a Cubic Bezier function.
73 * @param t time in range of [0..LV_BEZIER_VAL_MAX]
74 * @param u0 must be 0
75 * @param u1 control value 1 values in range of [0..LV_BEZIER_VAL_MAX]
76 * @param u2 control value 2 in range of [0..LV_BEZIER_VAL_MAX]
77 * @param u3 must be LV_BEZIER_VAL_MAX
78 * @return the value calculated from the given parameters in range of [0..LV_BEZIER_VAL_MAX]
79 */
80 int32_t lv_bezier3(int32_t t, int32_t u0, uint32_t u1, int32_t u2, int32_t u3);
81
82
83 /**
84 * Calculate the atan2 of a vector.
85 * @param x
86 * @param y
87 * @return the angle in degree calculated from the given parameters in range of [0..360]
88 */
89 uint16_t lv_atan2(int x, int y);
90
91 //! @cond Doxygen_Suppress
92
93 /**
94 * Get the square root of a number
95 * @param x integer which square root should be calculated
96 * @param q store the result here. q->i: integer part, q->f: fractional part in 1/256 unit
97 * @param mask optional to skip some iterations if the magnitude of the root is known.
98 * Set to 0x8000 by default.
99 * If root < 16: mask = 0x80
100 * If root < 256: mask = 0x800
101 * Else: mask = 0x8000
102 */
103 void /* LV_ATTRIBUTE_FAST_MEM */ lv_sqrt(uint32_t x, lv_sqrt_res_t * q, uint32_t mask);
104
105 //! @endcond
106
107 /**
108 * Alternative (fast, approximate) implementation for getting the square root of an integer.
109 * @param x integer which square root should be calculated
110 */
111 int32_t /* LV_ATTRIBUTE_FAST_MEM */ lv_sqrt32(uint32_t x);
112
113 /**
114 * Calculate the square of an integer (input range is 0..32767).
115 * @param x input
116 * @return square
117 */
lv_sqr(int32_t x)118 static inline int32_t lv_sqr(int32_t x)
119 {
120 return x * x;
121 }
122
123 /**
124 * Calculate the integer exponents.
125 * @param base
126 * @param exp
127 * @return base raised to the power exponent
128 */
129 int64_t lv_pow(int64_t base, int8_t exp);
130
131 /**
132 * Get the mapped of a number given an input and output range
133 * @param x integer which mapped value should be calculated
134 * @param min_in min input range
135 * @param max_in max input range
136 * @param min_out max output range
137 * @param max_out max output range
138 * @return the mapped number
139 */
140 int32_t lv_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min_out, int32_t max_out);
141
142 /**
143 * Set the seed of the pseudo random number generator
144 * @param seed a number to initialize the random generator
145 */
146 void lv_rand_set_seed(uint32_t seed);
147
148 /**
149 * Get a pseudo random number in the given range
150 * @param min the minimum value
151 * @param max the maximum value
152 * @return return the random number. min <= return_value <= max
153 */
154 uint32_t lv_rand(uint32_t min, uint32_t max);
155
156 /**********************
157 * MACROS
158 **********************/
159 #define LV_MIN(a, b) ((a) < (b) ? (a) : (b))
160 #define LV_MIN3(a, b, c) (LV_MIN(LV_MIN(a,b), c))
161 #define LV_MIN4(a, b, c, d) (LV_MIN(LV_MIN(a,b), LV_MIN(c,d)))
162
163 #define LV_MAX(a, b) ((a) > (b) ? (a) : (b))
164 #define LV_MAX3(a, b, c) (LV_MAX(LV_MAX(a,b), c))
165 #define LV_MAX4(a, b, c, d) (LV_MAX(LV_MAX(a,b), LV_MAX(c,d)))
166
167 #define LV_CLAMP(min, val, max) (LV_MAX(min, (LV_MIN(val, max))))
168
169 #define LV_ABS(x) ((x) > 0 ? (x) : (-(x)))
170 #define LV_UDIV255(x) (((x) * 0x8081U) >> 0x17)
171
172 #define LV_IS_SIGNED(t) (((t)(-1)) < ((t)0))
173 #define LV_UMAX_OF(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0xFULL << ((sizeof(t) * 8ULL) - 4ULL)))
174 #define LV_SMAX_OF(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0x7ULL << ((sizeof(t) * 8ULL) - 4ULL)))
175 #define LV_MAX_OF(t) ((unsigned long)(LV_IS_SIGNED(t) ? LV_SMAX_OF(t) : LV_UMAX_OF(t)))
176
177 #ifdef __cplusplus
178 } /*extern "C"*/
179 #endif
180
181 #endif
182