1 /**
2  * @file lv_utils.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include <stdbool.h>
10 
11 #include "lv_utils.h"
12 #include "lv_math.h"
13 #include "lv_printf.h"
14 #include "lv_txt.h"
15 
16 /*********************
17  *      DEFINES
18  *********************/
19 
20 /**********************
21  *      TYPEDEFS
22  **********************/
23 
24 /**********************
25  *  STATIC PROTOTYPES
26  **********************/
27 
28 /**********************
29  *  STATIC VARIABLES
30  **********************/
31 
32 /**********************
33  *      MACROS
34  **********************/
35 
36 /**********************
37  *   GLOBAL FUNCTIONS
38  **********************/
39 
40 /**
41  * Convert a number to string
42  * @param num a number
43  * @param buf pointer to a `char` buffer. The result will be stored here (max 10 elements)
44  * @return same as `buf` (just for convenience)
45  */
_lv_utils_num_to_str(int32_t num,char * buf)46 char * _lv_utils_num_to_str(int32_t num, char * buf)
47 {
48     if(num == 0) {
49         buf[0] = '0';
50         buf[1] = '\0';
51         return buf;
52     }
53     int8_t digitCount = 0;
54     int8_t i          = 0;
55     if(num < 0) {
56         buf[digitCount++] = '-';
57         num               = LV_MATH_ABS(num);
58         ++i;
59     }
60     while(num) {
61         char digit        = num % 10;
62         buf[digitCount++] = digit + 48;
63         num /= 10;
64     }
65     buf[digitCount] = '\0';
66     digitCount--;
67     while(digitCount > i) {
68         char temp       = buf[i];
69         buf[i]          = buf[digitCount];
70         buf[digitCount] = temp;
71         digitCount--;
72         i++;
73     }
74     return buf;
75 }
76 
77 /** Searches base[0] to base[n - 1] for an item that matches *key.
78  *
79  * @note The function cmp must return negative if its first
80  *  argument (the search key) is less that its second (a table entry),
81  *  zero if equal, and positive if greater.
82  *
83  *  @note Items in the array must be in ascending order.
84  *
85  * @param key    Pointer to item being searched for
86  * @param base   Pointer to first element to search
87  * @param n      Number of elements
88  * @param size   Size of each element
89  * @param cmp    Pointer to comparison function (see #lv_font_codeCompare as a comparison function
90  * example)
91  *
92  * @return a pointer to a matching item, or NULL if none exists.
93  */
_lv_utils_bsearch(const void * key,const void * base,uint32_t n,uint32_t size,int32_t (* cmp)(const void * pRef,const void * pElement))94 void * _lv_utils_bsearch(const void * key, const void * base, uint32_t n, uint32_t size,
95                          int32_t (*cmp)(const void * pRef, const void * pElement))
96 {
97     const char * middle;
98     int32_t c;
99 
100     for(middle = base; n != 0;) {
101         middle += (n / 2) * size;
102         if((c = (*cmp)(key, middle)) > 0) {
103             n    = (n / 2) - ((n & 1) == 0);
104             base = (middle += size);
105         }
106         else if(c < 0) {
107             n /= 2;
108             middle = base;
109         }
110         else {
111             return (char *)middle;
112         }
113     }
114     return NULL;
115 }
116 
117 /**********************
118  *   STATIC FUNCTIONS
119  **********************/
120