kBuffer  1.1
Functions
kBuffer.c File Reference

A universal ringbuffer library. More...

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

Functions

bufferStatus_t bufferInit (buffer_t *buffer, uint16_t bufferSize)
 init a new buffer This function inits a new buffer_t.
More...
 
bufferStatus_t bufferInitStatic (buffer_t *buffer, uint16_t bufferSize, bufferDatatype *bufferArray)
 init a new buffer This function inits a new buffer_t, but doesn't allocate the memory dynamically
You've to provide an array of the datatype and the required length when calling this function.
This function might be useful, if you want to save the overhead of the malloc() function
More...
 
bufferStatus_t bufferWriteToIndex (buffer_t *buffer, uint16_t index, bufferDatatype data)
 write data to a specific index of the buffer. WARNING: Take care when using this function, it is against the main concept of a ringbuffer More...
 
bufferStatus_t bufferReadFromIndex (buffer_t *buffer, uint16_t index, bufferDatatype *data)
 read data from a specifig index of the buffer WARNING: Take care when using this function, it is against the main concept of a ringbuffer More...
 
uint8_t bufferIsEmpty (buffer_t *buffer)
 Checks, wheter the buffer is empty. More...
 
uint8_t bufferIsFull (buffer_t *buffer)
 Checks, wheter the buffer is full. More...
 
bufferStatus_t bufferWrite (buffer_t *buffer, bufferDatatype data)
 add data to the end of the ringbuffer More...
 
bufferStatus_t bufferWriteOverwrite (buffer_t *buffer, bufferDatatype data)
 Add data to the end of the ringbuffer. If the buffer is full, overwrite the first data. More...
 
bufferStatus_t bufferRead (buffer_t *buffer, bufferDatatype *data)
 read data from the beginning of the buffer More...
 
bufferStatus_t bufferFill (buffer_t *buffer, bufferDatatype data, uint8_t silent)
 fill the whole buffer with given dummy data. More...
 
bufferStatus_t bufferAvailable (buffer_t *buffer, uint16_t *available)
 return, how many elements are stored and available in the buffer More...
 
bufferStatus_t bufferPeek (buffer_t *buffer, bufferDatatype *data)
 have a look at the next element in the buffer, but do not increase the read pointer More...
 
bufferStatus_t bufferMean (buffer_t *buffer, bufferDatatype *meanOut)
 take the average of the whole buffer More...
 
bufferStatus_t bufferMeanRMS (buffer_t *buffer, bufferDatatype *meanOut)
 take the root mean square of the whole buffer More...
 

Detailed Description

A universal ringbuffer library.

Author
Peter Kappelt
See also
https://github.com/peterkappelt/kBuffer

Function Documentation

bufferStatus_t bufferAvailable ( buffer_t buffer,
uint16_t *  available 
)

return, how many elements are stored and available in the buffer

Parameters
bufferpointer to a buffer_t instance
availablepointer to a variable where the number of available elements should be stored
Returns
an element of bufferStatus_t
Return values
bufferOKit worked as expected
bufferNotInitializedthe buffer wasn't initialized
bufferStatus_t bufferFill ( buffer_t buffer,
bufferDatatype  data,
uint8_t  silent 
)

fill the whole buffer with given dummy data.

Parameters
bufferpointer buffer_t instance
datadata to fill the buffer with
silentif this parameter is 1, the buffer will be filled with data, but the write pointer stays at its current position (usefull, if you take the mean but the buffer is not full yet. You can just prefill it, the mean will be taken with the prefilled values)
Returns
an element of bufferStatus_t
Return values
bufferOKit worked as expected
bufferNotInitializedthe buffer wasn't initialized
bufferStatus_t bufferInit ( buffer_t buffer,
uint16_t  bufferSize 
)

init a new buffer This function inits a new buffer_t.

Parameters
bufferPointer (&) to a buffer_t object.
bufferSizedesired size of the buffer, the total buffer size (e.g. length-of-datatype * bufferSize) may not exceed 2^16 bytes
Returns
an element of bufferStatus_t
Return values
bufferMemoryAllocationFailedThe memory allocation with malloc failed. Make sure, you have enough memory available
bufferOKIt seems, like everything went well
bufferStatus_t bufferInitStatic ( buffer_t buffer,
uint16_t  bufferSize,
bufferDatatype bufferArray 
)

init a new buffer This function inits a new buffer_t, but doesn't allocate the memory dynamically
You've to provide an array of the datatype and the required length when calling this function.
This function might be useful, if you want to save the overhead of the malloc() function

