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@0:5d058c917599, 2013-09-16 (annotated)
- Committer:
- feb11
- Date:
- Mon Sep 16 14:35:39 2013 +0000
- Revision:
- 0:5d058c917599
- Child:
- 1:9953890d59e2
initial import
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 | 0:5d058c917599 | 23 | readIndex(0), |
feb11 | 0:5d058c917599 | 24 | writeIndex(1) |
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 | 0:5d058c917599 | 32 | while((readIndex+1)%T != writeIndex && read < length) |
feb11 | 0:5d058c917599 | 33 | { |
feb11 | 0:5d058c917599 | 34 | data[read++] = buffer[readIndex++]; |
feb11 | 0:5d058c917599 | 35 | if(readIndex == T) |
feb11 | 0:5d058c917599 | 36 | readIndex = 0; |
feb11 | 0:5d058c917599 | 37 | } |
feb11 | 0:5d058c917599 | 38 | |
feb11 | 0:5d058c917599 | 39 | return read; |
feb11 | 0:5d058c917599 | 40 | } |
feb11 | 0:5d058c917599 | 41 | |
feb11 | 0:5d058c917599 | 42 | template<size_t T> |
feb11 | 0:5d058c917599 | 43 | int CircularBuffer<T>::write(uint8_t *data, uint32_t length) |
feb11 | 0:5d058c917599 | 44 | { |
feb11 | 0:5d058c917599 | 45 | uint32_t wrote = 0; |
feb11 | 0:5d058c917599 | 46 | while(writeIndex != readIndex && wrote < length) |
feb11 | 0:5d058c917599 | 47 | { |
feb11 | 0:5d058c917599 | 48 | buffer[writeIndex++] = data[wrote++]; |
feb11 | 0:5d058c917599 | 49 | if(writeIndex == T) |
feb11 | 0:5d058c917599 | 50 | writeIndex = 0; |
feb11 | 0:5d058c917599 | 51 | } |
feb11 | 0:5d058c917599 | 52 | |
feb11 | 0:5d058c917599 | 53 | return wrote; |
feb11 | 0:5d058c917599 | 54 | } |
feb11 | 0:5d058c917599 | 55 | |
feb11 | 0:5d058c917599 | 56 | typedef CircularBuffer<32> SmallCircularBuffer; |
feb11 | 0:5d058c917599 | 57 | typedef CircularBuffer<128> MediumCircularBuffer; |
feb11 | 0:5d058c917599 | 58 | typedef CircularBuffer<512> BigCircularBuffer; |
feb11 | 0:5d058c917599 | 59 | |
feb11 | 0:5d058c917599 | 60 | #endif |