CircularBitBuffer.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. /** \file
  28. *
  29. * Header file for CircularBitBuffer.c.
  30. */
  31. #ifndef _CIRCULARBITBUFFER_H_
  32. #define _CIRCULARBITBUFFER_H_
  33. /* Includes: */
  34. #include <avr/io.h>
  35. #include <stdbool.h>
  36. #include <LUFA/Common/Common.h>
  37. /* Macros: */
  38. #if (defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB647__) || \
  39. defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__)) || defined(__DOXYGEN__)
  40. /** Maximum number of bits which can be stored into a bit buffer. The memory usage is one eighth of this value per buffer. */
  41. #define MAX_BITS 8192
  42. #else
  43. #define MAX_BITS 1024
  44. #endif
  45. /* Type Defines: */
  46. /** Type define for a pointer to a bit in a bit buffer. */
  47. typedef struct
  48. {
  49. uint8_t* CurrentByte; /**< Pointer to the current byte in the buffer */
  50. uint8_t ByteMask; /**< Mask of the current bit in the buffer */
  51. } BitBufferPointer_t;
  52. /** Type define for a circular packet bit buffer. */
  53. typedef struct
  54. {
  55. uint8_t Data[MAX_BITS / 8]; /**< Buffer to hold the stored bits in packed form */
  56. uint16_t Elements; /**< Number of stored bits in the bit buffer */
  57. BitBufferPointer_t In; /**< Bit pointer to the next storage location in the buffer */
  58. BitBufferPointer_t Out; /**< Bit pointer to the next retrieval location in the buffer */
  59. } BitBuffer_t;
  60. /* Function Prototypes: */
  61. /** Initializes or resets a given bit buffer, ready to store new bits.
  62. *
  63. * \param[in,out] Buffer Bit buffer to initialize
  64. */
  65. void BitBuffer_Init(BitBuffer_t* const Buffer) ATTR_NON_NULL_PTR_ARG(1);
  66. /** Stores a bit into the next location inside a given bit buffer.
  67. *
  68. * \param[in,out] Buffer Bit buffer to store a bit into
  69. * \param[in] Bit Bit to store into the buffer
  70. */
  71. void BitBuffer_StoreNextBit(BitBuffer_t* const Buffer,
  72. const bool Bit) ATTR_NON_NULL_PTR_ARG(1);
  73. /** Retrieves a bit from the next location inside a given bit buffer.
  74. *
  75. * \param[in,out] Buffer Bit buffer to retrieve a bit from
  76. *
  77. * \return Next bit from the buffer
  78. */
  79. bool BitBuffer_GetNextBit(BitBuffer_t* const Buffer) ATTR_NON_NULL_PTR_ARG(1);
  80. #endif