e

Dependencies:   NeoStrip USBDevice mbed

Committer:
baraki
Date:
Tue Sep 22 21:49:44 2015 +0000
Revision:
0:5b9f87a086ce
for use with blue mbed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
baraki 0:5b9f87a086ce 1 #include "bluetoothComm.h"
baraki 0:5b9f87a086ce 2
baraki 0:5b9f87a086ce 3 //USBSerial console1; // tx, rx
baraki 0:5b9f87a086ce 4 Serial bt(p9,p10); // tx, rx
baraki 0:5b9f87a086ce 5 bool bluetoothConnected;
baraki 0:5b9f87a086ce 6 bool validBluetoothData;
baraki 0:5b9f87a086ce 7 char bluetoothData[50]={0};
baraki 0:5b9f87a086ce 8 int getIndex=0;
baraki 0:5b9f87a086ce 9 // Read data from the bluetooth
baraki 0:5b9f87a086ce 10 // Stores data in the bluetoothData array
baraki 0:5b9f87a086ce 11 // Messages must end in the null termination character ('\0')
baraki 0:5b9f87a086ce 12 // Will return true if a complete bluetooth message has been received (and thus if the data in bluetoothData is valid)
baraki 0:5b9f87a086ce 13 // This method DOES NOT BLOCK
baraki 0:5b9f87a086ce 14 // it will read as long as data is available and then stop, whether or not the message is complete
baraki 0:5b9f87a086ce 15 // at the next call, it will continue reading and adding to the array until '\0' is received
baraki 0:5b9f87a086ce 16 // Thus, this method is meant to be called once per loop
baraki 0:5b9f87a086ce 17 bool getBluetoothData()
baraki 0:5b9f87a086ce 18 {
baraki 0:5b9f87a086ce 19 if(validBluetoothData) // reset array
baraki 0:5b9f87a086ce 20 {
baraki 0:5b9f87a086ce 21 getIndex = 0;
baraki 0:5b9f87a086ce 22 validBluetoothData = false;
baraki 0:5b9f87a086ce 23 }
baraki 0:5b9f87a086ce 24
baraki 0:5b9f87a086ce 25 if(!bt.readable())
baraki 0:5b9f87a086ce 26 return false;
baraki 0:5b9f87a086ce 27
baraki 0:5b9f87a086ce 28 while(bt.readable() && !validBluetoothData)
baraki 0:5b9f87a086ce 29 {
baraki 0:5b9f87a086ce 30 bluetoothData[getIndex] = getBluetoothChar();
baraki 0:5b9f87a086ce 31 validBluetoothData = (bluetoothData[getIndex] == '\0');
baraki 0:5b9f87a086ce 32 getIndex++;
baraki 0:5b9f87a086ce 33 }
baraki 0:5b9f87a086ce 34 return validBluetoothData;
baraki 0:5b9f87a086ce 35 }
baraki 0:5b9f87a086ce 36
baraki 0:5b9f87a086ce 37 // Returns the value of bluetoothConnected
baraki 0:5b9f87a086ce 38 // This variable must be set somewhere, probably in processBluetoothData
baraki 0:5b9f87a086ce 39 bool isBluetoothConnected()
baraki 0:5b9f87a086ce 40 {
baraki 0:5b9f87a086ce 41 return bluetoothConnected;
baraki 0:5b9f87a086ce 42 }
baraki 0:5b9f87a086ce 43
baraki 0:5b9f87a086ce 44 void setBluetoothConnected(bool btCon){
baraki 0:5b9f87a086ce 45 bluetoothConnected = btCon;
baraki 0:5b9f87a086ce 46 }
baraki 0:5b9f87a086ce 47
baraki 0:5b9f87a086ce 48 // Read bluetooth data and then process it
baraki 0:5b9f87a086ce 49 void processBluetoothData()
baraki 0:5b9f87a086ce 50 {
baraki 0:5b9f87a086ce 51 if(!getBluetoothData())
baraki 0:5b9f87a086ce 52 return;
baraki 0:5b9f87a086ce 53 // DO SOMETHING WITH bluetoothData HERE
baraki 0:5b9f87a086ce 54 // If it is a valid message, set bluetoothConnected = true
baraki 0:5b9f87a086ce 55 bluetoothConnected = true;
baraki 0:5b9f87a086ce 56 }
baraki 0:5b9f87a086ce 57
baraki 0:5b9f87a086ce 58 char* returnBluetoothData(){
baraki 0:5b9f87a086ce 59 return bluetoothData;
baraki 0:5b9f87a086ce 60 }
baraki 0:5b9f87a086ce 61
baraki 0:5b9f87a086ce 62 bool isBluetoothDataValid()
baraki 0:5b9f87a086ce 63 {
baraki 0:5b9f87a086ce 64 return validBluetoothData;
baraki 0:5b9f87a086ce 65 }
baraki 0:5b9f87a086ce 66
baraki 0:5b9f87a086ce 67 void robotLoop()
baraki 0:5b9f87a086ce 68 {
baraki 0:5b9f87a086ce 69 //robotPrintlnDebug();
baraki 0:5b9f87a086ce 70 processBluetoothData();
baraki 0:5b9f87a086ce 71 }
baraki 0:5b9f87a086ce 72
baraki 0:5b9f87a086ce 73 void robotSetup(int baud_rate)
baraki 0:5b9f87a086ce 74 {
baraki 0:5b9f87a086ce 75 bt.baud(baud_rate);
baraki 0:5b9f87a086ce 76 bluetoothConnected = false;
baraki 0:5b9f87a086ce 77 validBluetoothData = false;
baraki 0:5b9f87a086ce 78 bluetoothData[0] = '\0';
baraki 0:5b9f87a086ce 79 }
baraki 0:5b9f87a086ce 80
baraki 0:5b9f87a086ce 81 void sendBluetoothData(const char* data)
baraki 0:5b9f87a086ce 82 {
baraki 0:5b9f87a086ce 83 //robotPrintDebug("Sending BT data <"); robotPrintDebug(data); robotPrintlnDebug(">");
baraki 0:5b9f87a086ce 84 int index = 0;
baraki 0:5b9f87a086ce 85 for(; index < length(data); index++)
baraki 0:5b9f87a086ce 86 {
baraki 0:5b9f87a086ce 87 sendBluetoothChar(data[index]);
baraki 0:5b9f87a086ce 88 wait_ms(5);
baraki 0:5b9f87a086ce 89 }
baraki 0:5b9f87a086ce 90 if(data[index-1] != '\0')
baraki 0:5b9f87a086ce 91 sendBluetoothChar('\0');
baraki 0:5b9f87a086ce 92 //robotPrintDebug("Sent BT data <"); robotPrintDebug(data); robotPrintlnDebug(">");
baraki 0:5b9f87a086ce 93 }