VersaMCU/src/CEventQueue.h
2026-03-29 14:47:13 +02:00

37 lines
1.2 KiB
C++

// CEventQueue.h
// Fester Ring-Buffer FIFO für SEvent-Objekte.
//
// Bewusst ohne Heap (kein new/delete) und ohne STL (kein std::vector/queue)
// um auf dem SAMD21 mit 16KB RAM deterministisches Verhalten zu garantieren.
//
// Kapazität: QUEUE_SIZE - 1 = 16 Events (ein Slot bleibt leer damit
// is_full() und is_empty() ohne extra Zähler unterscheidbar sind).
//
// Thread-Sicherheit:
// push() wird aus ISR-Kontext aufgerufen (encoder_cb).
// pop() wird aus Loop-Kontext aufgerufen (processEvents).
// Auf Cortex-M0+ sind 8-Bit-Lese/Schreibzugriffe atomar → kein Mutex nötig
// solange nur ein Producer (ISR) und ein Consumer (Loop) existieren.
#pragma once
#include "SEvent.h"
#include <stdint.h>
class CEventQueue
{
public:
// Event einreihen. Gibt false zurück wenn Queue voll (Event wird verworfen).
bool push(SEvent ev);
// Ältestes Event abholen (FIFO). Gibt false zurück wenn Queue leer.
bool pop(SEvent& out);
bool is_empty() const;
bool is_full() const;
private:
static const uint8_t QUEUE_SIZE = 17; // 16 nutzbare Slots
SEvent m_buf[QUEUE_SIZE];
uint8_t m_head = 0; // Nächster Lese-Index (Consumer: pop)
uint8_t m_tail = 0; // Nächster Schreib-Index (Producer: push)
};