#pragma once
#include "Arduino.h"
template <typename T, unsigned int BufferSize>
class RingBuffer {
public:
    RingBuffer() : start(0), end(0) {}

    bool isFull() const {
        return ((end + 1) % BufferSize) == start;
    }

    bool isEmpty() const {
        return start == end;
    }

    bool push(T element) {
        bool pushed = false;
        noInterrupts();
        if (!isFull()) {
            data[end] = element;
            end = (end + 1) % BufferSize;
            pushed = true;
        }
        interrupts();
        return pushed;
    }

    T* pop() {
        noInterrupts();
        T* value = nullptr;
        if (!isEmpty()) {
            value = &data[start];
            start = (start + 1) % BufferSize;
        }
        interrupts();
        return value;
    }

private:
    T data[BufferSize];
    unsigned int start, end;
};