Comms Library for communicating via bluetooth
Revision 0:7d01a895b45d, committed 2017-09-19
- Comitter:
- Armand
- Date:
- Tue Sep 19 11:53:38 2017 +0000
- Commit message:
- First publish
Changed in this revision
Comms.cpp | Show annotated file Show diff for this revision Revisions of this file |
Comms.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 7d01a895b45d Comms.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Comms.cpp Tue Sep 19 11:53:38 2017 +0000 @@ -0,0 +1,39 @@ +#include "Comms.h" +#include "mbed.h" + +Comms::Comms(PinName TX, PinName RX) : RawSerial(TX, RX) +{ + msg_counter = 0; + newmsg = false; + + attach(this, &Comms::commsinterrupt); +} + +void Comms::commsinterrupt() +{ + char c = getc(); //read the incoming character + + if(c == '!') + { + msg_counter = 0; + } + else if(c == '#') + { + msg[msg_counter] = '\0'; + newmsg = true; //enable the new message flag to indicate a COMPLETE message was received + msg_counter = 0; //clear the message string + } + else + { + msg[msg_counter] = c; //add the character to the message string + msg_counter++; //move to the next character in the string + } + + if(newmsg == true) + { + sscanf(msg,"%s %f", &cmd, &data); + newmsg = false; + } +} + + \ No newline at end of file
diff -r 000000000000 -r 7d01a895b45d Comms.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Comms.h Tue Sep 19 11:53:38 2017 +0000 @@ -0,0 +1,97 @@ +/* Comms Library for communicating with the bluetooth car. v1.0 + * Copyright (c) 2017 Armand Coetzer + * s213293048@nmmu.ac.za + * + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef Comms_H +#define Comms_H + +#include "mbed.h" + +/** Class library to create a serial connection from the MCU to the bluetooth device. + * + * Example: + * @code + * #include "mbed.h" + *#include "Comms.h" + * + *Comms BT(PC_6, PC_7); + * + *int main() + *{ + * while(1) + * { + * if(BT.newmsg == true) + * { + * BT.printf("%s\n",BT.cmd); + * BT. printf("%.2f\n", BT.data); + * BT.clrmsg(); + * } + * + * } + *} + * @endcode + */ + +class Comms : public RawSerial{ + public: + /** Create a serial connection from the MCU to the bluetooth device. + * @param TX Pin for transmitting data. + * @param RX Pin for Receiving data. + */ + Comms(PinName TX, PinName RX); + + // /** changing the boolen variable to false. + // * @param + // * None + // */ + // void clrmsg(); + + /** float variable for storing the speed value. + * @param + * None + */ + float data; + + /** Character string for storing the direction. + * @param + * None + */ + char cmd[50]; + + /** boolean variable to state if there is a new message or not. + * @param + * None + */ + bool newmsg; + + + private: + + void commsinterrupt(); + + char msg[50]; //incoming message string + int msg_counter; //character position counter in the message string + +}; + +#endif