MBot Software Library  v1.0
An API documentation to mbot_firmware repository
ring_buffer.h
1 
22 #ifndef RC_RING_BUFFER_H
23 #define RC_RING_BUFFER_H
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 
34 typedef struct rc_ringbuf_t {
35  double* d;
36  int size;
37  int index;
40 
41 #define RC_RINGBUF_INITIALIZER {\
42  .d = NULL,\
43  .size = 0,\
44  .index = 0,\
45  .initialized = 0}
46 
59 
73 int rc_ringbuf_alloc(rc_ringbuf_t* buf, int size);
74 
86 
96 
109 int rc_ringbuf_insert(rc_ringbuf_t* buf, double val);
110 
124 double rc_ringbuf_get_value(rc_ringbuf_t* buf, int position);
125 
137 double rc_ringbuf_std_dev(rc_ringbuf_t buf);
138 
139 
140 #ifdef __cplusplus
141 }
142 #endif
143 
144 #endif // RC_RING_BUFFER_H
145 
int rc_ringbuf_alloc(rc_ringbuf_t *buf, int size)
Allocates memory for a ring buffer and initializes an rc_ringbuf_t struct.
Definition: ring_buffer.c:37
double rc_ringbuf_std_dev(rc_ringbuf_t buf)
Returns the standard deviation of all values in the ring buffer.
Definition: ring_buffer.c:144
double rc_ringbuf_get_value(rc_ringbuf_t *buf, int position)
Fetches the float which is 'position' steps behind the last value added to the buffer.
Definition: ring_buffer.c:121
rc_ringbuf_t rc_ringbuf_empty(void)
Returns an rc_ringbuf_t struct which is completely zero'd out with no memory allocated for it.
Definition: ring_buffer.c:30
int rc_ringbuf_insert(rc_ringbuf_t *buf, double val)
Puts a new float into the ring buffer and updates the index accordingly.
Definition: ring_buffer.c:99
int rc_ringbuf_reset(rc_ringbuf_t *buf)
Sets all values in the buffer to 0.0f and sets the buffer index back to 0.
Definition: ring_buffer.c:81
int rc_ringbuf_free(rc_ringbuf_t *buf)
Frees the memory allocated for buffer buf.
Definition: ring_buffer.c:68
struct rc_ringbuf_t rc_ringbuf_t
Struct containing state of a ringbuffer and pointer to dynamically allocated memory.
Struct containing state of a ringbuffer and pointer to dynamically allocated memory.
Definition: ring_buffer.h:34
int index
index of the most recently added value
Definition: ring_buffer.h:37
double * d
pointer to dynamically allocated data
Definition: ring_buffer.h:35
int initialized
flag indicating if memory has been allocated for the buffer
Definition: ring_buffer.h:38
int size
number of elements the buffer can hold
Definition: ring_buffer.h:36