]> git.gir.st - tmk_keyboard.git/blob - protocol/lufa/LUFA-git/Projects/Magstripe/Lib/CircularBitBuffer.c
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
[tmk_keyboard.git] / protocol / lufa / LUFA-git / Projects / Magstripe / Lib / CircularBitBuffer.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2014.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
11 Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12
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.
21
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
29 this software.
30 */
31
32 /** Circular bit buffer library. This will allow for individual bits
33 * to be stored in packed form inside circular buffers, to reduce
34 * overall RAM usage.
35 */
36
37 #include "CircularBitBuffer.h"
38
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)
41 {
42 /* Reset the number of stored bits in the buffer */
43 Buffer->Elements = 0;
44
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);
50 }
51
52 /** Function to store the given bit into the given bit buffer. */
53 void BitBuffer_StoreNextBit(BitBuffer_t* const Buffer,
54 const bool Bit)
55 {
56 /* If the bit to store is true, set the next bit in the buffer */
57 if (Bit)
58 *Buffer->In.CurrentByte |= Buffer->In.ByteMask;
59
60 /* Increment the number of stored bits in the buffer counter */
61 Buffer->Elements++;
62
63 /* Check if the current buffer byte is full of stored bits */
64 if (Buffer->In.ByteMask == (1 << 7))
65 {
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++;
69 else
70 Buffer->In.CurrentByte = Buffer->Data;
71
72 /* Reset the storage bit mask in the current buffer byte to the first bit */
73 Buffer->In.ByteMask = (1 << 0);
74 }
75 else
76 {
77 /* Shift the current storage bit mask to the next bit in the current byte */
78 Buffer->In.ByteMask <<= 1;
79 }
80 }
81
82 /** Function to retrieve the next bit stored in the given bit buffer. */
83 bool BitBuffer_GetNextBit(BitBuffer_t* const Buffer)
84 {
85 /* Retrieve the value of the next bit stored in the buffer */
86 bool Bit = ((*Buffer->Out.CurrentByte & Buffer->Out.ByteMask) != 0);
87
88 /* Clear the buffer bit */
89 *Buffer->Out.CurrentByte &= ~Buffer->Out.ByteMask;
90
91 /* Decrement the number of stored bits in the buffer counter */
92 Buffer->Elements--;
93
94 /* Check if the current buffer byte is empty of stored bits */
95 if (Buffer->Out.ByteMask == (1 << 7))
96 {
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++;
100 else
101 Buffer->Out.CurrentByte = Buffer->Data;
102
103 /* Reset the retrieval bit mask in the current buffer byte to the first bit */
104 Buffer->Out.ByteMask = (1 << 0);
105 }
106 else
107 {
108 /* Shift the current retrieval bit mask to the next bit in the current byte */
109 Buffer->Out.ByteMask <<= 1;
110 }
111
112 /* Return the retrieved bit from the buffer */
113 return Bit;
114 }
115
Imprint / Impressum