Small library for using circular buffers (forked from François Berder's implementation in order to add methods and fix problems)
Dependents: CircularBufferTest XBeeApi
Fork of CircularBuffer by
Test suite can be found in this application, CircularBufferTest
CircularBuffer.h
- Committer:
- feb11
- Date:
- 2013-09-16
- Revision:
- 1:9953890d59e2
- Parent:
- 0:5d058c917599
- Child:
- 2:6f3630f5fa06
File content as of revision 1:9953890d59e2:
#ifndef CIRCULAR_BUFFER_H #define CIRCULAR_BUFFER_H template<size_t T> class CircularBuffer { public : CircularBuffer(); int read(uint8_t *data, uint32_t length); int write(uint8_t *data, uint32_t length); private : uint32_t readIndex, writeIndex; uint8_t buffer[T]; }; template<size_t T> CircularBuffer<T>::CircularBuffer(): readIndex(T), writeIndex(0) { } template<size_t T> int CircularBuffer<T>::read(uint8_t *data, uint32_t length) { uint32_t read = 0; while(readIndex%T != writeIndex && read < length) { if(readIndex >= T) readIndex %= T; data[read++] = buffer[readIndex++]; } return read; } template<size_t T> int CircularBuffer<T>::write(uint8_t *data, uint32_t length) { uint32_t wrote = 0; while((writeIndex+1)%T != readIndex%T && wrote < length) { buffer[writeIndex++] = data[wrote++]; if(writeIndex == T) writeIndex = 0; } return wrote; } typedef CircularBuffer<32> SmallCircularBuffer; typedef CircularBuffer<128> MediumCircularBuffer; typedef CircularBuffer<256> BigCircularBuffer; #endif