mirror of
https://github.com/Show-maket/IR-protocol.git
synced 2026-09-21 20:39:35 +00:00
archive: freeze IR-protocol WIP before stepwise integration
This commit is contained in:
16
RingBuffer.h
16
RingBuffer.h
@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
#include "Arduino.h"
|
||||
#include "IrInterruptGuard.h"
|
||||
template <typename T, unsigned int BufferSize>
|
||||
class RingBuffer {
|
||||
public:
|
||||
@ -15,43 +15,39 @@ public:
|
||||
|
||||
bool push(T element) {
|
||||
bool pushed = false;
|
||||
noInterrupts();
|
||||
IrInterruptGuard guard;
|
||||
if (!isFull()) {
|
||||
data[end] = element;
|
||||
end = (end + 1) % BufferSize;
|
||||
pushed = true;
|
||||
}
|
||||
interrupts();
|
||||
return pushed;
|
||||
}
|
||||
|
||||
T* pop() {
|
||||
noInterrupts();
|
||||
IrInterruptGuard guard;
|
||||
T* value = nullptr;
|
||||
if (!isEmpty()) {
|
||||
value = &data[start];
|
||||
start = (start + 1) % BufferSize;
|
||||
}
|
||||
interrupts();
|
||||
return value;
|
||||
}
|
||||
|
||||
// B5: безопасный pop — копирует элемент под ОДНОЙ критсекцией и отдаёт по значению.
|
||||
// (T* pop() отдаёт указатель во внутренний слот; его внутренний interrupts() снимает внешнюю
|
||||
// защиту вызывающего ДО чтения *ptr → торн-рид, если кольцо переполнится в этом окне.)
|
||||
// (T* pop() отдаёт указатель во внутренний слот; после выхода слот снова может быть перезаписан.)
|
||||
bool pop(T &out) {
|
||||
bool popped = false;
|
||||
noInterrupts();
|
||||
IrInterruptGuard guard;
|
||||
if (!isEmpty()) {
|
||||
out = data[start];
|
||||
start = (start + 1) % BufferSize;
|
||||
popped = true;
|
||||
}
|
||||
interrupts();
|
||||
return popped;
|
||||
}
|
||||
|
||||
private:
|
||||
T data[BufferSize];
|
||||
unsigned int start, end;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user