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