2016-01-24 13:13:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-10 00:56:32 +00:00
|
|
|
#include <string.h>
|
2016-01-24 13:13:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ringbuffer structure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct ringbuf {
|
|
|
|
char *buf; ///< Pointer to buffer memory
|
|
|
|
unsigned bufsize; ///< Size of buffer memory
|
|
|
|
volatile unsigned pos; ///< Current read position
|
|
|
|
volatile unsigned len; ///< Length of data in buffer
|
|
|
|
};
|
|
|
|
|
2017-01-10 08:48:07 +00:00
|
|
|
#define RINGBUF(size) { .buf = (char[(size)]) {0}, .bufsize = (size) }
|
2016-01-24 13:13:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a single byte from a buffer
|
|
|
|
*
|
|
|
|
* \param rb pointer to ringbuffer struct
|
|
|
|
* \param data pointer to data byte
|
|
|
|
* \return number of bytes read (0 if buffer was empty)
|
|
|
|
*/
|
2017-01-10 00:56:32 +00:00
|
|
|
int rb_getc(struct ringbuf *rb, char *data);
|
2016-01-24 13:13:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a single byte to a buffer
|
|
|
|
*
|
|
|
|
* \param rb pointer to ringbuffer struct
|
|
|
|
* \param data pointer to data byte
|
|
|
|
* \return number of bytes written (0 if buffer was full)
|
|
|
|
*/
|
2017-01-10 00:56:32 +00:00
|
|
|
int rb_putc(struct ringbuf *rb, const char data);
|
2016-01-24 13:13:40 +00:00
|
|
|
/**
|
|
|
|
* Read from a buffer
|
|
|
|
*
|
|
|
|
*/
|
2017-01-10 00:56:32 +00:00
|
|
|
int rb_read(struct ringbuf *rb, void *data, int len);
|
2016-01-24 13:13:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write to a buffer
|
|
|
|
*
|
|
|
|
* \param rb pointer to ringbuffer struct
|
|
|
|
* \param data pointer to data byte
|
|
|
|
* \return number of bytes written (0 if buffer was full)
|
|
|
|
*
|
|
|
|
*/
|
2017-01-10 00:56:32 +00:00
|
|
|
int rb_write(struct ringbuf *rb, const void *data, int len);
|
2016-01-24 13:13:40 +00:00
|
|
|
|
2017-01-10 00:56:32 +00:00
|
|
|
/**
|
|
|
|
* undo rb_read
|
|
|
|
*
|
|
|
|
* \param rb pointer to ringbuffer struct
|
|
|
|
* \param len length to reset read pointer
|
|
|
|
* \return 0 on error
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int rb_undo(struct ringbuf *rb, int len);
|
2016-01-24 13:13:40 +00:00
|
|
|
|
2017-01-10 00:56:32 +00:00
|
|
|
int rb_getline(struct ringbuf *rb, char *ptr, int len);
|