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: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
Diff: utils/MTSCircularBuffer.cpp
- Revision:
- 64:0ca9c7123ffc
- Parent:
- 46:b30547bf07d5
- Child:
- 65:ed113472b2c1
--- a/utils/MTSCircularBuffer.cpp Fri Dec 20 21:11:56 2013 +0000 +++ b/utils/MTSCircularBuffer.cpp Sat Dec 21 04:22:12 2013 +0000 @@ -5,7 +5,7 @@ using namespace mts; -MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(0), _threshold(-1), _op(Vars::GREATER) +MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(0), bytes(0), _threshold(-1), _op(Vars::GREATER) { buffer = new char[bufferSize]; } @@ -23,11 +23,12 @@ int MTSCircularBuffer::read(char* data, int length) { int i = 0; - while ((i < length) && (available() > 0)) { + while ((i < length) && (bytes > 0)) { if (readIndex == bufferSize) { readIndex = 0; } data[i++] = buffer[readIndex++]; + bytes--; } checkThreshold(); return i; @@ -35,13 +36,14 @@ int MTSCircularBuffer::read(char& data) { - if (available() == 0) { + if (bytes == 0) { return 0; } if (readIndex == bufferSize) { readIndex = 0; } data = buffer[readIndex++]; + bytes--; checkThreshold(); return 1; } @@ -49,11 +51,12 @@ int MTSCircularBuffer::write(const char* data, int length) { int i = 0; - while((i < length) && (available() < bufferSize)) { + while((i < length) && (bytes < bufferSize)) { if(writeIndex == bufferSize) { writeIndex = 0; } buffer[writeIndex++] = data[i++]; + bytes++; } checkThreshold(); return i; @@ -61,34 +64,31 @@ int MTSCircularBuffer::write(char data) { - if (available() == bufferSize) { + if (bytes == bufferSize) { return 0; } if(writeIndex == bufferSize) { writeIndex = 0; } buffer[writeIndex++] = data; + bytes++; checkThreshold(); return 1; } int MTSCircularBuffer::capacity() { - return bufferSize - available(); + return bufferSize - bytes; } int MTSCircularBuffer::available() { - if (readIndex <= writeIndex) { - return writeIndex - readIndex; - } else { - return bufferSize + writeIndex - readIndex; - } + return bytes; } bool MTSCircularBuffer::isFull() { - if (available() == bufferSize) { + if (bytes == bufferSize) { return true; } else { return false; @@ -97,7 +97,7 @@ bool MTSCircularBuffer::isEmpty() { - if (available() == 0) { + if (bytes == 0) { return true; } else { return false; @@ -106,7 +106,7 @@ void MTSCircularBuffer::clear() { - writeIndex = readIndex; + writeIndex = readIndex = bytes = 0; } void MTSCircularBuffer::checkThreshold() @@ -116,27 +116,27 @@ } switch (_op) { case Vars::GREATER: - if (available() > _threshold) { + if (bytes > _threshold) { notify.call(); } break; case Vars::LESS: - if (available() < _threshold) { + if (bytes < _threshold) { notify.call(); } break; case Vars::GREATER_EQUAL: - if (available() >= _threshold) { + if (bytes >= _threshold) { notify.call(); } break; case Vars::LESS_EQUAL: - if (available() <= _threshold) { + if (bytes <= _threshold) { notify.call(); } break; case Vars::EQUAL: - if (available() == _threshold) { + if (bytes == _threshold) { notify.call(); } break;