CircularBitBuffer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2010 Denver Gingerich (denver [at] ossguy [dot] com)
  9. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  10. Permission to use, copy, modify, distribute, and sell this
  11. software and its documentation for any purpose is hereby granted
  12. without fee, provided that the above copyright notice appear in
  13. all copies and that both that the copyright notice and this
  14. permission notice and warranty disclaimer appear in supporting
  15. documentation, and that the name of the author not be used in
  16. advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. The author disclaims all warranties with regard to this
  19. software, including all implied warranties of merchantability
  20. and fitness. In no event shall the author be liable for any
  21. special, indirect or consequential damages or any damages
  22. whatsoever resulting from loss of use, data or profits, whether
  23. in an action of contract, negligence or other tortious action,
  24. arising out of or in connection with the use or performance of
  25. this software.
  26. */
  27. /** Circular bit buffer library. This will allow for individual bits
  28. * to be stored in packed form inside circular buffers, to reduce
  29. * overall RAM usage.
  30. */
  31. #include "CircularBitBuffer.h"
  32. /** Function to initialize or reset a bit buffer, ready for data to be stored into it. */
  33. void BitBuffer_Init(BitBuffer_t* const Buffer)
  34. {
  35. /* Reset the number of stored bits in the buffer */
  36. Buffer->Elements = 0;
  37. /* Reset the data in and out pointer structures in the buffer to the first buffer bit */
  38. Buffer->In.CurrentByte = Buffer->Data;
  39. Buffer->In.ByteMask = (1 << 0);
  40. Buffer->Out.CurrentByte = Buffer->Data;
  41. Buffer->Out.ByteMask = (1 << 0);
  42. }
  43. /** Function to store the given bit into the given bit buffer. */
  44. void BitBuffer_StoreNextBit(BitBuffer_t* const Buffer,
  45. const bool Bit)
  46. {
  47. /* If the bit to store is true, set the next bit in the buffer */
  48. if (Bit)
  49. *Buffer->In.CurrentByte |= Buffer->In.ByteMask;
  50. /* Increment the number of stored bits in the buffer counter */
  51. Buffer->Elements++;
  52. /* Check if the current buffer byte is full of stored bits */
  53. if (Buffer->In.ByteMask == (1 << 7))
  54. {
  55. /* Check if the end of the buffer has been reached, if so reset to start of buffer, otherwise advance to next bit */
  56. if (Buffer->In.CurrentByte != &Buffer->Data[sizeof(Buffer->Data) - 1])
  57. Buffer->In.CurrentByte++;
  58. else
  59. Buffer->In.CurrentByte = Buffer->Data;
  60. /* Reset the storage bit mask in the current buffer byte to the first bit */
  61. Buffer->In.ByteMask = (1 << 0);
  62. }
  63. else
  64. {
  65. /* Shift the current storage bit mask to the next bit in the current byte */
  66. Buffer->In.ByteMask <<= 1;
  67. }
  68. }
  69. /** Function to retrieve the next bit stored in the given bit buffer. */
  70. bool BitBuffer_GetNextBit(BitBuffer_t* const Buffer)
  71. {
  72. /* Retrieve the value of the next bit stored in the buffer */
  73. bool Bit = ((*Buffer->Out.CurrentByte & Buffer->Out.ByteMask) != 0);
  74. /* Clear the buffer bit */
  75. *Buffer->Out.CurrentByte &= ~Buffer->Out.ByteMask;
  76. /* Decrement the number of stored bits in the buffer counter */
  77. Buffer->Elements--;
  78. /* Check if the current buffer byte is empty of stored bits */
  79. if (Buffer->Out.ByteMask == (1 << 7))
  80. {
  81. /* Check if the end of the buffer has been reached, if so reset to start of buffer, otherwise advance to next bit */
  82. if (Buffer->Out.CurrentByte != &Buffer->Data[sizeof(Buffer->Data) - 1])
  83. Buffer->Out.CurrentByte++;
  84. else
  85. Buffer->Out.CurrentByte = Buffer->Data;
  86. /* Reset the retrieval bit mask in the current buffer byte to the first bit */
  87. Buffer->Out.ByteMask = (1 << 0);
  88. }
  89. else
  90. {
  91. /* Shift the current retrieval bit mask to the next bit in the current byte */
  92. Buffer->Out.ByteMask <<= 1;
  93. }
  94. /* Return the retrieved bit from the buffer */
  95. return Bit;
  96. }