diff --git a/CVReader.ino b/CVReader.ino index 9b2480d..4314d79 100644 --- a/CVReader.ino +++ b/CVReader.ino @@ -1,6 +1,7 @@ #include "DCC.h" #include "DIAG.h" #include "DCCEXParser.h" +#include "WifiInterface.h" /* this code is here to test the waveform generator and reveal the issues involved in programming track operations. @@ -17,23 +18,27 @@ void myCallback(int result) { DIAG(F("\n getting Loco Id callback result=%d"),result); } -DCCEXParser serialParser(Serial);; -DCCEXParser wifiParser(Serial1); +DCCEXParser serialParser; +DCCEXParser wifiParser; void setup() { Serial.begin(115200); - DCC::begin(); - + DCC::begin(); + // if (WIFI_PORT>0) WifiInterface::setup(); DIAG(F("\n===== CVReader demonstrating DCC::getLocoId() call ==========\n")); DCC::getLocoId(myCallback); // myCallback will be called with the result DIAG(F("\n===== DCC::getLocoId has returned, but wont be executed until we are in loop() ======\n")); DIAG(F("\nReady for JMRI commands\n")); } -void loop() { - DCC::loop(); // required to keep locos running and check powwer +void loop() { + DCC::loop(); // required to keep locos running and check powwer // This line passes input on Serial to the DCCEXParser - serialParser.loop(); - wifiParser.loop(); + serialParser.loop(Serial); + + if (WIFI_PORT>0) { + // wifiParser=WifiInterface::getSingleClient(wifiParser); + // if (wifiParser) wifiParser->loop(); + } } diff --git a/Config.h b/Config.h index bfa4225..2e35479 100644 --- a/Config.h +++ b/Config.h @@ -1,6 +1,13 @@ #ifndef Config_h #define Config_h +const int WIFI_PORT = 0; // OR set to zero for no wifi +const char WIFI_SSID[] = "BTHub5-M6PT"; // your network SSID (name) +const char WIFI_PASS[] = "49de8d4862"; // your network password +const long WIFI_BAUD_RATE=115200; +const long WIFI_CONNECT_RETRIES=10; + + // This hardware configuration would normally be setup using a bunch of #ifdefs. const byte MAIN_POWER_PIN = 3; diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 41c57f3..a0f7000 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -7,17 +7,14 @@ #include "Sensors.h" #include "EEStore.h" +#include "DIAG.h" const char VERSION[]="99.666"; int DCCEXParser::stashP[MAX_PARAMS]; - bool DCCEXParser::stashBusy; - Stream & DCCEXParser::stashStream=Serial; // keep compiler happy but ovevride in constructor - - -DCCEXParser::DCCEXParser(Stream & myStream) { - stream=myStream; -} +bool DCCEXParser::stashBusy; + + Stream & DCCEXParser::stashStream=Serial; // keep compiler happy but ovevride in constructor // This is a JMRI command parser, one instance per incoming stream // It doesnt know how the string got here, nor how it gets back. @@ -25,7 +22,10 @@ DCCEXParser::DCCEXParser(Stream & myStream) { // calls the corresponding DCC api. // Non-DCC things like turnouts, pins and sensors are handled in additional JMRI interface classes. -void DCCEXParser::loop() { +DCCEXParser::DCCEXParser() {} + +void DCCEXParser::loop(Stream & stream) { + //DIAG(F("\nDCCEXParser Loop in %d "),stream.available()); while(stream.available()) { if (bufferLength==MAX_BUFFER) { bufferLength=0; @@ -39,13 +39,14 @@ void DCCEXParser::loop() { } else if (ch == '>') { buffer[bufferLength]='\0'; - parse(buffer); + parse( stream, buffer); inCommandPayload = false; break; } else if(inCommandPayload) { buffer[bufferLength++]= ch; } } +//DIAG(F(" out\n")); } int DCCEXParser::splitValues( int result[MAX_PARAMS]) { @@ -92,7 +93,7 @@ void DCCEXParser::loop() { } // See documentation on DCC class for info on this section -void DCCEXParser::parse(const char *com) { +void DCCEXParser::parse(Stream & stream, const char *com) { (void) EEPROM; // tell compiler not to warn thi is unused int p[MAX_PARAMS]; int params=splitValues(p); @@ -117,15 +118,15 @@ void DCCEXParser::parse(const char *com) { return; case 'T': // TURNOUT - if (parseT(params,p)) return; + if (parseT(stream,params,p)) return; break; case 'Z': // OUTPUT - if (parseZ(params,p)) return; + if (parseZ(stream,params,p)) return; break; case 'S': // SENSOR - if (parseS(params,p)) return; + if (parseS(stream,params,p)) return; break; case 'w': // WRITE CV on MAIN @@ -201,7 +202,7 @@ void DCCEXParser::parse(const char *com) { StringFormatter::send(stream, F("")); } -bool DCCEXParser::parseZ( int params, int p[]){ +bool DCCEXParser::parseZ( Stream & stream,int params, int p[]){ switch (params) { @@ -232,7 +233,7 @@ bool DCCEXParser::parseZ( int params, int p[]){ //=================================== -bool DCCEXParser::parseT( int params, int p[]) { +bool DCCEXParser::parseT(Stream & stream, int params, int p[]) { switch(params){ case 0: // return (Turnout::showAll(stream)); break; @@ -257,7 +258,7 @@ bool DCCEXParser::parseT( int params, int p[]) { } } -bool DCCEXParser::parseS( int params, int p[]) { +bool DCCEXParser::parseS( Stream & stream,int params, int p[]) { switch(params){ @@ -281,7 +282,7 @@ bool DCCEXParser::parseS( int params, int p[]) { // CALLBACKS must be static -bool DCCEXParser::stashCallback(Stream & stream, int p[MAX_PARAMS]) { +bool DCCEXParser::stashCallback(Stream & stream,int p[MAX_PARAMS]) { if (stashBusy) return false; stashBusy=true; stashStream=stream; diff --git a/DCCEXParser.h b/DCCEXParser.h index a038d98..d072826 100644 --- a/DCCEXParser.h +++ b/DCCEXParser.h @@ -2,23 +2,22 @@ #define DCCEXParser_h struct DCCEXParser { - DCCEXParser(Stream & myStream); - void loop(); + DCCEXParser(); + void loop(Stream & pstream); private: static const int MAX_PARAMS=10; // longest command sent in static const int MAX_BUFFER=50; // longest command sent in - - Stream & stream; + Stream & stream; byte bufferLength=0; bool inCommandPayload=false; - char buffer[MAX_BUFFER]; - void parse(const char * command); - int splitValues( int result[MAX_PARAMS]); - - bool parseT(int params, int p[]); - bool parseZ(int params, int p[]); - bool parseS( int params, int p[]); + char buffer[MAX_BUFFER+2]; + int splitValues( int result[MAX_PARAMS]); + void parse(Stream & stream, const char * command); + + bool parseT(Stream & stream, int params, int p[]); + bool parseZ(Stream & stream, int params, int p[]); + bool parseS(Stream & stream, int params, int p[]); static bool stashBusy; diff --git a/StringFormatter.cpp b/StringFormatter.cpp index 188d435..0f6393f 100644 --- a/StringFormatter.cpp +++ b/StringFormatter.cpp @@ -26,13 +26,12 @@ void StringFormatter::send(Stream & stream,const __FlashStringHelper* format, va i++; c=pgm_read_byte_near(flash+i); switch(c) { - case '%': stream.write('%'); break; + 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 'c': stream.write(va_arg(args, int)); break; case 'f': stream.print(va_arg(args, double), 2); break; } } diff --git a/WifiInterface.cpp b/WifiInterface.cpp new file mode 100644 index 0000000..5849931 --- /dev/null +++ b/WifiInterface.cpp @@ -0,0 +1,49 @@ +// +//#include "WifiInterface.h" +//#include "Config.h" +//#include "DIAG.h" +// +// +//WiFiEspServer WifiInterface::server(WIFI_PORT); +// +//bool WifiInterface::connected=false; +// +//void WifiInterface::setup() +//{ +// Serial1.begin(WIFI_BAUD_RATE); // initialize serial for ESP module +// WiFi.init(&Serial1); // initialize ESP module +// +// // check for the presence of the shield +// if (WiFi.status() == WL_NO_SHIELD) { +// Serial.println("WiFi shield not present"); +// return; +// } +// +// // attempt to connect to WiFi network +// int status = WL_IDLE_STATUS; +// for (int retries=0;status != WL_CONNECTED && retries +//#include "DCCEXParser.h" +// +//class WifiInterface { +// +// public: +// static void setup(); +// static DCCEXParser * getSingleClient(DCCEXParser * existing); +// +// private: +// static WiFiEspServer server; +// static bool connected; +//}; +// +//#endif +//