/****************************************************************************/
/* Copyright 2015 MBARI                                                     */
/****************************************************************************/
/* Summary  : Ring Buffer Routines for OASIS5 on PIC324FJ128GA406           */
/* Filename : buffer.h                                                      */
/* Author   : Robert Herlien (rah)                                          */
/* Project  : OASIS Mooring Replacement (OASIS5)                            */
/* Revision: 1.0                                                            */
/* Created  : 07/13/2015                                                    */
/*                                                                          */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*                                                                          */
/****************************************************************************/
/* Modification History:                                                    */
/* Written by Mike Risi for Respirometer project in 2009                    */
/* Modified by Bob Herlien for SensorNode for xFOCE, May 2013               */
/* 13jul2015 rah - Modified for Oasis5 on PIC32MX                           */
/* 9nov2015 rah - Modified for Oasis5 on PIC24FJ                            */
/****************************************************************************/

#ifndef BUFFER_H
#define BUFFER_H

typedef struct
{
    unsigned char *startptr;
    unsigned char *inptr;
    unsigned char *outptr;
    unsigned char *endptr;
    int size;
    int occupied;
} ByteBuffer;


/********************************/
/*  Function Prototypes         */
/********************************/

void bufInit(ByteBuffer *bbp, unsigned char *dp, int size);
int  bufPut(unsigned char b, ByteBuffer *bbp);
int  bufGet(unsigned char *b, ByteBuffer *bbp);
void bufClear(ByteBuffer *bbp);


/********************************/
/*    Macros                    */
/********************************/

/* Macros manipulating ByteBuffer ptrs     */
#define bufIsEmptyP(bp)   (bp->occupied == 0)
#define bufIsFullP(bp)    (bp->occupied >= bp->size)
#define bufAvailableP(bp) (bp->occupied)

/* Macros manipulating ByteBuffers directly */
#define bufIsEmpty(buf)   (buf.occupied == 0)
#define bufIsFull(buf)    (buf.occupied >= buf.size)
#define bufAvailable(buf) (buf.occupied)

#endif
