Weimen Li / HC06Bluetooth

Dependents:   DISCO-F746NG_rtos_test MbedTableControl

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HC06Bluetooth.h Source File

HC06Bluetooth.h

00001 /**
00002  * @file HC06Bluetooth.hpp
00003  * @date June 4th, 2016
00004  * @author Weimen Li
00005  * @class HC06Bluetooth
00006  * @brief This class creates an object representing the HC06 Bluetooth Module
00007  * The baud rate for the device is configured to be 115200.
00008  */
00009 
00010 
00011 #ifndef HC06BLUETOOTH_H_
00012 #define HC06BLUETOOTH_H_
00013 #include "mbed.h"
00014 #include "rtos.h"
00015 #include <string>
00016 #include <queue>
00017 
00018 const uint32_t SIG_BT_BYTE = (1 << 2);
00019 // WARNING: DO NOT CHANGE THESE VALUES, AS THEY ARE USED TO INDEX INTO AN ARRAY FOR IMPLEMENTATION.
00020 enum Baudrate {B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600, B1382400, END};
00021 
00022 class HC06Bluetooth {
00023 public: // Public methods.
00024     /**
00025      * @brief Constructor for the HC06_Bluetooth class.
00026      * @param TX The pin that the TX line is attached to.
00027      * @param RX The pin that the RX line is attached to.
00028      * @param deviceName The name that you want your system to be identified as when you connect to it i.e. "Weimen's MAE433Robot"
00029      * @param password A 4-digit numeric PIN that you want your device to connect with. It defaults to "1234".
00030      * @param lineCallbackFunc The callback function that will be called once a newline character is encountered on the receiving data.
00031      * The callback function takes as an argument a string containing the line that has been read.
00032      * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs.
00033      * @param charCallbackFunc A function that will be called once a new character has been read. It should return void and take as an argument
00034      * the character that has been read.
00035      * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs.
00036      */
00037     HC06Bluetooth(PinName TX, PinName RX, Baudrate baudrate = B115200, void (*lineCallbackFunc) (const char* readString, size_t strlen) = NULL, void (*charCallbackFunc) (char readChar) = NULL);
00038     virtual ~HC06Bluetooth();
00039     /**
00040      * @brief Run the setup routine to configure the device name and the device pin.
00041      * @remark: You only need to run this once during the entire time you have the module, since the device name and PIN are saved
00042      * by the module itself! However, you may run the function again if you would like to change the deviceName or PIN.
00043      * @param deviceName The name of the device, as a string. It must only contain alphanumeric characters - no spaces allowed.
00044      * @param PIN The device PIN.
00045      */
00046     void runSetup(std::string deviceName, std::string PIN = "1234");
00047     /**
00048      * @brief Print information in buffer to the output.
00049      * @param buffer A null-terminated buffer containing the data you want to send.
00050      */
00051     void print(const char *buffer);
00052     void print(std::string s) {
00053        print(s.c_str());
00054     }
00055     /**
00056      * @brief Print information in buffer to the output followed by a newline
00057      * @param buffer A null-terminated buffer containing the data you want to send.
00058      */
00059     void println(const char *buffer);
00060     void println(std::string s) {
00061        println(s.c_str());
00062     }
00063     /**
00064      * @brief Print a character to output.
00065      * @param char The character you want to print to output.
00066      */
00067     void print(char c);
00068 
00069 private:
00070     RawSerial btSerialObj;
00071     Baudrate baudrate;
00072     void receiveByteISR();
00073     void receiveByteThread();
00074     Thread receiveByteThreadObj;
00075     /// Pointer to a callback function the client provides when a line is received.
00076     void (*lineCallbackFunc) (const char*, size_t strlen);
00077     /// Pointer to a callback function the client provides when a character is received.
00078     void (*charCallbackFunc) (char);
00079     Queue<char, 64> dataReceivedBuffer;
00080     std::queue<char> dataReceivedBufferCopy;
00081     std::vector<char> dataReceivedToClient;
00082 };
00083 
00084 #endif /* HC06BLUETOOTH_H_ */