Driver for the JY-MCU v1.06 HC-06 Bluetooth module.

Dependents:   DISCO-F746NG_rtos_test MbedTableControl

Revision:
20:13283edd1aba
Parent:
18:85c0f6580cd8
Child:
21:cce827364df1
--- a/HC06Bluetooth.cpp	Fri Aug 05 17:13:14 2016 -0400
+++ b/HC06Bluetooth.cpp	Mon Aug 08 15:03:13 2016 -0400
@@ -6,7 +6,7 @@
  */
 
 #include <HC06Bluetooth.h>
-#include <algorithm>
+#include "rtos.h"
 
 /* Static methods used to help configure the Baudrate. */
 
@@ -18,7 +18,7 @@
 
 /* HC06 Bluetooth Class Implementation: */
 HC06Bluetooth::HC06Bluetooth(PinName TX, PinName RX, Baudrate baudrate, void (*lineCallbackFunc) (const char* readString, size_t strlen), void (*charCallbackFunc) (char readChar))
-    : btSerialObj(TX, RX), baudrate(baudrate), lineCallbackFunc(lineCallbackFunc), charCallbackFunc(charCallbackFunc)
+    : btSerialObj(TX, RX), baudrate(baudrate), receiveByteThreadObj(osPriorityRealtime), lineCallbackFunc(lineCallbackFunc), charCallbackFunc(charCallbackFunc)
 {
     btSerialObj.baud(BaudValue[baudrate]);
 
@@ -26,6 +26,7 @@
     if ((lineCallbackFunc != NULL) || (charCallbackFunc != NULL))
     {
         btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR);
+        receiveByteThreadObj.start(this, &HC06Bluetooth::receiveByteThread);
     }
 }
 
@@ -125,39 +126,51 @@
     {
         dataReceivedBuffer.push(btSerialObj.getc());
     }
+    receiveByteThreadObj.signal_set(SIG_BT_BYTE);
+}
 
+void HC06Bluetooth::receiveByteThread()
+{
     // Now that all characters have been read, process them.
-    while(!dataReceivedBuffer.empty())
+    while(true)
     {
-       char receivedChar = dataReceivedBuffer.front();
-       dataReceivedBuffer.pop();
-        // Call the character callback function if it is not null.
-        if (charCallbackFunc != NULL) charCallbackFunc(receivedChar);
+        if (!dataReceivedBuffer.empty())
+        {
+            char receivedChar = dataReceivedBuffer.front();
+            dataReceivedBuffer.pop();
+            // Call the character callback function if it is not null.
+            if (charCallbackFunc != NULL) charCallbackFunc(receivedChar);
 
-        if (lineCallbackFunc != NULL)
-        {
-           // If the character is a newline or carriage return, then call the line callback function.
-           if ((receivedChar == '\n') || (receivedChar == '\r'))
+            if (lineCallbackFunc != NULL)
             {
-               // Clear whatever was in the toClient buffer before.
-               dataReceivedToClient.clear();
-               // Copy everything from the queue to the client buffer.
-               while(!dataReceivedBufferCopy.empty()) {
-                  dataReceivedToClient.push_back(dataReceivedBufferCopy.front());
-                  dataReceivedBufferCopy.pop();
-               }
-               // Null-terminate the string.
-               dataReceivedToClient.push_back('\0');
+                // If the character is a newline or carriage return, then call the line callback function.
+                if ((receivedChar == '\n') || (receivedChar == '\r'))
+                {
+                    // Clear whatever was in the toClient buffer before.
+                    dataReceivedToClient.clear();
+                    // Copy everything from the queue to the client buffer.
+                    while(!dataReceivedBufferCopy.empty())
+                    {
+                        dataReceivedToClient.push_back(dataReceivedBufferCopy.front());
+                        dataReceivedBufferCopy.pop();
+                    }
+                    // Null-terminate the string.
+                    dataReceivedToClient.push_back('\0');
 
-               // Call the callback function with the toClient buffer.
-               lineCallbackFunc(&dataReceivedToClient[0], dataReceivedToClient.size());
-            }
+                    // Call the callback function with the toClient buffer.
+                    lineCallbackFunc(&dataReceivedToClient[0], dataReceivedToClient.size());
+                }
 
-            // Otherwise, enqueue it in the copy.
-            else {
-               dataReceivedBufferCopy.push(receivedChar);
+                // Otherwise, enqueue it in the copy.
+                else
+                {
+                    dataReceivedBufferCopy.push(receivedChar);
+                }
             }
         }
+        else {
+            receiveByteThreadObj.signal_wait(SIG_BT_BYTE);
+        }
     }
 }