-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathringbuffer.c
46 lines (41 loc) · 968 Bytes
/
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
/*
* ringbuffer.c
*
* Created on: Jan 28, 2019
* Author: Rock Boynton
*
* Implements functions specified in ringbuffer.h to use a ring buffer for the purpose of storing
* key presses
*/
#include "ringbuffer.h";
#include <inttypes.h>
void put(RingBuffer* buffer, uint32_t element) {
while (!hasSpace(buffer)) {
}
buffer->buffer[buffer->put] = element;
// check if wrap needed
if (buffer->put >= BUF_SIZE) {
buffer->put = 0; // wrap back to 0
} else {
buffer->put++;
}
buffer->used++;
}
uint32_t get(RingBuffer* buffer) {
while (!hasElement(buffer)) {
}
uint32_t element = buffer->buffer[buffer->get];
if (buffer->get >= BUF_SIZE) {
buffer->get = 0;
} else {
buffer->get++;
}
buffer->used--;
return element;
}
int hasSpace(RingBuffer* buffer) {
return BUF_SIZE != buffer->used;
}
int hasElement(RingBuffer* buffer) {
return buffer->used > 0;
}