1 /**
2 * @file lv_circle_buf.h
3 *
4  */
5 
6 
7 #ifndef LV_CIRCLE_BUF_H
8 #define LV_CIRCLE_BUF_H
9 
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13 
14 /*********************
15  *      INCLUDES
16  *********************/
17 
18 #include "lv_types.h"
19 
20 /*********************
21  *      DEFINES
22  *********************/
23 
24 /**********************
25  *      TYPEDEFS
26  **********************/
27 
28 typedef bool (*lv_circle_buf_fill_cb_t)(void * buf, uint32_t buff_len, int32_t index, void * user_data);
29 
30 /**********************
31  * GLOBAL PROTOTYPES
32  **********************/
33 
34 /**
35  * Create a circle buffer
36  * @param capacity the maximum number of elements in the buffer
37  * @param element_size the size of an element in bytes
38  * @return pointer to the created buffer
39  */
40 lv_circle_buf_t * lv_circle_buf_create(uint32_t capacity, uint32_t element_size);
41 
42 /**
43  * Create a circle buffer from an existing buffer
44  * @param buf pointer to a buffer
45  * @param capacity the maximum number of elements in the buffer
46  * @param element_size the size of an element in bytes
47  * @return pointer to the created buffer
48  */
49 lv_circle_buf_t * lv_circle_buf_create_from_buf(void * buf, uint32_t capacity, uint32_t element_size);
50 
51 /**
52  * Create a circle buffer from an existing array
53  * @param array pointer to an array
54  * @return pointer to the created buffer
55  */
56 lv_circle_buf_t * lv_circle_buf_create_from_array(const lv_array_t * array);
57 
58 /**
59  * Resize the buffer
60  * @param circle_buf pointer to a buffer
61  * @param capacity the new capacity of the buffer
62  * @return LV_RESULT_OK: the buffer is resized; LV_RESULT_INVALID: the buffer is not resized
63  */
64 lv_result_t lv_circle_buf_resize(lv_circle_buf_t * circle_buf, uint32_t capacity);
65 
66 /**
67  * Destroy a circle buffer
68  * @param circle_buf pointer to buffer
69  */
70 void lv_circle_buf_destroy(lv_circle_buf_t * circle_buf);
71 
72 /**
73  * Get the size of the buffer
74  * @param circle_buf pointer to buffer
75  * @return the number of elements in the buffer
76  */
77 uint32_t lv_circle_buf_size(const lv_circle_buf_t * circle_buf);
78 
79 /**
80  * Get the capacity of the buffer
81  * @param circle_buf pointer to buffer
82  * @return the maximum number of elements in the buffer
83  */
84 uint32_t lv_circle_buf_capacity(const lv_circle_buf_t * circle_buf);
85 
86 /**
87  * Get the remaining space in the buffer
88  * @param circle_buf pointer to buffer
89  * @return the number of elements that can be written to the buffer
90  */
91 uint32_t lv_circle_buf_remain(const lv_circle_buf_t * circle_buf);
92 
93 /**
94  * Check if the buffer is empty
95  * @param circle_buf pointer to buffer
96  * @return true: the buffer is empty; false: the buffer is not empty
97  */
98 bool lv_circle_buf_is_empty(const lv_circle_buf_t * circle_buf);
99 
100 /**
101  * Check if the buffer is full
102  * @param circle_buf pointer to buffer
103  * @return true: the buffer is full; false: the buffer is not full
104  */
105 bool lv_circle_buf_is_full(const lv_circle_buf_t * circle_buf);
106 
107 /**
108  * Reset the buffer
109  * @param circle_buf pointer to buffer
110  * @return LV_RESULT_OK: the buffer is reset; LV_RESULT_INVALID: the buffer is not reset
111  */
112 void lv_circle_buf_reset(lv_circle_buf_t * circle_buf);
113 
114 /**
115  * Get the head of the buffer
116  * @param circle_buf pointer to buffer
117  * @return pointer to the head of the buffer
118  */
119 void * lv_circle_buf_head(const lv_circle_buf_t * circle_buf);
120 
121 /**
122  * Get the tail of the buffer
123  * @param circle_buf pointer to buffer
124  * @return pointer to the tail of the buffer
125  */
126 void * lv_circle_buf_tail(const lv_circle_buf_t * circle_buf);
127 
128 /**
129  * Read a value
130  * @param circle_buf pointer to buffer
131  * @param data pointer to a variable to store the read value
132  * @return LV_RESULT_OK: the value is read; LV_RESULT_INVALID: the value is not read
133  */
134 lv_result_t lv_circle_buf_read(lv_circle_buf_t * circle_buf, void * data);
135 
136 /**
137  * Write a value
138  * @param circle_buf pointer to buffer
139  * @param data pointer to the value to write
140  * @return LV_RESULT_OK: the value is written; LV_RESULT_INVALID: the value is not written
141  */
142 lv_result_t lv_circle_buf_write(lv_circle_buf_t * circle_buf, const void * data);
143 
144 /**
145  * Fill the buffer with values
146  * @param circle_buf pointer to buffer
147  * @param count the number of values to fill
148  * @param fill_cb the callback function to fill the buffer
149  * @param user_data
150  * @return the number of values filled
151  */
152 uint32_t lv_circle_buf_fill(lv_circle_buf_t * circle_buf, uint32_t count, lv_circle_buf_fill_cb_t fill_cb,
153                             void * user_data);
154 
155 /**
156  * Skip a value
157  * @param circle_buf pointer to buffer
158  * @return LV_RESULT_OK: the value is skipped; LV_RESULT_INVALID: the value is not skipped
159  */
160 lv_result_t lv_circle_buf_skip(lv_circle_buf_t * circle_buf);
161 
162 /**
163  * Peek a value
164  * @param circle_buf pointer to buffer
165  * @param data pointer to a variable to store the peeked value
166  * @return LV_RESULT_OK: the value is peeked; LV_RESULT_INVALID: the value is not peeked
167  */
168 lv_result_t lv_circle_buf_peek(const lv_circle_buf_t * circle_buf, void * data);
169 
170 /**
171  * Peek a value at an index
172  * @param circle_buf pointer to buffer
173  * @param index the index of the value to peek, if the index is greater than the size of the buffer, it will return looply.
174  * @param data pointer to a variable to store the peeked value
175  * @return LV_RESULT_OK: the value is peeked; LV_RESULT_INVALID: the value is not peeked
176  */
177 lv_result_t lv_circle_buf_peek_at(const lv_circle_buf_t * circle_buf, uint32_t index, void * data);
178 
179 /*************************
180  *    GLOBAL VARIABLES
181  *************************/
182 
183 /**********************
184  *      MACROS
185  **********************/
186 
187 #ifdef __cplusplus
188 } /*extern "C"*/
189 #endif
190 
191 #endif /*LV_CIRCLE_BUF_H*/
192