From bc53b3e5999564fc2386b912e2ead1a63c7c8501 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Sun, 6 Sep 2020 20:23:26 +0100 Subject: [PATCH] Cleaned DIAG interface All DIAGS can be switched off by StringFormatter::diagSerial=NULL; --- DCCEX.h | 7 +++++++ DIAG.h | 3 +-- StringFormatter.cpp | 51 ++++++++++++++++++++++++++++----------------- StringFormatter.h | 27 +++++++++++++----------- WifiInterface.cpp | 8 +++---- 5 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 DCCEX.h diff --git a/DCCEX.h b/DCCEX.h new file mode 100644 index 0000000..e94b193 --- /dev/null +++ b/DCCEX.h @@ -0,0 +1,7 @@ +#ifndef DCCEX_h +#define DCCEX_h +#include "DCC.h" +#include "DIAG.h" +#include "DCCEXParser.h" +#include "WifiInterface.h" +#endif diff --git a/DIAG.h b/DIAG.h index a00217c..e0d4383 100644 --- a/DIAG.h +++ b/DIAG.h @@ -18,7 +18,6 @@ */ #ifndef DIAG_h #define DIAG_h - #include "StringFormatter.h" -#define DIAG StringFormatter::print +#define DIAG StringFormatter::diag #endif diff --git a/StringFormatter.cpp b/StringFormatter.cpp index 3c4cbf1..89fd8cc 100644 --- a/StringFormatter.cpp +++ b/StringFormatter.cpp @@ -18,26 +18,41 @@ */ #include "StringFormatter.h" #include - -void StringFormatter::print( const __FlashStringHelper* input...) { + +#if defined(ARDUINO_ARCH_SAMD) + // Some processors use a gcc compiler that renames va_list!!! + #include + Print * StringFormatter::diagSerial= &SerialUSB; + +#elif defined(ARDUINO_ARCH_AVR) + Print * StringFormatter::diagSerial= &Serial; +#elif defined(ARDUINO_ARCH_MEGAAVR) + Print * StringFormatter::diagSerial=&Serial; + #define __FlashStringHelper char +#endif + + +void StringFormatter::diag( const __FlashStringHelper* input...) { + if (!diagSerial) return; va_list args; va_start(args, input); - send(& DIAGSERIAL,input,args); + send2(diagSerial,input,args); +} + +void StringFormatter::send(Print * stream, const __FlashStringHelper* input...) { + va_list args; + va_start(args, input); + send2(stream,input,args); } void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) { va_list args; va_start(args, input); - send(& stream,input,args); -} -void StringFormatter::send(Print * stream, const __FlashStringHelper* input...) { - va_list args; - va_start(args, input); - send(stream,input,args); + send2(&stream,input,args); } -void StringFormatter::send(Print * stream,const __FlashStringHelper* format, va_list args) { +void StringFormatter::send2(Print * stream,const __FlashStringHelper* format, va_list args) { // thanks to Jan TuroĊˆ https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output @@ -53,7 +68,6 @@ void StringFormatter::send(Print * stream,const __FlashStringHelper* format, va_ case 'c': stream->print((char) va_arg(args, int)); break; case 's': stream->print(va_arg(args, char*)); break; case 'e': printEscapes(stream,va_arg(args, char*)); break; - case 'E': printEscapes(stream,(const __FlashStringHelper*)va_arg(args, char*)); break; case 'S': stream->print((const __FlashStringHelper*)va_arg(args, char*)); break; case 'd': stream->print(va_arg(args, int), DEC); break; case 'l': stream->print(va_arg(args, long), DEC); break; @@ -66,22 +80,21 @@ void StringFormatter::send(Print * stream,const __FlashStringHelper* format, va_ va_end(args); } -void StringFormatter::printEscapes(Print * stream, char * input) { +void StringFormatter::printEscapes(Print * stream,char * input) { + if (!stream) return; for(int i=0; ; ++i) { char c=input[i]; printEscape(stream,c); if (c=='\0') return; } } -void StringFormatter::printEscapes(Print * stream, const __FlashStringHelper* input) { - char* flash=(char*)input; - for(int i=0; ; ++i) { - char c=pgm_read_byte_near(flash+i); - printEscape(stream,c); - if (c=='\0') return; - } + +void StringFormatter::printEscape( char c) { + printEscape(diagSerial,c); } + void StringFormatter::printEscape(Print * stream, char c) { + if (!stream) return; switch(c) { case '\n': stream->print(F("\\n")); break; case '\r': stream->print(F("\\r")); break; diff --git a/StringFormatter.h b/StringFormatter.h index f750eec..3d43ebf 100644 --- a/StringFormatter.h +++ b/StringFormatter.h @@ -22,25 +22,28 @@ #if defined(ARDUINO_ARCH_SAMD) // Some processors use a gcc compiler that renames va_list!!! - #include - #define DIAGSERIAL SerialUSB -#elif defined(ARDUINO_ARCH_AVR) - #define DIAGSERIAL Serial + #include #elif defined(ARDUINO_ARCH_MEGAAVR) - #define DIAGSERIAL Serial #define __FlashStringHelper char #endif class StringFormatter { public: - static void print( const __FlashStringHelper* input...); - static void send(Print & serial, const __FlashStringHelper* input...); static void send(Print * serial, const __FlashStringHelper* input...); - static void printEscapes(Print * stream, char * input); - static void printEscapes(Print * stream, const __FlashStringHelper* input); - static void printEscape(Print * stream, char c); - static void send(Print * serial, const __FlashStringHelper* input,va_list args); - + static void send(Print & serial, const __FlashStringHelper* input...); + + static void printEscapes(Print * serial,char * input); + static void printEscape(Print * serial, char c); + + // DIAG support + static Print * diagSerial; + static void diag( const __FlashStringHelper* input...); + static void printEscapes(char * input); + static void printEscape( char c); + + private: + static void send2(Print * serial, const __FlashStringHelper* input,va_list args); + }; #endif diff --git a/WifiInterface.cpp b/WifiInterface.cpp index a31c7ed..c778510 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -89,7 +89,7 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH for (int i=0; i<17;i++) { while(!wifiStream->available()); macAddress[i]=wifiStream->read(); - StringFormatter::printEscape(&DIAGSERIAL,macAddress[i]); + StringFormatter::printEscape(macAddress[i]); } } char macTail[]={macAddress[9],macAddress[10],macAddress[12],macAddress[13],macAddress[15],macAddress[16],'\0'}; @@ -105,7 +105,7 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH // Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n")); if (checkForOK(2000, OK_SEARCH, true)) { - while (wifiStream->available()) StringFormatter::printEscape(&DIAGSERIAL, wifiStream->read()); /// THIS IS A DIAG IN DISGUISE + while (wifiStream->available()) StringFormatter::printEscape( wifiStream->read()); /// THIS IS A DIAG IN DISGUISE // AT command early version supports CWJAP/CWSAP if (SSid) { @@ -183,7 +183,7 @@ bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor while (wifiStream->available()) { int ch = wifiStream->read(); if (echo) { - if (escapeEcho) StringFormatter::printEscape(&DIAGSERIAL, ch); /// THIS IS A DIAG IN DISGUISE + if (escapeEcho) StringFormatter::printEscape( ch); /// THIS IS A DIAG IN DISGUISE else DIAG(F("%c"), ch); } if (ch != pgm_read_byte_near(locator)) locator = waitfor; @@ -231,7 +231,7 @@ void WifiInterface::loop() { int ch = wifiStream->read(); // echo the char to the diagnostic stream in escaped format - StringFormatter::printEscape(&DIAGSERIAL,ch); // DIAG in disguise + StringFormatter::printEscape(ch); // DIAG in disguise switch (loopstate) { case 0: // looking for +IPD