-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusart_buffer.c
More file actions
50 lines (44 loc) · 1.08 KB
/
usart_buffer.c
File metadata and controls
50 lines (44 loc) · 1.08 KB
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
/*
* 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 "usart_buffer.h";
#include <inttypes.h>
void usart_put(UsartBuffer* buffer, char 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++;
}
char usart_get(UsartBuffer* buffer) {
while (!hasElement(buffer)) {
}
char element = buffer->buffer[buffer->get];
if (buffer->get >= BUF_SIZE) {
buffer->get = 0;
} else {
buffer->get++;
}
buffer->used--;
return element;
}
int usart_hasSpace(UsartBuffer* buffer) {
return BUF_SIZE != buffer->used;
}
int usart_hasElement(UsartBuffer* buffer) {
return buffer->used > 0;
}
int usart_cmp(UsartBuffer* buffer1, UsartBuffer* buffer2) {
return memcmp(buffer1, buffer2, sizeof(buffer1));
}