-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircbuf_final.h
82 lines (69 loc) · 2.66 KB
/
circbuf_final.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* circbuf_final.h
*
* Created on: Dec 2, 2017
* Author: danie
*/
#ifndef CIRCBUF_FINAL_H_
#define CIRCBUF_FINAL_H_
typedef struct {
uint8_t* buffer; // pointer to the base of the buffer in heap
volatile uint8_t* head; // pointer to first item
volatile uint8_t* tail; // pointer to last item
volatile uint32_t num_items; // number of items in buffer
volatile uint32_t length; // max number of items the buffer can hold
} CircBuf_t;
typedef enum {
CB_NO_ERROR = 0, // no error
CB_INIT_ERROR, // failed to initialize buffer
CB_CLEAR_ERROR, // failed to clear buffer
CB_DELETE_ERROR, // failed to delete buffer
CB_ADD_ERROR, // failed to add item
CB_REMOVE_ERROR, // failed to remove item
CB_COPY_ERROR, // failed to copy buffer
ERROR // generic error
} CB_Status;
/* Setup the buffer in the heap and initialize the structure variables
* @param buf = name of a circular buffer
* @param length = initialization length
* @ret CB_NO_ERROR if everything works, else CB_INIT_ERROR
*/
CB_Status CB_initialize_buffer(CircBuf_t** buf, uint32_t length);
/* Set the contents of a buffer to 0 and reset the buffer pointers
* @param buf = name of a circular buffer
* @ret CB_NO_ERROR if everything works, else CB_CLEAR_ERROR
*/
CB_Status CB_clear_buffer(CircBuf_t* buf);
/* Delete the buffer from dynamic memory
* @param buf = name of a circular buffer
* @ret CB_NO_ERROR if everything works, else CB_DELETE_ERROR
*/
CB_Status CB_delete_buffer(CircBuf_t* buf);
/* Checks if buffer is full
* @param buf = name of a circular buffer
* @ret nonzero value if buffer is full, else 0
*/
int8_t CB_is_buffer_full(CircBuf_t* buf);
/* Checks if buffer is empty
* @param buf = name of a circular buffer
* @ret nonzero value if buffer is empty, else 0
*/
int8_t CB_is_buffer_empty(CircBuf_t* buf);
/* Add a new item to a buffer
* @param buf = name of a circular buffer
* @param item = new item to add to buffer
* @ret CB_NO_ERROR if everything works, else CB_ADD_ERROR
*/
CB_Status CB_add_item_to_buffer(CircBuf_t* buf, uint8_t item);
/* Remove an item from a buffer
* @param buf = name of a circular buffer
* @ret CB_NO_ERROR if everything works, else CB_REMOVE_ERROR
*/
uint8_t CB_remove_item_from_buffer(CircBuf_t* buf);
/* Copies contents of buffer to a char* without altering buffer
* @param buf = name of a circular buffer
* @param c = char* where contents will be stored
* @ret CB_NO_ERROR if everything works, else CB_COPY_ERROR
*/
CB_Status CB_copy_buffer(CircBuf_t* buf, char* c);
#endif /* CIRCBUF_FINAL_H_ */