NMEA2000 Library  0.1
Library to handle NMEA 2000 Communication written in C++
RingBuffer.h File Reference

Simple tRingBuffer and tPriorityRingBuffer template classes. More...

#include <cstdint>
#include "RingBuffer.tpp"
Include dependency graph for RingBuffer.h:

Go to the source code of this file.

Classes

class  tPriorityRingBuffer< T >
 Template Class that holds values with given priorities in a ring buffer. More...
 
struct  tPriorityRingBuffer< T >::tPriorityRef
 Structure handles meta data for each priority. More...
 
struct  tPriorityRingBuffer< T >::tValueSlot
 Structure that holds the actual value and meta data. More...
 
class  tRingBuffer< T >
 Template Class that holds values in a ring buffer. More...
 

Macros

#define INVALID_PRIORITY_REF   (uint8_t)(-1)
 Constant for an invalid priority reference. More...
 
#define INVALID_RING_REF   (uint16_t)(-1)
 Constant for an invalid index reference for the buffer. More...
 

Detailed Description

Simple tRingBuffer and tPriorityRingBuffer template classes.

With tRingBuffer one can save values to a ring buffer. Value can be simple value or data structure.

Note
You have to take care of data locking from other threads or interrupts around calls using buffer handling routines.

Example of ring buffer usage

struct tCANData {
uint32_t id;
uint8_t len;
uint8_t data[8];
};
...
void SendMessage() {
tCANData msgOut;
msgOut.id=1;
msgOut.len=1;
msgOut.data[0]=1;
lockData();
CANMessages.add(msgOut);
releaseData();
}
Template Class that holds values with given priorities in a ring buffer.
Definition: RingBuffer.h:255

As an alternative if you want to avoid data copying twice, you can request pointer to next value to be saved and copy data directly to it.

void SendMessage() {
tCANData *msgOut;
lockData();
if ( (msgOut=CANMessages.getAddRef())!=0 ) {
msgOut->id=1;
msgOut->len=1;
msgOut->data[0]=1;
}
releaseData();
}

tPriorityRingBuffer is similar as tRingBuffer, but it extends functionality with value priority. When you add values to buffer, you can give them priority and when during read you can define which priority value should be read out or read highest priority value.

Debugging

To improve the debugging of the module there is the possibility to enable some print out to the DebugStream

#define RING_BUFFER_ERROR_DEBUG
#define RING_BUFFER_DEBUG
#define RING_BUFFER_INIT_DEBUG

Definition in file RingBuffer.h.

Macro Definition Documentation

◆ INVALID_PRIORITY_REF

#define INVALID_PRIORITY_REF   (uint8_t)(-1)

Constant for an invalid priority reference.

Definition at line 260 of file RingBuffer.h.

◆ INVALID_RING_REF

#define INVALID_RING_REF   (uint16_t)(-1)

Constant for an invalid index reference for the buffer.

Definition at line 258 of file RingBuffer.h.