Driver for the JY-MCU v1.06 HC-06 Bluetooth module.

Dependents:   DISCO-F746NG_rtos_test MbedTableControl

Committer:
Electrotiger
Date:
Wed Aug 03 18:49:53 2016 +0000
Revision:
16:1030b80a28f4
Parent:
15:6a0aeaa39291
Child:
18:85c0f6580cd8
Correct Header file placed in.

Who changed what in which revision?

UserRevisionLine numberNew 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"
Electrotiger 16:1030b80a28f4 14 #include <string>
Electrotiger 15:6a0aeaa39291 15
Electrotiger 16:1030b80a28f4 16 const int dataBufferSize = 256;
Electrotiger 16:1030b80a28f4 17 // WARNING: DO NOT CHANGE THESE VALUES, AS THEY ARE USED TO INDEX INTO AN ARRAY FOR IMPLEMENTATION.
Electrotiger 16:1030b80a28f4 18 enum Baudrate {B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600, B1382400, END};
Electrotiger 10:b0a0a82a9ff5 19
Electrotiger 16:1030b80a28f4 20 class HC06Bluetooth {
Electrotiger 16:1030b80a28f4 21 public: // Public methods.
Electrotiger 16:1030b80a28f4 22 /**
Electrotiger 16:1030b80a28f4 23 * @brief Constructor for the HC06_Bluetooth class.
Electrotiger 16:1030b80a28f4 24 * @param TX The pin that the TX line is attached to.
Electrotiger 16:1030b80a28f4 25 * @param RX The pin that the RX line is attached to.
Electrotiger 16:1030b80a28f4 26 * @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 27 * @param password A 4-digit numeric PIN that you want your device to connect with. It defaults to "1234".
Electrotiger 16:1030b80a28f4 28 * @param lineCallbackFunc The callback function that will be called once a newline character is encountered on the receiving data.
Electrotiger 16:1030b80a28f4 29 * The callback function takes as an argument a string containing the line that has been read.
Electrotiger 16:1030b80a28f4 30 * @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 31 * @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 32 * the character that has been read.
Electrotiger 16:1030b80a28f4 33 * @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 34 */
Electrotiger 16:1030b80a28f4 35 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 36 virtual ~HC06Bluetooth();
Electrotiger 16:1030b80a28f4 37 /**
Electrotiger 16:1030b80a28f4 38 * @brief Run the setup routine to configure the device name and the device pin.
Electrotiger 16:1030b80a28f4 39 * @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 40 * by the module itself! However, you may run the function again if you would like to change the deviceName or PIN.
Electrotiger 16:1030b80a28f4 41 * @param deviceName The name of the device, as a string. It must only contain alphanumeric characters - no spaces allowed.
Electrotiger 16:1030b80a28f4 42 * @param PIN The device PIN.
Electrotiger 16:1030b80a28f4 43 */
Electrotiger 16:1030b80a28f4 44 void runSetup(std::string deviceName, std::string PIN = "1234");
Electrotiger 16:1030b80a28f4 45 /**
Electrotiger 16:1030b80a28f4 46 * @brief Print information in buffer to the output.
Electrotiger 16:1030b80a28f4 47 * @param buffer A null-terminated buffer containing the data you want to send.
Electrotiger 16:1030b80a28f4 48 */
Electrotiger 16:1030b80a28f4 49 void print(const char *buffer);
Electrotiger 16:1030b80a28f4 50 /**
Electrotiger 16:1030b80a28f4 51 * @brief Print information in buffer to the output followed by a newline
Electrotiger 16:1030b80a28f4 52 * @param buffer A null-terminated buffer containing the data you want to send.
Electrotiger 16:1030b80a28f4 53 */
Electrotiger 16:1030b80a28f4 54 void println(const char *buffer);
Electrotiger 16:1030b80a28f4 55 /**
Electrotiger 16:1030b80a28f4 56 * @brief Print a character to output.
Electrotiger 16:1030b80a28f4 57 * @param char The character you want to print to output.
Electrotiger 16:1030b80a28f4 58 */
Electrotiger 16:1030b80a28f4 59 void print(char c);
Electrotiger 10:b0a0a82a9ff5 60
Electrotiger 16:1030b80a28f4 61 private:
Electrotiger 16:1030b80a28f4 62 RawSerial btSerialObj;
Electrotiger 16:1030b80a28f4 63 Baudrate baudrate;
Electrotiger 16:1030b80a28f4 64 void receiveByteISR();
Electrotiger 16:1030b80a28f4 65 /// Pointer to a callback function the client provides when a line is received.
Electrotiger 16:1030b80a28f4 66 void (*lineCallbackFunc) (const char*, size_t strlen);
Electrotiger 16:1030b80a28f4 67 /// Pointer to a callback function the client provides when a character is received.
Electrotiger 16:1030b80a28f4 68 void (*charCallbackFunc) (char);
Electrotiger 16:1030b80a28f4 69 char dataReceivedBuffer[dataBufferSize];
Electrotiger 16:1030b80a28f4 70 int32_t dataReceivedBufferPos;
Electrotiger 16:1030b80a28f4 71 char dataReceivedBufferCopy[dataBufferSize];
Electrotiger 16:1030b80a28f4 72 };
Electrotiger 10:b0a0a82a9ff5 73
Electrotiger 16:1030b80a28f4 74 #endif /* HC06BLUETOOTH_H_ */