BleSerialPeripheralRK
|
Thread and interrupt-safe (with caveats) circular buffer (ring buffer) class. More...
#include <RingBuffer.h>
Public Member Functions | |
RingBuffer (T *elems, size_t size) | |
Construct a buffer of size elements of T. More... | |
~RingBuffer () | |
Destructor. | |
size_t | availableForRead () const |
Returns the number of elements that can be read right now (0 = nothing can be read right now) More... | |
T * | preRead () const |
Non-copy version of read. Returns a pointer to the data to be read next or NULL if there is no data right now. More... | |
void | postRead () |
Indicates that you have finished reading the data in the pointer returned by preRead() and it can be reused. More... | |
bool | read (T *elem) |
Read with copy. You can use this instead of preRead() and postRead(). More... | |
void | readClear () |
Clear outstanding entries, called from the read thread. | |
T * | preWrite () const |
Non-copy version of write. Returns a pointer to the buffer to write to or NULL if there is no space. More... | |
void | postWrite () |
Commits the write. Only call if preWrite() returned a non-NULL value. | |
bool | write (const T *elem) |
Write with copy. You can use this instead of preWrite() and postWrite(). elem is copied. More... | |
Thread and interrupt-safe (with caveats) circular buffer (ring buffer) class.
Home: https://github.com/rickkas7/SerialBufferRK License: MIT This class assumes a single reader thread and a single writer thread (or interrupt). For example, it works great if you read out of loop() and write from a single interrupt handler. It is not safe for multiple reader or multiple writer use cases!
Assumption: Writing a size_t value is atomic. It definitely is safe on ARM processors.
|
inline |
Construct a buffer of size elements of T.
elems | Pointer to a buffer of size elements of type T |
size | Number of elements |
|
inline |
Returns the number of elements that can be read right now (0 = nothing can be read right now)
This is mainly for informational purposes. It's more efficient to call preRead() and check for a non-NULL return value than it is to call availableForRead().
|
inline |
Indicates that you have finished reading the data in the pointer returned by preRead() and it can be reused.
Only call postRead() if preRead() returned a non-null value!
|
inline |
Non-copy version of read. Returns a pointer to the data to be read next or NULL if there is no data right now.
If preRead() returns a non-null value you must call postRead() to consume the data, otherwise the next time you call preRead() you'll get the same data back. Don't call postRead() if you get NULL back from preRead()!
It's OK to not call postRead() if you're doing a peek at the data - look at the data that will be read without removing it.
|
inline |
Non-copy version of write. Returns a pointer to the buffer to write to or NULL if there is no space.
If preWrite() returns a non-null value you must call postWrite() to commit the data, otherwise the data will not be saved. Don't call postWrite() if you get NULL back from preWrite()!
|
inline |
Read with copy. You can use this instead of preRead() and postRead().
elem | Filled in with the data that was read. Left unchanged if there's no data to be read. |
|
inline |
Write with copy. You can use this instead of preWrite() and postWrite(). elem is copied.