Wirepas SDK
ringbuffer.h File Reference

Detailed Description

Example:

#define BUFFER_SIZE 128
#include "ringbuffer.h"
static ringbuffer_t RxBuffer = { 0, 0, }; //tbuf.in = 0 tbuf.out = 0
static void StdioRxHandler(uint8_t ch);
int16_t StdioReceive(void)
{
int16_t ret;
EnterCriticalSection();
if (Ringbuffer_elements(RxBuffer) == 0)
{
ret = (-1);
// Buffer empty -> get out
}
else
{
ret = Ringbuffer_getTailByte(RxBuffer);
Ringbuffer_incrementTail(RxBuffer, 1);
}
ExitCriticalSection();
return ret;
}
static void StdioRxHandler(uint8_t ch)
{
if (Ringbuffer_elements(RxBuffer) == 0)
{
Ringbuffer_getHeadByte(RxBuffer) = (uint8_t) (ch & 0xFF);
Ringbuffer_incrementHead(RxBuffer,1);
}
}

Definition in file ringbuffer.h.

Go to the source code of this file.

Data Structures

struct  ringbuffer_t
 

Macros

#define MASK   (BUFFER_SIZE-1)
 
#define Ringbuffer_size(p)   (BUFFER_SIZE)
 
#define Ringbuffer_usage(p)   ((uint16_t)((p).in - (p).out))
 
#define Ringbuffer_free(p)   (Ringbuffer_size(p)-Ringbuffer_usage(p))
 
#define Ringbuffer_contents(p)   ((p).buf)
 
#define Ringbuffer_reset(p)   do{ (p).out = 0; (p).in = 0; }while(false)
 
#define Ringbuffer_isReset(p)   ((p).out == 0 && (p).in == 0)
 
#define Ringbuffer_getHeadIdx(p)   ((p).in & MASK)
 
#define Ringbuffer_getHeadByte(p)   ((p).buf[Ringbuffer_getHeadIdx(p)])
 
#define Ringbuffer_getHeadPtr(p)   (uint8_t *)(&Ringbuffer_getHeadByte(p))
 
#define Ringbuffer_incrHead(p, a)   ((p).in += (a))
 
#define Ringbuffer_getTailIdx(p)   ((p).out & MASK)
 
#define Ringbuffer_getTailByte(p)   ((p).buf[Ringbuffer_getTailIdx(p)])
 
#define Ringbuffer_getTailPtr(p)   (uint8_t *)(&Ringbuffer_getTailByte(p))
 
#define Ringbuffer_incrTail(p, a)   ((p).out += (a))
 

Data Structure Documentation

◆ ringbuffer_t

struct ringbuffer_t

Definition at line 30 of file ringbuffer.h.

Data Fields
uint8_t buf[BUFFER_SIZE]
uint16_t in
uint16_t out

Macro Definition Documentation

◆ MASK

#define MASK   (BUFFER_SIZE-1)

Definition at line 28 of file ringbuffer.h.

◆ Ringbuffer_contents

#define Ringbuffer_contents (   p)    ((p).buf)

Access the contents of the buffer

Definition at line 45 of file ringbuffer.h.

◆ Ringbuffer_free

#define Ringbuffer_free (   p)    (Ringbuffer_size(p)-Ringbuffer_usage(p))

Number of free bytes

Definition at line 42 of file ringbuffer.h.

◆ Ringbuffer_getHeadByte

#define Ringbuffer_getHeadByte (   p)    ((p).buf[Ringbuffer_getHeadIdx(p)])

Byte at the current head position

Definition at line 55 of file ringbuffer.h.

◆ Ringbuffer_getHeadIdx

#define Ringbuffer_getHeadIdx (   p)    ((p).in & MASK)

Array index of the head

Definition at line 53 of file ringbuffer.h.

◆ Ringbuffer_getHeadPtr

#define Ringbuffer_getHeadPtr (   p)    (uint8_t *)(&Ringbuffer_getHeadByte(p))

Pointer to the current head position

Definition at line 57 of file ringbuffer.h.

◆ Ringbuffer_getTailByte

#define Ringbuffer_getTailByte (   p)    ((p).buf[Ringbuffer_getTailIdx(p)])

Byte at the current tail position

Definition at line 64 of file ringbuffer.h.

◆ Ringbuffer_getTailIdx

#define Ringbuffer_getTailIdx (   p)    ((p).out & MASK)

Array index of the tail

Definition at line 62 of file ringbuffer.h.

◆ Ringbuffer_getTailPtr

#define Ringbuffer_getTailPtr (   p)    (uint8_t *)(&Ringbuffer_getTailByte(p))

Pointer to the current tail position

Definition at line 66 of file ringbuffer.h.

◆ Ringbuffer_incrHead

#define Ringbuffer_incrHead (   p,
 
)    ((p).in += (a))

Move head forward by 'a' bytes

Definition at line 59 of file ringbuffer.h.

◆ Ringbuffer_incrTail

#define Ringbuffer_incrTail (   p,
 
)    ((p).out += (a))

Move tail forward by 'a' bytes

Definition at line 68 of file ringbuffer.h.

◆ Ringbuffer_isReset

#define Ringbuffer_isReset (   p)    ((p).out == 0 && (p).in == 0)

Check if buffer is reset

Definition at line 50 of file ringbuffer.h.

◆ Ringbuffer_reset

#define Ringbuffer_reset (   p)    do{ (p).out = 0; (p).in = 0; }while(false)

Reset the buffer

Definition at line 48 of file ringbuffer.h.

◆ Ringbuffer_size

#define Ringbuffer_size (   p)    (BUFFER_SIZE)

Total capacity of the buffer in bytes

Definition at line 38 of file ringbuffer.h.

◆ Ringbuffer_usage

#define Ringbuffer_usage (   p)    ((uint16_t)((p).in - (p).out))

Number of used bytes

Definition at line 40 of file ringbuffer.h.

ringbuffer.h
Ringbuffer_getHeadByte
#define Ringbuffer_getHeadByte(p)
Definition: ringbuffer.h:55
Ringbuffer_getTailByte
#define Ringbuffer_getTailByte(p)
Definition: ringbuffer.h:64
ringbuffer_t
Definition: ringbuffer.h:30