mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2025-04-19 03:40:12 +02:00
Cleaned DIAG interface
All DIAGS can be switched off by StringFormatter::diagSerial=NULL;
This commit is contained in:
parent
4e0c227012
commit
bc53b3e599
7
DCCEX.h
Normal file
7
DCCEX.h
Normal 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
3
DIAG.h
@ -18,7 +18,6 @@
|
|||||||
*/
|
*/
|
||||||
#ifndef DIAG_h
|
#ifndef DIAG_h
|
||||||
#define DIAG_h
|
#define DIAG_h
|
||||||
|
|
||||||
#include "StringFormatter.h"
|
#include "StringFormatter.h"
|
||||||
#define DIAG StringFormatter::print
|
#define DIAG StringFormatter::diag
|
||||||
#endif
|
#endif
|
||||||
|
@ -19,25 +19,40 @@
|
|||||||
#include "StringFormatter.h"
|
#include "StringFormatter.h"
|
||||||
#include <stdarg.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_list args;
|
||||||
va_start(args, input);
|
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...) {
|
void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) {
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, input);
|
va_start(args, input);
|
||||||
send(& stream,input,args);
|
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* 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
|
// 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 'c': stream->print((char) va_arg(args, int)); break;
|
||||||
case 's': stream->print(va_arg(args, char*)); break;
|
case 's': stream->print(va_arg(args, char*)); break;
|
||||||
case 'e': printEscapes(stream,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 'S': stream->print((const __FlashStringHelper*)va_arg(args, char*)); break;
|
||||||
case 'd': stream->print(va_arg(args, int), DEC); break;
|
case 'd': stream->print(va_arg(args, int), DEC); break;
|
||||||
case 'l': stream->print(va_arg(args, long), 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);
|
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) {
|
for(int i=0; ; ++i) {
|
||||||
char c=input[i];
|
char c=input[i];
|
||||||
printEscape(stream,c);
|
printEscape(stream,c);
|
||||||
if (c=='\0') return;
|
if (c=='\0') return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void StringFormatter::printEscapes(Print * stream, const __FlashStringHelper* input) {
|
|
||||||
char* flash=(char*)input;
|
void StringFormatter::printEscape( char c) {
|
||||||
for(int i=0; ; ++i) {
|
printEscape(diagSerial,c);
|
||||||
char c=pgm_read_byte_near(flash+i);
|
|
||||||
printEscape(stream,c);
|
|
||||||
if (c=='\0') return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringFormatter::printEscape(Print * stream, char c) {
|
void StringFormatter::printEscape(Print * stream, char c) {
|
||||||
|
if (!stream) return;
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case '\n': stream->print(F("\\n")); break;
|
case '\n': stream->print(F("\\n")); break;
|
||||||
case '\r': stream->print(F("\\r")); break;
|
case '\r': stream->print(F("\\r")); break;
|
||||||
|
@ -23,24 +23,27 @@
|
|||||||
#if defined(ARDUINO_ARCH_SAMD)
|
#if defined(ARDUINO_ARCH_SAMD)
|
||||||
// Some processors use a gcc compiler that renames va_list!!!
|
// Some processors use a gcc compiler that renames va_list!!!
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#define DIAGSERIAL SerialUSB
|
|
||||||
#elif defined(ARDUINO_ARCH_AVR)
|
|
||||||
#define DIAGSERIAL Serial
|
|
||||||
#elif defined(ARDUINO_ARCH_MEGAAVR)
|
#elif defined(ARDUINO_ARCH_MEGAAVR)
|
||||||
#define DIAGSERIAL Serial
|
|
||||||
#define __FlashStringHelper char
|
#define __FlashStringHelper char
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
class StringFormatter
|
class StringFormatter
|
||||||
{
|
{
|
||||||
public:
|
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 send(Print * serial, const __FlashStringHelper* input...);
|
||||||
static void printEscapes(Print * stream, char * input);
|
static void send(Print & serial, const __FlashStringHelper* input...);
|
||||||
static void printEscapes(Print * stream, const __FlashStringHelper* input);
|
|
||||||
static void printEscape(Print * stream, char c);
|
static void printEscapes(Print * serial,char * input);
|
||||||
static void send(Print * serial, const __FlashStringHelper* input,va_list args);
|
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
|
#endif
|
||||||
|
@ -89,7 +89,7 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH
|
|||||||
for (int i=0; i<17;i++) {
|
for (int i=0; i<17;i++) {
|
||||||
while(!wifiStream->available());
|
while(!wifiStream->available());
|
||||||
macAddress[i]=wifiStream->read();
|
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'};
|
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
|
// Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME
|
||||||
StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n"));
|
StringFormatter::send(wifiStream, F("AT+CWJAP?\r\n"));
|
||||||
if (checkForOK(2000, OK_SEARCH, true)) {
|
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
|
// AT command early version supports CWJAP/CWSAP
|
||||||
if (SSid) {
|
if (SSid) {
|
||||||
@ -183,7 +183,7 @@ bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor
|
|||||||
while (wifiStream->available()) {
|
while (wifiStream->available()) {
|
||||||
int ch = wifiStream->read();
|
int ch = wifiStream->read();
|
||||||
if (echo) {
|
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);
|
else DIAG(F("%c"), ch);
|
||||||
}
|
}
|
||||||
if (ch != pgm_read_byte_near(locator)) locator = waitfor;
|
if (ch != pgm_read_byte_near(locator)) locator = waitfor;
|
||||||
@ -231,7 +231,7 @@ void WifiInterface::loop() {
|
|||||||
int ch = wifiStream->read();
|
int ch = wifiStream->read();
|
||||||
|
|
||||||
// echo the char to the diagnostic stream in escaped format
|
// 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) {
|
switch (loopstate) {
|
||||||
case 0: // looking for +IPD
|
case 0: // looking for +IPD
|
||||||
|
Loading…
Reference in New Issue
Block a user