mirror of
https://github.com/Show-maket/IR-protocol.git
synced 2025-05-03 23:00:16 +00:00
39 lines
782 B
C++
39 lines
782 B
C++
#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;
|
|
}
|
|
|
|
void push(T element) {
|
|
noInterrupts();
|
|
if (!isFull()) {
|
|
data[end] = element;
|
|
end = (end + 1) % BufferSize;
|
|
}
|
|
interrupts();
|
|
}
|
|
|
|
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;
|
|
}; |