Jesper Jakobsen / Mbed 2 deprecated nanoNucleo_Serial-Bluetooth_test

Dependencies:   HC05 mbed

Committer:
Kio_Liex
Date:
Thu Feb 04 18:20:16 2016 +0000
Revision:
4:9c0216df5c8e
Parent:
2:b60cb847489c
Child:
5:47575b30c19a
First working version. ; Gets X, Y and the buttons C value from the Androide app: "BT Bot Control"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:005629fe3609 1 #include "mbed.h"
screamer 0:005629fe3609 2
Kio_Liex 4:9c0216df5c8e 3 #define SOP 's'
Kio_Liex 4:9c0216df5c8e 4 #define EOP 'e'
Kio_Liex 4:9c0216df5c8e 5
Kio_Liex 4:9c0216df5c8e 6 DigitalOut myLed(LED1);
Kio_Liex 4:9c0216df5c8e 7
Kio_Liex 4:9c0216df5c8e 8 Serial pc(USBTX, USBRX); // tx, rx
Kio_Liex 4:9c0216df5c8e 9 Serial myBT(D1, D0); //Tx, Rx
Kio_Liex 4:9c0216df5c8e 10
Kio_Liex 4:9c0216df5c8e 11 int index = 0;
Kio_Liex 4:9c0216df5c8e 12
Kio_Liex 4:9c0216df5c8e 13 int xVal = 0, yVal = 0, cVal = 0;
Kio_Liex 4:9c0216df5c8e 14
Kio_Liex 4:9c0216df5c8e 15 char btData[20];
Kio_Liex 4:9c0216df5c8e 16
Kio_Liex 4:9c0216df5c8e 17 bool started = false;
Kio_Liex 4:9c0216df5c8e 18 bool ended = false;
screamer 0:005629fe3609 19
screamer 0:005629fe3609 20 int main() {
Kio_Liex 4:9c0216df5c8e 21
Kio_Liex 4:9c0216df5c8e 22 pc.baud(115200);
Kio_Liex 4:9c0216df5c8e 23 myBT.baud(9600);
Kio_Liex 4:9c0216df5c8e 24 pc.printf("\r\n -= Serial Bluetooth test \r\n");
Kio_Liex 4:9c0216df5c8e 25
Kio_Liex 4:9c0216df5c8e 26 char btChar; //Char to hold the bluetooth command lette
Kio_Liex 4:9c0216df5c8e 27
Kio_Liex 4:9c0216df5c8e 28 while(1) {
Kio_Liex 4:9c0216df5c8e 29 while(myBT.readable())
Kio_Liex 4:9c0216df5c8e 30 {
Kio_Liex 4:9c0216df5c8e 31 btChar = myBT.getc();
Kio_Liex 4:9c0216df5c8e 32
Kio_Liex 4:9c0216df5c8e 33 if(btChar == SOP)
Kio_Liex 4:9c0216df5c8e 34 {
Kio_Liex 4:9c0216df5c8e 35 index = 0;
Kio_Liex 4:9c0216df5c8e 36 btData[index] = '\0';
Kio_Liex 4:9c0216df5c8e 37 started = true;
Kio_Liex 4:9c0216df5c8e 38 ended = false;
Kio_Liex 4:9c0216df5c8e 39 }
Kio_Liex 4:9c0216df5c8e 40 else if(btChar == EOP)
Kio_Liex 4:9c0216df5c8e 41 {
Kio_Liex 4:9c0216df5c8e 42 ended = true;
Kio_Liex 4:9c0216df5c8e 43 break;
Kio_Liex 4:9c0216df5c8e 44 }
Kio_Liex 4:9c0216df5c8e 45 else
Kio_Liex 4:9c0216df5c8e 46 {
Kio_Liex 4:9c0216df5c8e 47 if(index < 19)
Kio_Liex 4:9c0216df5c8e 48 {
Kio_Liex 4:9c0216df5c8e 49 btData[index] = btChar;
Kio_Liex 4:9c0216df5c8e 50 index++;
Kio_Liex 4:9c0216df5c8e 51 btData[index] = '\0';
Kio_Liex 4:9c0216df5c8e 52 }
Kio_Liex 4:9c0216df5c8e 53 }
Kio_Liex 4:9c0216df5c8e 54 }
Kio_Liex 4:9c0216df5c8e 55
Kio_Liex 4:9c0216df5c8e 56 if(started && ended)
Kio_Liex 4:9c0216df5c8e 57 {
Kio_Liex 4:9c0216df5c8e 58 char *name = strtok(btData, "=");
Kio_Liex 4:9c0216df5c8e 59
Kio_Liex 4:9c0216df5c8e 60 while(name)
Kio_Liex 4:9c0216df5c8e 61 {
Kio_Liex 4:9c0216df5c8e 62 char *valToken = strtok(NULL, ",");
Kio_Liex 4:9c0216df5c8e 63 if(valToken)
Kio_Liex 4:9c0216df5c8e 64 {
Kio_Liex 4:9c0216df5c8e 65 int val = atoi(valToken);
Kio_Liex 4:9c0216df5c8e 66
Kio_Liex 4:9c0216df5c8e 67 if(strcmp(name, "X") == 0)
Kio_Liex 4:9c0216df5c8e 68 {
Kio_Liex 4:9c0216df5c8e 69 xVal = val;
Kio_Liex 4:9c0216df5c8e 70 }
Kio_Liex 4:9c0216df5c8e 71 else if(strcmp(name, "Y") == 0)
Kio_Liex 4:9c0216df5c8e 72 {
Kio_Liex 4:9c0216df5c8e 73 yVal = val;
Kio_Liex 4:9c0216df5c8e 74 }
Kio_Liex 4:9c0216df5c8e 75 else if(strcmp(name, "C") == 0)
Kio_Liex 4:9c0216df5c8e 76 {
Kio_Liex 4:9c0216df5c8e 77 cVal = val;
Kio_Liex 4:9c0216df5c8e 78 }
Kio_Liex 4:9c0216df5c8e 79 }
Kio_Liex 4:9c0216df5c8e 80 name = strtok(NULL, "=");
Kio_Liex 4:9c0216df5c8e 81 }
Kio_Liex 4:9c0216df5c8e 82
Kio_Liex 4:9c0216df5c8e 83 // Reset for the next packet
Kio_Liex 4:9c0216df5c8e 84 started = false;
Kio_Liex 4:9c0216df5c8e 85 ended = false;
Kio_Liex 4:9c0216df5c8e 86 index = 0;
Kio_Liex 4:9c0216df5c8e 87 btData[index] = '\0';
Kio_Liex 4:9c0216df5c8e 88 pc.printf("X= %d, Y= %d, C= %d \r\n",xVal, yVal, cVal);
Kio_Liex 4:9c0216df5c8e 89 }
Kio_Liex 4:9c0216df5c8e 90
Kio_Liex 4:9c0216df5c8e 91
Kio_Liex 4:9c0216df5c8e 92 // myBT.scanf("%c", &btChar); //Read a letter from the bluetooth stream.
Kio_Liex 4:9c0216df5c8e 93 // pc.printf("btChar: %c ", btChar);
Kio_Liex 4:9c0216df5c8e 94
Kio_Liex 4:9c0216df5c8e 95 myLed = !myLed;
Kio_Liex 4:9c0216df5c8e 96 // wait(0.5);
Kio_Liex 4:9c0216df5c8e 97 // pc.printf("loop %d\r\n", counter);
Kio_Liex 4:9c0216df5c8e 98 // counter++;
screamer 0:005629fe3609 99 }
screamer 0:005629fe3609 100 }