Comms Library for communicating via bluetooth

Dependents:   EHWM

Committer:
Armand
Date:
Tue Sep 19 11:53:38 2017 +0000
Revision:
0:7d01a895b45d
First publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Armand 0:7d01a895b45d 1 #include "Comms.h"
Armand 0:7d01a895b45d 2 #include "mbed.h"
Armand 0:7d01a895b45d 3
Armand 0:7d01a895b45d 4 Comms::Comms(PinName TX, PinName RX) : RawSerial(TX, RX)
Armand 0:7d01a895b45d 5 {
Armand 0:7d01a895b45d 6 msg_counter = 0;
Armand 0:7d01a895b45d 7 newmsg = false;
Armand 0:7d01a895b45d 8
Armand 0:7d01a895b45d 9 attach(this, &Comms::commsinterrupt);
Armand 0:7d01a895b45d 10 }
Armand 0:7d01a895b45d 11
Armand 0:7d01a895b45d 12 void Comms::commsinterrupt()
Armand 0:7d01a895b45d 13 {
Armand 0:7d01a895b45d 14 char c = getc(); //read the incoming character
Armand 0:7d01a895b45d 15
Armand 0:7d01a895b45d 16 if(c == '!')
Armand 0:7d01a895b45d 17 {
Armand 0:7d01a895b45d 18 msg_counter = 0;
Armand 0:7d01a895b45d 19 }
Armand 0:7d01a895b45d 20 else if(c == '#')
Armand 0:7d01a895b45d 21 {
Armand 0:7d01a895b45d 22 msg[msg_counter] = '\0';
Armand 0:7d01a895b45d 23 newmsg = true; //enable the new message flag to indicate a COMPLETE message was received
Armand 0:7d01a895b45d 24 msg_counter = 0; //clear the message string
Armand 0:7d01a895b45d 25 }
Armand 0:7d01a895b45d 26 else
Armand 0:7d01a895b45d 27 {
Armand 0:7d01a895b45d 28 msg[msg_counter] = c; //add the character to the message string
Armand 0:7d01a895b45d 29 msg_counter++; //move to the next character in the string
Armand 0:7d01a895b45d 30 }
Armand 0:7d01a895b45d 31
Armand 0:7d01a895b45d 32 if(newmsg == true)
Armand 0:7d01a895b45d 33 {
Armand 0:7d01a895b45d 34 sscanf(msg,"%s %f", &cmd, &data);
Armand 0:7d01a895b45d 35 newmsg = false;
Armand 0:7d01a895b45d 36 }
Armand 0:7d01a895b45d 37 }
Armand 0:7d01a895b45d 38
Armand 0:7d01a895b45d 39