Small library for using circular buffers

Dependents:   CircularBufferExample

This library provides circular buffers. The main difference with other circular buffer libraries is that it does not use dynamic memory allocation for storing data. Instead, the buffer is allocated statically.

Three types of buffer exist by default :

  • SmallCircularBuffer (32 bytes)
  • MediumCircularBuffer (128 bytes)
  • BigCircularBuffer (512 bytes)

You can also define buffers with specific size :

CircularBuffer<4> buffer;    // 4 bytes buffer
CircularBuffer<102> buffer2; // 102 bytes buffer

Import programCircularBufferExample

This example shows how to use the CircularBuffer library.

Committer:
feb11
Date:
Fri Sep 20 10:04:23 2013 +0000
Revision:
3:9a45d6675e65
Parent:
2:6f3630f5fa06
Child:
4:e15dee1d59ee
added functions

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