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.

191 lines
3.4 KiB
C

#include "putil.h"
#include <PCircularBuffer.h>
// Error handler used for debugging only
#ifdef PB_CB_DEBUG
#include <stdio.h>
static void handle_status(const char *func, PB_CB_STATUS status_code)
{
if (status_code != PB_CB_GOOD)
{
PDEBUG("%s failed: error code: %d\r\n", func, status_code);
}
}
#endif
// Circular Buffer Prototypes -- uint8_t
static PB_CB_STATUS p_cb_u8_push(p_cb_u8 *cbuffer, uint8_t value);
static PB_CB_STATUS p_cb_u8_empty(p_cb_u8 *cbuffer);
// serial_pkt
static PB_CB_STATUS p_cb_serial_pkt_push(p_cb_serial_pkt_t *cbuffer, serial_pkt_t value);
static PB_CB_STATUS p_cb_serial_pkt_empty(p_cb_serial_pkt_t *cbuffer);
// Circular Buffer Definitions -- uint8_t
PB_CB_STATUS p_cb_u8_init(p_cb_u8 *circ_buffer, uint8_t *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;
}
} while (0);
// Debugging
#ifdef PB_CB_DEBUG
handle_status(__func__, ret);
#endif
return ret;
}
PB_CB_STATUS p_cb_u8_push(p_cb_u8 *cbuffer, uint8_t 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;
}
// Debugging
#ifdef PB_CB_DEBUG
handle_status(__func__, ret);
#endif
return ret;
}
PB_CB_STATUS p_cb_u8_empty(p_cb_u8 *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, cbuffer->max_len);
cbuffer->head = 0;
cbuffer->b_empty = true;
cbuffer->b_filled = false;
} while (0);
// Debugging
#ifdef PB_CB_DEBUG
handle_status(__func__, ret);
#endif
return ret;
}
// Circular Buffer Definitions -- uint8_t
PB_CB_STATUS p_cb_serial_pkt_init(p_cb_serial_pkt_t *inst, serial_pkt_t *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;
}
for (int ind = 0; ind < max_length; ind++)
{
memset(inst->buffer[ind].frame_data, 0, 256);
}
} while (0);
// Debugging
#ifdef PB_CB_DEBUG
handle_status(__func__, ret);
#endif
return ret;
}
PB_CB_STATUS p_cb_serial_pkt_push(p_cb_serial_pkt_t *cbuffer, serial_pkt_t 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;
}
// Debugging
#ifdef PB_CB_DEBUG
handle_status(__func__, ret);
#endif
return ret;
}
PB_CB_STATUS p_cb_serial_pkt_empty(p_cb_serial_pkt_t *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(serial_pkt_t) * cbuffer->max_len);
cbuffer->head = 0;
cbuffer->b_empty = true;
cbuffer->b_filled = false;
} while (0);
// Debugging
#ifdef PB_CB_DEBUG
handle_status(__func__, ret);
#endif
return ret;
}