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