Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

# buffer.c

Go to the documentation of this file.

    00001 /*! \file buffer.c \brief Multipurpose byte buffer structure and methods. */
    00002 //*****************************************************************************
    00003 //
    00004 // File Name    : 'buffer.c'
    00005 // Title        : Multipurpose byte buffer structure and methods
    00006 // Author       : Pascal Stang - Copyright (C) 2001-2006
    00007 // Created      : 9/23/2001
    00008 // Revised      : 9/23/2001
    00009 // Version      : 1.0
    00010 // Target MCU   : any
    00011 // Editor Tabs  : 4
    00012 //
    00013 // This code is distributed under the GNU Public License
    00014 //      which can be found at http://www.gnu.org/licenses/gpl.txt
    00015 //
    00016 //*****************************************************************************
    00017
    00018 #include "global.h"
    00019 #include "processor.h"
    00020 #include "buffer.h"
    00021
    00022 // global variables
    00023
    00024 // initialization
    00025
    00026 void bufferInit(cBuffer* buffer, unsigned char *start, unsigned short size)
    00027 {
    00028     // begin critical section
    00029     CRITICAL_SECTION_BEGIN;
    00030     // set start pointer of the buffer
    00031     buffer->dataptr = start;
    00032     buffer->size = size;
    00033     // initialize index and length
    00034     buffer->dataindex = 0;
    00035     buffer->datalength = 0;
    00036     // end critical section
    00037     CRITICAL_SECTION_END;
    00038 }
    00039
    00040 // access routines
    00041 unsigned char  bufferGetFromFront(cBuffer* buffer)
    00042 {
    00043     unsigned char data = 0;
    00044     // begin critical section
    00045     CRITICAL_SECTION_BEGIN;
    00046     // check to see if there's data in the buffer
    00047     if(buffer->datalength)
    00048     {
    00049         // get the first character from buffer
    00050         data = buffer->dataptr[buffer->dataindex];
    00051         // move index down and decrement length
    00052         buffer->dataindex++;
    00053         if(buffer->dataindex >= buffer->size)
    00054         {
    00055             buffer->dataindex -= buffer->size;
    00056         }
    00057         buffer->datalength--;
    00058     }
    00059     // end critical section
    00060     CRITICAL_SECTION_END;
    00061     // return
    00062     return data;
    00063 }
    00064
    00065 void bufferDumpFromFront(cBuffer* buffer, unsigned short numbytes)
    00066 {
    00067     // begin critical section
    00068     CRITICAL_SECTION_BEGIN;
    00069     // dump numbytes from the front of the buffer
    00070     // are we dumping less than the entire buffer?
    00071     if(numbytes < buffer->datalength)
    00072     {
    00073         // move index down by numbytes and decrement length by numbytes
    00074         buffer->dataindex += numbytes;
    00075         if(buffer->dataindex >= buffer->size)
    00076         {
    00077             buffer->dataindex -= buffer->size;
    00078         }
    00079         buffer->datalength -= numbytes;
    00080     }
    00081     else
    00082     {
    00083         // flush the whole buffer
    00084         buffer->datalength = 0;
    00085     }
    00086     // end critical section
    00087     CRITICAL_SECTION_END;
    00088 }
    00089
    00090 unsigned char bufferGetAtIndex(cBuffer* buffer, unsigned short index)
    00091 {
    00092     // begin critical section
    00093     CRITICAL_SECTION_BEGIN;
    00094     // return character at index in buffer
    00095     unsigned char data = buffer->dataptr[(buffer->dataindex+index)%(buffer->size)];
    00096     // end critical section
    00097     CRITICAL_SECTION_END;
    00098     return data;
    00099 }
    00100
    00101 unsigned char bufferAddToEnd(cBuffer* buffer, unsigned char data)
    00102 {
    00103     // begin critical section
    00104     CRITICAL_SECTION_BEGIN;
    00105     // make sure the buffer has room
    00106     if(buffer->datalength < buffer->size)
    00107     {
    00108         // save data byte at end of buffer
    00109         buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = data;
    00110         // increment the length
    00111         buffer->datalength++;
    00112         // end critical section
    00113         CRITICAL_SECTION_END;
    00114         // return success
    00115         return -1;
    00116     }
    00117     // end critical section
    00118     CRITICAL_SECTION_END;
    00119     // return failure
    00120     return 0;
    00121 }
    00122
    00123 unsigned short bufferIsNotFull(cBuffer* buffer)
    00124 {
    00125     // begin critical section
    00126     CRITICAL_SECTION_BEGIN;
    00127     // check to see if the buffer has room
    00128     // return true if there is room
    00129     unsigned short bytesleft = (buffer->size - buffer->datalength);
    00130     // end critical section
    00131     CRITICAL_SECTION_END;
    00132     return bytesleft;
    00133 }
    00134
    00135 void bufferFlush(cBuffer* buffer)
    00136 {
    00137     // begin critical section
    00138     CRITICAL_SECTION_BEGIN;
    00139     // flush contents of the buffer
    00140     buffer->datalength = 0;
    00141     // end critical section
    00142     CRITICAL_SECTION_END;
    00143 }
    00144