Weimen Li / HC06Bluetooth

Dependents:   DISCO-F746NG_rtos_test MbedTableControl

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HC06Bluetooth.cpp Source File

HC06Bluetooth.cpp

00001 /*
00002  * HC06Bluetooth.cpp
00003  *
00004  *  Created on: Jun 4, 2016
00005  *      Author: Developer
00006  */
00007 
00008 #include <HC06Bluetooth.h>
00009 #include "rtos.h"
00010 
00011 /* Static methods used to help configure the Baudrate. */
00012 
00013 // WARNING: DO NOT CHANGE THESE VALUES, AS THEY ARE USED TO INDEX INTO AN ARRAY FOR IMPLEMENTATION.
00014 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"};
00015 const int32_t BaudATReplyLength[] = {6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 8, 9};
00016 //const char* BaudATReplyPattern[] = {"OK1200", "OK2400", "OK4800","OK9600","OK19200","OK38400","OK57600","OK115200","OK230400","OK460800","OK921600","OK1382400"};
00017 const int32_t BaudValue[] = {1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 1382400};
00018 
00019 /* HC06 Bluetooth Class Implementation: */
00020 HC06Bluetooth::HC06Bluetooth(PinName TX, PinName RX, Baudrate baudrate, void (*lineCallbackFunc) (const char* readString, size_t strlen), void (*charCallbackFunc) (char readChar))
00021     : btSerialObj(TX, RX), baudrate(baudrate), receiveByteThreadObj(osPriorityRealtime), lineCallbackFunc(lineCallbackFunc), charCallbackFunc(charCallbackFunc)
00022 {
00023     btSerialObj.baud(BaudValue[baudrate]);
00024 
00025     // Set the interrupt to be called when a byte is received.
00026     if ((lineCallbackFunc != NULL) || (charCallbackFunc != NULL))
00027     {
00028         btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR);
00029         receiveByteThreadObj.start(this, &HC06Bluetooth::receiveByteThread);
00030     }
00031 }
00032 
00033 void HC06Bluetooth::runSetup(std::string deviceName, std::string PIN)
00034 {
00035     int numCharsReceived = 0;
00036     // Detatch the interrupt.
00037     btSerialObj.attach(NULL);
00038     /* Sweep through a list of Baud rates until we find the one that the device has previously been set to. */
00039     bool baudFound = false;
00040     Timer timeOut;
00041     timeOut.start();
00042     // For every baud rate in the list:
00043     for (volatile int i = 0; (i < END) && (!baudFound); i++)
00044     {
00045         // Set the communication baud rate to it.
00046         btSerialObj.baud(BaudValue[i]);
00047         // Send the test command "AT" to the device.
00048         btSerialObj.puts("AT");
00049         // While the time out has not been reached:
00050         for(timeOut.reset(); timeOut.read_ms() < 1000; )
00051         {
00052             // If the serial object is readable, make sure the read character matches the reply string "OK".
00053             if (btSerialObj.readable() && !baudFound)
00054             {
00055                 baudFound = true;
00056                 break;
00057             }
00058         }
00059     }
00060     // Flush whatever's in the input buffer.
00061     while(btSerialObj.readable())
00062     {
00063         btSerialObj.getc();
00064     }
00065     //Overwrite the Baud rate to 115200.
00066     btSerialObj.puts(BaudATString[baudrate]);
00067     btSerialObj.baud(BaudValue[baudrate]);
00068     // Wait for the 8 character reply "OK115200"
00069     for(numCharsReceived = 0 ; numCharsReceived < BaudATReplyLength[baudrate]; numCharsReceived++)
00070     {
00071         //while(!btSerialObj.readable());
00072         //btSerialObj.getc();
00073     }
00074     wait_ms(1000);
00075 
00076     // Set the name of the device.
00077     btSerialObj.puts(("AT+NAME" + deviceName.substr(0,20)).c_str());
00078     // Wait for the 6 character reply "OKname"
00079     for(numCharsReceived = 0 ; numCharsReceived < 6; numCharsReceived++)
00080     {
00081         //while(!btSerialObj.readable());
00082         //btSerialObj.getc();
00083     }
00084     wait_ms(1000);
00085 
00086     //Set the password of the device.
00087     btSerialObj.puts(("AT+PIN" + PIN.substr(0, 4)).c_str());
00088     // Wait for the 8 character reply "OKsetpin"
00089     for(numCharsReceived = 0 ; numCharsReceived < 8; numCharsReceived++)
00090     {
00091         //while(!btSerialObj.readable());
00092         //btSerialObj.getc();
00093     }
00094     wait_ms(1000);
00095     // Reattach the interrupt.
00096     btSerialObj.attach(this, &HC06Bluetooth::receiveByteISR);
00097 }
00098 
00099 HC06Bluetooth::~HC06Bluetooth()
00100 {
00101     // TODO Auto-generated destructor stub
00102 }
00103 
00104 void HC06Bluetooth::print(const char* buffer)
00105 {
00106     // TODO: Code hangs if buffer is too long! Not sure why.
00107     btSerialObj.puts(buffer);
00108 }
00109 
00110 void HC06Bluetooth::println(const char* buffer)
00111 {
00112     btSerialObj.puts(buffer);
00113     btSerialObj.putc('\n');
00114 }
00115 
00116 void HC06Bluetooth::print(char c)
00117 {
00118     btSerialObj.putc(c);
00119 }
00120 
00121 void HC06Bluetooth::receiveByteISR()
00122 {
00123     // Get all available characters in the input and place them in the buffer.
00124 
00125     while(btSerialObj.readable())
00126     {
00127         dataReceivedBuffer.put((char*) btSerialObj.getc());
00128     }
00129 }
00130 
00131 void HC06Bluetooth::receiveByteThread()
00132 {
00133     // Now that all characters have been read, process them.
00134     char receivedChar;
00135     while(true)
00136     {
00137       receivedChar = (uint32_t) dataReceivedBuffer.get().value.p;
00138         // Call the character callback function if it is not null.
00139         if (charCallbackFunc != NULL) charCallbackFunc(receivedChar);
00140 
00141         if (lineCallbackFunc != NULL)
00142         {
00143             // If the character is a newline or carriage return, then call the line callback function.
00144             if ((receivedChar == '\n') || (receivedChar == '\r'))
00145             {
00146                 // Clear whatever was in the toClient buffer before.
00147                 dataReceivedToClient.clear();
00148                 // Copy everything from the queue to the client buffer.
00149                 while(!dataReceivedBufferCopy.empty())
00150                 {
00151                     dataReceivedToClient.push_back(dataReceivedBufferCopy.front());
00152                     dataReceivedBufferCopy.pop();
00153                 }
00154                 // Null-terminate the string.
00155                 dataReceivedToClient.push_back('\0');
00156 
00157                 // Call the callback function with the toClient buffer.
00158                 lineCallbackFunc(&dataReceivedToClient[0], dataReceivedToClient.size());
00159             }
00160 
00161             // Otherwise, enqueue it in the copy.
00162             else
00163             {
00164                 dataReceivedBufferCopy.push(receivedChar);
00165             }
00166         }
00167     }
00168 }
00169