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.

Files at this revision

API Documentation at this revision

Comitter:
feb11
Date:
Mon Sep 16 14:54:26 2013 +0000
Parent:
0:5d058c917599
Child:
2:6f3630f5fa06
Commit message:
fixed bug

Changed in this revision

CircularBuffer.h Show annotated file Show diff for this revision Revisions of this file
--- a/CircularBuffer.h	Mon Sep 16 14:35:39 2013 +0000
+++ b/CircularBuffer.h	Mon Sep 16 14:54:26 2013 +0000
@@ -20,8 +20,8 @@
 
 template<size_t T>
 CircularBuffer<T>::CircularBuffer():
-readIndex(0),
-writeIndex(1)
+readIndex(T),
+writeIndex(0)
 {
 }
 
@@ -29,11 +29,12 @@
 int CircularBuffer<T>::read(uint8_t *data, uint32_t length)
 {
     uint32_t read = 0;
-    while((readIndex+1)%T != writeIndex && read < length)
+    while(readIndex%T != writeIndex && read < length)
     {
+        if(readIndex >= T)
+            readIndex %= T;
         data[read++] = buffer[readIndex++];
-        if(readIndex == T)
-            readIndex = 0;
+
     }
     
     return read;
@@ -43,7 +44,7 @@
 int CircularBuffer<T>::write(uint8_t *data, uint32_t length)
 {
     uint32_t wrote = 0;
-    while(writeIndex != readIndex && wrote < length)
+    while((writeIndex+1)%T != readIndex%T && wrote < length)
     {
         buffer[writeIndex++] = data[wrote++];
         if(writeIndex == T)
@@ -55,6 +56,6 @@
 
 typedef CircularBuffer<32> SmallCircularBuffer;
 typedef CircularBuffer<128> MediumCircularBuffer;
-typedef CircularBuffer<512> BigCircularBuffer;
+typedef CircularBuffer<256> BigCircularBuffer;
 
 #endif