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

circularqueue.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 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. #ifndef __CIRCULARQUEUE_H__
  23. #define __CIRCULARQUEUE_H__
  24. #include <Arduino.h>
  25. /**
  26. * @brief Circular Queue class
  27. * @details Implementation of the classic ring buffer data structure
  28. */
  29. template<typename T, int N>
  30. class CircularQueue {
  31. private:
  32. /**
  33. * @brief Buffer structure
  34. * @details This structure consolidates all the overhead required to handle
  35. * a circular queue such as the pointers and the buffer vector.
  36. */
  37. struct buffer_t {
  38. uint8_t head;
  39. uint8_t tail;
  40. uint8_t size;
  41. uint8_t length;
  42. T queue[N];
  43. } buffer;
  44. public:
  45. /**
  46. * @brief Class constructor
  47. * @details This class requires two template parameters, T defines the type
  48. * of the items this queue will handle and N defines the maximum number of
  49. * items that can be stored on the queue.
  50. */
  51. CircularQueue<T, N>() {
  52. this->buffer.length = N;
  53. this->buffer.size = this->buffer.head = this->buffer.tail = 0;
  54. }
  55. /**
  56. * @brief Removes and returns a item from the queue
  57. * @details Removes the oldest item on the queue which is pointed by the
  58. * buffer_t head variable, this item is then returned to the caller.
  59. * @return type T item
  60. */
  61. T dequeue() {
  62. if (this->isEmpty()) return T();
  63. T const item = this->buffer.queue[this->buffer.head++];
  64. --this->buffer.size;
  65. if (this->buffer.head == this->buffer.length)
  66. this->buffer.head = 0;
  67. return item;
  68. }
  69. /**
  70. * @brief Adds an item to the queue
  71. * @details Adds a item to the queue on the location pointed by the buffer_t
  72. * tail vairable, will return false if there is no queue space available.
  73. *
  74. * @param item Item to be added to the queue
  75. * @return true if the operation was successfull
  76. */
  77. bool enqueue(T const &item) {
  78. if (this->isFull()) return false;
  79. this->buffer.queue[this->buffer.tail++] = item;
  80. ++this->buffer.size;
  81. if (this->buffer.tail == this->buffer.length)
  82. this->buffer.tail = 0;
  83. return true;
  84. }
  85. /**
  86. * @brief Checks if the queue has no items
  87. * @details Returns true if there are no items on the queue, false otherwise.
  88. * @return true if queue is empty
  89. */
  90. bool isEmpty() {
  91. return this->buffer.size == 0;
  92. }
  93. /**
  94. * @brief Checks if the queue is full
  95. * @details Returns true if the queue is full, false otherwise.
  96. * @return true if queue is full
  97. */
  98. bool isFull() {
  99. return this->buffer.size == this->buffer.length;
  100. }
  101. /**
  102. * @brief Gets the queue size
  103. * @details Returns the maximum number of items a queue can have.
  104. * @return the queue lenght
  105. */
  106. uint8_t length() {
  107. return this->buffer.length;
  108. }
  109. /**
  110. * @brief Gets the next item from the queue without removing it
  111. * @details Returns the next item on the queue but the item is not removed
  112. * from the queue nor the pointers updated.
  113. * @return the queue size
  114. */
  115. uint8_t peek() {
  116. return this->buffer.queue[this->buffer.head];
  117. }
  118. /**
  119. * @brief Gets the number of items on the queue
  120. * @details Returns the current number of items stored on the queue.
  121. * @return type T item
  122. */
  123. uint8_t size() {
  124. return this->buffer.size;
  125. }
  126. };
  127. #endif