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 Francois Berder

Test suite can be found in this application, CircularBufferTest

Committer:
feb11
Date:
Fri Sep 20 09:16:45 2013 +0000
Revision:
2:6f3630f5fa06
Parent:
1:9953890d59e2
Child:
3:9a45d6675e65
initial import

Who changed what in which revision?

UserRevisionLine numberNew 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 2:6f3630f5fa06 11 uint32_t read(uint8_t *data, uint32_t length);
feb11 2:6f3630f5fa06 12 uint32_t write(uint8_t *data, uint32_t length);
feb11 2:6f3630f5fa06 13 uint32_t getCapacity() const;
feb11 2:6f3630f5fa06 14 uint32_t getSize() const;
feb11 2:6f3630f5fa06 15
feb11 0:5d058c917599 16 private :
feb11 0:5d058c917599 17
feb11 2:6f3630f5fa06 18 uint16_t readIndex, writeIndex;
feb11 0:5d058c917599 19 uint8_t buffer[T];
feb11 0:5d058c917599 20
feb11 0:5d058c917599 21 };
feb11 0:5d058c917599 22
feb11 0:5d058c917599 23 template<size_t T>
feb11 0:5d058c917599 24 CircularBuffer<T>::CircularBuffer():
feb11 2:6f3630f5fa06 25 readIndex(0),
feb11 1:9953890d59e2 26 writeIndex(0)
feb11 0:5d058c917599 27 {
feb11 0:5d058c917599 28 }
feb11 0:5d058c917599 29
feb11 0:5d058c917599 30 template<size_t T>
feb11 2:6f3630f5fa06 31 uint32_t CircularBuffer<T>::read(uint8_t *data, uint32_t length)
feb11 0:5d058c917599 32 {
feb11 2:6f3630f5fa06 33 uint32_t n = 0;
feb11 2:6f3630f5fa06 34 while(n < length && getSize() < T)
feb11 0:5d058c917599 35 {
feb11 2:6f3630f5fa06 36 if(readIndex == T)
feb11 2:6f3630f5fa06 37 readIndex = 0;
feb11 2:6f3630f5fa06 38 data[n++] = buffer[readIndex++];
feb11 0:5d058c917599 39 }
feb11 0:5d058c917599 40
feb11 2:6f3630f5fa06 41 return n;
feb11 0:5d058c917599 42 }
feb11 0:5d058c917599 43
feb11 0:5d058c917599 44 template<size_t T>
feb11 2:6f3630f5fa06 45 uint32_t CircularBuffer<T>::write(uint8_t *data, uint32_t length)
feb11 0:5d058c917599 46 {
feb11 2:6f3630f5fa06 47 uint32_t n = 0;
feb11 2:6f3630f5fa06 48 while(n < length && getSize() > 0)
feb11 0:5d058c917599 49 {
feb11 0:5d058c917599 50 if(writeIndex == T)
feb11 0:5d058c917599 51 writeIndex = 0;
feb11 2:6f3630f5fa06 52 buffer[writeIndex++] = data[n++];
feb11 0:5d058c917599 53 }
feb11 0:5d058c917599 54
feb11 2:6f3630f5fa06 55 return n;
feb11 2:6f3630f5fa06 56 }
feb11 2:6f3630f5fa06 57
feb11 2:6f3630f5fa06 58 template<size_t T>
feb11 2:6f3630f5fa06 59 uint32_t CircularBuffer<T>::getCapacity() const
feb11 2:6f3630f5fa06 60 {
feb11 2:6f3630f5fa06 61 return T;
feb11 2:6f3630f5fa06 62 }
feb11 2:6f3630f5fa06 63
feb11 2:6f3630f5fa06 64 template<size_t T>
feb11 2:6f3630f5fa06 65 uint32_t CircularBuffer<T>::getSize() const
feb11 2:6f3630f5fa06 66 {
feb11 2:6f3630f5fa06 67 return ((writeIndex >= readIndex) ? (T - writeIndex + readIndex) : (readIndex - writeIndex));
feb11 0:5d058c917599 68 }
feb11 0:5d058c917599 69
feb11 0:5d058c917599 70 typedef CircularBuffer<32> SmallCircularBuffer;
feb11 0:5d058c917599 71 typedef CircularBuffer<128> MediumCircularBuffer;
feb11 2:6f3630f5fa06 72 typedef CircularBuffer<512> BigCircularBuffer;
feb11 0:5d058c917599 73
feb11 0:5d058c917599 74 #endif