Main Page   Compound List   File List   Compound Members   File Members  

bitbuf.c

Go to the documentation of this file.
00001 
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  : 3
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 // access routines
00023 void bitbufInit(BitBuf* bitBuffer, unsigned char *start, unsigned short bytesize)
00024 {
00025         // set start pointer of the buffer
00026         bitBuffer->dataptr = start;
00027         bitBuffer->size = bytesize;
00028         // initialize indexing and length
00029         bitBuffer->dataindex = 0;
00030         bitbufFlush(bitBuffer);
00031 }
00032 
00033 unsigned char bitbufGet(BitBuf* bitBuffer)
00034 {
00035         unsigned char byte;
00036         unsigned char bit;
00037         
00038         // get current working byte
00039         byte = bitBuffer->dataptr[bitBuffer->bytePos];
00040         // read data bit
00041         bit = (byte & (1<<bitBuffer->bitPos))?(1):(0);
00042 
00043         // increment bit counter
00044         if(bitBuffer->bitPos < 7)
00045         {
00046                 bitBuffer->bitPos++;
00047         }
00048         else
00049         {
00050                 // increment byte counter
00051                 bitBuffer->bitPos = 0;
00052                 bitBuffer->bytePos++;
00053         }
00054 
00055         // return bit value
00056         return bit;
00057 }
00058 
00059 unsigned char bitbufGetAtIndex(BitBuf* bitBuffer, unsigned short bitIndex)
00060 {
00061         // return bit at index in buffer
00062         return (bitBuffer->dataptr[bitIndex>>3] & (1<<(bitIndex & 0x07)))?(1):(0);
00063 }
00064 
00065 void bitbufStore(BitBuf* bitBuffer, unsigned char bit)
00066 {
00067         unsigned char byte;
00068         // get current working byte
00069         byte = bitBuffer->dataptr[bitBuffer->bytePos];
00070         // apply data bit
00071         if(bit)
00072                 byte |=  (1<<bitBuffer->bitPos);
00073         else
00074                 byte &= ~(1<<bitBuffer->bitPos);
00075         // store data
00076         bitBuffer->dataptr[bitBuffer->bytePos] = byte;
00077         bitBuffer->datalength++;
00078 
00079         // increment bit counter
00080         if(bitBuffer->bitPos < 7)
00081         {
00082                 bitBuffer->bitPos++;
00083         }
00084         else
00085         {
00086                 // increment byte counter
00087                 bitBuffer->bitPos = 0;
00088                 bitBuffer->bytePos++;
00089         }
00090 }
00091 
00092 void bitbufReset(BitBuf* bitBuffer)
00093 {
00094         // reset counters
00095         bitBuffer->bytePos = 0;
00096         bitBuffer->bitPos = 0;
00097 }
00098 
00099 void bitbufFlush(BitBuf* bitBuffer)
00100 {
00101         // flush contents of the buffer
00102         bitBuffer->datalength = 0;
00103         // reset indexing
00104         bitbufReset(bitBuffer);
00105 }
00106 
00107 unsigned short bitbufGetDataLength(BitBuf* bitBuffer)
00108 {
00109         return bitBuffer->datalength;
00110 }
00111 
00112 /*
00113 
00114 
00115 unsigned char bitbufIsNotFull(cBuffer* buffer)
00116 {
00117         // check to see if the buffer has room
00118         // return true if there is room
00119         return (buffer->datalength < buffer->size);
00120 }
00121 */
00122 

Generated at Mon Oct 14 00:10:51 2002 for avrlib by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001