123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include "CircularBitBuffer.h"
- void BitBuffer_Init(BitBuffer_t* const Buffer)
- {
-
- Buffer->Elements = 0;
-
- Buffer->In.CurrentByte = Buffer->Data;
- Buffer->In.ByteMask = (1 << 0);
- Buffer->Out.CurrentByte = Buffer->Data;
- Buffer->Out.ByteMask = (1 << 0);
- }
- void BitBuffer_StoreNextBit(BitBuffer_t* const Buffer,
- const bool Bit)
- {
-
- if (Bit)
- *Buffer->In.CurrentByte |= Buffer->In.ByteMask;
-
- Buffer->Elements++;
-
- if (Buffer->In.ByteMask == (1 << 7))
- {
-
- if (Buffer->In.CurrentByte != &Buffer->Data[sizeof(Buffer->Data) - 1])
- Buffer->In.CurrentByte++;
- else
- Buffer->In.CurrentByte = Buffer->Data;
-
- Buffer->In.ByteMask = (1 << 0);
- }
- else
- {
-
- Buffer->In.ByteMask <<= 1;
- }
- }
- bool BitBuffer_GetNextBit(BitBuffer_t* const Buffer)
- {
-
- bool Bit = ((*Buffer->Out.CurrentByte & Buffer->Out.ByteMask) != 0);
-
- *Buffer->Out.CurrentByte &= ~Buffer->Out.ByteMask;
-
- Buffer->Elements--;
-
- if (Buffer->Out.ByteMask == (1 << 7))
- {
-
- if (Buffer->Out.CurrentByte != &Buffer->Data[sizeof(Buffer->Data) - 1])
- Buffer->Out.CurrentByte++;
- else
- Buffer->Out.CurrentByte = Buffer->Data;
-
- Buffer->Out.ByteMask = (1 << 0);
- }
- else
- {
-
- Buffer->Out.ByteMask <<= 1;
- }
-
- return Bit;
- }
|