My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

circularqueue.h 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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, uint8_t 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 count;
  41. uint8_t size;
  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 item 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.size = N;
  53. this->buffer.count = 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, pointed to by the
  58. * buffer_t head field. The item is returned to the caller.
  59. * @return type T item
  60. */
  61. T dequeue() {
  62. if (this->isEmpty()) return T();
  63. uint8_t index = this->buffer.head;
  64. --this->buffer.count;
  65. if (++this->buffer.head == this->buffer.size)
  66. this->buffer.head = 0;
  67. return this->buffer.queue[index];
  68. }
  69. /**
  70. * @brief Adds an item to the queue
  71. * @details Adds an item to the queue on the location pointed by the buffer_t
  72. * tail variable. Returns false if no queue space is available.
  73. * @param item Item to be added to the queue
  74. * @return true if the operation was successful
  75. */
  76. bool enqueue(T const &item) {
  77. if (this->isFull()) return false;
  78. this->buffer.queue[this->buffer.tail] = item;
  79. ++this->buffer.count;
  80. if (++this->buffer.tail == this->buffer.size)
  81. this->buffer.tail = 0;
  82. return true;
  83. }
  84. /**
  85. * @brief Checks if the queue has no items
  86. * @details Returns true if there are no items on the queue, false otherwise.
  87. * @return true if queue is empty
  88. */
  89. bool isEmpty() {
  90. return this->buffer.count == 0;
  91. }
  92. /**
  93. * @brief Checks if the queue is full
  94. * @details Returns true if the queue is full, false otherwise.
  95. * @return true if queue is full
  96. */
  97. bool isFull() {
  98. return this->buffer.count == this->buffer.size;
  99. }
  100. /**
  101. * @brief Gets the queue size
  102. * @details Returns the maximum number of items a queue can have.
  103. * @return the queue size
  104. */
  105. uint8_t size() {
  106. return this->buffer.size;
  107. }
  108. /**
  109. * @brief Gets the next item from the queue without removing it
  110. * @details Returns the next item in the queue without removing it
  111. * or updating the pointers.
  112. * @return first item in the queue
  113. */
  114. T peek() {
  115. return this->buffer.queue[this->buffer.head];
  116. }
  117. /**
  118. * @brief Gets the number of items on the queue
  119. * @details Returns the current number of items stored on the queue.
  120. * @return number of items in the queue
  121. */
  122. uint8_t count() {
  123. return this->buffer.count;
  124. }
  125. };
  126. #endif