queue.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #ifndef __QUEUE_H
  2. #define __QUEUE_H
  3. /**@brief 队列模式类型
  4. * @details
  5. * Also, it keeps the information about which memory is allocated for the buffer
  6. * and its size. This structure must be initialized by app_fifo_init() before use.
  7. */
  8. typedef enum
  9. {
  10. QUEUE_MODE_OVERFLOW, //!< If the queue is full, new element will overwrite the oldest.
  11. QUEUE_MODE_NO_OVERFLOW, //!< If the queue is full, new element will not be accepted.
  12. } queue_mode_t;
  13. /**@brief A FIFO instance structure.
  14. * @details Keeps track of which bytes to read and write next.
  15. * Also, it keeps the information about which memory is allocated for the buffer
  16. * and its size. This structure must be initialized by app_fifo_init() before use.
  17. */
  18. typedef enum
  19. {
  20. size_4 = 0x0004U,
  21. size_8 = 0x0008U,
  22. size_16 = 0x0010U,
  23. size_32 = 0x0020U,
  24. size_64 = 0x0040U,
  25. size_128 = 0x0080U,
  26. size_256 = 0x0100U,
  27. size_512 = 0x0200U,
  28. size_1024 = 0x0400U,
  29. size_2048 = 0x0800U,
  30. size_4096 = 0x1000U,
  31. size_8192 = 0x2000U,
  32. size_16384 = 0x4000U,
  33. size_32768 = 0x8000U,
  34. } queue_size_t;
  35. typedef struct
  36. {
  37. volatile unsigned short front; //!< Queue front index.
  38. volatile unsigned short back; //!< Queue back index.
  39. void * p_buffer; //!< Pointer to the memory that is used as storage.
  40. queue_size_t size; //!< Size of the queue.
  41. unsigned short element_count;
  42. unsigned short element_size; //!< Size of one element.
  43. queue_mode_t mode;
  44. } queue_t;
  45. /******************************API 接口*************************************/
  46. #define QUEUE_DEF(element_type, _name, _size, _mode) \
  47. element_type _name##_queue_buffer[(_size)]; \
  48. queue_t _name= \
  49. { \
  50. .front = 0, \
  51. .back = 0, \
  52. .p_buffer = _name##_queue_buffer, \
  53. .size = (_size), \
  54. .element_count = 0, \
  55. .element_size = sizeof(element_type), \
  56. .mode = _mode, \
  57. }
  58. //元素进队列
  59. unsigned short queue_in(queue_t * p_queue, void const * p_element);
  60. //元素出队列
  61. unsigned short queue_out(queue_t * p_queue, void * p_element);
  62. //查看队列中最后一个元素,队列中元素并没有减少
  63. unsigned short queue_peek(queue_t * p_queue, void * p_element);
  64. //清空队列
  65. void queue_reset(queue_t * p_queue);
  66. //获取队列中元素个数
  67. unsigned short queue_element_count(queue_t * p_queue);
  68. //查看队列中指定数组下标的元素的从最早进入队列的元素下标index==0开始算
  69. unsigned short queue_read(queue_t * p_queue, void * p_element ,int index);
  70. //扔掉掉列最后一个元素
  71. unsigned short queue_throw_element(queue_t * p_queue);
  72. #endif