Parameters
bufferPointer (&) to a buffer_t object.
bufferSizedesired size of the buffer, the total buffer size (e.g. length-of-datatype * bufferSize) may not exceed 2^16 bytes
bufferArraypointer to a array of the type bufferDatatype, which is bufferSize elements long
Returns
an element of bufferStatus_t
Return values
bufferOKIt seems, like everything went well
uint8_t bufferIsEmpty ( buffer_t buffer)

Checks, wheter the buffer is empty.

Parameters
bufferPointer to a buffer_t instance
Return values
1buffer is empty
0buffer is not empty
uint8_t bufferIsFull ( buffer_t buffer)

Checks, wheter the buffer is full.

Parameters
bufferPointer to a buffer_t instance
Return values
1buffer is full
0buffer is not full
bufferStatus_t bufferMean ( buffer_t buffer,
bufferDatatype meanOut 
)

take the average of the whole buffer

Parameters
bufferpointer to a buffer_t instance
meanOutpointer to a variable, where the mean will be stored
Returns
a element of bufferStatus_t
Return values
bufferOKit worked as expected, the mean is stored at the given variable
bufferNotInitializedthe buffer is not initialized
Bug:
The sum of the buffer is taken. Take precautions, that this variable won't overflow
bufferStatus_t bufferMeanRMS ( buffer_t buffer,
bufferDatatype meanOut 
)

take the root mean square of the whole buffer

Parameters
bufferpointer to a buffer_t instance
meanOutpointer to a variable, where the mean will be stored
Returns
a element of bufferStatus_t
Return values
bufferOKit worked as expected, the mean is stored at the given variable
bufferNotInitializedthe buffer is not initialized
Bug:
The sum of squared buffer elements is taken. Take precautions, that this variable won't overflow
bufferStatus_t bufferPeek ( buffer_t buffer,
bufferDatatype data 
)

have a look at the next element in the buffer, but do not increase the read pointer

Parameters
bufferpointer to a buffer_t instance
datapointer to a variable where data should be stored
Returns
a element of bufferStatus_t
Return values
bufferOKit worked as expected
bufferNotInitializedthe bufferInit() method hasn't been called or failed before
bufferEmptythe buffer is empty an no more data can be read
bufferStatus_t bufferRead ( buffer_t buffer,
bufferDatatype data 
)

read data from the beginning of the buffer

Parameters
bufferpointer to a buffer_t instance
datapointer to a variable where data should be stored
Returns
a element of bufferStatus_t
Return values
bufferOKit worked as expected
bufferNotInitializedthe bufferInit() method hasn't been called or failed before
bufferEmptythe buffer is empty an no more data can be read
bufferStatus_t bufferReadFromIndex ( buffer_t buffer,
uint16_t  index,
bufferDatatype data 
)

read data from a specifig index of the buffer WARNING: Take care when using this function, it is against the main concept of a ringbuffer

Parameters
bufferPointer to a buffer_t instance
indexThe index, where data should be written.
It can be in range 0 to length - 1
dataPointer to a variable where the read data should be written to.
Returns
an element of bufferStatus_t
Return values
bufferOKIt went successfull
bufferNotInitializedThe buffer is not initialized. You have to call bufferInit before (or the init failed before)
bufferErrorThe desired data index is out of range
bufferStatus_t bufferWrite ( buffer_t buffer,
bufferDatatype  data 
)

add data to the end of the ringbuffer

Parameters
bufferpointer to a buffer_t instance
datadata which should be written
Returns
a element of bufferStatus_t
Return values
bufferOKit worked as expected
bufferNotInitializedthe bufferInit() method hasn't been called or failed before
bufferFullthe buffer is full an no more data can be written
bufferStatus_t bufferWriteOverwrite ( buffer_t buffer,
bufferDatatype  data 
)

Add data to the end of the ringbuffer. If the buffer is full, overwrite the first data.

Parameters
bufferpointer to a buffer_t instance
datadata which should be written
Returns
a element of bufferStatus_t
Return values
bufferOKit worked as expected
bufferNotInitializedthe bufferInit() method hasn't been called or failed before
bufferStatus_t bufferWriteToIndex ( buffer_t buffer,
uint16_t  index,
bufferDatatype  data 
)

write data to a specific index of the buffer. WARNING: Take care when using this function, it is against the main concept of a ringbuffer

Parameters
bufferPointer to a buffer_t instance
indexThe index, where data should be written.
It can be in range 0 to length - 1
dataThe actual data which should be written
Returns
an element of bufferStatus_t
Return values
bufferOKIt went successfull
bufferNotInitializedThe buffer is not initialized. You have to call bufferInit before (or the init failed before)
bufferErrorThe desired data index is out of range