Jesper Jakobsen / Mbed 2 deprecated nanoNucleo_Serial-Bluetooth_test

Dependencies:   HC05 mbed

main.cpp

Committer:
Kio_Liex
Date:
2016-02-04
Revision:
4:9c0216df5c8e
Parent:
2:b60cb847489c
Child:
5:47575b30c19a

File content as of revision 4:9c0216df5c8e:

#include "mbed.h"

#define SOP 's'
#define EOP 'e'

DigitalOut myLed(LED1);

Serial pc(USBTX, USBRX); // tx, rx
Serial myBT(D1, D0); //Tx, Rx

int index = 0;

int xVal = 0, yVal = 0, cVal = 0;

char btData[20];

bool started = false;
bool ended = false;

int main() {
    
    pc.baud(115200);
    myBT.baud(9600);
    pc.printf("\r\n -= Serial Bluetooth test \r\n");
    
    char btChar; //Char to hold the bluetooth command lette
    
    while(1) { 
        while(myBT.readable())
        {
            btChar = myBT.getc();
            
            if(btChar == SOP)
            {
                index = 0;
                btData[index] = '\0';
                started = true;
                ended = false;
            }
            else if(btChar == EOP)
            {
                ended = true;
                break;
            }
            else
            {
                if(index < 19)
                {
                    btData[index] = btChar;
                    index++;
                    btData[index] = '\0';
                }
            }
        }
        
        if(started && ended)
        {
            char *name = strtok(btData, "=");
                        
            while(name)
            {                  
                char *valToken = strtok(NULL, ",");
                if(valToken)
                {                    
                    int val = atoi(valToken);
                                    
                    if(strcmp(name, "X") == 0)
                    {
                        xVal = val;            
                    }
                    else if(strcmp(name, "Y") == 0) 
                    {
                        yVal = val;
                    }
                    else if(strcmp(name, "C") == 0) 
                    {
                        cVal = val;
                    }
                }
                name = strtok(NULL, "=");
            }
                
            // Reset for the next packet
            started = false;
            ended = false;
            index = 0;
            btData[index] = '\0';
            pc.printf("X= %d, Y= %d, C= %d \r\n",xVal, yVal, cVal);
        }
        
        
//        myBT.scanf("%c", &btChar); //Read a letter from the bluetooth stream.
//        pc.printf("btChar: %c ", btChar);
        
        myLed = !myLed;
//        wait(0.5);
//        pc.printf("loop %d\r\n", counter);
//        counter++;
    }
}