/* * This is the Serial_1_ConnectionTestSketch sketch for boards, like FioV3, that use Serial1 to talk to the bluetooth module * For Uno and most other board use pfodConnectionTestSketch. * * It just waits for {.} from Serial and then sends back on button * *(c)2014 Forward Computing and Control Pty. Ltd. * This code may be freely used for both private and commerical use. * Provide this copyright is maintained. */ // the setup routine runs once, when you press reset void setup() { Serial.begin(9600); // set up Serial1 instead of Serial for (int i=5; i>0; i--) { Serial.print(i); delay(1000); // wait a few secs, help re-programming } parserSetup(); // initialize parser Serial.println(); Serial1.begin(9600); // set up Serial1 to talk to bluetooth module } // the loop routine runs over and over again forever: void loop() { byte in = 0; if (Serial1.available()) { in = Serial1.read(); byte cmd = parse(in); // parse the pfod msgs if (cmd != 0) { // have parsed a complete msg { to } if (cmd == (byte)'.') { // pfodApp has asked for main menu Serial1.println("Received Main Menu . cmd"); // send menu with just one button Serial1.println(F("{.Test Sketch Main Menu|a~Success}")); // this is the main menu } else { // this should never happen Serial1.println("Unknown cmd"); Serial1.println("{}"); // always respond with something in this case an empty msg } cmd = 0; // have processed this cmd now // so clear it and wait for next one } } } // ==================================== // This is the pfod parser code // byte cmdByte; byte parserByteCounter; byte parserState; static const byte pfodMaxMsgLen = 0xff; // == 255, if no closing } by now ignore msg static const byte pfodStartMsg = (byte)'{'; static const byte pfodEndMsg = (byte)'}'; static const byte pfodWaitingForStart = 0xff; static const byte pfodInMsg = 0; void parserSetup() { parserByteCounter = 0; cmdByte = 0; // not started yet parserState = pfodWaitingForStart; // switch to pfodInMsg when see { } //------------------------------------- //PROCESS_RECEIVED_CHAR // { char } // save first char after { // store in cmdByte // // before { parserState == pfodWaitingForStart // when { seen parserState == pfodInMsg // after { first char ( != } ) is stored in cmdByte // cmdByte returned when terminating } seen but // not if no } seen for 254 bytes after starting { then // ignore msg and start looking for { again // //------------------------------------- // return 0 if no cmd found yet // else return cmd when see } // or ignore if >254 bytes after { // // in is byte read from Serial port byte parse(byte in) { parserByteCounter++; if (parserState == pfodWaitingForStart) { if (in == pfodStartMsg) { // found { parserSetup(); // clean out last cmd parserState = pfodInMsg; } // else ignore this char as waiting for start { // always reset counter if waiting for { parserByteCounter = 1; return 0; } // else have seen { if ((parserByteCounter == pfodMaxMsgLen) && (in != pfodEndMsg)) { // ignore this msg and reset // should not happen as pfodApp should limit // msgs sent to pfodDevice to <=255 bytes parserSetup(); return 0; } // else is this the end of the msg if (in == pfodEndMsg) { byte pfodMsgCmd = cmdByte; // this will return 0 when parsing {} msg // set up to wait for next { parserSetup(); // return command byte found return pfodMsgCmd; // else normal byte } else if (cmdByte == 0) { // save first cmd byte (only the first) cmdByte = in; } return 0; }