16#define LOGCircularBufferDebug LOGvv
18#define CS_CIRCULAR_BUFFER_INDEX_NOT_FOUND 0xFFFF
40 uint16_t
getMaxSize(uint16_t byteSize) {
return byteSize /
sizeof(T); }
220 uint16_t
find(
const T& value)
const {
221 for (uint16_t index = 0; index <
size(); ++index) {
222 if (
operator[](index) == value) {
Circular Buffer implementation.
Definition: cs_CircularBuffer.h:27
virtual ~CircularBuffer()
Default destructor.
Definition: cs_CircularBuffer.h:36
uint16_t size() const
Returns the number of elements stored.
Definition: cs_CircularBuffer.h:132
bool release()
Release the buffer that was assigned.
Definition: cs_CircularBuffer.h:101
void clear()
Clears the buffer.
Definition: cs_CircularBuffer.h:122
uint16_t getMaxByteSize(uint16_t capacity)
Definition: cs_CircularBuffer.h:38
bool full() const
Checks if the buffer is full.
Definition: cs_CircularBuffer.h:153
uint16_t getMaxByteSize()
Definition: cs_CircularBuffer.h:39
uint16_t getMaxSize(uint16_t byteSize)
Definition: cs_CircularBuffer.h:40
CircularBuffer(uint16_t capacity)
Default constructor.
Definition: cs_CircularBuffer.h:31
uint16_t find(const T &value) const
Find a value in the buffer.
Definition: cs_CircularBuffer.h:220
bool _allocatedSelf
Whether the array was allocated by init() or not.
Definition: cs_CircularBuffer.h:246
uint16_t capacity() const
Returns the capacity of the buffer.
Definition: cs_CircularBuffer.h:141
void incHead()
Increases the head.
Definition: cs_CircularBuffer.h:264
bool isInitialized()
Returns true when the buffer has been allocated, either via init() or via assign().
Definition: cs_CircularBuffer.h:112
bool empty() const
Checks if the buffer is empty.
Definition: cs_CircularBuffer.h:147
uint16_t _capacity
The capacity of the buffer (maximum number of elements)
Definition: cs_CircularBuffer.h:234
const T & pop()
Get the oldest element.
Definition: cs_CircularBuffer.h:192
uint16_t _head
Index of the head (next element to be removed)
Definition: cs_CircularBuffer.h:237
T * getBuffer()
Definition: cs_CircularBuffer.h:114
bool assign(buffer_ptr_t buffer, uint16_t bufferSize)
Assign the buffer used to store the data, instead of allocating it via init().
Definition: cs_CircularBuffer.h:84
T & operator[](uint16_t idx) const
Returns the Nth value, starting from oldest element.
Definition: cs_CircularBuffer.h:212
bool pushUnique(const T &value)
Add an element to the end of the buffer, but only when it's not already in the buffer.
Definition: cs_CircularBuffer.h:177
bool init()
Initializes and allocates memory for the buffer based on the capacity.
Definition: cs_CircularBuffer.h:48
bool deinit()
Definition: cs_CircularBuffer.h:69
void incTail()
Increases the tail.
Definition: cs_CircularBuffer.h:253
void push(const T &value)
Add an element to the end of the buffer.
Definition: cs_CircularBuffer.h:163
uint16_t _contentsSize
Number of elements stored in the buffer.
Definition: cs_CircularBuffer.h:243
uint16_t _tail
Index of the tail (where the next element will be inserted)
Definition: cs_CircularBuffer.h:240
T & peek() const
Peek at the oldest element without removing it.
Definition: cs_CircularBuffer.h:206
T * _array
Pointer to the array storing the elements.
Definition: cs_CircularBuffer.h:231
#define LOGCircularBufferDebug
Definition: cs_CircularBuffer.h:16
#define CS_CIRCULAR_BUFFER_INDEX_NOT_FOUND
Definition: cs_CircularBuffer.h:18
#define LOGw(fmt,...)
Definition: cs_Logger.h:92
#define LOGd(fmt,...)
Definition: cs_Logger.h:90
#define FMT_ALLOCATE_MEMORY
Definition: cs_Strings.h:186
#define FMT_ERR_ASSIGN_BUFFER
Definition: cs_Strings.h:187
#define STR_ERR_ALLOCATE_MEMORY
Definition: cs_Strings.h:102
#define FMT_ASSIGN_BUFFER_LEN
Definition: cs_Strings.h:188
uint8_t * buffer_ptr_t
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: 10 May....
Definition: cs_Typedefs.h:19