Drivers for the mini robot designed for Princeton's MAE 433 course.
Dependencies: mbed-dsp mbed-rtos mbed
Dependents: MAE433_Library_Tester RobotBalancerv2
HC06Bluetooth.hpp@0:9afc272fa65f, 2016-06-24 (annotated)
- Committer:
- Electrotiger
- Date:
- Fri Jun 24 21:03:20 2016 +0000
- Revision:
- 0:9afc272fa65f
- Child:
- 1:918a505314ea
First Commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Electrotiger | 0:9afc272fa65f | 1 | /** |
Electrotiger | 0:9afc272fa65f | 2 | * @file HC06Bluetooth.hpp |
Electrotiger | 0:9afc272fa65f | 3 | * @date June 4th, 2016 |
Electrotiger | 0:9afc272fa65f | 4 | * @author Weimen Li |
Electrotiger | 0:9afc272fa65f | 5 | * @class HC06Bluetooth |
Electrotiger | 0:9afc272fa65f | 6 | * @brief This class creates an object representing the HC06 Bluetooth Module |
Electrotiger | 0:9afc272fa65f | 7 | * The baud rate for the device is configured to be 115200. |
Electrotiger | 0:9afc272fa65f | 8 | * @code |
Electrotiger | 0:9afc272fa65f | 9 | // First, make sure to have included the hpp file: |
Electrotiger | 0:9afc272fa65f | 10 | #include HC06Bluetooth.hpp |
Electrotiger | 0:9afc272fa65f | 11 | // In the scope that you need it, create a new instance of the HC06Bluetooth object by calling: |
Electrotiger | 0:9afc272fa65f | 12 | HC06Bluetooth HC06BluetoothObj(TXPin, RXPin, "My Cool Robot", someCallBackFunction); |
Electrotiger | 0:9afc272fa65f | 13 | // The "someCallBackFunction" is a function you write that is called when a new line has been read. |
Electrotiger | 0:9afc272fa65f | 14 | // For instance, one simple callback is to simply echo (reprint to output) the received line: |
Electrotiger | 0:9afc272fa65f | 15 | |
Electrotiger | 0:9afc272fa65f | 16 | void BTEchoCallback(const char* receivedLine) { |
Electrotiger | 0:9afc272fa65f | 17 | HC06BluetoothObj.print(receivedLine); |
Electrotiger | 0:9afc272fa65f | 18 | } |
Electrotiger | 0:9afc272fa65f | 19 | |
Electrotiger | 0:9afc272fa65f | 20 | // A more sophisticated callback would be one that understands the received line contains commands, |
Electrotiger | 0:9afc272fa65f | 21 | // and parses them: |
Electrotiger | 0:9afc272fa65f | 22 | // Callback function to read a command from input |
Electrotiger | 0:9afc272fa65f | 23 | // Include the <sstream> library, since that is what we use to process information. |
Electrotiger | 0:9afc272fa65f | 24 | #include <sstream> |
Electrotiger | 0:9afc272fa65f | 25 | #include <string> |
Electrotiger | 0:9afc272fa65f | 26 | // Variables to read information into. |
Electrotiger | 0:9afc272fa65f | 27 | volatile float kConst; |
Electrotiger | 0:9afc272fa65f | 28 | volatile float iConst; |
Electrotiger | 0:9afc272fa65f | 29 | volatile float dConst; |
Electrotiger | 0:9afc272fa65f | 30 | void BTCommandCallback(const char* receivedString) { |
Electrotiger | 0:9afc272fa65f | 31 | stringstream stringStream(receivedString); |
Electrotiger | 0:9afc272fa65f | 32 | // The received string has the form "newDeviceName kConst iConst dConst" |
Electrotiger | 0:9afc272fa65f | 33 | // Necessary to use these temporary variables since stringStream cannot read into |
Electrotiger | 0:9afc272fa65f | 34 | // "volatile float", but it can read into "float". |
Electrotiger | 0:9afc272fa65f | 35 | float kConstTemp; |
Electrotiger | 0:9afc272fa65f | 36 | float iConstTemp; |
Electrotiger | 0:9afc272fa65f | 37 | float dConstTemp; |
Electrotiger | 0:9afc272fa65f | 38 | stringStream >> kConstTemp; |
Electrotiger | 0:9afc272fa65f | 39 | stringStream >> iConstTemp; |
Electrotiger | 0:9afc272fa65f | 40 | stringStream >> dConstTemp; |
Electrotiger | 0:9afc272fa65f | 41 | kConst = kConstTemp; |
Electrotiger | 0:9afc272fa65f | 42 | iConst = iConstTemp; |
Electrotiger | 0:9afc272fa65f | 43 | dConst = dConstTemp; |
Electrotiger | 0:9afc272fa65f | 44 | // Echo the received commands: |
Electrotiger | 0:9afc272fa65f | 45 | char output[256]; |
Electrotiger | 0:9afc272fa65f | 46 | sprintf(output, "Received Command: kConst = %f, iConst = %f, dConst = %f", kConst, iConst, dConst); |
Electrotiger | 0:9afc272fa65f | 47 | bluetoothObj.print(output); |
Electrotiger | 0:9afc272fa65f | 48 | } |
Electrotiger | 0:9afc272fa65f | 49 | |
Electrotiger | 0:9afc272fa65f | 50 | } |
Electrotiger | 0:9afc272fa65f | 51 | * @endcode |
Electrotiger | 0:9afc272fa65f | 52 | */ |
Electrotiger | 0:9afc272fa65f | 53 | |
Electrotiger | 0:9afc272fa65f | 54 | //TODO: Complete this example. |
Electrotiger | 0:9afc272fa65f | 55 | |
Electrotiger | 0:9afc272fa65f | 56 | |
Electrotiger | 0:9afc272fa65f | 57 | #ifndef HC06BLUETOOTH_H_ |
Electrotiger | 0:9afc272fa65f | 58 | #define HC06BLUETOOTH_H_ |
Electrotiger | 0:9afc272fa65f | 59 | #include "mbed.h" |
Electrotiger | 0:9afc272fa65f | 60 | #include <string> |
Electrotiger | 0:9afc272fa65f | 61 | |
Electrotiger | 0:9afc272fa65f | 62 | class HC06Bluetooth { |
Electrotiger | 0:9afc272fa65f | 63 | public: // Public methods. |
Electrotiger | 0:9afc272fa65f | 64 | /** |
Electrotiger | 0:9afc272fa65f | 65 | * @brief Constructor for the HC06_Bluetooth class. |
Electrotiger | 0:9afc272fa65f | 66 | * @param TX The pin that the TX line is attached to. |
Electrotiger | 0:9afc272fa65f | 67 | * @param RX The pin that the RX line is attached to. |
Electrotiger | 0:9afc272fa65f | 68 | * @param deviceName The name that you want your system to be identified as when you connect to it i.e. "Weimen's MAE433Robot" |
Electrotiger | 0:9afc272fa65f | 69 | * @param password A 4-digit numeric PIN that you want your device to connect with. It defaults to "1234". |
Electrotiger | 0:9afc272fa65f | 70 | * @param lineCallbackFunc The callback function that will be called once a newline character is encountered on the receiving data. |
Electrotiger | 0:9afc272fa65f | 71 | * The callback function takes as an argument a string containing the line that has been read. |
Electrotiger | 0:9afc272fa65f | 72 | * @remark The callback function is run within within an interrupt service routine, so it should be written to be safe for ISRs. |
Electrotiger | 0:9afc272fa65f | 73 | * @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 | 0:9afc272fa65f | 74 | * the character that has been read. |
Electrotiger | 0:9afc272fa65f | 75 | */ |
Electrotiger | 0:9afc272fa65f | 76 | HC06Bluetooth(PinName TX, PinName RX, void (*lineCallbackFunc) (const char* readString) = NULL, void (*charCallbackFunc) (char readChar) = NULL); |
Electrotiger | 0:9afc272fa65f | 77 | virtual ~HC06Bluetooth(); |
Electrotiger | 0:9afc272fa65f | 78 | /** |
Electrotiger | 0:9afc272fa65f | 79 | * @brief Run the setup routine to configure the device name and the device pin. |
Electrotiger | 0:9afc272fa65f | 80 | * @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 | 0:9afc272fa65f | 81 | * by the module itself! However, you may run the function again if you would like to change the deviceName or PIN. |
Electrotiger | 0:9afc272fa65f | 82 | * @param deviceName The name of the device, as a string. It must only contain alphanumeric characters - no spaces allowed. |
Electrotiger | 0:9afc272fa65f | 83 | * @param PIN The device PIN. |
Electrotiger | 0:9afc272fa65f | 84 | */ |
Electrotiger | 0:9afc272fa65f | 85 | void runSetup(std::string deviceName, std::string PIN = "1234"); |
Electrotiger | 0:9afc272fa65f | 86 | /** |
Electrotiger | 0:9afc272fa65f | 87 | * @brief Print information in buffer to the output. |
Electrotiger | 0:9afc272fa65f | 88 | * @param buffer A null-terminated buffer containing the data you want to send. |
Electrotiger | 0:9afc272fa65f | 89 | */ |
Electrotiger | 0:9afc272fa65f | 90 | void print(const char *buffer); |
Electrotiger | 0:9afc272fa65f | 91 | /** |
Electrotiger | 0:9afc272fa65f | 92 | * @brief Print a character to output. |
Electrotiger | 0:9afc272fa65f | 93 | * @param char The character you want to print to output. |
Electrotiger | 0:9afc272fa65f | 94 | */ |
Electrotiger | 0:9afc272fa65f | 95 | void print(char c); |
Electrotiger | 0:9afc272fa65f | 96 | |
Electrotiger | 0:9afc272fa65f | 97 | private: |
Electrotiger | 0:9afc272fa65f | 98 | RawSerial btSerialObj; |
Electrotiger | 0:9afc272fa65f | 99 | void receiveByteISR(); |
Electrotiger | 0:9afc272fa65f | 100 | /// Pointer to a callback function the client provides when a line is received. |
Electrotiger | 0:9afc272fa65f | 101 | void (*lineCallbackFunc) (const char*); |
Electrotiger | 0:9afc272fa65f | 102 | /// Pointer to a callback function the client provides when a character is received. |
Electrotiger | 0:9afc272fa65f | 103 | void (*charCallbackFunc) (char); |
Electrotiger | 0:9afc272fa65f | 104 | char dataReceivedBuffer[256]; |
Electrotiger | 0:9afc272fa65f | 105 | int32_t dataReceivedBufferPos; |
Electrotiger | 0:9afc272fa65f | 106 | char dataReceivedBufferCopy[256]; |
Electrotiger | 0:9afc272fa65f | 107 | }; |
Electrotiger | 0:9afc272fa65f | 108 | |
Electrotiger | 0:9afc272fa65f | 109 | #endif /* HC06BLUETOOTH_H_ */ |