/****************************************************************************/
/* Copyright 2009 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
#include "buffer.h"
#include "sys_defs.h"

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif
    
/****************************************************************************/
/*                 Byte buffer access and status functions                  */
/****************************************************************************/
int bufBytePut(unsigned char b, ByteBuffer* buff)
{
    if ( bufByteIsFull(buff) )
    {
        return FALSE;
    }   
    else
    {   
        buff->data[buff->head] = b;
        buff->head = (buff->head + 1) & buff->ptr_mask;
        return TRUE;
    }
}

int bufByteGet(unsigned char* b, ByteBuffer* buff)
{

    if ( bufByteIsEmpty(buff) )
    {
        *b = 0;
//		serPutString(SER_CONSOLE, "BUF BYTE IS EMPTY\r\n");
        return FALSE;
    }
    else
    {
        *b = buff->data[buff->tail];
        buff->tail = (buff->tail + 1) & buff->ptr_mask;
//		serPutString(SER_CONSOLE, "BUF BYTE IS FULL\r\n");
        return TRUE;
    }
}

void bufByteClear(ByteBuffer* buff)
{
    buff->tail = 0;
    buff->head = 0;
}

int bufByteIsFull(ByteBuffer* buff)
{
    if ( ((buff->head + 1) & buff->ptr_mask) == buff->tail )
        return TRUE;
    else
        return FALSE;
}

int bufByteIsEmpty(ByteBuffer* buff)
{
    if (buff->tail == buff->head) 
        return TRUE;
    else
        return FALSE;
}

int bufByteAvailable(ByteBuffer* buff)
{
    return (buff->head - buff->tail) & buff->ptr_mask;
}


/****************************************************************************/
/*                 Word buffer access and status functions                  */
/****************************************************************************/
int bufWordPut(unsigned int w, WordBuffer* buff)
{
    if ( bufWordIsFull(buff) )
    {
        return FALSE;
    }   
    else
    {   
        buff->data[buff->head] = w;
        buff->head = (buff->head + 1) & buff->ptr_mask;
        return TRUE;
    }
}

int bufWordGet(unsigned int* w, WordBuffer* buff)
{
    if ( bufWordIsEmpty(buff) )
    {
        *w = 0;
        return FALSE;
    }
    else
    {
        *w = buff->data[buff->tail];
        buff->tail = (buff->tail + 1) & buff->ptr_mask;
        return TRUE;
    }
}

void bufWordClear(WordBuffer* buff)
{
    buff->tail = 0;
    buff->head = 0;
}

int bufWordIsFull(WordBuffer* buff)
{
    if ( ((buff->head + 1) & buff->ptr_mask) == buff->tail )
        return TRUE;
    else
        return FALSE;
}

int bufWordIsEmpty(WordBuffer* buff)
{
    if (buff->tail == buff->head) 
        return TRUE;
    else
        return FALSE;
}

int bufWordAvailable(WordBuffer* buff)
{
    return (buff->head - buff->tail) & buff->ptr_mask;
}

