ringbuffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * @file ringbuffer.h
  3. * @brief ringbuffer header file.
  4. *
  5. * @details
  6. *
  7. * @author jlgwch
  8. * @date 2023-10-16
  9. * @version 1.0
  10. *
  11. * @history
  12. * - 2023-10-16: jlgwch - Created the file.
  13. *
  14. * @copyright Copyright (c) 2023~ jlgwch. All rights reserved.
  15. */
  16. #ifndef __RINGBUFFER_H__
  17. #define __RINGBUFFER_H__
  18. #include <stdint.h>
  19. #ifndef __cplusplus
  20. #ifndef bool
  21. typedef enum boolean { false, true } bool;
  22. #endif
  23. #endif
  24. typedef uint8_t data_t;
  25. typedef struct
  26. {
  27. int head;
  28. int tail;
  29. int full_size;
  30. int buffer_size;
  31. data_t* data;
  32. } ringbuffer_st;
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /**
  37. * @brief Initialize a ring buffer.
  38. *
  39. * This function initializes a ring buffer with the given size.
  40. *
  41. * @param ringbuffer Pointer to the ring buffer structure to be initialized.
  42. * @param size Size of the ring buffer.
  43. * @return true if initialization is successful, false otherwise.
  44. */
  45. bool ringbuffer_init(ringbuffer_st* ringbuffer, int size);
  46. /**
  47. * @brief Release resources used by the ring buffer.
  48. *
  49. * This function releases any dynamically allocated memory associated with the ring buffer.
  50. *
  51. * @param ringbuffer Pointer to the ring buffer to be released.
  52. */
  53. void ringbuffer_release(ringbuffer_st* ringbuffer);
  54. /**
  55. * @brief Append data to the ring buffer.
  56. *
  57. * This function appends data to the ring buffer. If the buffer is full, do nothing.
  58. *
  59. * @param ringbuffer Pointer to the ring buffer.
  60. * @param buffer Pointer to the data to be appended.
  61. * @param size Size of the data to be appended.
  62. * @return 0 if append is successful, -1 otherwise.
  63. */
  64. int ringbuffer_append(ringbuffer_st* ringbuffer, const data_t* buffer, int size);
  65. /**
  66. * @brief Read data from the ring buffer.
  67. *
  68. * This function reads data from the ring buffer. The function will attempt to read up to the given size.
  69. *
  70. * @param ringbuffer The ring buffer from which data is to be read.
  71. * @param data Pointer to the buffer where read data will be stored.
  72. * @param size Size of data to be read from the buffer.
  73. * @return The number of bytes successfully read.
  74. */
  75. int ringbuffer_read(ringbuffer_st ringbuffer, data_t* data, int size);
  76. /**
  77. * @brief Get data from a specific index in the ring buffer.
  78. *
  79. * This function retrieves data from a specific index in the ring buffer without removing it.
  80. *
  81. * @param ringbuffer The ring buffer from which data is to be retrieved.
  82. * @param index Index of the data to retrieve.
  83. * @param data Pointer to the variable where the retrieved data will be stored.
  84. * @return `0` if the data was successfully retrieved and stored in `data`;
  85. * `-1` if the index is out of bounds or the data could not be retrieved.
  86. */
  87. int ringbuffer_data(ringbuffer_st ringbuffer, data_t* data, int index);
  88. /**
  89. * @brief Pop data from the front of the ring buffer.
  90. *
  91. * This function removes the specified number of elements from the front of the buffer.
  92. *
  93. * @param ringbuffer Pointer to the ring buffer.
  94. * @param size Number of elements to remove from the front of the buffer.
  95. */
  96. void ringbuffer_popup(ringbuffer_st* ringbuffer, int size);
  97. /**
  98. * @brief Clear the ring buffer.
  99. *
  100. * This function clears all the data in the ring buffer, resetting it to an empty state.
  101. *
  102. * @param ringbuffer Pointer to the ring buffer.
  103. */
  104. void ringbuffer_clear(ringbuffer_st* ringbuffer);
  105. /**
  106. * @brief Check if the ring buffer is empty.
  107. *
  108. * This function checks if the ring buffer is empty, meaning no data is currently stored.
  109. *
  110. * @param ringbuffer The ring buffer to check for emptiness.
  111. * @return true if the buffer is empty, false otherwise.
  112. */
  113. bool ringbuffer_empty(ringbuffer_st ringbuffer);
  114. /**
  115. * @brief Print the contents of the ring buffer.
  116. *
  117. * This function prints the info stored in the ring buffer.
  118. *
  119. * @param ringbuffer The ring buffer to print.
  120. * @param print_buffer A boolean flag that controls whether the entire buffer contents should be printed.
  121. * If `true`, the entire buffer will be printed; if `false`, only summary information will be printed.
  122. */
  123. void ringbuffer_print(ringbuffer_st ringbuffer, bool print_buffer);
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. #endif // __RINGBUFFER_H__