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 #include <string.h>
13
14 /*********************
15 * DEFINES
16 *********************/
17
18 /**********************
19 * TYPEDEFS
20 **********************/
21
22 /**********************
23 * STATIC PROTOTYPES
24 **********************/
25
26 /**********************
27 * STATIC VARIABLES
28 **********************/
29 static const int16_t sin0_90_table[] = {
30 0, 572, 1144, 1715, 2286, 2856, 3425, 3993, 4560, 5126, 5690, 6252, 6813, 7371, 7927, 8481,
31 9032, 9580, 10126, 10668, 11207, 11743, 12275, 12803, 13328, 13848, 14364, 14876, 15383, 15886, 16383, 16876,
32 17364, 17846, 18323, 18794, 19260, 19720, 20173, 20621, 21062, 21497, 21925, 22347, 22762, 23170, 23571, 23964,
33 24351, 24730, 25101, 25465, 25821, 26169, 26509, 26841, 27165, 27481, 27788, 28087, 28377, 28659, 28932, 29196,
34 29451, 29697, 29934, 30162, 30381, 30591, 30791, 30982, 31163, 31335, 31498, 31650, 31794, 31927, 32051, 32165,
35 32269, 32364, 32448, 32523, 32587, 32642, 32687, 32722, 32747, 32762, 32767
36 };
37
38
39
40 /**********************
41 * MACROS
42 **********************/
43
44 /**********************
45 * GLOBAL FUNCTIONS
46 **********************/
47
48 /**
49 * Return with sinus of an angle
50 * @param angle
51 * @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767
52 */
_lv_trigo_sin(int16_t angle)53 LV_ATTRIBUTE_FAST_MEM int16_t _lv_trigo_sin(int16_t angle)
54 {
55 int16_t ret = 0;
56 angle = angle % 360;
57
58 if(angle < 0) angle = 360 + angle;
59
60 if(angle < 90) {
61 ret = sin0_90_table[angle];
62 }
63 else if(angle >= 90 && angle < 180) {
64 angle = 180 - angle;
65 ret = sin0_90_table[angle];
66 }
67 else if(angle >= 180 && angle < 270) {
68 angle = angle - 180;
69 ret = -sin0_90_table[angle];
70 }
71 else { /*angle >=270*/
72 angle = 360 - angle;
73 ret = -sin0_90_table[angle];
74 }
75
76 return ret;
77 }
78
79 /**
80 * Calculate a value of a Cubic Bezier function.
81 * @param t time in range of [0..LV_BEZIER_VAL_MAX]
82 * @param u0 start values in range of [0..LV_BEZIER_VAL_MAX]
83 * @param u1 control value 1 values in range of [0..LV_BEZIER_VAL_MAX]
84 * @param u2 control value 2 in range of [0..LV_BEZIER_VAL_MAX]
85 * @param u3 end values in range of [0..LV_BEZIER_VAL_MAX]
86 * @return the value calculated from the given parameters in range of [0..LV_BEZIER_VAL_MAX]
87 */
_lv_bezier3(uint32_t t,int32_t u0,int32_t u1,int32_t u2,int32_t u3)88 int32_t _lv_bezier3(uint32_t t, int32_t u0, int32_t u1, int32_t u2, int32_t u3)
89 {
90 uint32_t t_rem = 1024 - t;
91 uint32_t t_rem2 = (t_rem * t_rem) >> 10;
92 uint32_t t_rem3 = (t_rem2 * t_rem) >> 10;
93 uint32_t t2 = (t * t) >> 10;
94 uint32_t t3 = (t2 * t) >> 10;
95
96 uint32_t v1 = ((uint32_t)t_rem3 * u0) >> 10;
97 uint32_t v2 = ((uint32_t)3 * t_rem2 * t * u1) >> 20;
98 uint32_t v3 = ((uint32_t)3 * t_rem * t2 * u2) >> 20;
99 uint32_t v4 = ((uint32_t)t3 * u3) >> 10;
100
101 return v1 + v2 + v3 + v4;
102 }
103
104 /**
105 * Get the square root of a number
106 * @param x integer which square root should be calculated
107 * @param q store the result here. q->i: integer part, q->f: fractional part in 1/256 unit
108 * @param mask: optional to skip some iterations if the magnitude of the root is known.
109 * Set to 0x8000 by default.
110 * If root < 16: mask = 0x80
111 * If root < 256: mask = 0x800
112 * Else: mask = 0x8000
113 */
_lv_sqrt(uint32_t x,lv_sqrt_res_t * q,uint32_t mask)114 LV_ATTRIBUTE_FAST_MEM void _lv_sqrt(uint32_t x, lv_sqrt_res_t * q, uint32_t mask)
115 {
116 x = x << 8; /*To get 4 bit precision. (sqrt(256) = 16 = 4 bit)*/
117
118 uint32_t root = 0;
119 uint32_t trial;
120 // http://ww1.microchip.com/...en/AppNotes/91040a.pdf
121 do {
122 trial = root + mask;
123 if((uint32_t)trial * trial <= x) root = trial;
124 mask = mask >> 1;
125 } while(mask);
126
127 q->i = (uint32_t) root >> 4;
128 q->f = (uint32_t)(root & 0xf) << 4;
129 }
130
131 /**
132 * Calculate the atan2 of a vector.
133 * @param x
134 * @param y
135 * @return the angle in degree calculated from the given parameters in range of [0..360]
136 */
_lv_atan2(int x,int y)137 uint16_t _lv_atan2(int x, int y)
138 {
139 // Fast XY vector to integer degree algorithm - Jan 2011 www.RomanBlack.com
140 // Converts any XY values including 0 to a degree value that should be
141 // within +/- 1 degree of the accurate value without needing
142 // large slow trig functions like ArcTan() or ArcCos().
143 // NOTE! at least one of the X or Y values must be non-zero!
144 // This is the full version, for all 4 quadrants and will generate
145 // the angle in integer degrees from 0-360.
146 // Any values of X and Y are usable including negative values provided
147 // they are between -1456 and 1456 so the 16bit multiply does not overflow.
148
149 unsigned char negflag;
150 unsigned char tempdegree;
151 unsigned char comp;
152 unsigned int degree; // this will hold the result
153 //signed int x; // these hold the XY vector at the start
154 //signed int y; // (and they will be destroyed)
155 unsigned int ux;
156 unsigned int uy;
157
158 // Save the sign flags then remove signs and get XY as unsigned ints
159 negflag = 0;
160 if(x < 0) {
161 negflag += 0x01; // x flag bit
162 x = (0 - x); // is now +
163 }
164 ux = x; // copy to unsigned var before multiply
165 if(y < 0) {
166 negflag += 0x02; // y flag bit
167 y = (0 - y); // is now +
168 }
169 uy = y; // copy to unsigned var before multiply
170
171 // 1. Calc the scaled "degrees"
172 if(ux > uy) {
173 degree = (uy * 45) / ux; // degree result will be 0-45 range
174 negflag += 0x10; // octant flag bit
175 }
176 else {
177 degree = (ux * 45) / uy; // degree result will be 0-45 range
178 }
179
180 // 2. Compensate for the 4 degree error curve
181 comp = 0;
182 tempdegree = degree; // use an unsigned char for speed!
183 if(tempdegree > 22) { // if top half of range
184 if(tempdegree <= 44) comp++;
185 if(tempdegree <= 41) comp++;
186 if(tempdegree <= 37) comp++;
187 if(tempdegree <= 32) comp++; // max is 4 degrees compensated
188 }
189 else { // else is lower half of range
190 if(tempdegree >= 2) comp++;
191 if(tempdegree >= 6) comp++;
192 if(tempdegree >= 10) comp++;
193 if(tempdegree >= 15) comp++; // max is 4 degrees compensated
194 }
195 degree += comp; // degree is now accurate to +/- 1 degree!
196
197 // Invert degree if it was X>Y octant, makes 0-45 into 90-45
198 if(negflag & 0x10) degree = (90 - degree);
199
200 // 3. Degree is now 0-90 range for this quadrant,
201 // need to invert it for whichever quadrant it was in
202 if(negflag & 0x02) { // if -Y
203 if(negflag & 0x01) // if -Y -X
204 degree = (180 + degree);
205 else // else is -Y +X
206 degree = (180 - degree);
207 }
208 else { // else is +Y
209 if(negflag & 0x01) // if +Y -X
210 degree = (360 - degree);
211 }
212 return degree;
213 }
214
215 /**
216 * Calculate the integer exponents.
217 * @param base
218 * @param power
219 * @return base raised to the power exponent
220 */
_lv_pow(int64_t base,int8_t exp)221 int64_t _lv_pow(int64_t base, int8_t exp)
222 {
223 int64_t result = 1;
224 while(exp) {
225 if(exp & 1)
226 result *= base;
227 exp >>= 1;
228 base *= base;
229 }
230
231 return result;
232 }
233
234 /**
235 * Get the mapped of a number given an input and output range
236 * @param x integer which mapped value should be calculated
237 * @param min_in min input range
238 * @param max_in max input range
239 * @param min_out max output range
240 * @param max_out max output range
241 * @return the mapped number
242 */
_lv_map(int32_t x,int32_t min_in,int32_t max_in,int32_t min_out,int32_t max_out)243 int16_t _lv_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min_out, int32_t max_out)
244 {
245 if(x <= min_in) return min_out;
246 if(x >= max_in) return max_out;
247
248 /* The equation should be:
249 * ((x - min_in) / delta in) * delta_out + min_out
250 * To avoid rounding error reorder the operations:
251 * (((x - min_in) * delta_out) / delta in) + min_out
252 */
253
254 int32_t delta_in = max_in - min_in;
255 int32_t delta_out = max_out - min_out;
256
257 return ((x - min_in) * delta_out) / delta_in + min_out;
258 }
259
260 /**********************
261 * STATIC FUNCTIONS
262 **********************/
263