AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Public Member Functions | List of all members
RingBuffer Class Reference

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.
 

Detailed Description

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.

Constructor & Destructor Documentation

§ RingBuffer()

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.

Member Function Documentation

§ isEmpty()

bool RingBuffer::isEmpty ( )
inline

Determine if the buffer is empty .

Returns
true if the buffer is empty; false if not.

§ isFull()

bool RingBuffer::isFull ( )

Determine if the buffer is full and cannot accept more bytes.

Returns
true if the buffer is full; false if not.

§ isNotEmpty()

bool RingBuffer::isNotEmpty ( )
inline

Determine if the buffer is not empty.

Returns
true if the buffer is not empty; false if it is empty.

§ isNotFull()

bool RingBuffer::isNotFull ( )

Determine if the buffer is not full and can accept more bytes.

Returns
true if the buffer is not full; false if it is full.

§ peek()

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.
Returns
the next element or -1 if there is no such element.

§ pull()

int RingBuffer::pull ( )

Extract the next (first) byte from the ring buffer.

Returns
the next byte, or -1 if the ring buffer is empty.

§ push()

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.
Returns
0 (false) if it succeeds; 1 (true) if it fails because the buffer is full.

The documentation for this class was generated from the following files: