Simple char ring buffer for microcontrollers. More...
#include <stdint.h>
Go to the source code of this file.
Data Structures | |
struct | CharRingBuf |
Holds all data for a buffer. More... | |
Defines | |
#define | CharRing_available(B) ((B)->count) |
#define | CharRing_free_space(B) ((B)->len - (B)->count) |
#define | CharRing_full(B) ((B)->count == (B)->len) |
#define | CharRing_empty(B) ((B)->count == 0) |
Functions | |
CharRingBuf * | CharRing_new (int16_t) |
Allocates and returns a pointer to a new CharRingBuf buffer object. | |
void | CharRing_free (CharRingBuf *) |
Destroys a character ring buffer. | |
void | CharRing_putchar (CharRingBuf *, char) |
Writes a character to buffer. | |
char | CharRing_getchar (CharRingBuf *) |
Reads a character from buffer. |
Simple char ring buffer for microcontrollers.
CharRing is a simple character ring/circular buffer designed for microcontrollers.
Definition in file charring.h.
struct CharRingBuf |
Holds all data for a buffer.
Definition at line 37 of file charring.h.
Data Fields | ||
---|---|---|
char * | buf | Pointer to buffer chars. |
int16_t | count | Number of chars in buffer. |
int16_t | head | Write offset. |
int16_t | len | Total buffer length. |
int16_t | tail | Read offset. |
#define CharRing_available | ( | B | ) | ((B)->count) |
Returns the number of available chars in CharRing buffer B
Definition at line 20 of file charring.h.
#define CharRing_empty | ( | B | ) | ((B)->count == 0) |
Returns whether CharRing buffer B is empty or not
Definition at line 29 of file charring.h.
#define CharRing_free_space | ( | B | ) | ((B)->len - (B)->count) |
Returns the number of free slots in CharRing buffer B
Definition at line 23 of file charring.h.
#define CharRing_full | ( | B | ) | ((B)->count == (B)->len) |
Returns whether CharRing buffer B is full or not
Definition at line 26 of file charring.h.
void CharRing_free | ( | CharRingBuf * | b | ) |
Destroys a character ring buffer.
Properly frees all memory that was allocated in the creation of CharRingBuf b.
b | CharRingBuf buffer to be destroyed |
Definition at line 43 of file charring.c.
char CharRing_getchar | ( | CharRingBuf * | b | ) |
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 }
b | CharRingBuf buffer to read |
Definition at line 98 of file charring.c.
CharRingBuf* CharRing_new | ( | int16_t | len | ) |
Allocates and returns a pointer to a new CharRingBuf buffer object.
len | size of buffer to be created |
Definition at line 26 of file charring.c.
void CharRing_putchar | ( | CharRingBuf * | b, |
char | c | ||
) |
Writes a character to buffer.
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
b | CharRingBuf buffer to write |
c | character to write |
Definition at line 66 of file charring.c.