VersaMCU/src/CEventQueue.cpp

42 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// CEventQueue.cpp
// Ring-Buffer FIFO-Implementierung.
//
// Funktionsprinzip (klassischer Power-of-2-freier Ring-Buffer):
// m_head = Lese-Index (pop)
// m_tail = Schreib-Index (push)
// Leer: m_head == m_tail
// Voll: (m_tail + 1) % SIZE == m_head → ein Slot bleibt immer frei
//
// Nebenläufigkeit:
// Encoder-ISRs und der Matrixcallback im Loop können beide push() aufrufen.
// Der Matrixcallback schützt seinen push() mit einer kurzen Critical Section,
// sodass m_tail nie von Loop und ISR gleichzeitig verändert wird.
#include "CEventQueue.h"
bool CEventQueue::is_empty() const
{
return m_head == m_tail;
}
bool CEventQueue::is_full() const
{
// Voll wenn der nächste Schreib-Index auf den Lese-Index zeigen würde
return ((m_tail + 1) % QUEUE_SIZE) == m_head;
}
bool CEventQueue::push(SEvent ev)
{
if (is_full()) return false; // Event verwerfen sollte bei 16 Slots nie passieren
m_buf[m_tail] = ev;
m_tail = (m_tail + 1) % QUEUE_SIZE;
return true;
}
bool CEventQueue::pop(SEvent& out)
{
if (is_empty()) return false;
out = m_buf[m_head];
m_head = (m_head + 1) % QUEUE_SIZE;
return true;
}