ring_buffer.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #ifndef _LIBMAPLE_RING_BUFFER_H_
  2. #define _LIBMAPLE_RING_BUFFER_H_
  3. typedef unsigned char u8; // 无符号8位整型变量
  4. typedef signed char int8; // 有符号8位整型变量
  5. typedef unsigned short u16; // 无符号16位整型变量
  6. typedef signed short int16; // 有符号16位整型变量
  7. typedef unsigned int u32; // 无符号32位整型变量
  8. typedef signed int int32; // 有符号32位整型变量
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. /**
  13. * Ring buffer type.
  14. *
  15. * The buffer is empty when head == tail.
  16. *
  17. * The buffer is full when the head is one byte in front of the tail,
  18. * modulo buffer length.
  19. *
  20. * One byte is left free to distinguish empty from full. */
  21. typedef struct ring_buffer {
  22. volatile u8 *buf; /**< Buffer items are stored into */
  23. u16 head; /**< Index of the next item to remove */
  24. u16 tail; /**< Index where the next item will get inserted */
  25. u16 size; /**< Buffer capacity minus one */
  26. } ring_buffer;
  27. typedef struct ring_buffer16 {
  28. volatile u16 *buf; /**< Buffer items are stored into */
  29. u16 head; /**< Index of the next item to remove */
  30. u16 tail; /**< Index where the next item will get inserted */
  31. u16 size; /**< Buffer capacity minus one */
  32. } ring_buffer16;
  33. /**
  34. * Initialise a ring buffer.
  35. *
  36. * @param rb Instance to initialise
  37. *
  38. * @param size Number of items in buf. The ring buffer will always
  39. * leave one element unoccupied, so the maximum number of
  40. * elements it can store will be size - 1. Thus, size
  41. * must be at least 2.
  42. *
  43. * @param buf Buffer to store items into
  44. */
  45. static inline void rb_init(ring_buffer *rb, u16 size, u8 *buf) {
  46. rb->head = 0;
  47. rb->tail = 0;
  48. rb->size = size - 1;
  49. rb->buf = buf;
  50. }
  51. static inline void rb_init16(ring_buffer16 *rb, u16 size, u16 *buf) {
  52. rb->head = 0;
  53. rb->tail = 0;
  54. rb->size = size - 1;
  55. rb->buf = buf;
  56. }
  57. /**
  58. * @brief Return the number of elements stored in the ring buffer.
  59. * @param rb Buffer whose elements to count.
  60. */
  61. static inline u16 rb_full_count(ring_buffer *rb) {
  62. volatile ring_buffer *arb = rb;
  63. u32 size = arb->tail - arb->head;
  64. if (arb->tail < arb->head) {
  65. size += arb->size + 1;
  66. }
  67. return (u16)size;
  68. }
  69. static inline u16 rb_full_count16(ring_buffer16 *rb) {
  70. volatile ring_buffer16 *arb = rb;
  71. u32 size = arb->tail - arb->head;
  72. if (arb->tail < arb->head) {
  73. size += arb->size + 1;
  74. }
  75. return (u16)size;
  76. }
  77. /**
  78. * @brief Returns true if and only if the ring buffer is full.
  79. * @param rb Buffer to test.
  80. */
  81. static inline int rb_is_full(ring_buffer *rb) {
  82. return (rb->tail + 1 == rb->head) ||
  83. (rb->tail == rb->size && rb->head == 0);
  84. }
  85. static inline int rb_is_full16(ring_buffer16 *rb) {
  86. return (rb->tail + 1 == rb->head) ||
  87. (rb->tail == rb->size && rb->head == 0);
  88. }
  89. /**
  90. * @brief Returns true if and only if the ring buffer is empty.
  91. * @param rb Buffer to test.
  92. */
  93. static inline int rb_is_empty(ring_buffer *rb) {
  94. return rb->head == rb->tail;
  95. }
  96. static inline int rb_is_empty16(ring_buffer16 *rb) {
  97. return rb->head == rb->tail;
  98. }
  99. /**
  100. * Append element onto the end of a ring buffer.
  101. * @param rb Buffer to append onto.
  102. * @param element Value to append.
  103. */
  104. static inline void rb_insert(ring_buffer *rb, u8 element) {
  105. rb->buf[rb->tail] = element;
  106. rb->tail = (rb->tail == rb->size) ? 0 : rb->tail + 1;
  107. }
  108. static inline void rb_insert16(ring_buffer16 *rb, u16 element) {
  109. rb->buf[rb->tail] = element;
  110. rb->tail = (rb->tail == rb->size) ? 0 : rb->tail + 1;
  111. }
  112. /**
  113. * @brief Remove and return the first item from a ring buffer.
  114. * @param rb Buffer to remove from, must contain at least one element.
  115. */
  116. static inline u8 rb_remove(ring_buffer *rb) {
  117. u8 ch = rb->buf[rb->head];
  118. rb->head = (rb->head == rb->size) ? 0 : rb->head + 1;
  119. return ch;
  120. }
  121. static inline u16 rb_remove16(ring_buffer16 *rb) {
  122. u8 ch = rb->buf[rb->head];
  123. rb->head = (rb->head == rb->size) ? 0 : rb->head + 1;
  124. return ch;
  125. }
  126. static inline u8 rb_peek(ring_buffer *rb) {
  127. u8 ch = rb->buf[rb->head];
  128. return ch;
  129. }
  130. static inline u8 rb_peek_byCount(ring_buffer *rb,u8 str[]) {
  131. u8 x;
  132. x=0;
  133. if (rb->buf[rb->head]==0x7E && rb_full_count(rb)>rb->buf[rb->head+1])
  134. {
  135. for(int i=0; i<rb->buf[rb->head+1]; i++)
  136. {
  137. str[i] = rb->buf[rb->head+i];
  138. x++;
  139. }
  140. }
  141. return x;
  142. }
  143. static inline u16 rb_peek16(ring_buffer16 *rb) {
  144. u8 ch = rb->buf[rb->head];
  145. return ch;
  146. }
  147. /**
  148. * @brief Attempt to remove the first item from a ring buffer.
  149. *
  150. * If the ring buffer is nonempty, removes and returns its first item.
  151. * If it is empty, does nothing and returns a negative value.
  152. *
  153. * @param rb Buffer to attempt to remove from.
  154. */
  155. static inline int16 rb_safe_remove(ring_buffer *rb) {
  156. return rb_is_empty(rb) ? -1 : rb_remove(rb);
  157. }
  158. static inline int16 rb_safe_remove16(ring_buffer16 *rb) {
  159. return rb_is_empty16(rb) ? -1 : rb_remove16(rb);
  160. }
  161. /**
  162. * @brief Attempt to insert an element into a ring buffer.
  163. *
  164. * @param rb Buffer to insert into.
  165. * @param element Value to insert into rb.
  166. * @sideeffect If rb is not full, appends element onto buffer.
  167. * @return If element was appended, then true; otherwise, false. */
  168. static inline int rb_safe_insert(ring_buffer *rb, u8 element) {
  169. if (rb_is_full(rb)) {
  170. return 0;
  171. }
  172. rb_insert(rb, element);
  173. return 1;
  174. }
  175. static inline int rb_safe_insert16(ring_buffer16 *rb, u16 element) {
  176. if (rb_is_full16(rb)) {
  177. return 0;
  178. }
  179. rb_insert16(rb, element);
  180. return 1;
  181. }
  182. /**
  183. * @brief Append an item onto the end of a non-full ring buffer.
  184. *
  185. * If the buffer is full, removes its first item, then inserts the new
  186. * element at the end.
  187. *
  188. * @param rb Ring buffer to insert into.
  189. * @param element Value to insert into ring buffer.
  190. * @return On success, returns -1. If an element was popped, returns
  191. * the popped value.
  192. */
  193. static inline int rb_push_insert(ring_buffer *rb, u8 element) {
  194. int ret = -1;
  195. if (rb_is_full(rb)) {
  196. ret = rb_remove(rb);
  197. }
  198. rb_insert(rb, element);
  199. return ret;
  200. }
  201. static inline int rb_push_insert16(ring_buffer16 *rb, u16 element) {
  202. int ret = -1;
  203. if (rb_is_full16(rb)) {
  204. ret = rb_remove16(rb);
  205. }
  206. rb_insert16(rb, element);
  207. return ret;
  208. }
  209. /**
  210. * @brief Discard all items from a ring buffer.
  211. * @param rb Ring buffer to discard all items from.
  212. */
  213. static inline void rb_reset(ring_buffer *rb) {
  214. rb->tail = rb->head;
  215. }
  216. static inline void rb_reset16(ring_buffer16 *rb) {
  217. rb->tail = rb->head;
  218. }
  219. #ifdef __cplusplus
  220. } // extern "C"
  221. #endif
  222. #endif