Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: CircularBufferExample
Revision 5:8204677dc3c4, committed 2014-03-17
- Comitter:
- feb11
- Date:
- Mon Mar 17 17:30:25 2014 +0000
- Parent:
- 4:e15dee1d59ee
- 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 Fri Sep 20 10:32:30 2013 +0000
+++ b/CircularBuffer.h Mon Mar 17 17:30:25 2014 +0000
@@ -1,6 +1,8 @@
#ifndef CIRCULAR_BUFFER_H
#define CIRCULAR_BUFFER_H
+#include <stdint.h>
+
/** This class implements a static circular buffer.
*/
template<size_t T>
@@ -56,13 +58,14 @@
uint16_t readIndex, writeIndex;
uint8_t buffer[T];
-
+ size_t bytesAvailable;
};
template<size_t T>
CircularBuffer<T>::CircularBuffer():
-readIndex(T),
-writeIndex(0)
+readIndex(0),
+writeIndex(0),
+bytesAvailable(0)
{
}
@@ -72,9 +75,10 @@
uint32_t n = 0;
while(n < length && getSize() > 0)
{
+ data[n++] = buffer[readIndex++];
if(readIndex == T)
readIndex = 0;
- data[n++] = buffer[readIndex++];
+ --bytesAvailable;
}
return n;
@@ -86,11 +90,12 @@
uint32_t n = 0;
while(n < length && getSize() < T)
{
+ buffer[writeIndex++] = data[n++];
if(writeIndex == T)
writeIndex = 0;
- buffer[writeIndex++] = data[n++];
+ ++bytesAvailable;
}
-
+
return n;
}
@@ -103,7 +108,7 @@
template<size_t T>
uint32_t CircularBuffer<T>::getSize() const
{
- return ((writeIndex > readIndex) ? (writeIndex - readIndex) : (T + writeIndex - readIndex));
+ return bytesAvailable;
}
template<size_t T>