Marcus Chang / AsyncSerial
Revision:
9:e183765bd81b
Parent:
8:de7aaaf557ba
Child:
10:9d3ae421081b
--- a/source/AsyncSerial.cpp	Tue Apr 07 12:40:44 2015 +0000
+++ b/source/AsyncSerial.cpp	Fri Apr 10 13:05:30 2015 +0000
@@ -2,6 +2,8 @@
 
 #include "mbed.h"
 
+#define MINIMUM_TIMEOUT 1
+
 AsyncSerial::AsyncSerial(PinName tx, PinName rx, PinName rts, PinName cts)
     :   SerialBase(tx, rx),
 
@@ -35,7 +37,8 @@
         conditionEndBuffer(NULL),
         conditionEndLength(0),
         conditionIndex(0),
-        timeout()
+        timeout(),
+        receiveStatus(AsyncSerial::RECEIVE_TIMEOUT)
 {
     SerialBase::attach<AsyncSerial>(this, &AsyncSerial::getReady, SerialBase::RxIrq);
     SerialBase::attach<AsyncSerial>(this, &AsyncSerial::putDone, SerialBase::TxIrq);
@@ -43,26 +46,21 @@
 
 void AsyncSerial::putDone()
 {
-    if (sendLength > 0)
-    {
-        sendIndex++;
+    sendIndex++;
 
-        if (sendIndex < sendLength)
-        {
-            SerialBase::_base_putc(sendBuffer[sendIndex]);
-        }
-        else
+    if (sendIndex < sendLength)
+    {
+        SerialBase::_base_putc(sendBuffer[sendIndex]);
+    }
+    else
+    {
+        if (sendDoneHandler)
         {
-            sendLength = 0;
-
-            if (sendDoneHandler)
-            {
-                sendDoneHandler(sendBuffer, sendLength);
-            }
-            else if (sendObject)
-            {
-                sendDoneObject(sendObject, sendMember, sendBuffer, sendLength);
-            }
+            sendDoneHandler();
+        }
+        else if (sendObject)
+        {
+            sendDoneObject(sendObject, sendMember);
         }
     }
 }
@@ -76,19 +74,7 @@
         DEBUG("%c", input);
 
         if (insideCondition)
-        {
-            if (receiveBuffer != NULL)
-            {
-                receiveBuffer[receiveIndex] = input;
-                receiveIndex++;
-
-                if (receiveIndex == receiveMaxLength)
-                {
-                    timeout.detach();
-                    getDone(AsyncSerial::RECEIVE_FULL);
-                }
-            }
-
+        {            
             if (conditionEndBuffer != NULL)
             {
                 if (input == conditionEndBuffer[conditionIndex])
@@ -96,9 +82,9 @@
                     conditionIndex++;
 
                     if (conditionIndex == conditionEndLength)
-                    {
-                        timeout.detach();
-                        getDone(AsyncSerial::RECEIVE_FOUND);
+                    {                        
+                        receiveStatus = AsyncSerial::RECEIVE_FOUND;
+                        timeout.attach_us<AsyncSerial>(this, &AsyncSerial::receiveTimeout, MINIMUM_TIMEOUT);
                     }
                 }
                 else
@@ -106,6 +92,18 @@
                     conditionIndex = 0;
                 }
             }
+
+            if (receiveBuffer != NULL)
+            {
+                receiveBuffer[receiveIndex] = input;
+                receiveIndex++;
+
+                if ((receiveIndex == receiveMaxLength) && (receiveStatus != AsyncSerial::RECEIVE_FOUND))
+                {
+                    receiveStatus = AsyncSerial::RECEIVE_FULL;
+                    timeout.attach_us<AsyncSerial>(this, &AsyncSerial::receiveTimeout, MINIMUM_TIMEOUT);
+                }
+            }
         }
         else
         {
@@ -118,6 +116,7 @@
                     if (conditionIndex == conditionStartLength)
                     {
                         insideCondition = true;
+                        conditionIndex = 0;
                     }
                 }
                 else
@@ -135,7 +134,7 @@
 
     DEBUG("getDone: %X %X %X\r\n", this, waitDoneHandler, waitObject);
 
-    if ((receiveBuffer == 0) && (conditionStartBuffer == 0))
+    if ((receiveBuffer == NULL) && (conditionStartBuffer == NULL))
     {
         if (waitDoneHandler)
         {
@@ -225,12 +224,14 @@
         insideCondition = true;
     }
 
+    receiveStatus = AsyncSerial::RECEIVE_TIMEOUT;
     timeout.attach_us<AsyncSerial>(this, &AsyncSerial::receiveTimeout, timeoutMilli * 1000);
     isReceiving = true;
 
-    DEBUG("receive: %X\r\n", waitObject);
+    DEBUG("receive: %p\r\n", receiveBuffer);
 }
 
+
 void AsyncSerial::wait(wait_done_t handler,
                        const char* conditionEndBuffer, uint16_t conditionEndLength,
                        uint32_t timeoutMilli)
@@ -252,7 +253,7 @@
 {
     DEBUG("timeout: %X %X\r\n", this, waitObject);
     
-    getDone(AsyncSerial::RECEIVE_TIMEOUT);
+    getDone(receiveStatus);
 }
 
 int AsyncSerial::getc()