Red Bird Racing VCU v2
 
Loading...
Searching...
No Matches
Queue.hpp
Go to the documentation of this file.
1/**
2 * @file Queue.hpp
3 * @author Planeson, Red Bird Racing
4 * @brief Declaration of a simple RingBuffer (circular buffer) template class
5 * @version 1.0
6 * @date 2026-01-26
7 * @dir Queue @brief The Queue library contains a simple RingBuffer (circular buffer) template class, used for buffering ADC readings for filtering.
8 */
9
10#ifndef QUEUE_HPP
11#define QUEUE_HPP
12
13#include <stdint.h>
14// using uint8_t for size
15// highest capacity is 255
16
17/**
18 * @brief RingBuffer (circular buffer) template class
19 * A small circular queue to hold a fixed number of elements.
20 * @tparam T Type of elements stored in the buffer
21 * @tparam size Capacity of the buffer
22 */
23template <typename T, uint8_t size>
25{
26public:
27 // Constructor
28 // Initializes the buffer and head pointer
29 constexpr RingBuffer() : buffer{0}, head(0), count(0) {}
30
31 /**
32 * @brief Pushes a new value into the ring buffer.
33 * This will overwrite the oldest value if the buffer is full.
34 *
35 * @param val The value to be added to the buffer.
36 */
37 void push(T val)
38 {
39 buffer[head] = val;
40 head = (head + 1) % size;
41 if (count < size)
42 ++count;
43 }
44
45 /**
46 * @brief Returns the elements in the buffer in linear order.
47 *
48 * @param out Pointer to an array where the linear buffer will be stored. This array shall be created by the caller and must have at least 'size' elements.
49 * @note The order of elements in the output array will be from oldest to newest.
50 */
51 void getLinearBuffer(T *out)
52 {
53 if (out == nullptr)
54 return;
55
56 for (int i = 0; i < count; ++i)
57 {
58 out[i] = buffer[(head + i) % size];
59 }
60 }
61
62 T buffer[size];
63 uint8_t head;
64 uint8_t count;
65};
66
67#endif // QUEUE_HPP
RingBuffer (circular buffer) template class A small circular queue to hold a fixed number of elements...
Definition: Queue.hpp:25
uint8_t count
Definition: Queue.hpp:64
T buffer[size]
Definition: Queue.hpp:62
void getLinearBuffer(T *out)
Returns the elements in the buffer in linear order.
Definition: Queue.hpp:51
uint8_t head
Definition: Queue.hpp:63
constexpr RingBuffer()
Definition: Queue.hpp:29
void push(T val)
Pushes a new value into the ring buffer. This will overwrite the oldest value if the buffer is full.
Definition: Queue.hpp:37