My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

circularqueue.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #pragma once
  23. #include <stdint.h>
  24. /**
  25. * @brief Circular Queue class
  26. * @details Implementation of the classic ring buffer data structure
  27. */
  28. template<typename T, uint8_t N>
  29. class CircularQueue {
  30. private:
  31. /**
  32. * @brief Buffer structure
  33. * @details This structure consolidates all the overhead required to handle
  34. * a circular queue such as the pointers and the buffer vector.
  35. */
  36. struct buffer_t {
  37. uint8_t head;
  38. uint8_t tail;
  39. uint8_t count;
  40. uint8_t size;
  41. T queue[N];
  42. } buffer;
  43. public:
  44. /**
  45. * @brief Class constructor
  46. * @details This class requires two template parameters, T defines the type
  47. * of item this queue will handle and N defines the maximum number of
  48. * items that can be stored on the queue.
  49. */
  50. CircularQueue<T, N>() {
  51. this->buffer.size = N;
  52. this->buffer.count = this->buffer.head = this->buffer.tail = 0;
  53. }
  54. /**
  55. * @brief Removes and returns a item from the queue
  56. * @details Removes the oldest item on the queue, pointed to by the
  57. * buffer_t head field. The item is returned to the caller.
  58. * @return type T item
  59. */
  60. T dequeue() {
  61. if (this->isEmpty()) return T();
  62. uint8_t index = this->buffer.head;
  63. --this->buffer.count;
  64. if (++this->buffer.head == this->buffer.size)
  65. this->buffer.head = 0;
  66. return this->buffer.queue[index];
  67. }
  68. /**
  69. * @brief Adds an item to the queue
  70. * @details Adds an item to the queue on the location pointed by the buffer_t
  71. * tail variable. Returns false if no queue space is available.
  72. * @param item Item to be added to the queue
  73. * @return true if the operation was successful
  74. */
  75. bool enqueue(T const &item) {
  76. if (this->isFull()) return false;
  77. this->buffer.queue[this->buffer.tail] = item;
  78. ++this->buffer.count;
  79. if (++this->buffer.tail == this->buffer.size)
  80. this->buffer.tail = 0;
  81. return true;
  82. }
  83. /**
  84. * @brief Checks if the queue has no items
  85. * @details Returns true if there are no items on the queue, false otherwise.
  86. * @return true if queue is empty
  87. */
  88. bool isEmpty() {
  89. return this->buffer.count == 0;
  90. }
  91. /**
  92. * @brief Checks if the queue is full
  93. * @details Returns true if the queue is full, false otherwise.
  94. * @return true if queue is full
  95. */
  96. bool isFull() {
  97. return this->buffer.count == this->buffer.size;
  98. }
  99. /**
  100. * @brief Gets the queue size
  101. * @details Returns the maximum number of items a queue can have.
  102. * @return the queue size
  103. */
  104. uint8_t size() {
  105. return this->buffer.size;
  106. }
  107. /**
  108. * @brief Gets the next item from the queue without removing it
  109. * @details Returns the next item in the queue without removing it
  110. * or updating the pointers.
  111. * @return first item in the queue
  112. */
  113. T peek() {
  114. return this->buffer.queue[this->buffer.head];
  115. }
  116. /**
  117. * @brief Gets the number of items on the queue
  118. * @details Returns the current number of items stored on the queue.
  119. * @return number of items in the queue
  120. */
  121. uint8_t count() {
  122. return this->buffer.count;
  123. }
  124. };