APM:Libraries
Classes | Typedefs | Functions
ring_buffer.h File Reference

Simple circular buffer. More...

#include "hal_types.h"
Include dependency graph for ring_buffer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  ring_buffer
 

Typedefs

typedef struct ring_buffer ring_buffer
 

Functions

static void rb_init (ring_buffer *rb, uint16_t size, uint8_t *buf)
 
static uint16_t rb_full_count (ring_buffer *rb)
 Return the number of elements stored in the ring buffer. More...
 
static int rb_is_full (ring_buffer *rb)
 Returns true if and only if the ring buffer is full. More...
 
static int rb_is_empty (ring_buffer *rb)
 Returns true if and only if the ring buffer is empty. More...
 
static void rb_insert (ring_buffer *rb, uint8_t element)
 
static uint8_t rb_remove (ring_buffer *rb)
 Remove and return the first item from a ring buffer. More...
 
static int16_t rb_safe_remove (ring_buffer *rb)
 Attempt to remove the first item from a ring buffer. More...
 
static int rb_safe_insert (ring_buffer *rb, uint8_t element)
 Attempt to insert an element into a ring buffer. More...
 
static int rb_push_insert (ring_buffer *rb, uint8_t element)
 Append an item onto the end of a non-full ring buffer. More...
 
static void rb_reset (ring_buffer *rb)
 Discard all items from a ring buffer. More...
 

Detailed Description

Simple circular buffer.

This implementation is not thread-safe. In particular, none of these functions is guaranteed re-entrant.

Definition in file ring_buffer.h.

Typedef Documentation

◆ ring_buffer

typedef struct ring_buffer ring_buffer

Ring buffer type.

The buffer is empty when head == tail.

The buffer is full when the head is one byte in front of the tail, modulo buffer length.

One byte is left free to distinguish empty from full.

Function Documentation

◆ rb_full_count()

static uint16_t rb_full_count ( ring_buffer rb)
inlinestatic

Return the number of elements stored in the ring buffer.

Parameters
rbBuffer whose elements to count.

Definition at line 84 of file ring_buffer.h.

Referenced by F4Light::UART_PPM::available(), usart_data_available(), usart_txfifo_freebytes(), and usart_txfifo_nbytes().

Here is the caller graph for this function:

◆ rb_init()

static void rb_init ( ring_buffer rb,
uint16_t  size,
uint8_t *  buf 
)
inlinestatic

Initialise a ring buffer.

Parameters
rbInstance to initialise
sizeNumber of items in buf. The ring buffer will always leave one element unoccupied, so the maximum number of elements it can store will be size - 1. Thus, size must be at least 2.
bufBuffer to store items into

Definition at line 73 of file ring_buffer.h.

Referenced by F4Light::UART_PPM::UART_PPM(), usart_setup(), and VCP_Init().

Here is the caller graph for this function:

◆ rb_insert()

static void rb_insert ( ring_buffer rb,
uint8_t  element 
)
inlinestatic

Append element onto the end of a ring buffer.

Parameters
rbBuffer to append onto.
elementValue to append.

Definition at line 118 of file ring_buffer.h.

Referenced by rb_push_insert(), rb_safe_insert(), and usart_tx().

Here is the caller graph for this function:

◆ rb_is_empty()

static int rb_is_empty ( ring_buffer rb)
inlinestatic

Returns true if and only if the ring buffer is empty.

Parameters
rbBuffer to test.

Definition at line 109 of file ring_buffer.h.

Referenced by rb_safe_remove(), and usart_tx_irq().

Here is the caller graph for this function:

◆ rb_is_full()

static int rb_is_full ( ring_buffer rb)
inlinestatic

Returns true if and only if the ring buffer is full.

Parameters
rbBuffer to test.

Definition at line 99 of file ring_buffer.h.

Referenced by rb_push_insert(), rb_safe_insert(), and usart_tx().

Here is the caller graph for this function:

◆ rb_push_insert()

static int rb_push_insert ( ring_buffer rb,
uint8_t  element 
)
inlinestatic

Append an item onto the end of a non-full ring buffer.

If the buffer is full, removes its first item, then inserts the new element at the end.

Parameters
rbRing buffer to insert into.
elementValue to insert into ring buffer.
Returns
On success, returns -1. If an element was popped, returns the popped value.

Definition at line 173 of file ring_buffer.h.

Referenced by usart_rx_irq().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rb_remove()

static uint8_t rb_remove ( ring_buffer rb)
inlinestatic

Remove and return the first item from a ring buffer.

Parameters
rbBuffer to remove from, must contain at least one element.

Definition at line 128 of file ring_buffer.h.

Referenced by rb_push_insert(), rb_safe_remove(), F4Light::UART_PPM::read(), usart_getc(), and usart_tx_irq().

Here is the caller graph for this function:

◆ rb_reset()

static void rb_reset ( ring_buffer rb)
inlinestatic

Discard all items from a ring buffer.

Parameters
rbRing buffer to discard all items from.

Definition at line 186 of file ring_buffer.h.

Referenced by usart_reset_rx(), usart_reset_tx(), usb_ioctl(), and usb_reset_rx().

Here is the caller graph for this function:

◆ rb_safe_insert()

static int rb_safe_insert ( ring_buffer rb,
uint8_t  element 
)
inlinestatic

Attempt to insert an element into a ring buffer.

Parameters
rbBuffer to insert into.
elementValue to insert into rb. If rb is not full, appends element onto buffer.
Returns
If element was appended, then true; otherwise, false.

Definition at line 154 of file ring_buffer.h.

Referenced by F4Light::UART_PPM::putch(), and usart_rx_irq().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ rb_safe_remove()

static int16_t rb_safe_remove ( ring_buffer rb)
inlinestatic

Attempt to remove the first item from a ring buffer.

If the ring buffer is nonempty, removes and returns its first item. If it is empty, does nothing and returns a negative value.

Parameters
rbBuffer to attempt to remove from.

Definition at line 143 of file ring_buffer.h.

Here is the call graph for this function: