Bluenet
5.7.0
Bluenet, firmware for nRF52 smart home devices
|
Differential Buffer implementation. More...
#include <cs_CircularDifferentialBuffer.h>
Public Member Functions | |
CircularDifferentialBuffer (uint16_t capacity=32, bool initialize=false) | |
Default constructor @capacity the size with which the buffer should be initialized. More... | |
virtual | ~CircularDifferentialBuffer () |
Default destructor. More... | |
bool | init () |
Initializes and allocates memory for the buffer based on the capacity used with the constructor. More... | |
void | clear () |
Clears the buffer. More... | |
uint16_t | size () const |
Returns the number of elements stored. More... | |
uint16_t | capacity () const |
Returns the capacity of the buffer. More... | |
bool | empty () const |
Checks if the buffer is empty. More... | |
bool | full () const |
Checks if the buffer is full. More... | |
T | peek () const |
Peek at the oldest element without removing it. More... | |
bool | getFirstElement (T &val) |
Get the first element of the buffer @val the value of the oldest element. More... | |
bool | getNextElement (T &val) |
Get the next value of the buffer, after calling <CircularDifferentialBuffer>>getFirstElement()> More... | |
bool | getLastElement (T &val) |
Get the first element of the buffer @val the value of the oldest element. More... | |
uint16_t | getSerializedLength () const |
Get the serialized length. More... | |
void | serialize (uint8_t *buffer) |
Write all elements to a buffer @buffer buffer to which to write the elements. More... | |
T | pop () |
Get and remove the oldest element. More... | |
bool | push (T value) |
Add an element to the end of the buffer @value the element to be added. More... | |
Private Member Functions | |
void | incTail () |
Increases the tail. More... | |
void | incHead () |
Increases the head. More... | |
Private Attributes | |
int8_t * | _array |
Pointer to the array storing the difference of elements compared to the previous element. More... | |
uint16_t | _capacity |
The capacity of the buffer (maximum number of elements) More... | |
uint16_t | _head |
Index of the head (next element to be removed) More... | |
uint16_t | _tail |
Index of the tail (where the next element will be inserted) More... | |
uint16_t | _contentSize |
Number of elements stored in the buffer. More... | |
T | _firstVal |
Value of the oldest element in the buffer. More... | |
T | _lastVal |
Value of the newest element in the buffer. More... | |
uint16_t | _readIdx |
Index of the last read value, used in getNextElement() More... | |
Differential Buffer implementation.
T | primitive type such as uint8_t |
Elements are added at the back and removed from the front. If the capacity of the buffer is reached, the oldest element will be overwritten.
|
inline |
Default constructor @capacity the size with which the buffer should be initialized.
A maximum of capacity elements can be stored in the buffer before the oldest element will be overwritten @initialize if true, the array will be directly initialized, otherwise <CircularDifferentialBuffer::init()> has to be called before accessing the buffer
If <initialize> is set to true, the buffer will be directly initialized. To avoid unnecessary allocation of memory, use initialize = false and call <CircularDifferentialBuffer::init()> only once the buffer is used.
|
inlinevirtual |
Default destructor.
Free memory allocated for the buffer
|
inline |
Returns the capacity of the buffer.
The capacity is the maximum number of elements that can be stored in the buffer
|
inline |
Clears the buffer.
The buffer is cleared by setting head and tail to the beginning of the buffer. The array itself doesn't have to be cleared
|
inline |
Checks if the buffer is empty.
|
inline |
Checks if the buffer is full.
|
inline |
Get the first element of the buffer @val the value of the oldest element.
|
inline |
Get the first element of the buffer @val the value of the oldest element.
|
inline |
Get the next value of the buffer, after calling <CircularDifferentialBuffer>>getFirstElement()>
@val the previous value, will be set to the value of the next element
|
inline |
Get the serialized length.
|
inlineprivate |
Increases the head.
Decreases the contentsSize and increases the index of the head. It also wraps around the head if the end of the array is reached.
|
inlineprivate |
Increases the tail.
Increases the contentsSize and the index of the tail. It also wraps the tail around if the end of the array is reached.
|
inline |
Initializes and allocates memory for the buffer based on the capacity used with the constructor.
If the constructor was called with initialize = false, then this function has to be called before the buffer can be accessed!
|
inline |
Peek at the oldest element without removing it.
This returns the value of the oldest element without removing the element from the buffer. Use <CircularBuffer>>pop()> to get the value and remove it at the same time
|
inline |
Get and remove the oldest element.
This returns the value of the oldest element and removes it from the buffer
|
inline |
Add an element to the end of the buffer @value the element to be added.
Elements are added at the end of the buffer and removed from the beginning. If the buffer is full the oldest element will be overwritten.
void CircularDifferentialBuffer< T >::serialize | ( | uint8_t * | buffer | ) |
Write all elements to a buffer @buffer buffer to which to write the elements.
The buffer has to be of the correct size, use <CircularDifferentialBuffer>>getSerializedLength()> First sizeof(T) bytes is the first value, the bytes afterwards are increments (int8_t).
|
inline |
Returns the number of elements stored.
|
private |
Pointer to the array storing the difference of elements compared to the previous element.
|
private |
The capacity of the buffer (maximum number of elements)
|
private |
Number of elements stored in the buffer.
|
private |
Value of the oldest element in the buffer.
|
private |
Index of the head (next element to be removed)
|
private |
Value of the newest element in the buffer.
|
private |
Index of the last read value, used in getNextElement()
|
private |
Index of the tail (where the next element will be inserted)