BlueTooth HC-05 module running on a ST-Nucleo-F303K8
Dependents: Nucleo_interf_bluetooth
Work version with the "BT Bot Control" app for Android. Running on a NuCleo-F303K8 and a HC-05 bluetooth module.
Revision 0:e0c6e95761d2, committed 2016-02-04
- Comitter:
- Kio_Liex
- Date:
- Thu Feb 04 20:18:06 2016 +0000
- Commit message:
- BlueTooth HC-05 module running on a ST-Nucleo-F303K8
Changed in this revision
HC05.cpp | Show annotated file Show diff for this revision Revisions of this file |
HC05.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HC05.cpp Thu Feb 04 20:18:06 2016 +0000 @@ -0,0 +1,82 @@ +#include "mbed.h" +#include "HC05.h" + +HC05::HC05(PinName tx, PinName rx): myBT(tx, rx) +{ +myBT.baud(9600); + +index = 0; + +started = false; +ended = false; +} + +void HC05::getValue(int16_t* x, int16_t* y, int16_t* c) +{ + if(!myBT.readable()) + { + *x = 0; + *y = 0; + *c = 0; + } + 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) + { + *x = val; + } + else if(strcmp(name, "Y") == 0) + { + *y = val; + } + else if(strcmp(name, "C") == 0) + { + *c = val; + } + } + name = strtok(NULL, "="); + } + + // Reset for the next packet + started = false; + ended = false; + index = 0; + btData[index] = '\0'; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HC05.h Thu Feb 04 20:18:06 2016 +0000 @@ -0,0 +1,26 @@ +#ifndef _HC05_H_ +#define _HC05_H_ + +#include "mbed.h" + +#define SOP 's' +#define EOP 'e' + +class HC05 +{ + Serial myBT; + + int index; + + char btData[20]; + char btChar; + + bool started; + bool ended; + +public: + HC05(PinName tx, PinName rx); + void getValue(int16_t* xVal, int16_t* yVal, int16_t* cVal); +}; + +#endif /* _HC05_H_ */ \ No newline at end of file