2020-06-10 18:31:26 +02:00
|
|
|
#include "StringFormatter.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
|
|
|
|
void StringFormatter::print( const __FlashStringHelper* input...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, input);
|
2020-06-28 13:45:31 +02:00
|
|
|
send2(Serial,input,args);
|
2020-06-10 18:31:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 15:28:35 +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-06-28 13:45:31 +02:00
|
|
|
send2(stream,input,args);
|
2020-06-10 18:31:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-28 13:45:31 +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;
|
|
|
|
if(c!='%') { stream.print(c); continue; }
|
|
|
|
i++;
|
|
|
|
c=pgm_read_byte_near(flash+i);
|
|
|
|
switch(c) {
|
2020-06-11 14:35:16 +02:00
|
|
|
case '%': stream.print('%'); break;
|
2020-06-12 15:28:35 +02:00
|
|
|
case 'c': stream.print((char) va_arg(args, int)); break;
|
2020-06-10 18:31:26 +02:00
|
|
|
case 's': stream.print(va_arg(args, char*)); break;
|
2020-06-13 16:53:46 +02:00
|
|
|
case 'S': stream.print((const __FlashStringHelper*)va_arg(args, char*)); break;
|
2020-06-10 18:31:26 +02:00
|
|
|
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);
|
|
|
|
}
|