Driver for the JY-MCU v1.06 HC-06 Bluetooth module.
Dependents: DISCO-F746NG_rtos_test MbedTableControl
HC06Bluetooth.cpp@10:b0a0a82a9ff5, 2016-08-02 (annotated)
- Committer:
- Electrotiger
- Date:
- Tue Aug 02 01:32:25 2016 +0000
- Revision:
- 10:b0a0a82a9ff5
- Parent:
- 9:3e23f3f615f2
- Child:
- 11:aeb8c5c27111
Removed references to FRDM-K22F Pins in header file. ; Deleted outdated example code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Electrotiger | 0:3ab5e47dde1e | 1 | /* |
Electrotiger | 0:3ab5e47dde1e | 2 | * HC06Bluetooth.cpp |
Electrotiger | 0:3ab5e47dde1e | 3 | * |
Electrotiger | 0:3ab5e47dde1e | 4 | * Created on: Jun 4, 2016 |
Electrotiger | 0:3ab5e47dde1e | 5 | * Author: Developer |
Electrotiger | 0:3ab5e47dde1e | 6 | */ |
Electrotiger | 0:3ab5e47dde1e | 7 | |
Electrotiger | 10:b0a0a82a9ff5 | 8 | #include <HC06Bluetooth.h> |
Electrotiger | 1:026034717620 | 9 | #include <algorithm> |
Electrotiger | 0:3ab5e47dde1e | 10 | |
Electrotiger | 9:3e23f3f615f2 | 11 | /* Static methods used to help configure the Baudrate. */ |
Electrotiger | 8:14bf9b541f9a | 12 | |
Electrotiger | 9:3e23f3f615f2 | 13 | // WARNING: DO NOT CHANGE THESE VALUES, AS THEY ARE USED TO INDEX INTO AN ARRAY FOR IMPLEMENTATION. |
Electrotiger | 9:3e23f3f615f2 | 14 | enum Baudrate {B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600, B1382400, END}; |
Electrotiger | 9:3e23f3f615f2 | 15 | 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"}; |
Electrotiger | 9:3e23f3f615f2 | 16 | const int32_t BaudATReplyLength[] = {6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9}; |
Electrotiger | 9:3e23f3f615f2 | 17 | //const char* BaudATReplyPattern[] = {"OK1200", "OK2400", "OK4800","OK9600","OK19200","OK38400","OK57600","OK115200","OK230400","OK460800","OK921600","OK1382400"}; |
Electrotiger | 9:3e23f3f615f2 | 18 | const int32_t BaudValue[] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1382400}; |
Electrotiger | 8:14bf9b541f9a | 19 | |
Electrotiger | 9:3e23f3f615f2 | 20 | /* HC06 Bluetooth Class Implementation: */ |
Electrotiger | 8:14bf9b541f9a | 21 | HC06Bluetooth::HC06Bluetooth(PinName TX, PinName RX, void (*lineCallbackFunc) (const char* readString), void (*charCallbackFunc) (char readChar)) |
Electrotiger | 8:14bf9b541f9a | 22 | : btSerialObj(TX, RX), lineCallbackFunc(lineCallbackFunc), charCallbackFunc(charCallbackFunc) { |
Electrotiger | 8:14bf9b541f9a | 23 | btSerialObj.baud(115200); |
Electrotiger | 8:14bf9b541f9a | 24 | |
Electrotiger | 8:14bf9b541f9a | 25 | // Set the interrupt to be called when a byte is received. |
Electrotiger | 8:14bf9b541f9a | 26 | if ((lineCallbackFunc != NULL) || (charCallbackFunc != NULL)) { |
Electrotiger | 8:14bf9b541f9a | 27 | btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR); |
Electrotiger | 8:14bf9b541f9a | 28 | } |
Electrotiger | 8:14bf9b541f9a | 29 | } |
Electrotiger | 8:14bf9b541f9a | 30 | |
Electrotiger | 8:14bf9b541f9a | 31 | void HC06Bluetooth::runSetup(std::string deviceName, std::string PIN) { |
Electrotiger | 8:14bf9b541f9a | 32 | int numCharsReceived = 0; |
Electrotiger | 9:3e23f3f615f2 | 33 | // Detatch the interrupt. |
Electrotiger | 9:3e23f3f615f2 | 34 | btSerialObj.attach(NULL); |
Electrotiger | 9:3e23f3f615f2 | 35 | /* Sweep through a list of Baud rates until we find the one that the device has previously been set to. */ |
Electrotiger | 9:3e23f3f615f2 | 36 | bool baudFound = false; |
Electrotiger | 9:3e23f3f615f2 | 37 | Timer timeOut; |
Electrotiger | 9:3e23f3f615f2 | 38 | timeOut.start(); |
Electrotiger | 9:3e23f3f615f2 | 39 | // For every baud rate in the list: |
Electrotiger | 9:3e23f3f615f2 | 40 | for (int i = 0; (i < END) && (!baudFound); i++) { |
Electrotiger | 9:3e23f3f615f2 | 41 | // Set the communication baud rate to it. |
Electrotiger | 9:3e23f3f615f2 | 42 | btSerialObj.baud(BaudValue[i]); |
Electrotiger | 9:3e23f3f615f2 | 43 | // Send the test command "AT" to the device. |
Electrotiger | 9:3e23f3f615f2 | 44 | btSerialObj.puts("AT"); |
Electrotiger | 9:3e23f3f615f2 | 45 | // While the time out has not been reached: |
Electrotiger | 9:3e23f3f615f2 | 46 | for(timeOut.reset(); timeOut.read_ms() < 1000; ) { |
Electrotiger | 9:3e23f3f615f2 | 47 | // If the serial object is readable, make sure the read character matches the reply string "OK". |
Electrotiger | 9:3e23f3f615f2 | 48 | if (btSerialObj.readable() && !baudFound) { |
Electrotiger | 9:3e23f3f615f2 | 49 | baudFound = true; |
Electrotiger | 9:3e23f3f615f2 | 50 | break; |
Electrotiger | 9:3e23f3f615f2 | 51 | } |
Electrotiger | 9:3e23f3f615f2 | 52 | } |
Electrotiger | 9:3e23f3f615f2 | 53 | } |
Electrotiger | 9:3e23f3f615f2 | 54 | // Flush whatever's in the input buffer. |
Electrotiger | 9:3e23f3f615f2 | 55 | while(btSerialObj.readable()) { |
Electrotiger | 9:3e23f3f615f2 | 56 | btSerialObj.getc(); |
Electrotiger | 9:3e23f3f615f2 | 57 | } |
Electrotiger | 9:3e23f3f615f2 | 58 | //Overwrite the Baud rate to 115200. |
Electrotiger | 9:3e23f3f615f2 | 59 | btSerialObj.puts(BaudATString[B115200]); |
Electrotiger | 9:3e23f3f615f2 | 60 | btSerialObj.baud(BaudValue[B115200]); |
Electrotiger | 8:14bf9b541f9a | 61 | // Wait for the 8 character reply "OK115200" |
Electrotiger | 9:3e23f3f615f2 | 62 | for(numCharsReceived = 0 ; numCharsReceived < BaudATReplyLength[B115200]; numCharsReceived++) { |
Electrotiger | 9:3e23f3f615f2 | 63 | //while(!btSerialObj.readable()); |
Electrotiger | 8:14bf9b541f9a | 64 | btSerialObj.getc(); |
Electrotiger | 8:14bf9b541f9a | 65 | } |
Electrotiger | 8:14bf9b541f9a | 66 | |
Electrotiger | 0:3ab5e47dde1e | 67 | // Set the name of the device. |
Electrotiger | 8:14bf9b541f9a | 68 | btSerialObj.puts(("AT+NAME" + deviceName.substr(0,20)).c_str()); |
Electrotiger | 8:14bf9b541f9a | 69 | // Wait for the 6 character reply "OKname" |
Electrotiger | 8:14bf9b541f9a | 70 | for(numCharsReceived = 0 ; numCharsReceived < 6; numCharsReceived++) { |
Electrotiger | 8:14bf9b541f9a | 71 | while(!btSerialObj.readable()); |
Electrotiger | 8:14bf9b541f9a | 72 | btSerialObj.getc(); |
Electrotiger | 8:14bf9b541f9a | 73 | } |
Electrotiger | 8:14bf9b541f9a | 74 | |
Electrotiger | 8:14bf9b541f9a | 75 | //Set the password of the device. |
Electrotiger | 8:14bf9b541f9a | 76 | btSerialObj.puts(("AT+PIN" + PIN.substr(0, 4)).c_str()); |
Electrotiger | 8:14bf9b541f9a | 77 | // Wait for the 8 character reply "OKsetpin" |
Electrotiger | 8:14bf9b541f9a | 78 | for(numCharsReceived = 0 ; numCharsReceived < 8; numCharsReceived++) { |
Electrotiger | 8:14bf9b541f9a | 79 | while(!btSerialObj.readable()); |
Electrotiger | 8:14bf9b541f9a | 80 | btSerialObj.getc(); |
Electrotiger | 3:ee17212e838e | 81 | } |
Electrotiger | 9:3e23f3f615f2 | 82 | // Reattach the interrupt. |
Electrotiger | 9:3e23f3f615f2 | 83 | btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR); |
Electrotiger | 0:3ab5e47dde1e | 84 | } |
Electrotiger | 0:3ab5e47dde1e | 85 | |
Electrotiger | 0:3ab5e47dde1e | 86 | HC06Bluetooth::~HC06Bluetooth() { |
Electrotiger | 0:3ab5e47dde1e | 87 | // TODO Auto-generated destructor stub |
Electrotiger | 0:3ab5e47dde1e | 88 | } |
Electrotiger | 0:3ab5e47dde1e | 89 | |
Electrotiger | 6:5ba0038a7a9a | 90 | void HC06Bluetooth::print(const char* buffer) { |
Electrotiger | 6:5ba0038a7a9a | 91 | btSerialObj.puts(buffer); |
Electrotiger | 6:5ba0038a7a9a | 92 | } |
Electrotiger | 6:5ba0038a7a9a | 93 | |
Electrotiger | 10:b0a0a82a9ff5 | 94 | void HC06Bluetooth::println(const char* buffer) { |
Electrotiger | 10:b0a0a82a9ff5 | 95 | btSerialObj.puts(buffer); |
Electrotiger | 10:b0a0a82a9ff5 | 96 | btSerialObj.putc('\n'); |
Electrotiger | 10:b0a0a82a9ff5 | 97 | } |
Electrotiger | 10:b0a0a82a9ff5 | 98 | |
Electrotiger | 8:14bf9b541f9a | 99 | void HC06Bluetooth::print(char c) { |
Electrotiger | 8:14bf9b541f9a | 100 | btSerialObj.putc(c); |
Electrotiger | 8:14bf9b541f9a | 101 | } |
Electrotiger | 8:14bf9b541f9a | 102 | |
Electrotiger | 0:3ab5e47dde1e | 103 | void HC06Bluetooth::receiveByteISR() { |
Electrotiger | 6:5ba0038a7a9a | 104 | while(btSerialObj.readable()) { |
Electrotiger | 1:026034717620 | 105 | // Get the character from the input. |
Electrotiger | 6:5ba0038a7a9a | 106 | char receivedChar = btSerialObj.getc(); |
Electrotiger | 8:14bf9b541f9a | 107 | |
Electrotiger | 8:14bf9b541f9a | 108 | // Call the character callback function if it is not null. |
Electrotiger | 8:14bf9b541f9a | 109 | if (charCallbackFunc != NULL) { |
Electrotiger | 8:14bf9b541f9a | 110 | charCallbackFunc(receivedChar); |
Electrotiger | 8:14bf9b541f9a | 111 | } |
Electrotiger | 8:14bf9b541f9a | 112 | |
Electrotiger | 10:b0a0a82a9ff5 | 113 | if (lineCallbackFunc != NULL) { |
Electrotiger | 10:b0a0a82a9ff5 | 114 | // If the character is a newline or carriage return, then call the line callback function. |
Electrotiger | 10:b0a0a82a9ff5 | 115 | if ((receivedChar == '\n') || (receivedChar == '\r')) { |
Electrotiger | 10:b0a0a82a9ff5 | 116 | // Terminate the buffer with a null character, since that is what strings end with. |
Electrotiger | 10:b0a0a82a9ff5 | 117 | receivedChar = '\0'; |
Electrotiger | 10:b0a0a82a9ff5 | 118 | dataReceivedBuffer[dataReceivedBufferPos] = receivedChar; |
Electrotiger | 10:b0a0a82a9ff5 | 119 | // Copy data from the buffer to a copy. |
Electrotiger | 10:b0a0a82a9ff5 | 120 | std::copy(dataReceivedBuffer, dataReceivedBuffer + dataReceivedBufferPos, dataReceivedBufferCopy); |
Electrotiger | 10:b0a0a82a9ff5 | 121 | // Reset the buffer position. |
Electrotiger | 10:b0a0a82a9ff5 | 122 | dataReceivedBufferPos = 0; |
Electrotiger | 10:b0a0a82a9ff5 | 123 | // Call the callback function. |
Electrotiger | 10:b0a0a82a9ff5 | 124 | if (lineCallbackFunc != NULL) { |
Electrotiger | 10:b0a0a82a9ff5 | 125 | lineCallbackFunc((const char*)dataReceivedBuffer); |
Electrotiger | 10:b0a0a82a9ff5 | 126 | } |
Electrotiger | 8:14bf9b541f9a | 127 | } |
Electrotiger | 10:b0a0a82a9ff5 | 128 | // Otherwise, just place it in the buffer and move on. |
Electrotiger | 10:b0a0a82a9ff5 | 129 | else { |
Electrotiger | 10:b0a0a82a9ff5 | 130 | if (dataReceivedBufferPos < dataBufferSize) { |
Electrotiger | 10:b0a0a82a9ff5 | 131 | dataReceivedBuffer[dataReceivedBufferPos] = receivedChar; |
Electrotiger | 10:b0a0a82a9ff5 | 132 | dataReceivedBufferPos++; |
Electrotiger | 10:b0a0a82a9ff5 | 133 | } |
Electrotiger | 10:b0a0a82a9ff5 | 134 | } |
Electrotiger | 1:026034717620 | 135 | } |
Electrotiger | 1:026034717620 | 136 | } |
Electrotiger | 2:7e0453895727 | 137 | } |
Electrotiger | 0:3ab5e47dde1e | 138 |