1 /** 2 * @file math_base.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_MATH_MIN(a, b) ((a) < (b) ? (a) : (b)) 23 #define LV_MATH_MIN3(a, b, c) (LV_MATH_MIN(LV_MATH_MIN(a,b), c)) 24 #define LV_MATH_MIN4(a, b, c, d) (LV_MATH_MIN(LV_MATH_MIN(a,b), LV_MATH_MIN(c,d))) 25 26 #define LV_MATH_MAX(a, b) ((a) > (b) ? (a) : (b)) 27 #define LV_MATH_MAX3(a, b, c) (LV_MATH_MAX(LV_MATH_MAX(a,b), c)) 28 #define LV_MATH_MAX4(a, b, c, d) (LV_MATH_MAX(LV_MATH_MAX(a,b), LV_MATH_MAX(c,d))) 29 30 #define LV_MATH_ABS(x) ((x) > 0 ? (x) : (-(x))) 31 32 #define LV_MATH_UDIV255(x) ((uint32_t)((uint32_t) (x) * 0x8081) >> 0x17) 33 34 #define LV_IS_SIGNED(t) (((t)(-1)) < ((t) 0)) 35 #define LV_UMAX_OF(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0xFULL << ((sizeof(t) * 8ULL) - 4ULL))) 36 #define LV_SMAX_OF(t) (((0x1ULL << ((sizeof(t) * 8ULL) - 1ULL)) - 1ULL) | (0x7ULL << ((sizeof(t) * 8ULL) - 4ULL))) 37 #define LV_MAX_OF(t) ((unsigned long) (LV_IS_SIGNED(t) ? LV_SMAX_OF(t) : LV_UMAX_OF(t))) 38 39 #define LV_TRIGO_SIN_MAX 32767 40 #define LV_TRIGO_SHIFT 15 /**< >> LV_TRIGO_SHIFT to normalize*/ 41 42 #define LV_BEZIER_VAL_MAX 1024 /**< Max time in Bezier functions (not [0..1] to use integers) */ 43 #define LV_BEZIER_VAL_SHIFT 10 /**< log2(LV_BEZIER_VAL_MAX): used to normalize up scaled values*/ 44 45 /********************** 46 * TYPEDEFS 47 **********************/ 48 49 typedef struct { 50 uint16_t i; 51 uint16_t f; 52 } lv_sqrt_res_t; 53 54 55 /********************** 56 * GLOBAL PROTOTYPES 57 **********************/ 58 59 //! @cond Doxygen_Suppress 60 /** 61 * Return with sinus of an angle 62 * @param angle 63 * @return sinus of 'angle'. sin(-90) = -32767, sin(90) = 32767 64 */ 65 LV_ATTRIBUTE_FAST_MEM int16_t _lv_trigo_sin(int16_t angle); 66 67 //! @endcond 68 69 /** 70 * Calculate a value of a Cubic Bezier function. 71 * @param t time in range of [0..LV_BEZIER_VAL_MAX] 72 * @param u0 start values in range of [0..LV_BEZIER_VAL_MAX] 73 * @param u1 control value 1 values in range of [0..LV_BEZIER_VAL_MAX] 74 * @param u2 control value 2 in range of [0..LV_BEZIER_VAL_MAX] 75 * @param u3 end values in range of [0..LV_BEZIER_VAL_MAX] 76 * @return the value calculated from the given parameters in range of [0..LV_BEZIER_VAL_MAX] 77 */ 78 int32_t _lv_bezier3(uint32_t t, int32_t u0, int32_t u1, int32_t u2, int32_t u3); 79 80 /** 81 * Calculate the atan2 of a vector. 82 * @param x 83 * @param y 84 * @return the angle in degree calculated from the given parameters in range of [0..360] 85 */ 86 uint16_t _lv_atan2(int x, int y); 87 88 89 //! @cond Doxygen_Suppress 90 91 /** 92 * Get the square root of a number 93 * @param x integer which square root should be calculated 94 * @param q store the result here. q->i: integer part, q->f: fractional part in 1/256 unit 95 * @param mask: optional to skip some iterations if the magnitude of the root is known. 96 * Set to 0x8000 by default. 97 * If root < 16: mask = 0x80 98 * If root < 256: mask = 0x800 99 * Else: mask = 0x8000 100 */ 101 LV_ATTRIBUTE_FAST_MEM void _lv_sqrt(uint32_t x, lv_sqrt_res_t * q, uint32_t mask); 102 103 //! @endcond 104 105 /** 106 * Calculate the integer exponents. 107 * @param base 108 * @param power 109 * @return base raised to the power exponent 110 */ 111 int64_t _lv_pow(int64_t base, int8_t exp); 112 113 /** 114 * Get the mapped of a number given an input and output range 115 * @param x integer which mapped value should be calculated 116 * @param min_in min input range 117 * @param max_in max input range 118 * @param min_out max output range 119 * @param max_out max output range 120 * @return the mapped number 121 */ 122 int16_t _lv_map(int32_t x, int32_t min_in, int32_t max_in, int32_t min, int32_t max); 123 124 /********************** 125 * MACROS 126 **********************/ 127 128 #ifdef __cplusplus 129 } /* extern "C" */ 130 #endif 131 132 #endif 133