-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrbuf.c
70 lines (52 loc) · 2.15 KB
/
rbuf.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "rbuf.h"
uint8_t rbuf_push(rbuf_t *c, uint8_t data) {
uint8_t next;
next = c->head + 1; // next is where head will point to after this write.
if (next >= c->maxlen)
next = 0;
if (next == c->tail) // if the head + 1 == tail, circular buffer is full
return 0;
c->buffer[c->head] = data; // Load data and then move
c->head = next; // head to next data offset.
return 1; // return success to indicate successful push.
}
uint8_t rbuf_push_isr(rbuf_t *c, uint8_t data) {
uint8_t next;
next = c->head + 1; // next is where head will point to after this write.
if (next >= c->maxlen)
next = 0;
if (next == c->tail) // if the head + 1 == tail, circular buffer is full
return 0;
c->buffer[c->head] = data; // Load data and then move
c->head = next; // head to next data offset.
return 1; // return success to indicate successful push.
}
uint8_t rbuf_pop(rbuf_t *c, uint8_t *data) {
uint8_t next;
if (c->head == c->tail) // if the head == tail, we don't have any data
return 0;
next = c->tail + 1; // next is where tail will point to after this read.
if(next >= c->maxlen)
next = 0;
*data = c->buffer[c->tail]; // Read data and then move
c->tail = next; // tail to next offset.
return 1; // return success to indicate successful push.
}
uint8_t rbuf_pop_isr(rbuf_t *c, uint8_t *data) {
uint8_t next;
if (c->head == c->tail) // if the head == tail, we don't have any data
return 0;
next = c->tail + 1; // next is where tail will point to after this read.
if(next >= c->maxlen)
next = 0;
*data = c->buffer[c->tail]; // Read data and then move
c->tail = next; // tail to next offset.
return 1; // return success to indicate successful push.
}
uint8_t rbuf_free_items(rbuf_t *c) {
if (c->head >= c->tail) {
uint8_t tmp = c->maxlen - c->head;
return (tmp + c->tail);
}
return (c->tail - c->head);
}