1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-22 23:56:13 +01:00

Cleaned DIAG interface

All DIAGS can be switched off by StringFormatter::diagSerial=NULL;
This commit is contained in:
Asbelos 2020-09-06 20:23:26 +01:00
parent 4e0c227012
commit bc53b3e599
5 changed files with 59 additions and 37 deletions

7
DCCEX.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef DCCEX_h
#define DCCEX_h
#include "DCC.h"
#include "DIAG.h"
#include "DCCEXParser.h"
#include "WifiInterface.h"
#endif

3
DIAG.h
View File

@ -18,7 +18,6 @@
*/
#ifndef DIAG_h
#define DIAG_h
#include "StringFormatter.h"
#define DIAG StringFormatter::print
#define DIAG StringFormatter::diag
#endif

View File

@ -19,25 +19,40 @@
#include "StringFormatter.h"
#include <stdarg.h>
void StringFormatter::print( const __FlashStringHelper* input...) {
#if defined(ARDUINO_ARCH_SAMD)
// Some processors use a gcc compiler that renames va_list!!!
#include <cstdarg>
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;

View File

@ -23,24 +23,27 @@
#if defined(ARDUINO_ARCH_SAMD)
// Some processors use a gcc compiler that renames va_list!!!
#include <cstdarg>
#define DIAGSERIAL SerialUSB
#elif defined(ARDUINO_ARCH_AVR)
#define DIAGSERIAL Serial
#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

View File

@ -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