Driver for the JY-MCU v1.06 HC-06 Bluetooth module.
Dependents: DISCO-F746NG_rtos_test MbedTableControl
HC06Bluetooth.h@21:cce827364df1, 2016-08-08 (annotated)
- Committer:
- Weimen Li
- Date:
- Mon Aug 08 19:48:19 2016 -0400
- Revision:
- 21:cce827364df1
- Parent:
- 20:13283edd1aba
Converted std::queue to RTOS's Queue to pass information from receivedByteISR to receivedByteThread, as std::queue is not ISR-safe.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Electrotiger | 16:1030b80a28f4 | 1 | /** |
Electrotiger | 16:1030b80a28f4 | 2 | * @file HC06Bluetooth.hpp |
Electrotiger | 16:1030b80a28f4 | 3 | * @date June 4th, 2016 |
Electrotiger | 16:1030b80a28f4 | 4 | * @author Weimen Li |
Electrotiger | 16:1030b80a28f4 | 5 | * @class HC06Bluetooth |
Electrotiger | 16:1030b80a28f4 | 6 | * @brief This class creates an object representing the HC06 Bluetooth Module |
Electrotiger | 16:1030b80a28f4 | 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 | 16:1030b80a28f4 | 11 | #ifndef HC06BLUETOOTH_H_ |
Electrotiger | 16:1030b80a28f4 | 12 | #define HC06BLUETOOTH_H_ |
Electrotiger | 16:1030b80a28f4 | 13 | #include "mbed.h" |
Weimen Li |
20:13283edd1aba | 14 | #include "rtos.h" |
Electrotiger | 16:1030b80a28f4 | 15 | #include <string> |
Weimen Li |
18:85c0f6580cd8 | 16 | #include <queue> |
Electrotiger | 15:6a0aeaa39291 | 17 | |
Weimen Li |
20:13283edd1aba | 18 | const uint32_t SIG_BT_BYTE = (1 << 2); |
Electrotiger | 16:1030b80a28f4 | 19 | // WARNING: DO NOT CHANGE THESE VALUES, AS THEY ARE USED TO INDEX INTO AN ARRAY FOR IMPLEMENTATION. |
Electrotiger | 16:1030b80a28f4 | 20 | enum Baudrate {B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600, B1382400, END}; |
Electrotiger | 10:b0a0a82a9ff5 | 21 | |
Electrotiger | 16:1030b80a28f4 | 22 | class HC06Bluetooth { |
Electrotiger | 16:1030b80a28f4 | 23 | public: // Public methods. |
Electrotiger | 16:1030b80a28f4 | 24 | /** |
Electrotiger | 16:1030b80a28f4 | 25 | * @brief Constructor for the HC06_Bluetooth class. |
Electrotiger | 16:1030b80a28f4 | 26 | * @param TX The pin that the TX line is attached to. |
Electrotiger | 16:1030b80a28f4 | 27 | * @param RX The pin that the RX line is attached to. |
Electrotiger | 16:1030b80a28f4 | 28 | * @param deviceName The name that you want your system to be identified as when you connect to it i.e. "Weimen's MAE433Robot" |
Electrotiger | 16:1030b80a28f4 | 29 | * @param password A 4-digit numeric PIN that you want your device to connect with. It defaults to "1234". |
Electrotiger | 16:1030b80a28f4 | 30 | * @param lineCallbackFunc The callback function that will be called once a newline character is encountered on the receiving data. |
Electrotiger | 16:1030b80a28f4 | 31 | * The callback function takes as an argument a string containing the line that has been read. |
Electrotiger | 16:1030b80a28f4 | 32 | * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs. |
Electrotiger | 16:1030b80a28f4 | 33 | * @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 | 16:1030b80a28f4 | 34 | * the character that has been read. |
Electrotiger | 16:1030b80a28f4 | 35 | * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs. |
Electrotiger | 16:1030b80a28f4 | 36 | */ |
Electrotiger | 16:1030b80a28f4 | 37 | HC06Bluetooth(PinName TX, PinName RX, Baudrate baudrate = B115200, void (*lineCallbackFunc) (const char* readString, size_t strlen) = NULL, void (*charCallbackFunc) (char readChar) = NULL); |
Electrotiger | 16:1030b80a28f4 | 38 | virtual ~HC06Bluetooth(); |
Electrotiger | 16:1030b80a28f4 | 39 | /** |
Electrotiger | 16:1030b80a28f4 | 40 | * @brief Run the setup routine to configure the device name and the device pin. |
Electrotiger | 16:1030b80a28f4 | 41 | * @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 | 16:1030b80a28f4 | 42 | * by the module itself! However, you may run the function again if you would like to change the deviceName or PIN. |
Electrotiger | 16:1030b80a28f4 | 43 | * @param deviceName The name of the device, as a string. It must only contain alphanumeric characters - no spaces allowed. |
Electrotiger | 16:1030b80a28f4 | 44 | * @param PIN The device PIN. |
Electrotiger | 16:1030b80a28f4 | 45 | */ |
Electrotiger | 16:1030b80a28f4 | 46 | void runSetup(std::string deviceName, std::string PIN = "1234"); |
Electrotiger | 16:1030b80a28f4 | 47 | /** |
Electrotiger | 16:1030b80a28f4 | 48 | * @brief Print information in buffer to the output. |
Electrotiger | 16:1030b80a28f4 | 49 | * @param buffer A null-terminated buffer containing the data you want to send. |
Electrotiger | 16:1030b80a28f4 | 50 | */ |
Electrotiger | 16:1030b80a28f4 | 51 | void print(const char *buffer); |
Weimen Li |
19:41da4bfc4d4d | 52 | void print(std::string s) { |
Weimen Li |
19:41da4bfc4d4d | 53 | print(s.c_str()); |
Weimen Li |
19:41da4bfc4d4d | 54 | } |
Electrotiger | 16:1030b80a28f4 | 55 | /** |
Electrotiger | 16:1030b80a28f4 | 56 | * @brief Print information in buffer to the output followed by a newline |
Electrotiger | 16:1030b80a28f4 | 57 | * @param buffer A null-terminated buffer containing the data you want to send. |
Electrotiger | 16:1030b80a28f4 | 58 | */ |
Electrotiger | 16:1030b80a28f4 | 59 | void println(const char *buffer); |
Weimen Li |
19:41da4bfc4d4d | 60 | void println(std::string s) { |
Weimen Li |
19:41da4bfc4d4d | 61 | println(s.c_str()); |
Weimen Li |
19:41da4bfc4d4d | 62 | } |
Electrotiger | 16:1030b80a28f4 | 63 | /** |
Electrotiger | 16:1030b80a28f4 | 64 | * @brief Print a character to output. |
Electrotiger | 16:1030b80a28f4 | 65 | * @param char The character you want to print to output. |
Electrotiger | 16:1030b80a28f4 | 66 | */ |
Electrotiger | 16:1030b80a28f4 | 67 | void print(char c); |
Electrotiger | 10:b0a0a82a9ff5 | 68 | |
Electrotiger | 16:1030b80a28f4 | 69 | private: |
Electrotiger | 16:1030b80a28f4 | 70 | RawSerial btSerialObj; |
Electrotiger | 16:1030b80a28f4 | 71 | Baudrate baudrate; |
Electrotiger | 16:1030b80a28f4 | 72 | void receiveByteISR(); |
Weimen Li |
20:13283edd1aba | 73 | void receiveByteThread(); |
Weimen Li |
20:13283edd1aba | 74 | Thread receiveByteThreadObj; |
Electrotiger | 16:1030b80a28f4 | 75 | /// Pointer to a callback function the client provides when a line is received. |
Electrotiger | 16:1030b80a28f4 | 76 | void (*lineCallbackFunc) (const char*, size_t strlen); |
Electrotiger | 16:1030b80a28f4 | 77 | /// Pointer to a callback function the client provides when a character is received. |
Electrotiger | 16:1030b80a28f4 | 78 | void (*charCallbackFunc) (char); |
Weimen Li |
21:cce827364df1 | 79 | Queue<char, 64> dataReceivedBuffer; |
Weimen Li |
18:85c0f6580cd8 | 80 | std::queue<char> dataReceivedBufferCopy; |
Weimen Li |
18:85c0f6580cd8 | 81 | std::vector<char> dataReceivedToClient; |
Electrotiger | 16:1030b80a28f4 | 82 | }; |
Electrotiger | 10:b0a0a82a9ff5 | 83 | |
Electrotiger | 16:1030b80a28f4 | 84 | #endif /* HC06BLUETOOTH_H_ */ |