Code to stream phone data to mbed using Adafruit Bluetooh BLE and app.

Dependencies:   mbed

Revision:
0:e9ff18ab0247
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon May 02 01:32:43 2016 +0000
@@ -0,0 +1,74 @@
+#include "mbed.h"
+#include "USBKeyboard.h"
+Serial bluemod(p28,p27);
+Serial pc(USBTX, USBRX);
+DigitalIn pb1(p20);
+DigitalIn pb2(p19);
+ 
+//C union can convert 4 chars to a float - puts them in same location in memory
+//trick to pack the 4 bytes from Bluetooth serial port back into a 32-bit float
+union f_or_char {
+    float f;
+    char  c[4];
+};
+
+char balls[] = {'p', 'r', 'b'};
+char throws = 'n'; 
+ 
+int main()
+{  
+    pb1.mode(PullUp);
+    pb2.mode(PullUp);
+    pc.baud(9600);
+    char bchecksum=0;
+    char temp=0;
+    char button = 'n';
+    union f_or_char x,y,z,w;
+    pc.printf("!");
+//    while (1) {
+//        pc.printf("h");
+//        pc.printf("%c", bluemod.getc());
+//    }
+    int ball = 0;
+    while(1) {
+        bchecksum=0;
+        if (!pb1) {
+            throws = 't';
+        }
+        if (!pb2) {
+            ball = (ball+1) %3;
+        }
+        if (bluemod.getc()=='!') {
+            char bmgc = bluemod.getc();
+            if (bmgc =='Q') { //Q data packet
+                for (int i=0; i<4; i++) {
+                    temp = bluemod.getc();
+                    x.c[i] = temp;
+                    bchecksum = bchecksum + temp;
+                }// for
+                for (int i=0; i<4; i++) {
+                    temp = bluemod.getc();
+                    y.c[i] = temp;
+                    bchecksum = bchecksum + temp;
+                }// for
+                for (int i=0; i<4; i++) {
+                    temp = bluemod.getc();
+                    z.c[i] = temp;
+                    bchecksum = bchecksum + temp;
+                }// for
+                for (int i=0; i<4; i++) {
+                    temp = bluemod.getc();
+                    w.c[i] = temp;
+                    bchecksum = bchecksum + temp;
+                }//for
+                if (bluemod.getc()==char(~('!' + 'Q' + bchecksum))) { //checksum OK?
+
+                  pc.printf("%0.3f,", x.f);
+                  pc.printf("%0.3f,", y.f);
+                  pc.printf("%c%c,", throws, balls[ball]);
+                  throws = 'n';
+                }//if
+            }// if
+        }// if
+    }// while
+}// main