Marcus Chang / AsyncSerial
Revision:
15:4d7d96cacc18
Parent:
13:dbb23efed611
Child:
16:55e6fbfc1e9d
--- a/source/AsyncSerial.cpp	Tue Apr 14 15:00:30 2015 +0000
+++ b/source/AsyncSerial.cpp	Wed Apr 15 17:30:47 2015 +0100
@@ -11,7 +11,6 @@
         sendLength(0),
         sendIndex(0),
 
-        isReceiving(false),
         receiveBuffer(NULL),
         receiveMaxLength(0),
         receiveIndex(0),
@@ -35,7 +34,7 @@
     SerialBase::attach<AsyncSerial>(this, &AsyncSerial::putDone, SerialBase::TxIrq);
 
 #if DEVICE_SERIAL_FC
-//    SerialBase::set_flow_control(SerialBase::RTSCTS, rts, cts);
+    SerialBase::set_flow_control(SerialBase::RTSCTS, rts, cts);
 #endif
 }
 
@@ -62,93 +61,85 @@
 */
 void AsyncSerial::getReady()
 {
-    /*  Only read characters from buffer if we are receiving.
-        On platforms with flow control, the full buffer will
-        force the sender to pause. This will prevent loss of
-        data between calls to receive.
-    */
-    if (isReceiving)
+    // read character from buffer
+    uint8_t input = SerialBase::_base_getc();
+
+    DEBUG("%c", input);
+
+    // check if start condition has been met
+    if (insideCondition)
     {
-        // read character from buffer
-        uint8_t input = SerialBase::_base_getc();
+        /*  If stop condition has been set, check if the
+            character matches. If it does increment counter
+            to point to next character in sequence. Otherwise
+            reset sequence search.
+        */
+        if (conditionEndBuffer != NULL)
+        {
+            if (input == conditionEndBuffer[conditionIndex])
+            {
+                conditionIndex++;
 
-        DEBUG("%c", input);
+                /*  End condition has been met.
+                    Set receive status to indicate sequence has been found
+                    and re-arm timer. The timeout is responsible for
+                    signaling the callback function and is useful for
+                    decoupling the callback from the receive ISR.
+                */
+                if (conditionIndex == conditionEndLength)
+                {
+                    receiveStatus = AsyncSerial::RECEIVE_FOUND;
+                    timeout.attach_us<AsyncSerial>(this, &AsyncSerial::receiveTimeout, MINIMUM_TIMEOUT);
+                }
+            }
+            else
+            {
+                // Character didn't match sequence. Start over.
+                conditionIndex = 0;
+            }
+        }
 
-        // check if start condition has been met
-        if (insideCondition)
+        /*  A receive buffer is available.
+            Store character in buffer and check if buffer is full,
+            set receive status and re-arm timeout if it is.
+        */
+        if (receiveBuffer != NULL)
         {
-            /*  If stop condition has been set, check if the
-                character matches. If it does increment counter
-                to point to next character in sequence. Otherwise
-                reset sequence search.
+            receiveBuffer[receiveIndex] = input;
+            receiveIndex++;
+
+            /*  If end condition has been met we still store the character
+                but we do not change the receive status nor re-arm the timer.
             */
-            if (conditionEndBuffer != NULL)
+            if ((receiveIndex == receiveMaxLength) && (receiveStatus != AsyncSerial::RECEIVE_FOUND))
             {
-                if (input == conditionEndBuffer[conditionIndex])
-                {
-                    conditionIndex++;
+                receiveStatus = AsyncSerial::RECEIVE_FULL;
+                timeout.attach_us<AsyncSerial>(this, &AsyncSerial::receiveTimeout, MINIMUM_TIMEOUT);
+            }
+        }
+    }
+    /*  Start condition has not been met.
+    */
+    else
+    {
+        if (conditionStartBuffer != NULL)
+        {
+            if (input == conditionStartBuffer[conditionIndex])
+            {
+                conditionIndex++;
 
-                    /*  End condition has been met.
-                        Set receive status to indicate sequence has been found
-                        and re-arm timer. The timeout is responsible for
-                        signaling the callback function and is useful for
-                        decoupling the callback from the receive ISR.
-                    */
-                    if (conditionIndex == conditionEndLength)
-                    {
-                        receiveStatus = AsyncSerial::RECEIVE_FOUND;
-                        timeout.attach_us<AsyncSerial>(this, &AsyncSerial::receiveTimeout, MINIMUM_TIMEOUT);
-                    }
-                }
-                else
+                /*  Set condition flag and reset index since it is reused.
+                */
+                if (conditionIndex == conditionStartLength)
                 {
-                    // Character didn't match sequence. Start over.
+                    insideCondition = true;
                     conditionIndex = 0;
                 }
             }
-
-            /*  A receive buffer is available.
-                Store character in buffer and check if buffer is full,
-                set receive status and re-arm timeout if it is.
-            */
-            if (receiveBuffer != NULL)
+            else
             {
-                receiveBuffer[receiveIndex] = input;
-                receiveIndex++;
-
-                /*  If end condition has been met we still store the character
-                    but we do not change the receive status nor re-arm the timer.
-                */
-                if ((receiveIndex == receiveMaxLength) && (receiveStatus != AsyncSerial::RECEIVE_FOUND))
-                {
-                    receiveStatus = AsyncSerial::RECEIVE_FULL;
-                    timeout.attach_us<AsyncSerial>(this, &AsyncSerial::receiveTimeout, MINIMUM_TIMEOUT);
-                }
-            }
-        }
-        /*  Start condition has not been met.
-        */
-        else
-        {
-            if (conditionStartBuffer != NULL)
-            {
-                if (input == conditionStartBuffer[conditionIndex])
-                {
-                    conditionIndex++;
-
-                    /*  Set condition flag and reset index since it is reused.
-                    */
-                    if (conditionIndex == conditionStartLength)
-                    {
-                        insideCondition = true;
-                        conditionIndex = 0;
-                    }
-                }
-                else
-                {
-                    // Character didn't match sequence. Start over.
-                    conditionIndex = 0;
-                }
+                // Character didn't match sequence. Start over.
+                conditionIndex = 0;
             }
         }
     }
@@ -158,9 +149,6 @@
 */
 void AsyncSerial::getDone(uint8_t status)
 {
-    // stop reception
-    isReceiving = false;
-
     DEBUG("getDone: %d\r\n", status);
 
     /*  Check whether to call the wait handler or the receive handler.
@@ -278,7 +266,6 @@
 
     // Arm timer and start receiving.
     timeout.attach_us<AsyncSerial>(this, &AsyncSerial::receiveTimeout, _timeoutMilli * 1000);
-    isReceiving = true;
 
     DEBUG("receive: %p\r\n", _receiveBuffer);
 }