My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

circularqueue.h 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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. buffer.size = N;
  52. buffer.count = buffer.head = 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 (isEmpty()) return T();
  62. uint8_t index = buffer.head;
  63. --buffer.count;
  64. if (++buffer.head == buffer.size)
  65. buffer.head = 0;
  66. return 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 (isFull()) return false;
  77. buffer.queue[buffer.tail] = item;
  78. ++buffer.count;
  79. if (++buffer.tail == buffer.size)
  80. 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() { return buffer.count == 0; }
  89. /**
  90. * @brief Checks if the queue is full
  91. * @details Returns true if the queue is full, false otherwise.
  92. * @return true if queue is full
  93. */
  94. bool isFull() { return buffer.count == buffer.size; }
  95. /**
  96. * @brief Gets the queue size
  97. * @details Returns the maximum number of items a queue can have.
  98. * @return the queue size
  99. */
  100. uint8_t size() { return buffer.size; }
  101. /**
  102. * @brief Gets the next item from the queue without removing it
  103. * @details Returns the next item in the queue without removing it
  104. * or updating the pointers.
  105. * @return first item in the queue
  106. */
  107. T peek() { return buffer.queue[buffer.head]; }
  108. /**
  109. * @brief Gets the number of items on the queue
  110. * @details Returns the current number of items stored on the queue.
  111. * @return number of items in the queue
  112. */
  113. uint8_t count() { return buffer.count; }
  114. };