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
utils/MTSCircularBuffer.cpp
- Committer:
- sgodinez
- Date:
- 2013-12-21
- Revision:
- 65:ed113472b2c1
- Parent:
- 60:ee9c7a700330
- Parent:
- 64:0ca9c7123ffc
- Child:
- 67:1003b410f781
File content as of revision 65:ed113472b2c1:
#ifndef MTSCIRCULARBUFFER_CPP
#define MTSCIRCULARBUFFER_CPP
#include "MTSCircularBuffer.h"
using namespace mts;
MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(0), bytes(0), _threshold(-1), _op(Vars::GREATER)
{
buffer = new char[bufferSize];
}
MTSCircularBuffer::~MTSCircularBuffer()
{
delete[] buffer;
}
int MTSCircularBuffer::getSize()
{
return bufferSize;
}
int MTSCircularBuffer::read(char* data, int length)
{
int i = 0;
while ((i < length) && (bytes > 0)) {
if (readIndex == bufferSize) {
readIndex = 0;
}
data[i++] = buffer[readIndex++];
bytes--;
checkThreshold();
}
return i;
}
int MTSCircularBuffer::read(char& data)
{
if (bytes == 0) {
return 0;
}
if (readIndex == bufferSize) {
readIndex = 0;
}
data = buffer[readIndex++];
bytes--;
checkThreshold();
return 1;
}
int MTSCircularBuffer::write(const char* data, int length)
{
int i = 0;
while((i < length) && (bytes < bufferSize)) {
if(writeIndex == bufferSize) {
writeIndex = 0;
}
buffer[writeIndex++] = data[i++];
bytes++;
checkThreshold();
}
return i;
}
int MTSCircularBuffer::write(char data)
{
if (bytes == bufferSize) {
return 0;
}
if(writeIndex == bufferSize) {
writeIndex = 0;
}
buffer[writeIndex++] = data;
bytes++;
checkThreshold();
return 1;
}
int MTSCircularBuffer::capacity()
{
return bufferSize - bytes;
}
int MTSCircularBuffer::available()
{
return bytes;
}
bool MTSCircularBuffer::isFull()
{
if (bytes == bufferSize) {
return true;
} else {
return false;
}
}
bool MTSCircularBuffer::isEmpty()
{
if (bytes == 0) {
return true;
} else {
return false;
}
}
void MTSCircularBuffer::clear()
{
writeIndex = readIndex = bytes = 0;
}
void MTSCircularBuffer::checkThreshold()
{
if (_threshold == -1) {
return;
}
switch (_op) {
case Vars::GREATER:
if (bytes > _threshold) {
notify.call();
}
break;
case Vars::LESS:
if (bytes < _threshold) {
notify.call();
}
break;
case Vars::GREATER_EQUAL:
if (bytes >= _threshold) {
notify.call();
}
break;
case Vars::LESS_EQUAL:
if (bytes <= _threshold) {
notify.call();
}
break;
case Vars::EQUAL:
if (bytes == _threshold) {
notify.call();
}
break;
}
}
#endif /* MTSCIRCULARBUFFER_CPP */
uIP Socket Modem Shield (Outdated - see below)