3 Copyright (C) Dean Camera, 2014.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
11 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
13 Permission to use, copy, modify, distribute, and sell this
14 software and its documentation for any purpose is hereby granted
15 without fee, provided that the above copyright notice appear in
16 all 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 disclaims 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* const 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* const Buffer,
56 /* If the bit to store is true, set the next bit in the buffer */
58 *Buffer->In.CurrentByte |= Buffer->In.ByteMask;
60 /* Increment the number of stored bits in the buffer counter */
63 /* Check if the current buffer byte is full of stored bits */
64 if (Buffer->In.ByteMask == (1 << 7))
66 /* Check if the end of the buffer has been reached, if so reset to start of buffer, otherwise advance to next bit */
67 if (Buffer->In.CurrentByte != &Buffer->Data[sizeof(Buffer->Data) - 1])
68 Buffer->In.CurrentByte++;
70 Buffer->In.CurrentByte = Buffer->Data;
72 /* Reset the storage bit mask in the current buffer byte to the first bit */
73 Buffer->In.ByteMask = (1 << 0);
77 /* Shift the current storage bit mask to the next bit in the current byte */
78 Buffer->In.ByteMask <<= 1;
82 /** Function to retrieve the next bit stored in the given bit buffer. */
83 bool BitBuffer_GetNextBit(BitBuffer_t* const Buffer)
85 /* Retrieve the value of the next bit stored in the buffer */
86 bool Bit = ((*Buffer->Out.CurrentByte & Buffer->Out.ByteMask) != 0);
88 /* Clear the buffer bit */
89 *Buffer->Out.CurrentByte &= ~Buffer->Out.ByteMask;
91 /* Decrement the number of stored bits in the buffer counter */
94 /* Check if the current buffer byte is empty of stored bits */
95 if (Buffer->Out.ByteMask == (1 << 7))
97 /* Check if the end of the buffer has been reached, if so reset to start of buffer, otherwise advance to next bit */
98 if (Buffer->Out.CurrentByte != &Buffer->Data[sizeof(Buffer->Data) - 1])
99 Buffer->Out.CurrentByte++;
101 Buffer->Out.CurrentByte = Buffer->Data;
103 /* Reset the retrieval bit mask in the current buffer byte to the first bit */
104 Buffer->Out.ByteMask = (1 << 0);
108 /* Shift the current retrieval bit mask to the next bit in the current byte */
109 Buffer->Out.ByteMask <<= 1;
112 /* Return the retrieved bit from the buffer */