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.

CircularBuffer.h

Committer:
feb11
Date:
2014-03-17
Revision:
5:8204677dc3c4
Parent:
4:e15dee1d59ee

File content as of revision 5:8204677dc3c4:

#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H

#include <stdint.h>

/** This class implements a static circular buffer.
*/
template<size_t T>
class CircularBuffer
{
    public :
    
        /** Default constructor
        */
        CircularBuffer();
        
        /** Reads data from buffer
        
            \param data output buffer 
            \param length Maximum number of bytes to read
            \return Number of bytes read
            
            \note The return value cannot exceed max(length,capacity)
        */
        uint32_t read(uint8_t *data, uint32_t length);
        
        /** Writes data in buffer
        
            \param data input buffer
            \param length Maximum number of bytes to write
            \return Number of bytes wrote
            
            \note The return value cannot exceed max(length,capacity)
        */
        uint32_t write(uint8_t *data, uint32_t length);
        
        /** Returns the total capacity of this buffer
            \return Capacity of buffer
        */
        uint32_t getCapacity() const;
        
        /** Returns the number of bytes available in the buffer
            \return Number of bytes available in the buffer
        */
        uint32_t getSize() const;  
        
        /** Checks if this buffer is empty
            \return True if the buffer is empty, false otherwise
        */
        bool isEmpty() const;
        
        /** Checks if this buffer is full
            \return True if the buffer is full, false otherwise
        */        
        bool isFull() const;
        
    private :
    
        uint16_t readIndex, writeIndex;
        uint8_t buffer[T]; 
        size_t bytesAvailable;
};

template<size_t T>
CircularBuffer<T>::CircularBuffer():
readIndex(0),
writeIndex(0),
bytesAvailable(0)
{
}

template<size_t T>
uint32_t CircularBuffer<T>::read(uint8_t *data, uint32_t length)
{
    uint32_t n = 0;
    while(n < length && getSize() > 0)
    {
        data[n++] = buffer[readIndex++];
        if(readIndex == T)
            readIndex = 0;
        --bytesAvailable;
    }
    
    return n;
}

template<size_t T>
uint32_t CircularBuffer<T>::write(uint8_t *data, uint32_t length)
{
    uint32_t n = 0;
    while(n < length && getSize() < T)
    {
        buffer[writeIndex++] = data[n++];
        if(writeIndex == T)
            writeIndex = 0;
        ++bytesAvailable;
    }
        
    return n;
}

template<size_t T>
uint32_t CircularBuffer<T>::getCapacity() const
{
    return T;
}
        
template<size_t T>
uint32_t CircularBuffer<T>::getSize() const
{
    return bytesAvailable;
}

template<size_t T>
bool CircularBuffer<T>::isEmpty() const
{
    return getSize() == 0;
}

template<size_t T>
bool CircularBuffer<T>::isFull() const
{
    return getSize() == T;
}

typedef CircularBuffer<32> SmallCircularBuffer;
typedef CircularBuffer<128> MediumCircularBuffer;
typedef CircularBuffer<512> BigCircularBuffer;

#endif