-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathstRingBuffer.cpp
96 lines (77 loc) · 2.14 KB
/
stRingBuffer.cpp
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "stRingBuffer.h"
stRingBuffer::stRingBuffer(uint32_t pBufferSize)
{
vcRBwindex = 0 ; // Fill pointer in ringbuffer
vcRBrindex = pBufferSize - 1 ; // Emptypointer in ringbuffer
vcRingBufferSize = pBufferSize;
vcRBcount = 0;
vcRingBuffer = (uint8_t *) malloc ( vcRingBufferSize ) ; // Create ring buffer
}
stRingBuffer::~stRingBuffer()
{
free(vcRingBuffer), vcRingBuffer = 0;
}
uint32_t stRingBuffer::getBufferSize()
{
return vcRingBufferSize;
}
void stRingBuffer::PrintRingBuffer(uint32_t pSize)
{
Serial.print("Array : ");
if(pSize > dataSize())
pSize = dataSize();
for(uint32_t i = 0; i < pSize; i++)
{
Serial.print((unsigned char) *(vcRingBuffer + vcRBrindex +i) , HEX);
}
Serial.println("");
}
void stRingBuffer::clearBuffer()
{
vcRBwindex = 0 ; // Reset ringbuffer administration
vcRBrindex = vcRingBufferSize - 1 ;
vcRBcount = 0 ;
}
uint8_t stRingBuffer::readDataAt(uint16_t x)
{
return *(vcRingBuffer + vcRBrindex + x);
}
/**
* Return true if free space in the buffer for at least one byte
*/
bool stRingBuffer::isFreeSpace()
{
return ( vcRBcount < vcRingBufferSize ) ;
}
/**
* Return data size in the buffer
*/
uint32_t stRingBuffer::dataSize()
{
return vcRBcount;
}
/**
* Push one byte of data to the ringbuffer
*/
void stRingBuffer::putData(uint8_t pData)
{
// No check on available space. See ringspace()
*(vcRingBuffer + vcRBwindex) = pData ; // Put byte in ringbuffer
if ( ++vcRBwindex == vcRingBufferSize ) // Increment pointer and
{
vcRBwindex = 0 ; // wrap at end
}
vcRBcount++ ; // Count number of bytes in the
}
/**
* Retreive one byte of data from the ringbuffer
*/
uint8_t stRingBuffer::getData()
{
if ( ++vcRBrindex == vcRingBufferSize ) // Increment pointer and
{
vcRBrindex = 0 ; // wrap at end
}
vcRBcount-- ; // Count is now one less
return *(vcRingBuffer + vcRBrindex) ; // return the oldest byte
}