Multi-Hackers / SocketModem

Dependents:   M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more

Revision:
0:563b70517320
Child:
2:8d3ea0dfce39
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils/MTSCircularBuffer.cpp	Mon Dec 09 15:29:35 2013 +0000
@@ -0,0 +1,162 @@
+#ifndef MTSCIRCULARBUFFER_CPP
+#define MTSCIRCULARBUFFER_CPP
+
+#include "MTSCircularBuffer.h"
+
+MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(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) && (available() > 0)) {
+        if (readIndex == bufferSize) {
+            readIndex = 0;
+        }
+        data[i++] = buffer[readIndex++];
+    }
+    checkThreshold();
+    return i;
+}
+
+int MTSCircularBuffer::read()
+{
+    if (available() == 0) {
+        return -1;
+    }
+    if (readIndex == bufferSize) {
+        readIndex = 0;
+    }
+    int tmp = buffer[readIndex++];
+    checkThreshold();
+    return tmp;
+}
+
+int MTSCircularBuffer::write(char* data, int length)
+{
+    if (length > capacity()) {
+        return -1;
+    }
+    int i = 0;
+    while((i < length) && (available() < bufferSize)) {
+        if(writeIndex == bufferSize) {
+            writeIndex = 0;
+        }
+        buffer[writeIndex++] = data[i++];
+    }
+    checkThreshold();
+    return capacity();
+}
+
+int MTSCircularBuffer::write(char data)
+{
+    if (available() == bufferSize) {
+        return -1;
+    }
+    if(writeIndex == bufferSize) {
+        writeIndex = 0;
+    }
+    buffer[writeIndex++] = data;
+    checkThreshold();
+    return capacity();
+}
+
+void MTSCircularBuffer::attach(void(*fptr)(void), int _threshold, Vars::RelationalOperator _op)
+{
+    threshold = _threshold;
+    op = _op;
+    notify.attach(fptr);
+}
+
+template<typename T>
+void MTSCircularBuffer::attach(T *tptr, void(T::*mptr)(void), int threshold, Vars::RelationalOperator op)
+{
+    this.threshold = threshold;
+    this.op = op;
+    notify.attach(tptr, mptr);
+}
+
+int MTSCircularBuffer::capacity()
+{
+    return bufferSize - available();
+}
+
+int MTSCircularBuffer::available()
+{
+    if (readIndex <= writeIndex) {
+        return writeIndex - readIndex;
+    } else {
+        return bufferSize + writeIndex - readIndex;
+    }
+}
+
+bool MTSCircularBuffer::isFull()
+{
+    if (available() == bufferSize) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+bool MTSCircularBuffer::isEmpty()
+{
+    if (available() == 0) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
+void MTSCircularBuffer::clear()
+{
+    writeIndex = readIndex;
+}
+
+void MTSCircularBuffer::checkThreshold()
+{
+    if (threshold == -1) {
+        return;
+    }
+    switch (op) {
+        case Vars::GREATER:
+            if (available() > threshold) {
+                notify.call();
+            }
+            break;
+        case Vars::LESS:
+            if (available() < threshold) {
+                notify.call();
+            }
+            break;
+        case Vars::GREATER_EQUAL:
+            if (available() >= threshold) {
+                notify.call();
+            }
+            break;
+        case Vars::LESS_EQUAL:
+            if (available() <= threshold) {
+                notify.call();
+            }
+            break;
+        case Vars::EQUAL:
+            if (available() == threshold) {
+                notify.call();
+            }
+            break;
+    }
+}
+
+#endif /* MTSCIRCULARBUFFER_CPP */
\ No newline at end of file