-
Notifications
You must be signed in to change notification settings - Fork 0
/
ringbuffer.c
52 lines (43 loc) · 1.25 KB
/
ringbuffer.c
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
/*********************** Includes *********************************/
#include "ringbuffer.h"
/*********************** Macros *********************************/
/*********************** Defines *********************************/
/*********************** Typedefs *********************************/
/*********************** Variables *********************************/
/*********************** Functions *********************************/
void initialize(ringBuffer_t *rb)
{
rb->head = 0;
rb->tail = 0;
rb->count = 0;
}
ringBufferStatus_e enqueue(ringBuffer_t *rb, uint32_t data)
{
if (isFull(rb))
{
return RINGBUFFER_ERROR_FULL_e;
}
rb->buffer[rb->tail] = data;
rb->tail = (rb->tail + 1) % BUFFER_SIZE;
rb->count++;
return RINGBUFFER_SUCCESS_e;
}
ringBufferStatus_e dequeue(ringBuffer_t *rb, uint32_t *data)
{
if (isEmpty(rb))
{
return RINGBUFFER_ERROR_EMPTY_e;
}
*data = rb->buffer[rb->head];
rb->head = (rb->head + 1) % BUFFER_SIZE;
rb->count--;
return RINGBUFFER_SUCCESS_e;
}
bool isFull(ringBuffer_t *rb)
{
return rb->count == BUFFER_SIZE;
}
bool isEmpty(ringBuffer_t *rb)
{
return rb->count == 0;
}