Main Page   Data Structures   File List   Data Fields   Globals  

/bitbuf.c

Go to the documentation of this file.
00001 /*! \file bitbuf.c \brief Multipurpose bit buffer structure and methods. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'bitbuf.c'
00005 // Title        : Multipurpose bit buffer structure and methods
00006 // Author       : Pascal Stang - Copyright (C) 2001-2002
00007 // Created      : 7/10/2002
00008 // Revised      : 7/10/2002
00009 // Version      : 0.5
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 "bitbuf.h"
00019 
00020 // global variables
00021 
00022 //! Initialize the bit buffer
00023 //  sets the start location and size of the buffer in memory
00024 void bitbufInit(BitBuf* bitBuffer, unsigned char *start, unsigned short bytesize)
00025 {
00026     // set start pointer of the buffer
00027     bitBuffer->dataptr = start;
00028     bitBuffer->size = bytesize;
00029     // initialize indexing and length
00030     bitBuffer->dataindex = 0;
00031     bitbufFlush(bitBuffer);
00032 }
00033 
00034 // access routines
00035 
00036 //! Get a bit from the current position in the buffer
00037 //  returns the bit at the current position in the buffer
00038 //  and increments the bit position
00039 unsigned char bitbufGet(BitBuf* bitBuffer)
00040 {
00041     unsigned char byte;
00042     unsigned char bit;
00043     
00044     // get current working byte
00045     byte = bitBuffer->dataptr[bitBuffer->bytePos];
00046     // read data bit
00047     bit = (byte & (1<<bitBuffer->bitPos))?(1):(0);
00048 
00049     // increment bit counter
00050     if(bitBuffer->bitPos < 7)
00051     {
00052         bitBuffer->bitPos++;
00053     }
00054     else
00055     {
00056         // increment byte counter
00057         bitBuffer->bitPos = 0;
00058         bitBuffer->bytePos++;
00059     }
00060 
00061     // return bit value
00062     return bit;
00063 }
00064 
00065 //! Get a bit from a given index into the buffer
00066 //  returns the bit at position [bitIndex] in the buffer
00067 unsigned char bitbufGetAtIndex(BitBuf* bitBuffer, unsigned short bitIndex)
00068 {
00069     // return bit at index in buffer
00070     return (bitBuffer->dataptr[bitIndex>>3] & (1<<(bitIndex & 0x07)))?(1):(0);
00071 }
00072 
00073 //! Store a bit at the current position in the buffer
00074 //  stores the bit at the current position in the buffer
00075 //  and increments the bit position
00076 void bitbufStore(BitBuf* bitBuffer, unsigned char bit)
00077 {
00078     unsigned char byte;
00079     // get current working byte
00080     byte = bitBuffer->dataptr[bitBuffer->bytePos];
00081     // apply data bit
00082     if(bit)
00083         byte |=  (1<<bitBuffer->bitPos);
00084     else
00085         byte &= ~(1<<bitBuffer->bitPos);
00086     // store data
00087     bitBuffer->dataptr[bitBuffer->bytePos] = byte;
00088     bitBuffer->datalength++;
00089 
00090     // increment bit counter
00091     if(bitBuffer->bitPos < 7)
00092     {
00093         bitBuffer->bitPos++;
00094     }
00095     else
00096     {
00097         // increment byte counter
00098         bitBuffer->bitPos = 0;
00099         bitBuffer->bytePos++;
00100     }
00101 }
00102 
00103 void bitbufReset(BitBuf* bitBuffer)
00104 {
00105     // reset counters
00106     bitBuffer->bytePos = 0;
00107     bitBuffer->bitPos = 0;
00108 }
00109 
00110 void bitbufFlush(BitBuf* bitBuffer)
00111 {
00112     // flush contents of the buffer
00113     bitBuffer->datalength = 0;
00114     // reset indexing
00115     bitbufReset(bitBuffer);
00116 }
00117 
00118 unsigned short bitbufGetDataLength(BitBuf* bitBuffer)
00119 {
00120     return bitBuffer->datalength;
00121 }
00122 
00123 /*
00124 unsigned char bitbufIsNotFull(cBuffer* buffer)
00125 {
00126     // check to see if the buffer has room
00127     // return true if there is room
00128     return (buffer->datalength < buffer->size);
00129 }
00130 */
00131 

Generated on Fri Aug 1 10:42:40 2003 for Procyon AVRlib by doxygen1.2.18