| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file bitbuf.c \brief Multipurpose bit buffer structure and methods. */ |
| 2 | //***************************************************************************** |
||
| 3 | // |
||
| 4 | // File Name : 'bitbuf.c' |
||
| 5 | // Title : Multipurpose bit buffer structure and methods |
||
| 6 | // Author : Pascal Stang - Copyright (C) 2001-2002 |
||
| 7 | // Created : 7/10/2002 |
||
| 8 | // Revised : 7/10/2002 |
||
| 9 | // Version : 0.5 |
||
| 10 | // Target MCU : any |
||
| 11 | // Editor Tabs : 4 |
||
| 12 | // |
||
| 13 | // This code is distributed under the GNU Public License |
||
| 14 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
| 15 | // |
||
| 16 | //***************************************************************************** |
||
| 17 | |||
| 18 | #include "bitbuf.h" |
||
| 19 | |||
| 20 | // global variables |
||
| 21 | |||
| 22 | //! Initialize the bit buffer |
||
| 23 | // sets the start location and size of the buffer in memory |
||
| 24 | void bitbufInit(BitBuf* bitBuffer, unsigned char *start, unsigned short bytesize) |
||
| 25 | { |
||
| 26 | // set start pointer of the buffer |
||
| 27 | bitBuffer->dataptr = start; |
||
| 28 | bitBuffer->size = bytesize; |
||
| 29 | // initialize indexing and length |
||
| 30 | bitBuffer->dataindex = 0; |
||
| 31 | bitbufFlush(bitBuffer); |
||
| 32 | } |
||
| 33 | |||
| 34 | // access routines |
||
| 35 | |||
| 36 | //! Get a bit from the current position in the buffer |
||
| 37 | // returns the bit at the current position in the buffer |
||
| 38 | // and increments the bit position |
||
| 39 | unsigned char bitbufGet(BitBuf* bitBuffer) |
||
| 40 | { |
||
| 41 | unsigned char byte; |
||
| 42 | unsigned char bit; |
||
| 43 | |||
| 44 | // get current working byte |
||
| 45 | byte = bitBuffer->dataptr[bitBuffer->bytePos]; |
||
| 46 | // read data bit |
||
| 47 | bit = (byte & (1<<bitBuffer->bitPos))?(1):(0); |
||
| 48 | |||
| 49 | // increment bit counter |
||
| 50 | if(bitBuffer->bitPos < 7) |
||
| 51 | { |
||
| 52 | bitBuffer->bitPos++; |
||
| 53 | } |
||
| 54 | else |
||
| 55 | { |
||
| 56 | // increment byte counter |
||
| 57 | bitBuffer->bitPos = 0; |
||
| 58 | bitBuffer->bytePos++; |
||
| 59 | } |
||
| 60 | |||
| 61 | // return bit value |
||
| 62 | return bit; |
||
| 63 | } |
||
| 64 | |||
| 65 | //! Get a bit from a given index into the buffer |
||
| 66 | // returns the bit at position [bitIndex] in the buffer |
||
| 67 | unsigned char bitbufGetAtIndex(BitBuf* bitBuffer, unsigned short bitIndex) |
||
| 68 | { |
||
| 69 | // return bit at index in buffer |
||
| 70 | return (bitBuffer->dataptr[bitIndex>>3] & (1<<(bitIndex & 0x07)))?(1):(0); |
||
| 71 | } |
||
| 72 | |||
| 73 | //! Store a bit at the current position in the buffer |
||
| 74 | // stores the bit at the current position in the buffer |
||
| 75 | // and increments the bit position |
||
| 76 | void bitbufStore(BitBuf* bitBuffer, unsigned char bit) |
||
| 77 | { |
||
| 78 | unsigned char byte; |
||
| 79 | // get current working byte |
||
| 80 | byte = bitBuffer->dataptr[bitBuffer->bytePos]; |
||
| 81 | // apply data bit |
||
| 82 | if(bit) |
||
| 83 | byte |= (1<<bitBuffer->bitPos); |
||
| 84 | else |
||
| 85 | byte &= ~(1<<bitBuffer->bitPos); |
||
| 86 | // store data |
||
| 87 | bitBuffer->dataptr[bitBuffer->bytePos] = byte; |
||
| 88 | bitBuffer->datalength++; |
||
| 89 | |||
| 90 | // increment bit counter |
||
| 91 | if(bitBuffer->bitPos < 7) |
||
| 92 | { |
||
| 93 | bitBuffer->bitPos++; |
||
| 94 | } |
||
| 95 | else |
||
| 96 | { |
||
| 97 | // increment byte counter |
||
| 98 | bitBuffer->bitPos = 0; |
||
| 99 | bitBuffer->bytePos++; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | void bitbufReset(BitBuf* bitBuffer) |
||
| 104 | { |
||
| 105 | // reset counters |
||
| 106 | bitBuffer->bytePos = 0; |
||
| 107 | bitBuffer->bitPos = 0; |
||
| 108 | } |
||
| 109 | |||
| 110 | void bitbufFlush(BitBuf* bitBuffer) |
||
| 111 | { |
||
| 112 | // flush contents of the buffer |
||
| 113 | bitBuffer->datalength = 0; |
||
| 114 | // reset indexing |
||
| 115 | bitbufReset(bitBuffer); |
||
| 116 | } |
||
| 117 | |||
| 118 | unsigned short bitbufGetDataLength(BitBuf* bitBuffer) |
||
| 119 | { |
||
| 120 | return bitBuffer->datalength; |
||
| 121 | } |
||
| 122 | |||
| 123 | /* |
||
| 124 | unsigned char bitbufIsNotFull(cBuffer* buffer) |
||
| 125 | { |
||
| 126 | // check to see if the buffer has room |
||
| 127 | // return true if there is room |
||
| 128 | return (buffer->datalength < buffer->size); |
||
| 129 | } |
||
| 130 | */ |
||
| 131 |
Powered by WebSVN v2.8.3