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

Dependencies:   mbed

main.cpp

Committer:
zjohnson31
Date:
2016-05-02
Revision:
0:e9ff18ab0247

File content as of revision 0:e9ff18ab0247:

#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