sensory_array / Mbed 2 deprecated mbed_bluetooth_slave

Dependencies:   USBDevice mbed

Committer:
rkk
Date:
Fri Mar 13 19:59:36 2015 +0000
Revision:
0:e005e19869fc
slave bluetooth first draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rkk 0:e005e19869fc 1 #include "robotLibrary.h"
rkk 0:e005e19869fc 2
rkk 0:e005e19869fc 3 USBSerial console1; // tx, rx
rkk 0:e005e19869fc 4 Serial bt(p9,p10); // tx, rx
rkk 0:e005e19869fc 5 char bluetoothData[50];
rkk 0:e005e19869fc 6 bool bluetoothConnected;
rkk 0:e005e19869fc 7 bool validBluetoothData;
rkk 0:e005e19869fc 8
rkk 0:e005e19869fc 9 // Read data from the bluetooth
rkk 0:e005e19869fc 10 // Stores data in the bluetoothData array
rkk 0:e005e19869fc 11 // Messages must end in the null termination character ('\0')
rkk 0:e005e19869fc 12 // Will return true if a complete bluetooth message has been received (and thus if the data in bluetoothData is valid)
rkk 0:e005e19869fc 13 // This method DOES NOT BLOCK
rkk 0:e005e19869fc 14 // it will read as long as data is available and then stop, whether or not the message is complete
rkk 0:e005e19869fc 15 // at the next call, it will continue reading and adding to the array until '\0' is received
rkk 0:e005e19869fc 16 // Thus, this method is meant to be called once per loop
rkk 0:e005e19869fc 17 bool getBluetoothData()
rkk 0:e005e19869fc 18 {
rkk 0:e005e19869fc 19 if(validBluetoothData) // reset array
rkk 0:e005e19869fc 20 {
rkk 0:e005e19869fc 21 for(int i = 0; i < 50; i++)
rkk 0:e005e19869fc 22 bluetoothData[i] = '\0';
rkk 0:e005e19869fc 23 validBluetoothData = false;
rkk 0:e005e19869fc 24 }
rkk 0:e005e19869fc 25
rkk 0:e005e19869fc 26 if(!bluetoothAvailable())
rkk 0:e005e19869fc 27 return false;
rkk 0:e005e19869fc 28
rkk 0:e005e19869fc 29 int index = length(bluetoothData);
rkk 0:e005e19869fc 30 while(bluetoothAvailable() && !validBluetoothData)
rkk 0:e005e19869fc 31 {
rkk 0:e005e19869fc 32 bluetoothData[index++] = getBluetoothChar();
rkk 0:e005e19869fc 33 validBluetoothData = (bluetoothData[index-1] == '\0');
rkk 0:e005e19869fc 34 }
rkk 0:e005e19869fc 35 if(validBluetoothData)
rkk 0:e005e19869fc 36 {
rkk 0:e005e19869fc 37 // If it is a heartbeat, respond to it now
rkk 0:e005e19869fc 38 if(equals(bluetoothData, "?"))
rkk 0:e005e19869fc 39 {
rkk 0:e005e19869fc 40 sendBluetoothData("?");
rkk 0:e005e19869fc 41 bluetoothData[0] = '\0';
rkk 0:e005e19869fc 42 validBluetoothData = false;
rkk 0:e005e19869fc 43 return false;
rkk 0:e005e19869fc 44 }
rkk 0:e005e19869fc 45 robotPrintDebug("Got BT data <"); robotPrintDebug(bluetoothData); robotPrintlnDebug(">\0");
rkk 0:e005e19869fc 46 }
rkk 0:e005e19869fc 47 return validBluetoothData;
rkk 0:e005e19869fc 48 }
rkk 0:e005e19869fc 49
rkk 0:e005e19869fc 50 // This method is similar to the above method, except that this version DOES BLOCK
rkk 0:e005e19869fc 51 // It will keep trying to read until a complete message has been receieved or a timeout is reached
rkk 0:e005e19869fc 52 // Returns true if a valid message was received
rkk 0:e005e19869fc 53 //bool getBluetoothData()
rkk 0:e005e19869fc 54 //{
rkk 0:e005e19869fc 55 // bluetoothData[0] = '\0';
rkk 0:e005e19869fc 56 // if(!bluetoothAvailable())
rkk 0:e005e19869fc 57 // return false;
rkk 0:e005e19869fc 58 // int timeout = 50;
rkk 0:e005e19869fc 59 // int index = 0;
rkk 0:e005e19869fc 60 // bool terminated = false;
rkk 0:e005e19869fc 61 // unsigned long start = millis();
rkk 0:e005e19869fc 62 // while(!terminated && millis() - start < timeout)
rkk 0:e005e19869fc 63 // {
rkk 0:e005e19869fc 64 // while(!bluetoothAvailable() && millis() - start < timeout);
rkk 0:e005e19869fc 65 // bluetoothData[index++] = getBluetoothChar();
rkk 0:e005e19869fc 66 // start = millis();
rkk 0:e005e19869fc 67 // terminated = (bluetoothData[index-1] == '\0');
rkk 0:e005e19869fc 68 // }
rkk 0:e005e19869fc 69 // if(index > 0 && bluetoothData[index-1] != '\0')
rkk 0:e005e19869fc 70 // bluetoothData[index] = '\0';
rkk 0:e005e19869fc 71 // // If it is a heartbeat, respond to it now
rkk 0:e005e19869fc 72 // if(equals(bluetoothData, "?"))
rkk 0:e005e19869fc 73 // {
rkk 0:e005e19869fc 74 // sendBluetoothData("?");
rkk 0:e005e19869fc 75 // bluetoothData[0] = '\0';
rkk 0:e005e19869fc 76 // return false;
rkk 0:e005e19869fc 77 // }
rkk 0:e005e19869fc 78 // robotPrintDebug("Got BT data <"); robotPrintDebug(bluetoothData); robotPrintlnDebug(">");
rkk 0:e005e19869fc 79 // return true;
rkk 0:e005e19869fc 80 //}
rkk 0:e005e19869fc 81
rkk 0:e005e19869fc 82 // Returns the value of bluetoothConnected
rkk 0:e005e19869fc 83 // This variable must be set somewhere, probably in processBluetoothData
rkk 0:e005e19869fc 84 bool isBluetoothConnected()
rkk 0:e005e19869fc 85 {
rkk 0:e005e19869fc 86 return bluetoothConnected;
rkk 0:e005e19869fc 87 }
rkk 0:e005e19869fc 88
rkk 0:e005e19869fc 89 // Read bluetooth data and then process it
rkk 0:e005e19869fc 90 void processBluetoothData()
rkk 0:e005e19869fc 91 {
rkk 0:e005e19869fc 92 if(!getBluetoothData())
rkk 0:e005e19869fc 93 return;
rkk 0:e005e19869fc 94 // DO SOMETHING WITH bluetoothData HERE
rkk 0:e005e19869fc 95 // If it is a valid message, set bluetoothConnected = true
rkk 0:e005e19869fc 96 bluetoothConnected = true;
rkk 0:e005e19869fc 97 }
rkk 0:e005e19869fc 98
rkk 0:e005e19869fc 99 char* returnBluetoothData(){
rkk 0:e005e19869fc 100 return bluetoothData;
rkk 0:e005e19869fc 101 }
rkk 0:e005e19869fc 102
rkk 0:e005e19869fc 103 bool isBluetoothDataValid()
rkk 0:e005e19869fc 104 {
rkk 0:e005e19869fc 105 return validBluetoothData;
rkk 0:e005e19869fc 106 }
rkk 0:e005e19869fc 107
rkk 0:e005e19869fc 108 void robotLoop()
rkk 0:e005e19869fc 109 {
rkk 0:e005e19869fc 110 //robotPrintlnDebug();
rkk 0:e005e19869fc 111 processBluetoothData();
rkk 0:e005e19869fc 112 }
rkk 0:e005e19869fc 113
rkk 0:e005e19869fc 114 void robotSetup()
rkk 0:e005e19869fc 115 {
rkk 0:e005e19869fc 116 bt.baud(9600);
rkk 0:e005e19869fc 117 //sendBluetoothData("AT+NAME");
rkk 0:e005e19869fc 118 //sendBluetoothData("myRobotBT");
rkk 0:e005e19869fc 119 bluetoothConnected = false;
rkk 0:e005e19869fc 120 validBluetoothData = false;
rkk 0:e005e19869fc 121 bluetoothData[0] = '\0';
rkk 0:e005e19869fc 122 }
rkk 0:e005e19869fc 123
rkk 0:e005e19869fc 124 void sendBluetoothData(const char* data)
rkk 0:e005e19869fc 125 {
rkk 0:e005e19869fc 126 robotPrintDebug("Sending BT data <"); robotPrintDebug(data); robotPrintlnDebug(">");
rkk 0:e005e19869fc 127 int index = 0;
rkk 0:e005e19869fc 128 for(; index < length(data); index++)
rkk 0:e005e19869fc 129 {
rkk 0:e005e19869fc 130 sendBluetoothChar(data[index]);
rkk 0:e005e19869fc 131 wait_ms(5);
rkk 0:e005e19869fc 132 }
rkk 0:e005e19869fc 133 if(data[index-1] != '\0')
rkk 0:e005e19869fc 134 sendBluetoothChar('\0');
rkk 0:e005e19869fc 135 robotPrintDebug("Sent BT data <"); robotPrintDebug(data); robotPrintlnDebug(">");
rkk 0:e005e19869fc 136 }