Driver for the JY-MCU v1.06 HC-06 Bluetooth module.
Dependents: DISCO-F746NG_rtos_test MbedTableControl
Diff: HC06Bluetooth.cpp
- Revision:
- 9:3e23f3f615f2
- Parent:
- 8:14bf9b541f9a
- Child:
- 10:b0a0a82a9ff5
--- a/HC06Bluetooth.cpp Thu Jun 16 23:24:08 2016 +0000 +++ b/HC06Bluetooth.cpp Sun Jun 19 00:30:18 2016 +0000 @@ -8,8 +8,16 @@ #include <HC06Bluetooth.hpp> #include <algorithm> +/* Static methods used to help configure the Baudrate. */ +// WARNING: DO NOT CHANGE THESE VALUES, AS THEY ARE USED TO INDEX INTO AN ARRAY FOR IMPLEMENTATION. +enum Baudrate {B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600, B1382400, END}; +const char* BaudATString[] = {"AT+BAUD1", "AT+BAUD2", "AT+BAUD3", "AT+BAUD4", "AT+BAUD5", "AT+BAUD6", "AT+BAUD7", "AT+BAUD8", "AT+BAUD9", "AT+BAUDA", "AT+BAUDB", "AT+BAUDC"}; +const int32_t BaudATReplyLength[] = {6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9}; +//const char* BaudATReplyPattern[] = {"OK1200", "OK2400", "OK4800","OK9600","OK19200","OK38400","OK57600","OK115200","OK230400","OK460800","OK921600","OK1382400"}; +const int32_t BaudValue[] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1382400}; +/* HC06 Bluetooth Class Implementation: */ HC06Bluetooth::HC06Bluetooth(PinName TX, PinName RX, void (*lineCallbackFunc) (const char* readString), void (*charCallbackFunc) (char readChar)) : btSerialObj(TX, RX), lineCallbackFunc(lineCallbackFunc), charCallbackFunc(charCallbackFunc) { btSerialObj.baud(115200); @@ -21,14 +29,38 @@ } void HC06Bluetooth::runSetup(std::string deviceName, std::string PIN) { - // TODO: Sweep through a list of Baud rates until we find one that works! int numCharsReceived = 0; - // The default baud rate is 9600. Overwrite it to 115200. - btSerialObj.puts("AT+BAUD8"); - btSerialObj.baud(115200); + // Detatch the interrupt. + btSerialObj.attach(NULL); + /* Sweep through a list of Baud rates until we find the one that the device has previously been set to. */ + bool baudFound = false; + Timer timeOut; + timeOut.start(); + // For every baud rate in the list: + for (int i = 0; (i < END) && (!baudFound); i++) { + // Set the communication baud rate to it. + btSerialObj.baud(BaudValue[i]); + // Send the test command "AT" to the device. + btSerialObj.puts("AT"); + // While the time out has not been reached: + for(timeOut.reset(); timeOut.read_ms() < 1000; ) { + // If the serial object is readable, make sure the read character matches the reply string "OK". + if (btSerialObj.readable() && !baudFound) { + baudFound = true; + break; + } + } + } + // Flush whatever's in the input buffer. + while(btSerialObj.readable()) { + btSerialObj.getc(); + } + //Overwrite the Baud rate to 115200. + btSerialObj.puts(BaudATString[B115200]); + btSerialObj.baud(BaudValue[B115200]); // Wait for the 8 character reply "OK115200" - for(numCharsReceived = 0 ; numCharsReceived < 8; numCharsReceived++) { - while(!btSerialObj.readable()); + for(numCharsReceived = 0 ; numCharsReceived < BaudATReplyLength[B115200]; numCharsReceived++) { + //while(!btSerialObj.readable()); btSerialObj.getc(); } @@ -47,6 +79,8 @@ while(!btSerialObj.readable()); btSerialObj.getc(); } + // Reattach the interrupt. + btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR); } HC06Bluetooth::~HC06Bluetooth() {