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