#ifndef RBUFFER_H_ #define RBUFFER_H_ /** * @file: RBuffer.h \brief Ringbuffer as template class * * \ingroup avr_tools * Implements a simple ringbuffer */ #include "libavr/globals.h" template < class IndexType, unsigned Size, class EntryType > /** * Template class for a ringbuffer. The buffer index, the type of * the buffered data and the buffer size can be defined. * * Instantiate via: RBuffer elements from the newest object. * Note that you are not able to retrieve more than the number * of elements on the buffer. This has to be checked in the * application code. */ EntryType getObjFromNewest(unsigned position) { IndexType idx; idx = m_putIndex - 1 - position + Size; idx = idx % Size; return m_buffer[idx]; } /* * Returns the current position of the index */ IndexType getCurrentIndex() { return (m_putIndex - 1 + Size) % Size; } /* * Clears the buffer and resets all indexes etc. */ void clear() { m_buffer = m_buffer[Size]; m_isFull = false; m_putIndex = 0; } private: // n is the last element in the buffer; m_putIndex points to n+1 volatile bool m_isFull; volatile IndexType m_putIndex; volatile EntryType m_buffer[ Size ]; }; #endif /*RBUFFER_H_*/