/* Simple Data Logging A pfodDevice for Arduino using parser Libraries available from www.pfod.com.au (c)2012-15 Forward Computing and Control Pty. Ltd. This code may be freely used for both private and commerical use. Provide this copyright is maintained. */ // Code updated 5th Jan 2015 to use same format as Version 2 of the pfodParser Library // Using Serial and 9600 for send and receive // Serial D0 (RX) and D1 (TX) on Arduino Uno, // This code uses Serial so remove shield when programming the board // ====================== // this is the pfodParser.h file with the class renamed pfodParser_codeGenerated and with comments, constants and un-used methods removed class pfodParser_codeGenerated: public Print { public: pfodParser_codeGenerated(); void connect(Stream* ioPtr); void closeConnection(); byte parse(); byte* getCmd(); byte* getFirstArg(); byte getArgsCount(); byte* parseLong(byte* idxPtr, long *result); size_t write(uint8_t c); void flush(); void init(); byte parse(byte in); Stream* getPfodAppStream(); private: Stream* io; byte argsCount; byte argsIdx; byte parserState; byte args[255]; }; //============= end of pfodParser_codeGenerated.h pfodParser_codeGenerated parser; // create a parser to handle the pfod messages static unsigned long lastMillis = 0; // holds the last read millis() static unsigned long dataSampleTimer = 0; // a repeating timer #define SAMPLE_INTERVAL 1000 // 1sec == 1000mS interval unsigned long counter = 0; // this is our dummy sample data just counts seconds since program started // the setup routine runs once, when you press reset: void setup() { Serial.begin(9600); for (int i = 5; i > 0; i--) { // wait a few secs to see if we are being programmed delay(1000); } parser.connect(&Serial); // connect the parser to the i/o stream dataSampleTimer = millis(); // do this last in setup } // the loop routine runs over and over again forever: void loop() { unsigned long thisMillis = millis(); // do this just once to prevent getting different answers from multiple calls to millis() byte cmd = parser.parse(); if (cmd != 0) { // have parsed a complete msg { to } if ('.' == cmd) { // pfodApp has asked for main menu // send back open raw data screen parser.print(F("{=Sample Data Logging Data}")); // this is the main menu } else if ('!' == cmd) { // CloseConnection command closeConnection(parser.getPfodAppStream()); } else { // unknown command parser.print(F("{}")); // always send back a pfod msg otherwise pfodApp will disconnect. } } // now send data if 1sec has passed if ((thisMillis - dataSampleTimer) > SAMPLE_INTERVAL) { dataSampleTimer += SAMPLE_INTERVAL; // update for next time counter++; // increment sample // send it parser.println(counter); } } void closeConnection(Stream * io) { // add any special code here to force connection to be dropped } //========================================================================= /* You can remove from here on if you have the pfodParser library installed * and add #include #include * at the top of this file * and replace the line pfodParser_codeGenerated parser; // create a parser to handle the pfod messages * with pfodParser parser; */ // this is the pfodParser.cpp file with the class renamed pfodParser_codeGenerated and with comments, constants and un-used methods removed pfodParser_codeGenerated::pfodParser_codeGenerated() { io = NULL; init(); } void pfodParser_codeGenerated::init() { argsCount = 0; argsIdx = 0; args[0] = 0; args[1] = 0; parserState = ((byte)0xff); } void pfodParser_codeGenerated::connect(Stream * ioPtr) { init(); io = ioPtr; } void pfodParser_codeGenerated::closeConnection() { init(); } Stream* pfodParser_codeGenerated::getPfodAppStream() { return io; } size_t pfodParser_codeGenerated::write(uint8_t c) { if (!io) { return 1; // cannot write if io null but just pretend to } return io->write(c); } void pfodParser_codeGenerated::flush() { if (!io) { return ; // cannot write if io null but just pretend to } io->flush(); } byte* pfodParser_codeGenerated::getCmd() { return args; } byte* pfodParser_codeGenerated::getFirstArg() { byte* idxPtr = args; while ( *idxPtr != 0) { ++idxPtr; } if (argsCount > 0) { ++idxPtr; } return idxPtr; } byte pfodParser_codeGenerated::getArgsCount() { return argsCount; } byte pfodParser_codeGenerated::parse() { byte rtn = 0; if (!io) { return rtn; } while (io->available()) { int in = io->read(); rtn = parse((byte)in); if (rtn != 0) { // found msg if (rtn == '!') { closeConnection(); } return rtn; } } return rtn; } byte pfodParser_codeGenerated::parse(byte in) { if ((parserState == ((byte)0xff)) || (parserState == ((byte)'}'))) { parserState = ((byte)0xff); if (in == ((byte)'{')) { init(); parserState = ((byte)'{'); } return 0; } if ((argsIdx >= (255 - 2)) && (in != ((byte)'}'))) { init(); return 0; } if (parserState == ((byte)'{')) { parserState = ((byte)0); } if ((in == ((byte)'}')) || (in == ((byte)'|')) || (in == ((byte)'~')) || (in == ((byte)'`'))) { args[argsIdx++] = 0; if (parserState == ((byte)0xfe)) { argsCount++; } if (in == ((byte)'}')) { parserState = ((byte)'}'); // reset state return args[0]; } else { parserState = ((byte)0xfe); } return 0; } args[argsIdx++] = in; return 0; } byte* pfodParser_codeGenerated::parseLong(byte * idxPtr, long * result) { long rtn = 0; boolean neg = false; while ( *idxPtr != 0) { if (*idxPtr == '-') { neg = true; } else { rtn = (rtn << 3) + (rtn << 1); rtn = rtn + (*idxPtr - '0'); } ++idxPtr; } if (neg) { rtn = -rtn; } *result = rtn; return ++idxPtr; } // ============= end generated code =========