-
Notifications
You must be signed in to change notification settings - Fork 9
/
CircularBuffer.cpp
104 lines (91 loc) · 2.16 KB
/
CircularBuffer.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
97
98
99
100
101
102
103
104
#include "CircularBuffer.h"
#include <stdint.h>
//****************************************************************************//
//
// Circular buffer
//
//****************************************************************************//
//Construct a CircularBuffer type with arguments
// uint16_t inputSize: number of elements
CircularBuffer::CircularBuffer(uint16_t inputSize)
{
cBufferData = new float[inputSize];
cBufferLastPtr = 0;
cBufferElementsUsed = 0;
cBufferSize = inputSize;
}
CircularBuffer::~CircularBuffer()
{
delete[] cBufferData;
}
//Get an element at some depth into the circular buffer
//zero is the push location. Max is cBufferSize - 1
//
//Arguments:
// uint16_t elementNum: number of element in
//
float CircularBuffer::getElement( uint16_t elementNum )
{
//Translate elementNum into terms of cBufferLastPtr.
int16_t virtualElementNum;
virtualElementNum = cBufferLastPtr - elementNum;
if( virtualElementNum < 0 )
{
virtualElementNum += cBufferSize;
}
//Output the value
return cBufferData[virtualElementNum];
}
//Put a new element into the buffer.
//This also expands the size up to the max size
//Arguments:
//
// int16_t elementVal: value of new element
//
void CircularBuffer::pushElement( float elementVal )
{
//inc. the pointer
cBufferLastPtr++;
//deal with roll
if( cBufferLastPtr >= cBufferSize )
{
cBufferLastPtr = 0;
}
//write data
cBufferData[cBufferLastPtr] = elementVal;
//increase length up to cBufferSize
if( cBufferElementsUsed < cBufferSize )
{
cBufferElementsUsed++;
}
}
//Averages the last n numbers and provides that. Discards fractions
float CircularBuffer::averageLast( uint16_t numElements )
{
if( numElements < recordLength() )
{
numElements = recordLength();
}
//Add up all the elements
float accumulator = 0;
int8_t i;
for( i = 0; i < numElements; i++ )
{
accumulator += getElement( i );
}
//Divide by number of elements
if( numElements != 0 )
{
accumulator /= numElements;
}
else
{
accumulator = 0;
}
return accumulator;
}
//Returns the current size of the buffer
uint16_t CircularBuffer::recordLength( void )
{
return cBufferElementsUsed;
}