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@1:9953890d59e2, 2013-09-16 (annotated)
- Committer:
- feb11
- Date:
- Mon Sep 16 14:54:26 2013 +0000
- Revision:
- 1:9953890d59e2
- Parent:
- 0:5d058c917599
- Child:
- 2:6f3630f5fa06
fixed bug
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
feb11 | 0:5d058c917599 | 1 | #ifndef CIRCULAR_BUFFER_H |
feb11 | 0:5d058c917599 | 2 | #define CIRCULAR_BUFFER_H |
feb11 | 0:5d058c917599 | 3 | |
feb11 | 0:5d058c917599 | 4 | template<size_t T> |
feb11 | 0:5d058c917599 | 5 | class CircularBuffer |
feb11 | 0:5d058c917599 | 6 | { |
feb11 | 0:5d058c917599 | 7 | public : |
feb11 | 0:5d058c917599 | 8 | |
feb11 | 0:5d058c917599 | 9 | CircularBuffer(); |
feb11 | 0:5d058c917599 | 10 | |
feb11 | 0:5d058c917599 | 11 | int read(uint8_t *data, uint32_t length); |
feb11 | 0:5d058c917599 | 12 | int write(uint8_t *data, uint32_t length); |
feb11 | 0:5d058c917599 | 13 | |
feb11 | 0:5d058c917599 | 14 | private : |
feb11 | 0:5d058c917599 | 15 | |
feb11 | 0:5d058c917599 | 16 | uint32_t readIndex, writeIndex; |
feb11 | 0:5d058c917599 | 17 | uint8_t buffer[T]; |
feb11 | 0:5d058c917599 | 18 | |
feb11 | 0:5d058c917599 | 19 | }; |
feb11 | 0:5d058c917599 | 20 | |
feb11 | 0:5d058c917599 | 21 | template<size_t T> |
feb11 | 0:5d058c917599 | 22 | CircularBuffer<T>::CircularBuffer(): |
feb11 | 1:9953890d59e2 | 23 | readIndex(T), |
feb11 | 1:9953890d59e2 | 24 | writeIndex(0) |
feb11 | 0:5d058c917599 | 25 | { |
feb11 | 0:5d058c917599 | 26 | } |
feb11 | 0:5d058c917599 | 27 | |
feb11 | 0:5d058c917599 | 28 | template<size_t T> |
feb11 | 0:5d058c917599 | 29 | int CircularBuffer<T>::read(uint8_t *data, uint32_t length) |
feb11 | 0:5d058c917599 | 30 | { |
feb11 | 0:5d058c917599 | 31 | uint32_t read = 0; |
feb11 | 1:9953890d59e2 | 32 | while(readIndex%T != writeIndex && read < length) |
feb11 | 0:5d058c917599 | 33 | { |
feb11 | 1:9953890d59e2 | 34 | if(readIndex >= T) |
feb11 | 1:9953890d59e2 | 35 | readIndex %= T; |
feb11 | 0:5d058c917599 | 36 | data[read++] = buffer[readIndex++]; |
feb11 | 1:9953890d59e2 | 37 | |
feb11 | 0:5d058c917599 | 38 | } |
feb11 | 0:5d058c917599 | 39 | |
feb11 | 0:5d058c917599 | 40 | return read; |
feb11 | 0:5d058c917599 | 41 | } |
feb11 | 0:5d058c917599 | 42 | |
feb11 | 0:5d058c917599 | 43 | template<size_t T> |
feb11 | 0:5d058c917599 | 44 | int CircularBuffer<T>::write(uint8_t *data, uint32_t length) |
feb11 | 0:5d058c917599 | 45 | { |
feb11 | 0:5d058c917599 | 46 | uint32_t wrote = 0; |
feb11 | 1:9953890d59e2 | 47 | while((writeIndex+1)%T != readIndex%T && wrote < length) |
feb11 | 0:5d058c917599 | 48 | { |
feb11 | 0:5d058c917599 | 49 | buffer[writeIndex++] = data[wrote++]; |
feb11 | 0:5d058c917599 | 50 | if(writeIndex == T) |
feb11 | 0:5d058c917599 | 51 | writeIndex = 0; |
feb11 | 0:5d058c917599 | 52 | } |
feb11 | 0:5d058c917599 | 53 | |
feb11 | 0:5d058c917599 | 54 | return wrote; |
feb11 | 0:5d058c917599 | 55 | } |
feb11 | 0:5d058c917599 | 56 | |
feb11 | 0:5d058c917599 | 57 | typedef CircularBuffer<32> SmallCircularBuffer; |
feb11 | 0:5d058c917599 | 58 | typedef CircularBuffer<128> MediumCircularBuffer; |
feb11 | 1:9953890d59e2 | 59 | typedef CircularBuffer<256> BigCircularBuffer; |
feb11 | 0:5d058c917599 | 60 | |
feb11 | 0:5d058c917599 | 61 | #endif |