Firmware2/Marlin/src/libs/circularqueue.h

132 lines
3.9 KiB
C
Raw Normal View History

2016-06-04 21:29:01 +02:00
/**
* Marlin 3D Printer Firmware
2020-02-03 15:00:57 +01:00
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
2016-06-04 21:29:01 +02:00
*
* Based on Sprinter and grbl.
2019-06-28 06:57:50 +02:00
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
2016-06-04 21:29:01 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once
2016-06-04 21:29:01 +02:00
#include <stdint.h>
2016-06-04 21:29:01 +02:00
/**
* @brief Circular Queue class
2016-06-04 21:29:01 +02:00
* @details Implementation of the classic ring buffer data structure
*/
template<typename T, uint8_t N>
2016-06-04 21:29:01 +02:00
class CircularQueue {
private:
/**
* @brief Buffer structure
2016-06-04 21:29:01 +02:00
* @details This structure consolidates all the overhead required to handle
* a circular queue such as the pointers and the buffer vector.
2016-06-04 21:29:01 +02:00
*/
struct buffer_t {
uint8_t head;
uint8_t tail;
uint8_t count;
2016-06-04 21:29:01 +02:00
uint8_t size;
T queue[N];
} buffer;
public:
/**
* @brief Class constructor
2016-06-04 21:29:01 +02:00
* @details This class requires two template parameters, T defines the type
* of item this queue will handle and N defines the maximum number of
* items that can be stored on the queue.
2016-06-04 21:29:01 +02:00
*/
CircularQueue<T, N>() {
2019-09-25 15:29:59 +02:00
buffer.size = N;
buffer.count = buffer.head = buffer.tail = 0;
2016-06-04 21:29:01 +02:00
}
/**
* @brief Removes and returns a item from the queue
* @details Removes the oldest item on the queue, pointed to by the
* buffer_t head field. The item is returned to the caller.
* @return type T item
2016-06-04 21:29:01 +02:00
*/
T dequeue() {
2019-09-25 15:29:59 +02:00
if (isEmpty()) return T();
2016-06-04 21:29:01 +02:00
2019-09-25 15:29:59 +02:00
uint8_t index = buffer.head;
2016-06-04 21:29:01 +02:00
2019-09-25 15:29:59 +02:00
--buffer.count;
if (++buffer.head == buffer.size)
buffer.head = 0;
2016-06-04 21:29:01 +02:00
2019-09-25 15:29:59 +02:00
return buffer.queue[index];
2016-06-04 21:29:01 +02:00
}
/**
* @brief Adds an item to the queue
* @details Adds an item to the queue on the location pointed by the buffer_t
* tail variable. Returns false if no queue space is available.
* @param item Item to be added to the queue
* @return true if the operation was successful
2016-06-04 21:29:01 +02:00
*/
bool enqueue(T const &item) {
2019-09-25 15:29:59 +02:00
if (isFull()) return false;
2016-06-04 21:29:01 +02:00
2019-09-25 15:29:59 +02:00
buffer.queue[buffer.tail] = item;
2016-06-04 21:29:01 +02:00
2019-09-25 15:29:59 +02:00
++buffer.count;
if (++buffer.tail == buffer.size)
buffer.tail = 0;
2016-06-04 21:29:01 +02:00
return true;
}
/**
* @brief Checks if the queue has no items
2016-06-04 21:29:01 +02:00
* @details Returns true if there are no items on the queue, false otherwise.
* @return true if queue is empty
2016-06-04 21:29:01 +02:00
*/
2019-09-25 15:29:59 +02:00
bool isEmpty() { return buffer.count == 0; }
2016-06-04 21:29:01 +02:00
/**
* @brief Checks if the queue is full
2016-06-04 21:29:01 +02:00
* @details Returns true if the queue is full, false otherwise.
* @return true if queue is full
2016-06-04 21:29:01 +02:00
*/
2019-09-25 15:29:59 +02:00
bool isFull() { return buffer.count == buffer.size; }
2016-06-04 21:29:01 +02:00
/**
* @brief Gets the queue size
2016-06-04 21:29:01 +02:00
* @details Returns the maximum number of items a queue can have.
* @return the queue size
2016-06-04 21:29:01 +02:00
*/
2019-09-25 15:29:59 +02:00
uint8_t size() { return buffer.size; }
2016-06-04 21:29:01 +02:00
/**
* @brief Gets the next item from the queue without removing it
* @details Returns the next item in the queue without removing it
* or updating the pointers.
* @return first item in the queue
2016-06-04 21:29:01 +02:00
*/
2019-09-25 15:29:59 +02:00
T peek() { return buffer.queue[buffer.head]; }
2016-06-04 21:29:01 +02:00
/**
* @brief Gets the number of items on the queue
* @details Returns the current number of items stored on the queue.
* @return number of items in the queue
2016-06-04 21:29:01 +02:00
*/
2019-09-25 15:29:59 +02:00
uint8_t count() { return buffer.count; }
2016-06-04 21:29:01 +02:00
};