diff --git a/CVReader.ino b/CVReader.ino index b64ef00..89e6b46 100644 --- a/CVReader.ino +++ b/CVReader.ino @@ -58,7 +58,7 @@ void loop() { } else if (ch == '>') { buffer[bufferLength]='\0'; - JMRIParser::parse(buffer); + JMRIParser::parse(Serial, buffer); inCommandPayload = false; } else if(inCommandPayload) { buffer[bufferLength++]= ch; diff --git a/DIAG.cpp b/DIAG.cpp deleted file mode 100644 index 580010f..0000000 --- a/DIAG.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -void Serialprint(const __FlashStringHelper* input...) { - // thanks to Jan Turoň https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output - - va_list args; - va_start(args, input); - char* flash=(char*)input; - for(int i=0; ; ++i) { - char c=pgm_read_byte_near(flash+i); - if (c=='\0') return; - if(c!='%') { Serial.print(c); continue; } - i++; - c=pgm_read_byte_near(flash+i); - switch(c) { - case '%': Serial.print('%'); break; - case 's': Serial.print(va_arg(args, char*)); break; - case 'd': Serial.print(va_arg(args, int), DEC); break; - case 'b': Serial.print(va_arg(args, int), BIN); break; - case 'o': Serial.print(va_arg(args, int), OCT); break; - case 'x': Serial.print(va_arg(args, int), HEX); break; - case 'f': Serial.print(va_arg(args, double), 2); break; - } - } - va_end(args); -} diff --git a/DIAG.h b/DIAG.h index 8ac3fdd..76526a5 100644 --- a/DIAG.h +++ b/DIAG.h @@ -1,5 +1,5 @@ -void Serialprint(const __FlashStringHelper* input...); +#include "StringParser.h" #ifndef DIAG_ENABLED #define DIAG_ENABLED true #endif -#define DIAG if (DIAG_ENABLED) Serialprint +#define DIAG if (DIAG_ENABLED) StringParser::print diff --git a/JMRIParser.cpp b/JMRIParser.cpp index 1ab6704..e78d2b6 100644 --- a/JMRIParser.cpp +++ b/JMRIParser.cpp @@ -2,7 +2,7 @@ #include "JMRIParser.h" #include "DCC.h" #include "DCCWaveform.h" -#include "DIAG.h" + // This is a JMRI command parser // It doesnt know how the string got here, nor how it gets back. // It knows nothing about hardware or tracks... it just parses strings and @@ -10,10 +10,10 @@ // int JMRIParser::p[MAX_PARAMS]; - + // See documentation on DCC class for info on this section -void JMRIParser::parse(const char *com) { - DIAG(F("\nParsing %s\n"),com); +void JMRIParser::parse(Stream & stream,const char *com) { + StringParser::send(stream,F("\nParsing %s\n"),com); bool result; int params=StringParser::parse(com+1,p,MAX_PARAMS); @@ -24,7 +24,7 @@ void JMRIParser::parse(const char *com) { case 't': // DCC::setThrottle(p[1],p[2],p[3]); - DIAG(F(""), p[0], p[2],p[3]); + StringParser::send(stream,F(""), p[0], p[2],p[3]); break; /***** OPERATE ENGINE DECODER FUNCTIONS F0-F28 ****/ @@ -233,7 +233,7 @@ void JMRIParser::parse(const char *com) { */ result=DCC::writeCVByte(p[0],p[1]); - DIAG(F(""), p[2], p[3],p[0],result?p[1]:-1); + StringParser::send(stream,F(""), p[2], p[3],p[0],result?p[1]:-1); break; /***** WRITE CONFIGURATION VARIABLE BIT TO ENGINE DECODER ON PROGRAMMING TRACK ****/ @@ -253,7 +253,7 @@ void JMRIParser::parse(const char *com) { */ result=DCC::writeCVBit(p[0],p[1],p[2]); - DIAG(F(""), p[3],p[4], p[0],p[1],result?p[2]:-1); + StringParser::send(stream,F(""), p[3],p[4], p[0],p[1],result?p[2]:-1); break; /***** READ CONFIGURATION VARIABLE BYTE FROM ENGINE DECODER ON PROGRAMMING TRACK ****/ @@ -270,7 +270,7 @@ void JMRIParser::parse(const char *com) { * where VALUE is a number from 0-255 as read from the requested CV, or -1 if read could not be verified */ - DIAG(F(""),p[1],p[2],p[0],DCC::readCV(p[0])); + StringParser::send(stream,F(""),p[1],p[2],p[0],DCC::readCV(p[0])); break; /***** TURN ON POWER FROM MOTOR SHIELD TO TRACKS ****/ @@ -283,7 +283,7 @@ void JMRIParser::parse(const char *com) { */ DCCWaveform::mainTrack.setPowerMode(POWERMODE::ON); DCCWaveform::progTrack.setPowerMode(POWERMODE::ON); - DIAG(F("")); + StringParser::send(stream,F("")); break; /***** TURN OFF POWER FROM MOTOR SHIELD TO TRACKS ****/ @@ -296,7 +296,7 @@ void JMRIParser::parse(const char *com) { */ DCCWaveform::mainTrack.setPowerMode(POWERMODE::OFF); DCCWaveform::progTrack.setPowerMode(POWERMODE::OFF); - DIAG(F("")); + StringParser::send(stream,F("")); break; #ifdef THIS_IS_NOT_YET_COMPLETE @@ -309,7 +309,7 @@ void JMRIParser::parse(const char *com) { * returns: * where CURRENT = 0-1024, based on exponentially-smoothed weighting scheme */ - DIAG(F(""), DCCWaveform:mainTrack->getLastRead()); + StringParser::send(stream,F(""), DCCWaveform:mainTrack->getLastRead()); break; /***** READ STATUS OF DCC++ BASE STATION ****/ @@ -360,15 +360,15 @@ void JMRIParser::parse(const char *com) { CommManager::printf(""); break; #endif -/***** PRINT CARRIAGE RETURN IN SERIAL MONITOR WINDOW ****/ +/***** PRINT CARRIAGE RETURN IN stream MONITOR WINDOW ****/ case ' ': // < > /* - * simply prints a carriage return - useful when interacting with Ardiuno through serial monitor window + * simply prints a carriage return - useful when interacting with Ardiuno through stream monitor window * * returns: a carriage return */ - DIAG(F("\n")); + StringParser::send(stream,F("\n")); break; } } diff --git a/JMRIParser.h b/JMRIParser.h index 53ce0e0..b1b7dc0 100644 --- a/JMRIParser.h +++ b/JMRIParser.h @@ -1,7 +1,7 @@ struct JMRIParser { - static void parse(const char * command); + static void parse(Stream & stream,const char * command); private: static const int MAX_PARAMS=10; diff --git a/StringParser.cpp b/StringParser.cpp index e6c9d3d..62881ac 100644 --- a/StringParser.cpp +++ b/StringParser.cpp @@ -1,4 +1,5 @@ #include "StringParser.h" +#include int StringParser::parse(const char * com, int result[], byte maxResults) { byte state=1; @@ -42,3 +43,42 @@ int StringParser::parse(const char * com, int result[], byte maxResults) { } return parameterCount; } + + + +void StringParser::print( const __FlashStringHelper* input...) { + va_list args; + va_start(args, input); + send(Serial,input,args); +} + +void StringParser::send(Stream & stream, const __FlashStringHelper* input...) { + va_list args; + va_start(args, input); + send(stream,input,args); +} + +void StringParser::send(Stream & stream,const __FlashStringHelper* format, va_list args) { + + + // thanks to Jan Turoň https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output + + char* flash=(char*)format; + for(int i=0; ; ++i) { + char c=pgm_read_byte_near(flash+i); + if (c=='\0') return; + if(c!='%') { stream.print(c); continue; } + i++; + c=pgm_read_byte_near(flash+i); + switch(c) { + case '%': stream.print('%'); break; + case 's': stream.print(va_arg(args, char*)); break; + case 'd': stream.print(va_arg(args, int), DEC); break; + case 'b': stream.print(va_arg(args, int), BIN); break; + case 'o': stream.print(va_arg(args, int), OCT); break; + case 'x': stream.print(va_arg(args, int), HEX); break; + case 'f': stream.print(va_arg(args, double), 2); break; + } + } + va_end(args); +} diff --git a/StringParser.h b/StringParser.h index 0286c4c..67c9cde 100644 --- a/StringParser.h +++ b/StringParser.h @@ -3,4 +3,9 @@ class StringParser { public: static int parse(const char * com, int result[], byte maxResults); + static void print( const __FlashStringHelper* input...); + static void send(Stream & serial, const __FlashStringHelper* input...); + private: + static void send(Stream & serial, const __FlashStringHelper* input,va_list args); + };