2020-07-03 18:35:02 +02:00
|
|
|
/*
|
|
|
|
* © 2020, Chris Harlow. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Asbelos DCC API
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* It is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-06-10 18:31:26 +02:00
|
|
|
#include "StringFormatter.h"
|
|
|
|
#include <stdarg.h>
|
2020-09-06 21:23:26 +02:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
2020-10-12 20:32:47 +02:00
|
|
|
#include "LCDDisplay.h"
|
|
|
|
|
2020-09-10 14:09:32 +02:00
|
|
|
bool Diag::ACK=false;
|
2020-12-29 03:00:18 +01:00
|
|
|
bool Diag::CMD=true;
|
|
|
|
bool Diag::WIFI=true;
|
2020-09-10 14:09:32 +02:00
|
|
|
bool Diag::WITHROTTLE=false;
|
2020-10-30 14:00:02 +01:00
|
|
|
bool Diag::ETHERNET=false;
|
2020-09-10 14:09:32 +02:00
|
|
|
|
2020-09-06 21:23:26 +02:00
|
|
|
|
|
|
|
void StringFormatter::diag( const __FlashStringHelper* input...) {
|
|
|
|
if (!diagSerial) return;
|
2020-06-10 18:31:26 +02:00
|
|
|
va_list args;
|
|
|
|
va_start(args, input);
|
2020-09-06 21:23:26 +02:00
|
|
|
send2(diagSerial,input,args);
|
2020-06-10 18:31:26 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 20:32:47 +02:00
|
|
|
void StringFormatter::lcd(byte row, const __FlashStringHelper* input...) {
|
2020-10-12 23:18:09 +02:00
|
|
|
va_list args;
|
|
|
|
|
|
|
|
// Issue the LCD as a diag first
|
|
|
|
diag(F("\nLCD%d:"),row);
|
|
|
|
va_start(args, input);
|
|
|
|
send2(diagSerial,input,args);
|
|
|
|
diag(F("\n"));
|
|
|
|
|
2020-10-12 20:32:47 +02:00
|
|
|
if (!LCDDisplay::lcdDisplay) return;
|
|
|
|
LCDDisplay::lcdDisplay->setRow(row);
|
|
|
|
va_start(args, input);
|
|
|
|
send2(LCDDisplay::lcdDisplay,input,args);
|
|
|
|
}
|
|
|
|
|
2020-09-06 21:23:26 +02:00
|
|
|
void StringFormatter::send(Print * stream, const __FlashStringHelper* input...) {
|
2020-06-10 18:31:26 +02:00
|
|
|
va_list args;
|
|
|
|
va_start(args, input);
|
2020-09-06 21:23:26 +02:00
|
|
|
send2(stream,input,args);
|
2020-06-10 18:31:26 +02:00
|
|
|
}
|
2020-09-06 21:23:26 +02:00
|
|
|
|
|
|
|
void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) {
|
2020-08-01 17:32:16 +02:00
|
|
|
va_list args;
|
|
|
|
va_start(args, input);
|
2020-09-06 21:23:26 +02:00
|
|
|
send2(&stream,input,args);
|
2020-08-01 17:32:16 +02:00
|
|
|
}
|
|
|
|
|
2020-09-06 21:23:26 +02:00
|
|
|
void StringFormatter::send2(Print * stream,const __FlashStringHelper* format, va_list args) {
|
2020-06-10 18:31:26 +02:00
|
|
|
|
|
|
|
// 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;
|
2020-08-01 17:32:16 +02:00
|
|
|
if(c!='%') { stream->print(c); continue; }
|
2020-10-12 20:32:47 +02:00
|
|
|
|
|
|
|
bool formatContinues=false;
|
|
|
|
byte formatWidth=0;
|
|
|
|
bool formatLeft=false;
|
|
|
|
do {
|
|
|
|
|
|
|
|
formatContinues=false;
|
2020-06-10 18:31:26 +02:00
|
|
|
i++;
|
|
|
|
c=pgm_read_byte_near(flash+i);
|
|
|
|
switch(c) {
|
2020-08-01 17:32:16 +02:00
|
|
|
case '%': stream->print('%'); break;
|
|
|
|
case 'c': stream->print((char) va_arg(args, int)); break;
|
|
|
|
case 's': stream->print(va_arg(args, char*)); break;
|
2020-07-03 18:35:02 +02:00
|
|
|
case 'e': printEscapes(stream,va_arg(args, char*)); break;
|
2020-10-12 20:32:47 +02:00
|
|
|
case 'E': printEscapes(stream,(const __FlashStringHelper*)va_arg(args, char*)); break;
|
2020-08-01 17:32:16 +02:00
|
|
|
case 'S': stream->print((const __FlashStringHelper*)va_arg(args, char*)); break;
|
2020-10-12 20:32:47 +02:00
|
|
|
case 'd': printPadded(stream,va_arg(args, int), formatWidth, formatLeft); break;
|
|
|
|
case 'l': printPadded(stream,va_arg(args, long), formatWidth, formatLeft); break;
|
2020-08-01 17:32:16 +02:00
|
|
|
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;
|
2020-10-12 20:32:47 +02:00
|
|
|
//format width prefix
|
|
|
|
case '-':
|
|
|
|
formatLeft=true;
|
|
|
|
formatContinues=true;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9':
|
|
|
|
formatWidth=formatWidth * 10 + (c-'0');
|
|
|
|
formatContinues=true;
|
|
|
|
break;
|
2020-06-10 18:31:26 +02:00
|
|
|
}
|
2020-10-12 20:32:47 +02:00
|
|
|
} while(formatContinues);
|
2020-06-10 18:31:26 +02:00
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
}
|
2020-07-03 18:35:02 +02:00
|
|
|
|
2020-09-06 21:23:26 +02:00
|
|
|
void StringFormatter::printEscapes(Print * stream,char * input) {
|
|
|
|
if (!stream) return;
|
2020-07-03 18:35:02 +02:00
|
|
|
for(int i=0; ; ++i) {
|
|
|
|
char c=input[i];
|
|
|
|
printEscape(stream,c);
|
|
|
|
if (c=='\0') return;
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 21:23:26 +02:00
|
|
|
|
2020-10-12 20:32:47 +02:00
|
|
|
void StringFormatter::printEscapes(Print * stream, const __FlashStringHelper * input) {
|
|
|
|
|
|
|
|
if (!stream) return;
|
|
|
|
char* flash=(char*)input;
|
|
|
|
for(int i=0; ; ++i) {
|
|
|
|
char c=pgm_read_byte_near(flash+i);
|
|
|
|
printEscape(stream,c);
|
|
|
|
if (c=='\0') return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-06 21:23:26 +02:00
|
|
|
void StringFormatter::printEscape( char c) {
|
|
|
|
printEscape(diagSerial,c);
|
2020-07-03 18:35:02 +02:00
|
|
|
}
|
2020-09-06 21:23:26 +02:00
|
|
|
|
2020-08-01 17:32:16 +02:00
|
|
|
void StringFormatter::printEscape(Print * stream, char c) {
|
2020-09-06 21:23:26 +02:00
|
|
|
if (!stream) return;
|
2020-07-03 18:35:02 +02:00
|
|
|
switch(c) {
|
2020-08-01 17:32:16 +02:00
|
|
|
case '\n': stream->print(F("\\n")); break;
|
|
|
|
case '\r': stream->print(F("\\r")); break;
|
|
|
|
case '\0': stream->print(F("\\0")); return;
|
|
|
|
case '\t': stream->print(F("\\t")); break;
|
|
|
|
case '\\': stream->print(F("\\")); break;
|
|
|
|
default: stream->print(c);
|
2020-07-03 18:35:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-12 20:32:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
void StringFormatter::printPadded(Print* stream, long value, byte width, bool formatLeft) {
|
|
|
|
if (width==0) {
|
|
|
|
stream->print(value, DEC);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int digits=(value <= 0)? 1: 0; // zero and negative need extra digot
|
|
|
|
long v=value;
|
|
|
|
while (v) {
|
|
|
|
v /= 10;
|
|
|
|
digits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (formatLeft) stream->print(value, DEC);
|
|
|
|
while(digits<width) {
|
|
|
|
stream->print(' ');
|
|
|
|
digits++;
|
|
|
|
}
|
|
|
|
if (!formatLeft) stream->print(value, DEC);
|
|
|
|
}
|
|
|
|
|
2020-07-03 18:35:02 +02:00
|
|
|
|