BlueTooth HC-05 module running on a ST-Nucleo-F303K8

Dependencies:   HC05 mbed

Revision:
4:9c0216df5c8e
Parent:
2:b60cb847489c
Child:
5:47575b30c19a
--- a/main.cpp	Thu Nov 06 10:13:20 2014 +0000
+++ b/main.cpp	Thu Feb 04 18:20:16 2016 +0000
@@ -1,12 +1,100 @@
 #include "mbed.h"
 
-DigitalOut myled(LED1);
+#define SOP 's'
+#define EOP 'e'
+
+DigitalOut myLed(LED1);
+
+Serial pc(USBTX, USBRX); // tx, rx
+Serial myBT(D1, D0); //Tx, Rx
+
+int index = 0;
+
+int xVal = 0, yVal = 0, cVal = 0;
+
+char btData[20];
+
+bool started = false;
+bool ended = false;
 
 int main() {
-    while(1) {
-        myled = 1; // LED is ON
-        wait(0.2); // 200 ms
-        myled = 0; // LED is OFF
-        wait(1.0); // 1 sec
+    
+    pc.baud(115200);
+    myBT.baud(9600);
+    pc.printf("\r\n -= Serial Bluetooth test \r\n");
+    
+    char btChar; //Char to hold the bluetooth command lette
+    
+    while(1) { 
+        while(myBT.readable())
+        {
+            btChar = myBT.getc();
+            
+            if(btChar == SOP)
+            {
+                index = 0;
+                btData[index] = '\0';
+                started = true;
+                ended = false;
+            }
+            else if(btChar == EOP)
+            {
+                ended = true;
+                break;
+            }
+            else
+            {
+                if(index < 19)
+                {
+                    btData[index] = btChar;
+                    index++;
+                    btData[index] = '\0';
+                }
+            }
+        }
+        
+        if(started && ended)
+        {
+            char *name = strtok(btData, "=");
+                        
+            while(name)
+            {                  
+                char *valToken = strtok(NULL, ",");
+                if(valToken)
+                {                    
+                    int val = atoi(valToken);
+                                    
+                    if(strcmp(name, "X") == 0)
+                    {
+                        xVal = val;            
+                    }
+                    else if(strcmp(name, "Y") == 0) 
+                    {
+                        yVal = val;
+                    }
+                    else if(strcmp(name, "C") == 0) 
+                    {
+                        cVal = val;
+                    }
+                }
+                name = strtok(NULL, "=");
+            }
+                
+            // Reset for the next packet
+            started = false;
+            ended = false;
+            index = 0;
+            btData[index] = '\0';
+            pc.printf("X= %d, Y= %d, C= %d \r\n",xVal, yVal, cVal);
+        }
+        
+        
+//        myBT.scanf("%c", &btChar); //Read a letter from the bluetooth stream.
+//        pc.printf("btChar: %c ", btChar);
+        
+        myLed = !myLed;
+//        wait(0.5);
+//        pc.printf("loop %d\r\n", counter);
+//        counter++;
     }
 }