sensory_array / Mbed 2 deprecated ymbed_haptic_belt_slave_onethread

Dependencies:   USBDevice mbed mbed-rtos

Committer:
baraki
Date:
Thu Mar 26 19:51:11 2015 +0000
Revision:
1:2af026a7c290
Parent:
0:7c2805142589
Child:
2:1a0d675eaa6f
seems to work, using many printfs to debug

Who changed what in which revision?

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