CharRing
Simple character ring buffer for mocrocontrollers
charring.c File Reference

Simple char ring buffer for microcontrollers. More...

#include "charring.h"
#include <stdlib.h>
Include dependency graph for charring.c:

Go to the source code of this file.

Functions

CharRingBufCharRing_new (int16_t len)
 Allocates and returns a pointer to a new CharRingBuf buffer object.
void CharRing_free (CharRingBuf *b)
 Destroys a character ring buffer.
void CharRing_putchar (CharRingBuf *b, char c)
 Writes a character to buffer.
char CharRing_getchar (CharRingBuf *b)
 Reads a character from buffer.

Detailed Description

Simple char ring buffer for microcontrollers.

Author:
Dan Fekete <thefekete@gmail.com>
Date:
March 10, 2015

CharRing is a simple character ring/circular buffer designed for microcontrollers.

Definition in file charring.c.


Function Documentation

void CharRing_free ( CharRingBuf b)

Destroys a character ring buffer.

Properly frees all memory that was allocated in the creation of CharRingBuf b.

Parameters:
bCharRingBuf buffer to be destroyed

Definition at line 43 of file charring.c.

Reads a character from buffer.

If there are no chars available in the buffer, this will always return '\0' (null char). But thats what CharRing_available() and CharRing_empty() are for:

  if (CharRing_available(my_buf)) {
      // do stuff
  }

  if (!CharRing_empty(my_buf)) {
      // do stuff
  }
Parameters:
bCharRingBuf buffer to read
Returns:
Next char from buffer ('\0' if buffer empty)

Definition at line 98 of file charring.c.

Here is the caller graph for this function:

CharRingBuf* CharRing_new ( int16_t  len)

Allocates and returns a pointer to a new CharRingBuf buffer object.

Warning:
A call to CharRing_free() is required to properly free allocated memory.
Parameters:
lensize of buffer to be created
Returns:
a new CharRingBuf buffer struct

Definition at line 26 of file charring.c.

void CharRing_putchar ( CharRingBuf b,
char  c 
)

Writes a character to buffer.

Warning:
If the buffer is full, it will discard the oldest char in the buffer to make room.

If losing data is unacceptable then you need to check on the buffer before writing to it. CharRing_free_space() will give you the free space in the buffer and you can check to see if the buffer is full with CharRing_full():

  while (CharRing_full(my_buf));  // wait for buffer to open up
Parameters:
bCharRingBuf buffer to write
ccharacter to write

Definition at line 66 of file charring.c.

Here is the call graph for this function:

 All Data Structures Files Functions Variables Defines