Driver for the JY-MCU v1.06 HC-06 Bluetooth module.
Dependents: DISCO-F746NG_rtos_test MbedTableControl
Diff: HC06Bluetooth.cpp
- Revision:
- 8:14bf9b541f9a
- Parent:
- 6:5ba0038a7a9a
- Child:
- 9:3e23f3f615f2
--- a/HC06Bluetooth.cpp Thu Jun 09 18:35:01 2016 +0000 +++ b/HC06Bluetooth.cpp Thu Jun 16 23:24:08 2016 +0000 @@ -8,16 +8,44 @@ #include <HC06Bluetooth.hpp> #include <algorithm> -HC06Bluetooth::HC06Bluetooth(PinName TX, PinName RX, std::string deviceName, void (*callbackFunc) (const char* readString)) -: btSerialObj(TX, RX), callbackFunc (callbackFunc) { - // The default baud rate is 9600. Overwrite it to 230400. - btSerialObj.puts("AT+BAUD9"); - btSerialObj.baud(230400); + + +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); + + // Set the interrupt to be called when a byte is received. + if ((lineCallbackFunc != NULL) || (charCallbackFunc != NULL)) { + btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR); + } +} + +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); + // Wait for the 8 character reply "OK115200" + for(numCharsReceived = 0 ; numCharsReceived < 8; numCharsReceived++) { + while(!btSerialObj.readable()); + btSerialObj.getc(); + } + // Set the name of the device. - btSerialObj.puts(("AT+NAME" + deviceName).c_str()); - // Set the interrupt to be called when a byte is received. - if (callbackFunc != NULL) { - btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR); + btSerialObj.puts(("AT+NAME" + deviceName.substr(0,20)).c_str()); + // Wait for the 6 character reply "OKname" + for(numCharsReceived = 0 ; numCharsReceived < 6; numCharsReceived++) { + while(!btSerialObj.readable()); + btSerialObj.getc(); + } + + //Set the password of the device. + btSerialObj.puts(("AT+PIN" + PIN.substr(0, 4)).c_str()); + // Wait for the 8 character reply "OKsetpin" + for(numCharsReceived = 0 ; numCharsReceived < 8; numCharsReceived++) { + while(!btSerialObj.readable()); + btSerialObj.getc(); } } @@ -29,12 +57,22 @@ btSerialObj.puts(buffer); } +void HC06Bluetooth::print(char c) { + btSerialObj.putc(c); +} + void HC06Bluetooth::receiveByteISR() { while(btSerialObj.readable()) { // Get the character from the input. char receivedChar = btSerialObj.getc(); - // If the character is a newline, then a full command has been read. - if (receivedChar == '\n') { + + // Call the character callback function if it is not null. + if (charCallbackFunc != NULL) { + charCallbackFunc(receivedChar); + } + + // If the character is a newline or carriage return, then call the line callback function. + if ((receivedChar == '\n') || (receivedChar == '\r')) { // Terminate the buffer with a null character, since that is what strings end with. receivedChar = '\0'; dataReceivedBuffer[dataReceivedBufferPos] = receivedChar; @@ -43,11 +81,13 @@ // Reset the buffer position. dataReceivedBufferPos = 0; // Call the callback function. - callbackFunc((const char*)dataReceivedBuffer); + if (lineCallbackFunc != NULL) { + lineCallbackFunc((const char*)dataReceivedBuffer); + } } // Otherwise, just place it in the buffer and move on. else { - dataReceivedBuffer[dataReceivedBufferPos] = btSerialObj.getc(); + dataReceivedBuffer[dataReceivedBufferPos] = receivedChar; dataReceivedBufferPos++; } }