Driver for the JY-MCU v1.06 HC-06 Bluetooth module.
Dependents: DISCO-F746NG_rtos_test MbedTableControl
Diff: HC06Bluetooth.hpp.orig
- Revision:
- 5:f169dd551a3a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HC06Bluetooth.hpp.orig Tue Jun 07 20:25:14 2016 +0000 @@ -0,0 +1,87 @@ +/** + * @file HC06Bluetooth.hpp + * @date June 4th, 2016 + * @author Weimen Li + * @class HC06Bluetooth + * @brief This class creates an object representing the HC06 Bluetooth Module + * @code + // First, make sure to have included the hpp file: + #include HC06Bluetooth.hpp + // In the scope that you need it, create a new instance of the HC06Bluetooth object by calling: + HC06Bluetooth HC06BluetoothObj(TXPin, RXPin, "My Cool Robot", someCallBackFunction); + // The "someCallBackFunction" is a function you write that is called when a new line has been read. + // For instance, one simple callback is to simply echo (reprint to output) the received line: + + void BTEchoCallback(const char* receivedLine) { + HC06BluetoothObj.print(receivedLine); + } + + // A more sophisticated callback would be one that understands the received line contains commands, + // and parses them: +// Callback function to read a command from input +// Include the <sstream> library, since that is what we use to process information. +#include <sstream> +#include <string> +// Variables to read information into. +volatile float kConst; +volatile float iConst; +volatile float dConst; +void BTCommandCallback(const char* receivedString) { + stringstream stringStream(receivedString); + // The received string has the form "newDeviceName kConst iConst dConst" + // Necessary to use these temporary variables since stringStream cannot read into + // "volatile float", but it can read into "float". + float kConstTemp; + float iConstTemp; + float dConstTemp; + stringStream >> kConstTemp; + stringStream >> iConstTemp; + stringStream >> dConstTemp; + kConst = kConstTemp; + iConst = iConstTemp; + dConst = dConstTemp; + // Echo the received commands: + char output[256]; + sprintf(output, "Received Command: kConst = %f, iConst = %f, dConst = %f", kConst, iConst, dConst); + bluetoothObj.print(output); +} + +} + * @endcode + */ + +//TODO: Complete this example. + + +#ifndef HC06BLUETOOTH_H_ +#define HC06BLUETOOTH_H_ +#include "mbed.h" +#include <string> + +class HC06Bluetooth : public RawSerial{ +public: + /** + * @brief Constructor for the HC06_Bluetooth class. + * @param TX The pin that the TX line is attached to. + * @param RX The pin that the RX line is attached to. + * @param deviceName The name that you want your system to be identified as when you connect to it i.e. "Weimen's MAE433Robot" + * @param callbackFunc The callback function that will be called once a newline character is encountered on the receiving data. + * The callback function takes as an argument a string containing the line that has been read. + * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs. + */ + HC06Bluetooth(PinName TX, PinName RX, std::string deviceName, void (*callbackFunc) (const char* readString) = NULL); + virtual ~HC06Bluetooth(); + /** + * @brief Print information in buffer to the output. + */ + void print(const char *buffer); + +private: + void receiveByteISR(); + void (*callbackFunc) (const char*); + char dataReceivedBuffer[256]; + int32_t dataReceivedBufferPos; + char dataReceivedBufferCopy[256]; +}; + +#endif /* HC06BLUETOOTH_H_ */