Driver for the JY-MCU v1.06 HC-06 Bluetooth module.
Dependents: DISCO-F746NG_rtos_test MbedTableControl
HC06Bluetooth.hpp.orig@21:cce827364df1, 2016-08-08 (annotated)
- Committer:
- Weimen Li
- Date:
- Mon Aug 08 19:48:19 2016 -0400
- Revision:
- 21:cce827364df1
- Parent:
- 5:f169dd551a3a
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 | 5:f169dd551a3a | 1 | /** |
Electrotiger | 5:f169dd551a3a | 2 | * @file HC06Bluetooth.hpp |
Electrotiger | 5:f169dd551a3a | 3 | * @date June 4th, 2016 |
Electrotiger | 5:f169dd551a3a | 4 | * @author Weimen Li |
Electrotiger | 5:f169dd551a3a | 5 | * @class HC06Bluetooth |
Electrotiger | 5:f169dd551a3a | 6 | * @brief This class creates an object representing the HC06 Bluetooth Module |
Electrotiger | 5:f169dd551a3a | 7 | * @code |
Electrotiger | 5:f169dd551a3a | 8 | // First, make sure to have included the hpp file: |
Electrotiger | 5:f169dd551a3a | 9 | #include HC06Bluetooth.hpp |
Electrotiger | 5:f169dd551a3a | 10 | // In the scope that you need it, create a new instance of the HC06Bluetooth object by calling: |
Electrotiger | 5:f169dd551a3a | 11 | HC06Bluetooth HC06BluetoothObj(TXPin, RXPin, "My Cool Robot", someCallBackFunction); |
Electrotiger | 5:f169dd551a3a | 12 | // The "someCallBackFunction" is a function you write that is called when a new line has been read. |
Electrotiger | 5:f169dd551a3a | 13 | // For instance, one simple callback is to simply echo (reprint to output) the received line: |
Electrotiger | 5:f169dd551a3a | 14 | |
Electrotiger | 5:f169dd551a3a | 15 | void BTEchoCallback(const char* receivedLine) { |
Electrotiger | 5:f169dd551a3a | 16 | HC06BluetoothObj.print(receivedLine); |
Electrotiger | 5:f169dd551a3a | 17 | } |
Electrotiger | 5:f169dd551a3a | 18 | |
Electrotiger | 5:f169dd551a3a | 19 | // A more sophisticated callback would be one that understands the received line contains commands, |
Electrotiger | 5:f169dd551a3a | 20 | // and parses them: |
Electrotiger | 5:f169dd551a3a | 21 | // Callback function to read a command from input |
Electrotiger | 5:f169dd551a3a | 22 | // Include the <sstream> library, since that is what we use to process information. |
Electrotiger | 5:f169dd551a3a | 23 | #include <sstream> |
Electrotiger | 5:f169dd551a3a | 24 | #include <string> |
Electrotiger | 5:f169dd551a3a | 25 | // Variables to read information into. |
Electrotiger | 5:f169dd551a3a | 26 | volatile float kConst; |
Electrotiger | 5:f169dd551a3a | 27 | volatile float iConst; |
Electrotiger | 5:f169dd551a3a | 28 | volatile float dConst; |
Electrotiger | 5:f169dd551a3a | 29 | void BTCommandCallback(const char* receivedString) { |
Electrotiger | 5:f169dd551a3a | 30 | stringstream stringStream(receivedString); |
Electrotiger | 5:f169dd551a3a | 31 | // The received string has the form "newDeviceName kConst iConst dConst" |
Electrotiger | 5:f169dd551a3a | 32 | // Necessary to use these temporary variables since stringStream cannot read into |
Electrotiger | 5:f169dd551a3a | 33 | // "volatile float", but it can read into "float". |
Electrotiger | 5:f169dd551a3a | 34 | float kConstTemp; |
Electrotiger | 5:f169dd551a3a | 35 | float iConstTemp; |
Electrotiger | 5:f169dd551a3a | 36 | float dConstTemp; |
Electrotiger | 5:f169dd551a3a | 37 | stringStream >> kConstTemp; |
Electrotiger | 5:f169dd551a3a | 38 | stringStream >> iConstTemp; |
Electrotiger | 5:f169dd551a3a | 39 | stringStream >> dConstTemp; |
Electrotiger | 5:f169dd551a3a | 40 | kConst = kConstTemp; |
Electrotiger | 5:f169dd551a3a | 41 | iConst = iConstTemp; |
Electrotiger | 5:f169dd551a3a | 42 | dConst = dConstTemp; |
Electrotiger | 5:f169dd551a3a | 43 | // Echo the received commands: |
Electrotiger | 5:f169dd551a3a | 44 | char output[256]; |
Electrotiger | 5:f169dd551a3a | 45 | sprintf(output, "Received Command: kConst = %f, iConst = %f, dConst = %f", kConst, iConst, dConst); |
Electrotiger | 5:f169dd551a3a | 46 | bluetoothObj.print(output); |
Electrotiger | 5:f169dd551a3a | 47 | } |
Electrotiger | 5:f169dd551a3a | 48 | |
Electrotiger | 5:f169dd551a3a | 49 | } |
Electrotiger | 5:f169dd551a3a | 50 | * @endcode |
Electrotiger | 5:f169dd551a3a | 51 | */ |
Electrotiger | 5:f169dd551a3a | 52 | |
Electrotiger | 5:f169dd551a3a | 53 | //TODO: Complete this example. |
Electrotiger | 5:f169dd551a3a | 54 | |
Electrotiger | 5:f169dd551a3a | 55 | |
Electrotiger | 5:f169dd551a3a | 56 | #ifndef HC06BLUETOOTH_H_ |
Electrotiger | 5:f169dd551a3a | 57 | #define HC06BLUETOOTH_H_ |
Electrotiger | 5:f169dd551a3a | 58 | #include "mbed.h" |
Electrotiger | 5:f169dd551a3a | 59 | #include <string> |
Electrotiger | 5:f169dd551a3a | 60 | |
Electrotiger | 5:f169dd551a3a | 61 | class HC06Bluetooth : public RawSerial{ |
Electrotiger | 5:f169dd551a3a | 62 | public: |
Electrotiger | 5:f169dd551a3a | 63 | /** |
Electrotiger | 5:f169dd551a3a | 64 | * @brief Constructor for the HC06_Bluetooth class. |
Electrotiger | 5:f169dd551a3a | 65 | * @param TX The pin that the TX line is attached to. |
Electrotiger | 5:f169dd551a3a | 66 | * @param RX The pin that the RX line is attached to. |
Electrotiger | 5:f169dd551a3a | 67 | * @param deviceName The name that you want your system to be identified as when you connect to it i.e. "Weimen's MAE433Robot" |
Electrotiger | 5:f169dd551a3a | 68 | * @param callbackFunc The callback function that will be called once a newline character is encountered on the receiving data. |
Electrotiger | 5:f169dd551a3a | 69 | * The callback function takes as an argument a string containing the line that has been read. |
Electrotiger | 5:f169dd551a3a | 70 | * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs. |
Electrotiger | 5:f169dd551a3a | 71 | */ |
Electrotiger | 5:f169dd551a3a | 72 | HC06Bluetooth(PinName TX, PinName RX, std::string deviceName, void (*callbackFunc) (const char* readString) = NULL); |
Electrotiger | 5:f169dd551a3a | 73 | virtual ~HC06Bluetooth(); |
Electrotiger | 5:f169dd551a3a | 74 | /** |
Electrotiger | 5:f169dd551a3a | 75 | * @brief Print information in buffer to the output. |
Electrotiger | 5:f169dd551a3a | 76 | */ |
Electrotiger | 5:f169dd551a3a | 77 | void print(const char *buffer); |
Electrotiger | 5:f169dd551a3a | 78 | |
Electrotiger | 5:f169dd551a3a | 79 | private: |
Electrotiger | 5:f169dd551a3a | 80 | void receiveByteISR(); |
Electrotiger | 5:f169dd551a3a | 81 | void (*callbackFunc) (const char*); |
Electrotiger | 5:f169dd551a3a | 82 | char dataReceivedBuffer[256]; |
Electrotiger | 5:f169dd551a3a | 83 | int32_t dataReceivedBufferPos; |
Electrotiger | 5:f169dd551a3a | 84 | char dataReceivedBufferCopy[256]; |
Electrotiger | 5:f169dd551a3a | 85 | }; |
Electrotiger | 5:f169dd551a3a | 86 | |
Electrotiger | 5:f169dd551a3a | 87 | #endif /* HC06BLUETOOTH_H_ */ |