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-2002
    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 "buffer.h"
    00019 #include "global.h"
    00020 #include "avr/io.h"
    00021
    00022 #ifndef CRITICAL_SECTION_START
    00023 #define CRITICAL_SECTION_START  unsigned char _sreg = SREG; cli()
    00024 #define CRITICAL_SECTION_END    SREG = _sreg
    00025 #endif
    00026
    00027 // global variables
    00028
    00029 // initialization
    00030
    00031 void bufferInit(cBuffer* buffer, unsigned char *start, unsigned short size)
    00032 {
    00033     // begin critical section
    00034     CRITICAL_SECTION_START;
    00035     // set start pointer of the buffer
    00036     buffer->dataptr = start;
    00037     buffer->size = size;
    00038     // initialize index and length
    00039     buffer->dataindex = 0;
    00040     buffer->datalength = 0;
    00041     // end critical section
    00042     CRITICAL_SECTION_END;
    00043 }
    00044
    00045 // access routines
    00046 unsigned char  bufferGetFromFront(cBuffer* buffer)
    00047 {
    00048     unsigned char data = 0;
    00049     // begin critical section
    00050     CRITICAL_SECTION_START;
    00051     // check to see if there's data in the buffer
    00052     if(buffer->datalength)
    00053     {
    00054         // get the first character from buffer
    00055         data = buffer->dataptr[buffer->dataindex];
    00056         // move index down and decrement length
    00057         buffer->dataindex++;
    00058         if(buffer->dataindex >= buffer->size)
    00059         {
    00060             buffer->dataindex -= buffer->size;
    00061         }
    00062         buffer->datalength--;
    00063     }
    00064     // end critical section
    00065     CRITICAL_SECTION_END;
    00066     // return
    00067     return data;
    00068 }
    00069
    00070 void bufferDumpFromFront(cBuffer* buffer, unsigned short numbytes)
    00071 {
    00072     // begin critical section
    00073     CRITICAL_SECTION_START;
    00074     // dump numbytes from the front of the buffer
    00075     // are we dumping less than the entire buffer?
    00076     if(numbytes < buffer->datalength)
    00077     {
    00078         // move index down by numbytes and decrement length by numbytes
    00079         buffer->dataindex += numbytes;
    00080         if(buffer->dataindex >= buffer->size)
    00081         {
    00082             buffer->dataindex -= buffer->size;
    00083         }
    00084         buffer->datalength -= numbytes;
    00085     }
    00086     else
    00087     {
    00088         // flush the whole buffer
    00089         buffer->datalength = 0;
    00090     }
    00091     // end critical section
    00092     CRITICAL_SECTION_END;
    00093 }
    00094
    00095 unsigned char bufferGetAtIndex(cBuffer* buffer, unsigned short index)
    00096 {
    00097     // begin critical section
    00098     CRITICAL_SECTION_START;
    00099     // return character at index in buffer
    00100     unsigned char data = buffer->dataptr[(buffer->dataindex+index)%(buffer->size)];
    00101     // end critical section
    00102     CRITICAL_SECTION_END;
    00103     return data;
    00104 }
    00105
    00106 unsigned char bufferAddToEnd(cBuffer* buffer, unsigned char data)
    00107 {
    00108     // begin critical section
    00109     CRITICAL_SECTION_START;
    00110     // make sure the buffer has room
    00111     if(buffer->datalength < buffer->size)
    00112     {
    00113         // save data byte at end of buffer
    00114         buffer->dataptr[(buffer->dataindex + buffer->datalength) % buffer->size] = data;
    00115         // increment the length
    00116         buffer->datalength++;
    00117         // end critical section
    00118         CRITICAL_SECTION_END;
    00119         // return success
    00120         return -1;
    00121     }
    00122     // end critical section
    00123     CRITICAL_SECTION_END;
    00124     // return failure
    00125     return 0;
    00126 }
    00127
    00128 unsigned short bufferIsNotFull(cBuffer* buffer)
    00129 {
    00130     // begin critical section
    00131     CRITICAL_SECTION_START;
    00132     // check to see if the buffer has room
    00133     // return true if there is room
    00134     unsigned short bytesleft = (buffer->size - buffer->datalength);
    00135     // end critical section
    00136     CRITICAL_SECTION_END;
    00137     return bytesleft;
    00138 }
    00139
    00140 void bufferFlush(cBuffer* buffer)
    00141 {
    00142     // begin critical section
    00143     CRITICAL_SECTION_START;
    00144     // flush contents of the buffer
    00145     buffer->datalength = 0;
    00146     // end critical section
    00147     CRITICAL_SECTION_END;
    00148 }
    00149