Driver for the JY-MCU v1.06 HC-06 Bluetooth module.
Dependents: DISCO-F746NG_rtos_test MbedTableControl
HC06Bluetooth.h@10:b0a0a82a9ff5, 2016-08-02 (annotated)
- Committer:
- Electrotiger
- Date:
- Tue Aug 02 01:32:25 2016 +0000
- Revision:
- 10:b0a0a82a9ff5
- 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 | 10:b0a0a82a9ff5 | 1 | /** |
Electrotiger | 10:b0a0a82a9ff5 | 2 | * @file HC06Bluetooth.hpp |
Electrotiger | 10:b0a0a82a9ff5 | 3 | * @date June 4th, 2016 |
Electrotiger | 10:b0a0a82a9ff5 | 4 | * @author Weimen Li |
Electrotiger | 10:b0a0a82a9ff5 | 5 | * @class HC06Bluetooth |
Electrotiger | 10:b0a0a82a9ff5 | 6 | * @brief This class creates an object representing the HC06 Bluetooth Module |
Electrotiger | 10:b0a0a82a9ff5 | 7 | * The baud rate for the device is configured to be 115200. |
Electrotiger | 10:b0a0a82a9ff5 | 8 | */ |
Electrotiger | 10:b0a0a82a9ff5 | 9 | |
Electrotiger | 10:b0a0a82a9ff5 | 10 | |
Electrotiger | 10:b0a0a82a9ff5 | 11 | #ifndef HC06BLUETOOTH_H_ |
Electrotiger | 10:b0a0a82a9ff5 | 12 | #define HC06BLUETOOTH_H_ |
Electrotiger | 10:b0a0a82a9ff5 | 13 | #include "mbed.h" |
Electrotiger | 10:b0a0a82a9ff5 | 14 | #include <string> |
Electrotiger | 10:b0a0a82a9ff5 | 15 | |
Electrotiger | 10:b0a0a82a9ff5 | 16 | const int dataBufferSize = 256; |
Electrotiger | 10:b0a0a82a9ff5 | 17 | |
Electrotiger | 10:b0a0a82a9ff5 | 18 | class HC06Bluetooth { |
Electrotiger | 10:b0a0a82a9ff5 | 19 | public: // Public methods. |
Electrotiger | 10:b0a0a82a9ff5 | 20 | /** |
Electrotiger | 10:b0a0a82a9ff5 | 21 | * @brief Constructor for the HC06_Bluetooth class. |
Electrotiger | 10:b0a0a82a9ff5 | 22 | * @param TX The pin that the TX line is attached to. |
Electrotiger | 10:b0a0a82a9ff5 | 23 | * @param RX The pin that the RX line is attached to. |
Electrotiger | 10:b0a0a82a9ff5 | 24 | * @param deviceName The name that you want your system to be identified as when you connect to it i.e. "Weimen's MAE433Robot" |
Electrotiger | 10:b0a0a82a9ff5 | 25 | * @param password A 4-digit numeric PIN that you want your device to connect with. It defaults to "1234". |
Electrotiger | 10:b0a0a82a9ff5 | 26 | * @param lineCallbackFunc The callback function that will be called once a newline character is encountered on the receiving data. |
Electrotiger | 10:b0a0a82a9ff5 | 27 | * The callback function takes as an argument a string containing the line that has been read. |
Electrotiger | 10:b0a0a82a9ff5 | 28 | * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs. |
Electrotiger | 10:b0a0a82a9ff5 | 29 | * @param charCallbackFunc A function that will be called once a new character has been read. It should return void and take as an argument |
Electrotiger | 10:b0a0a82a9ff5 | 30 | * the character that has been read. |
Electrotiger | 10:b0a0a82a9ff5 | 31 | * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs. |
Electrotiger | 10:b0a0a82a9ff5 | 32 | */ |
Electrotiger | 10:b0a0a82a9ff5 | 33 | HC06Bluetooth(PinName TX, PinName RX, void (*lineCallbackFunc) (const char* readString) = NULL, void (*charCallbackFunc) (char readChar) = NULL); |
Electrotiger | 10:b0a0a82a9ff5 | 34 | virtual ~HC06Bluetooth(); |
Electrotiger | 10:b0a0a82a9ff5 | 35 | /** |
Electrotiger | 10:b0a0a82a9ff5 | 36 | * @brief Run the setup routine to configure the device name and the device pin. |
Electrotiger | 10:b0a0a82a9ff5 | 37 | * @remark: You only need to run this once during the entire time you have the module, since the device name and PIN are saved |
Electrotiger | 10:b0a0a82a9ff5 | 38 | * by the module itself! However, you may run the function again if you would like to change the deviceName or PIN. |
Electrotiger | 10:b0a0a82a9ff5 | 39 | * @param deviceName The name of the device, as a string. It must only contain alphanumeric characters - no spaces allowed. |
Electrotiger | 10:b0a0a82a9ff5 | 40 | * @param PIN The device PIN. |
Electrotiger | 10:b0a0a82a9ff5 | 41 | */ |
Electrotiger | 10:b0a0a82a9ff5 | 42 | void runSetup(std::string deviceName, std::string PIN = "1234"); |
Electrotiger | 10:b0a0a82a9ff5 | 43 | /** |
Electrotiger | 10:b0a0a82a9ff5 | 44 | * @brief Print information in buffer to the output. |
Electrotiger | 10:b0a0a82a9ff5 | 45 | * @param buffer A null-terminated buffer containing the data you want to send. |
Electrotiger | 10:b0a0a82a9ff5 | 46 | */ |
Electrotiger | 10:b0a0a82a9ff5 | 47 | void print(const char *buffer); |
Electrotiger | 10:b0a0a82a9ff5 | 48 | /** |
Electrotiger | 10:b0a0a82a9ff5 | 49 | * @brief Print information in buffer to the output followed by a newline |
Electrotiger | 10:b0a0a82a9ff5 | 50 | * @param buffer A null-terminated buffer containing the data you want to send. |
Electrotiger | 10:b0a0a82a9ff5 | 51 | */ |
Electrotiger | 10:b0a0a82a9ff5 | 52 | void println(const char *buffer); |
Electrotiger | 10:b0a0a82a9ff5 | 53 | /** |
Electrotiger | 10:b0a0a82a9ff5 | 54 | * @brief Print a character to output. |
Electrotiger | 10:b0a0a82a9ff5 | 55 | * @param char The character you want to print to output. |
Electrotiger | 10:b0a0a82a9ff5 | 56 | */ |
Electrotiger | 10:b0a0a82a9ff5 | 57 | void print(char c); |
Electrotiger | 10:b0a0a82a9ff5 | 58 | |
Electrotiger | 10:b0a0a82a9ff5 | 59 | private: |
Electrotiger | 10:b0a0a82a9ff5 | 60 | RawSerial btSerialObj; |
Electrotiger | 10:b0a0a82a9ff5 | 61 | void receiveByteISR(); |
Electrotiger | 10:b0a0a82a9ff5 | 62 | /// Pointer to a callback function the client provides when a line is received. |
Electrotiger | 10:b0a0a82a9ff5 | 63 | void (*lineCallbackFunc) (const char*); |
Electrotiger | 10:b0a0a82a9ff5 | 64 | /// Pointer to a callback function the client provides when a character is received. |
Electrotiger | 10:b0a0a82a9ff5 | 65 | void (*charCallbackFunc) (char); |
Electrotiger | 10:b0a0a82a9ff5 | 66 | char dataReceivedBuffer[dataBufferSize]; |
Electrotiger | 10:b0a0a82a9ff5 | 67 | int32_t dataReceivedBufferPos; |
Electrotiger | 10:b0a0a82a9ff5 | 68 | char dataReceivedBufferCopy[dataBufferSize]; |
Electrotiger | 10:b0a0a82a9ff5 | 69 | }; |
Electrotiger | 10:b0a0a82a9ff5 | 70 | |
Electrotiger | 10:b0a0a82a9ff5 | 71 | #endif /* HC06BLUETOOTH_H_ */ |