-
Notifications
You must be signed in to change notification settings - Fork 44
/
ringBuf.c
81 lines (73 loc) · 2.05 KB
/
ringBuf.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
71
72
73
74
75
76
77
78
79
80
81
#include "ringBuf.h"
/*
* Initialize ringBuf_t struct
* ringBuf_t instance must be declared
* before the call of this function
*/
void rBufInit(ringBuf_t *_this)
{
// memset(_this->buf, 0, RING_BUF_SIZE);
// _this->head = 0;
// _this->tail = 0;
// or
memset(_this, 0, sizeof(*_this));
}
/*
* Set all data to zero (technically same as rBufInit)
*/
void rBufFlush(ringBuf_t *_this)
{
memset(_this, 0, sizeof(*_this)); // or //rBufInit(_this);
}
/*
* Push data to buf[head] and increment head
* if head + 1 exceeds BUF_SIZE, null it
* if head + 1, just return because buffer is full
*/
void rBufPushFront(ringBuf_t *_this, uint16_t data)
{
// uint8_t next = _this->head + 1;
// if (next >= RING_BUF_SIZE)
// next = 0;
uint8_t next = (_this->head + 1) & RING_BUF_MASK;
// Ring buffer is full
if (rBufIsFull(_this)) // (next == _this->tail)
return; // -1;
_this->buf[_this->head] = data;
_this->head = next;
}
/*
* Pop buf[tail] element and increment tail
* if tail == head, just return because buffer is empty
* if tail exceeds BUF_SIZE, null it
*/
void rBufPopBack(ringBuf_t *_this, uint16_t *data)
{
if (rBufIsEmpty(_this)) // (_this->head == _this->tail)
return; // -1;
*data = _this->buf[_this->tail]; // Store value by pointer
_this->buf[_this->tail] = 0; // Clear the data, optional
// Update index with masking
_this->tail = (_this->tail + 1) & RING_BUF_MASK;
}
/*
* Read buf[tail] element, but do not move tail or overwrite the element
*/
void rBufPeekBack(ringBuf_t *_this, uint16_t *data)
{
if (rBufIsEmpty(_this)) // (_this->head == _this->tail)
return;
*data = _this->buf[_this->tail];
}
uint8_t rBufIsEmpty(ringBuf_t *_this)
{
return ( _this->head == _this->tail );
}
uint8_t rBufIsFull(ringBuf_t *_this)
{
return ( _this->head + 1 == _this->tail );
}
uint8_t rBufElemCount(ringBuf_t *_this)
{
return (( _this->head - _this->tail ) & RING_BUF_MASK);
}