mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-26 17:46:14 +01:00
e0c76a9dc4
In prep for Wifi siolution, all output functions changed to expect Print class instead of Stream... Can still pass Serial1 etc because Stream extends Print, but this allows for an output-only class extending Print to collect a response buffer for Wifi sending with AT commands.
41 lines
1.3 KiB
C++
41 lines
1.3 KiB
C++
#include "StringFormatter.h"
|
|
#include <stdarg.h>
|
|
|
|
|
|
void StringFormatter::print( const __FlashStringHelper* input...) {
|
|
va_list args;
|
|
va_start(args, input);
|
|
send(Serial,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) {
|
|
|
|
// 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;
|
|
if(c!='%') { stream.print(c); continue; }
|
|
i++;
|
|
c=pgm_read_byte_near(flash+i);
|
|
switch(c) {
|
|
case '%': stream.print('%'); break;
|
|
case 'c': stream.print((char) va_arg(args, int)); 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 'f': stream.print(va_arg(args, double), 2); break;
|
|
}
|
|
}
|
|
va_end(args);
|
|
}
|