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

Revision:
1:9953890d59e2
Parent:
0:5d058c917599
Child:
2:6f3630f5fa06
diff -r 5d058c917599 -r 9953890d59e2 CircularBuffer.h
--- 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