AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
|
This class provides an efficient ring buffer implementation for storing bytes. Ring buffers are particularly useful for memory constrained microcontrollers such as the ATmega328 and ATmega2650. For maximum efficiency, this class is focused on the storgage of bytes, providing a single code base that is shared by all instances of this class. More...
#include <RingBuffer.h>
Public Member Functions | |
RingBuffer (unsigned char *buffer, unsigned short size) | |
Construct a ring buffer by providing the storage area for the ring buffer. More... | |
int | pull () |
Extract the next (first) byte from the ring buffer. More... | |
int | peek (unsigned short index=0) |
Examine an element in the ring buffer. More... | |
bool | push (unsigned char element) |
Push a byte into the ring buffer. The element is appended to the back of the buffer. More... | |
bool | isFull () |
Determine if the buffer is full and cannot accept more bytes. More... | |
bool | isNotFull () |
Determine if the buffer is not full and can accept more bytes. More... | |
bool | isEmpty () |
Determine if the buffer is empty . More... | |
bool | isNotEmpty () |
Determine if the buffer is not empty. More... | |
void | clear () |
Clear the ring buffer, leaving it empty. | |
This class provides an efficient ring buffer implementation for storing bytes. Ring buffers are particularly useful for memory constrained microcontrollers such as the ATmega328 and ATmega2650. For maximum efficiency, this class is focused on the storgage of bytes, providing a single code base that is shared by all instances of this class.
For maximum flexiblity, the caller must provide the storage to be used for each RingBuffer object instantiated (this allows the use of different sized ring bufferss without having to make dynamic memory allocations).
The implementation of RingBuffer is interrupt safe: the key operations are atomic, allowing for RingBuffer objects to be shared between interrupt functions and ordinary code.
The template-based RingBufferT class provides a more flexible ring buffer implementation that can store a variety of data types. However, this comes at the cost of replicating code for each template instantiation of RingBufferT.
RingBuffer::RingBuffer | ( | unsigned char * | buffer, |
unsigned short | size | ||
) |
Construct a ring buffer by providing the storage area for the ring buffer.
buffer
the storage for the ring buffer. size
the size of the storage for the ring buffer.
|
inline |
Determine if the buffer is empty .
bool RingBuffer::isFull | ( | ) |
Determine if the buffer is full and cannot accept more bytes.
|
inline |
Determine if the buffer is not empty.
bool RingBuffer::isNotFull | ( | ) |
Determine if the buffer is not full and can accept more bytes.
int RingBuffer::peek | ( | unsigned short | index = 0 | ) |
Examine an element in the ring buffer.
index
the element to examine; 0 means the first (= next) element in the buffer. The default if the argument is omitted is to return the first element.int RingBuffer::pull | ( | ) |
Extract the next (first) byte from the ring buffer.
bool RingBuffer::push | ( | unsigned char | element | ) |
Push a byte into the ring buffer. The element is appended to the back of the buffer.
element
is the byte to append to the ring buffer.