3      Copyright (C) Dean Camera, 2009. 
   5   dean [at] fourwalledcubicle [dot] com 
   6       www.fourwalledcubicle.com 
  10   Copyright 2009  Denver Gingerich (denver [at] ossguy [dot] com) 
  11   Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  13   Permission to use, copy, modify, and distribute this software 
  14   and its documentation for any purpose and without fee is hereby 
  15   granted, provided that the above copyright notice appear in all 
  16   copies and that both that the copyright notice and this 
  17   permission notice and warranty disclaimer appear in supporting 
  18   documentation, and that the name of the author not be used in 
  19   advertising or publicity pertaining to distribution of the 
  20   software without specific, written prior permission. 
  22   The author disclaim all warranties with regard to this 
  23   software, including all implied warranties of merchantability 
  24   and fitness.  In no event shall the author be liable for any 
  25   special, indirect or consequential damages or any damages 
  26   whatsoever resulting from loss of use, data or profits, whether 
  27   in an action of contract, negligence or other tortious action, 
  28   arising out of or in connection with the use or performance of 
  32 /** Circular bit buffer library. This will allow for individual bits 
  33  *  to be stored in packed form inside circular buffers, to reduce 
  37 #include "CircularBitBuffer.h" 
  39 /** Function to initialize or reset a bit buffer, ready for data to be stored into it. */ 
  40 void BitBuffer_Init(BitBuffer_t
* Buffer
) 
  42         /* Reset the number of stored bits in the buffer */ 
  45         /* Reset the data in and out pointer structures in the buffer to the first buffer bit */ 
  46         Buffer
->In
.CurrentByte  
= Buffer
->Data
; 
  47         Buffer
->In
.ByteMask     
= (1 << 0); 
  48         Buffer
->Out
.CurrentByte 
= Buffer
->Data
; 
  49         Buffer
->Out
.ByteMask    
= (1 << 0); 
  52 /** Function to store the given bit into the given bit buffer. */ 
  53 void BitBuffer_StoreNextBit(BitBuffer_t
* Buffer
, bool Bit
) 
  55         /* If the bit to store is true, set the next bit in the buffer */ 
  57           *Buffer
->In
.CurrentByte 
|= Buffer
->In
.ByteMask
; 
  59         /* Increment the number of stored bits in the buffer counter */ 
  62         /* Check if the current buffer byte is full of stored bits */ 
  63         if (Buffer
->In
.ByteMask 
== (1 << 7)) 
  65                 /* Check if the end of the buffer has been reached, if so reset to start of buffer, otherwise advance to next bit */ 
  66                 if (Buffer
->In
.CurrentByte 
!= &Buffer
->Data
[sizeof(Buffer
->Data
) - 1]) 
  67                   Buffer
->In
.CurrentByte
++; 
  69                   Buffer
->In
.CurrentByte 
= Buffer
->Data
; 
  71                 /* Reset the storage bit mask in the current buffer byte to the first bit */             
  72                 Buffer
->In
.ByteMask 
= (1 << 0); 
  76                 /* Shift the current storage bit mask to the next bit in the current byte */ 
  77                 Buffer
->In
.ByteMask 
<<= 1; 
  81 /** Function to retrieve the next bit stored in the given bit buffer. */ 
  82 bool BitBuffer_GetNextBit(BitBuffer_t
* Buffer
) 
  84         /* Retrieve the value of the next bit stored in the buffer */ 
  85         bool Bit 
= ((*Buffer
->Out
.CurrentByte 
& Buffer
->Out
.ByteMask
) != 0); 
  87         /* Clear the buffer bit */ 
  88         *Buffer
->Out
.CurrentByte 
&= ~Buffer
->Out
.ByteMask
; 
  90         /* Decrement the number of stored bits in the buffer counter */ 
  93         /* Check if the current buffer byte is empty of stored bits */   
  94         if (Buffer
->Out
.ByteMask 
== (1 << 7)) 
  96                 /* Check if the end of the buffer has been reached, if so reset to start of buffer, otherwise advance to next bit */ 
  97                 if (Buffer
->Out
.CurrentByte 
!= &Buffer
->Data
[sizeof(Buffer
->Data
) - 1]) 
  98                   Buffer
->Out
.CurrentByte
++; 
 100                   Buffer
->Out
.CurrentByte 
= Buffer
->Data
;                
 102                 /* Reset the retrieval bit mask in the current buffer byte to the first bit */           
 103                 Buffer
->Out
.ByteMask 
= (1 << 0); 
 107                 /* Shift the current retrieval bit mask to the next bit in the current byte */ 
 108                 Buffer
->Out
.ByteMask 
<<= 1; 
 111         /* Return the retrieved bit from the buffer */