kBuffer  1.1
Fundamental Usage

Buffer datatype definition

A ringbuffer consists of variables, which can be accessed in a continuous way.
You have to define, which datatype you want to have the elements.
By default, the elements are unsigned 16bit integers (uint16_t).
You can change the datatype by defining it. This definition must be before the inclusion of th kBuffer.h header file

#define bufferDatatype your_datatype

Instead of uint16_t, you can insert (almost) any datatype you want.

Initializing a ringbuffer

At first, you have to include the kBuffer library into your project. This can be done by copying the files from src/kBuffer to your project's directory. You can include the header as usual:

#include "kBuffer.h"

In your code, you have to define an instance of buffer_t. You have to init this instance with the function bufferInit(). If you want to have a ringbuffer with 8 elements:

buffer_t ringbuffer;
bufferInit(&ringbuffer, 8);

To check, if the initialization was successfull, you need to parse the return value of bufferInit():

buffer_t ringbuffer;
if(bufferInit(&ringbuffer, 8) == bufferOK){
do_something_it_worked_ok();
}else{
do_something_there_was_an_error();
}

If you want to avoid the memory overhead of the dynamic memory allocation of the malloc() function you could use the bufferInitStatic() function.

buffer_t ringbuffer;
bufferDatatype ringbufferPayload[8];
bufferInitStatic(&ringbuffer, 8, &ringbufferPayload[0]);

Writing data to the buffer

To write data to the buffer, you can use the bufferWrite() function:

#include "kBuffer.h"
int main(void){
buffer_t ringbuffer; // Declare an buffer instance
bufferInit(&ringbuffer, 8); // Init the buffer with 8 elements
//Notice, that no errorhandling has been done. We just expect a success
bufferWrite(&ringbuffer, 42); // Write the integer "42" to the buffer.
return 0;
}

Reading data from the buffer

To read data from the buffer, you can use the bufferRead() function:

#include "kBuffer.h"
int main(void){
buffer_t ringbuffer; // Declare an buffer instance
bufferInit(&ringbuffer, 8); // Init the buffer with 8 elements
//Notice, that no errorhandling has been done. We just expect a success
bufferWrite(&ringbuffer, 42); // Write the integer "42" to the buffer.
uint16_t dataRead; // Declare an integer, where the read data should be stored
bufferRead(&ringbuffer, &dataRead); // We expect, that dataRead is now 42 (because we have written 42 to the buffer before)
return 0;
}