Buffer.hpp

Submitted by Webbot on January 1, 2009 - 11:49pm.

This class wraps a character array into a buffer that may be accessed in a thread safe way. IE it can be accessed by your main program, as well as by interrupts. One of its main uses is to create a buffer for accessing the UART whereby you can read/write characters at the same time as the UART reads/writes characters. Or it may be used to write info to a logging output which may go to your PC, an LCD display etc.

 

To create a 48 byte buffer:-

uint8_t  byteBuffer[48];       // allocate the space

BUFFER buffer(byteBuffer);   // wrap it into a safe buffer

 

Methods:

uint8_t get();                     // Get the next byte and remove it from the buffer, or 0 if it is empty

uint8_t get(uint16_t index);  // Get the value from a given index (0=current), but don't remove it

void skip(uint16_t bytes);    // Remove the next 'bytes' elements from the buffer

void flush();                      // Clear the buffer

bool isFull();                      // return 'true' if the buffer is full

bool isEmpty();                  // return 'true' if the buffer is empty

bool put(uint8_t byte);       // try to add 'byte' to the buffer. Return false if the buffer was full, or true if it was added ok