You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.6 KiB
C

#include <PCircularBuffer.h>
// Error handler used for debugging only
// Circular Buffer Prototypes -- Double
static PB_CB_STATUS p_cb_double_push(p_cb_double *cbuffer, double value);
static PB_CB_STATUS p_cb_double_empty(p_cb_double *cbuffer);
// Circular Buffer Definitions -- double
PB_CB_STATUS p_cb_double_init(p_cb_double *circ_buffer, double *buff, uint32_t max_length)
{
PB_CB_STATUS ret = PB_CB_GOOD;
do
{
// Make sure the buffer isn't bad (null)
if (!buff)
{
ret = PB_CB_NULL_BUFFER;
break;
}
// Make sure the max buffer is a useable size
if (max_length > PB_CB_MAX_BUFFER_SIZE || max_length <= 0)
{
ret = PB_CB_BAD_BUFFER_SIZE;
break;
}
circ_buffer->buffer = buff;
circ_buffer->max_len = (uint16_t)max_length;
circ_buffer->head = 0;
circ_buffer->push = p_cb_double_push;
circ_buffer->empty = p_cb_double_empty;
circ_buffer->empty(circ_buffer);
} while (0);
return ret;
}
PB_CB_STATUS p_cb_double_push(p_cb_double *cbuffer, double value)
{
PB_CB_STATUS ret = PB_CB_GOOD;
if (!cbuffer)
{
ret = PB_CB_NULL_CBUFFER;
}
else
{
cbuffer->buffer[cbuffer->head] = value;
cbuffer->head = (cbuffer->head + 1) % cbuffer->max_len;
}
return ret;
}
PB_CB_STATUS p_cb_double_empty(p_cb_double *cbuffer)
{
PB_CB_STATUS ret = PB_CB_GOOD;
do
{
if (!cbuffer)
{
ret = PB_CB_NULL_CBUFFER;
break;
}
if (!cbuffer->buffer)
{
ret = PB_CB_NULL_BUFFER;
break;
}
memset(cbuffer->buffer, 0, sizeof(double) * cbuffer->max_len);
cbuffer->head = 0;
cbuffer->b_empty = true;
cbuffer->b_filled = false;
} while (0);
return ret;
}