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 10:32:30 2013 +0000
Revision:
4:e15dee1d59ee
Parent:
3:9a45d6675e65
Child:
5:abe8909f9603
added comments

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 4:e15dee1d59ee 4 /** This class implements a static circular buffer.
feb11 4:e15dee1d59ee 5 */
feb11 0:5d058c917599 6 template<size_t T>
feb11 0:5d058c917599 7 class CircularBuffer
feb11 0:5d058c917599 8 {
feb11 0:5d058c917599 9 public :
feb11 0:5d058c917599 10
feb11 4:e15dee1d59ee 11 /** Default constructor
feb11 4:e15dee1d59ee 12 */
feb11 0:5d058c917599 13 CircularBuffer();
feb11 0:5d058c917599 14
feb11 4:e15dee1d59ee 15 /** Reads data from buffer
feb11 4:e15dee1d59ee 16
feb11 4:e15dee1d59ee 17 \param data output buffer
feb11 4:e15dee1d59ee 18 \param length Maximum number of bytes to read
feb11 4:e15dee1d59ee 19 \return Number of bytes read
feb11 4:e15dee1d59ee 20
feb11 4:e15dee1d59ee 21 \note The return value cannot exceed max(length,capacity)
feb11 4:e15dee1d59ee 22 */
feb11 2:6f3630f5fa06 23 uint32_t read(uint8_t *data, uint32_t length);
feb11 4:e15dee1d59ee 24
feb11 4:e15dee1d59ee 25 /** Writes data in buffer
feb11 4:e15dee1d59ee 26
feb11 4:e15dee1d59ee 27 \param data input buffer
feb11 4:e15dee1d59ee 28 \param length Maximum number of bytes to write
feb11 4:e15dee1d59ee 29 \return Number of bytes wrote
feb11 4:e15dee1d59ee 30
feb11 4:e15dee1d59ee 31 \note The return value cannot exceed max(length,capacity)
feb11 4:e15dee1d59ee 32 */
feb11 2:6f3630f5fa06 33 uint32_t write(uint8_t *data, uint32_t length);
feb11 3:9a45d6675e65 34
feb11 4:e15dee1d59ee 35 /** Returns the total capacity of this buffer
feb11 4:e15dee1d59ee 36 \return Capacity of buffer
feb11 4:e15dee1d59ee 37 */
feb11 2:6f3630f5fa06 38 uint32_t getCapacity() const;
feb11 4:e15dee1d59ee 39
feb11 4:e15dee1d59ee 40 /** Returns the number of bytes available in the buffer
feb11 4:e15dee1d59ee 41 \return Number of bytes available in the buffer
feb11 4:e15dee1d59ee 42 */
feb11 3:9a45d6675e65 43 uint32_t getSize() const;
feb11 4:e15dee1d59ee 44
feb11 4:e15dee1d59ee 45 /** Checks if this buffer is empty
feb11 4:e15dee1d59ee 46 \return True if the buffer is empty, false otherwise
feb11 4:e15dee1d59ee 47 */
feb11 3:9a45d6675e65 48 bool isEmpty() const;
feb11 4:e15dee1d59ee 49
feb11 4:e15dee1d59ee 50 /** Checks if this buffer is full
feb11 4:e15dee1d59ee 51 \return True if the buffer is full, false otherwise
feb11 4:e15dee1d59ee 52 */
feb11 3:9a45d6675e65 53 bool isFull() const;
feb11 2:6f3630f5fa06 54
feb11 0:5d058c917599 55 private :
feb11 0:5d058c917599 56
feb11 2:6f3630f5fa06 57 uint16_t readIndex, writeIndex;
feb11 0:5d058c917599 58 uint8_t buffer[T];
feb11 0:5d058c917599 59
feb11 0:5d058c917599 60 };
feb11 0:5d058c917599 61
feb11 0:5d058c917599 62 template<size_t T>
feb11 0:5d058c917599 63 CircularBuffer<T>::CircularBuffer():
feb11 3:9a45d6675e65 64 readIndex(T),
feb11 1:9953890d59e2 65 writeIndex(0)
feb11 0:5d058c917599 66 {
feb11 0:5d058c917599 67 }
feb11 0:5d058c917599 68
feb11 0:5d058c917599 69 template<size_t T>
feb11 2:6f3630f5fa06 70 uint32_t CircularBuffer<T>::read(uint8_t *data, uint32_t length)
feb11 0:5d058c917599 71 {
feb11 2:6f3630f5fa06 72 uint32_t n = 0;
feb11 3:9a45d6675e65 73 while(n < length && getSize() > 0)
feb11 0:5d058c917599 74 {
feb11 2:6f3630f5fa06 75 if(readIndex == T)
feb11 2:6f3630f5fa06 76 readIndex = 0;
feb11 2:6f3630f5fa06 77 data[n++] = buffer[readIndex++];
feb11 0:5d058c917599 78 }
feb11 0:5d058c917599 79
feb11 2:6f3630f5fa06 80 return n;
feb11 0:5d058c917599 81 }
feb11 0:5d058c917599 82
feb11 0:5d058c917599 83 template<size_t T>
feb11 2:6f3630f5fa06 84 uint32_t CircularBuffer<T>::write(uint8_t *data, uint32_t length)
feb11 0:5d058c917599 85 {
feb11 2:6f3630f5fa06 86 uint32_t n = 0;
feb11 3:9a45d6675e65 87 while(n < length && getSize() < T)
feb11 0:5d058c917599 88 {
feb11 0:5d058c917599 89 if(writeIndex == T)
feb11 0:5d058c917599 90 writeIndex = 0;
feb11 2:6f3630f5fa06 91 buffer[writeIndex++] = data[n++];
feb11 0:5d058c917599 92 }
feb11 0:5d058c917599 93
feb11 2:6f3630f5fa06 94 return n;
feb11 2:6f3630f5fa06 95 }
feb11 2:6f3630f5fa06 96
feb11 2:6f3630f5fa06 97 template<size_t T>
feb11 2:6f3630f5fa06 98 uint32_t CircularBuffer<T>::getCapacity() const
feb11 2:6f3630f5fa06 99 {
feb11 2:6f3630f5fa06 100 return T;
feb11 2:6f3630f5fa06 101 }
feb11 2:6f3630f5fa06 102
feb11 2:6f3630f5fa06 103 template<size_t T>
feb11 2:6f3630f5fa06 104 uint32_t CircularBuffer<T>::getSize() const
feb11 2:6f3630f5fa06 105 {
feb11 3:9a45d6675e65 106 return ((writeIndex > readIndex) ? (writeIndex - readIndex) : (T + writeIndex - readIndex));
feb11 3:9a45d6675e65 107 }
feb11 3:9a45d6675e65 108
feb11 3:9a45d6675e65 109 template<size_t T>
feb11 3:9a45d6675e65 110 bool CircularBuffer<T>::isEmpty() const
feb11 3:9a45d6675e65 111 {
feb11 3:9a45d6675e65 112 return getSize() == 0;
feb11 3:9a45d6675e65 113 }
feb11 3:9a45d6675e65 114
feb11 3:9a45d6675e65 115 template<size_t T>
feb11 3:9a45d6675e65 116 bool CircularBuffer<T>::isFull() const
feb11 3:9a45d6675e65 117 {
feb11 3:9a45d6675e65 118 return getSize() == T;
feb11 0:5d058c917599 119 }
feb11 0:5d058c917599 120
feb11 0:5d058c917599 121 typedef CircularBuffer<32> SmallCircularBuffer;
feb11 0:5d058c917599 122 typedef CircularBuffer<128> MediumCircularBuffer;
feb11 2:6f3630f5fa06 123 typedef CircularBuffer<512> BigCircularBuffer;
feb11 0:5d058c917599 124
feb11 0:5d058c917599 125 #endif