From b1371c21ce69c50ae5cd0fedcf3c1c7cb4bd2c3c Mon Sep 17 00:00:00 2001 From: u48 Date: Wed, 1 Feb 2017 16:11:33 +0300 Subject: [PATCH 1/2] Add "printf()" This "printf" realization not use "sprintf" and buffers in RAM. If it is not used, the compiler does not add any extra code. Exelent for AVR. --- hardware/arduino/avr/cores/arduino/Print.cpp | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/hardware/arduino/avr/cores/arduino/Print.cpp b/hardware/arduino/avr/cores/arduino/Print.cpp index 1e4c99a6552..c877302b710 100644 --- a/hardware/arduino/avr/cores/arduino/Print.cpp +++ b/hardware/arduino/avr/cores/arduino/Print.cpp @@ -264,3 +264,56 @@ size_t Print::printFloat(double number, uint8_t digits) return n; } + +//---------------------------------------- +//printf begin + +#include +#include + +//non class function for callback from stdio.FILE steam. +//in field FILE.udata passed class->this +static int dummy_putchar(char c, FILE *stream ) +{ + Print* pPrint = (Print*)(stream->udata); + if(c=='\n') pPrint->print('\r'); + pPrint->print(c); + return 1; +} + +size_t Print::printf(const char *fmt, ...) +{ + FILE oStream; //size 12 bytes on Stack + oStream.put = dummy_putchar; + oStream.flags |= __SWR; + oStream.udata = (void*) this; + //---- + oStream.flags &= ~__SPGM; + // + va_list ap; + va_start( (ap), (fmt) ); + size_t i = vfprintf(&oStream, fmt, ap); + va_end(ap); + return i; +} + +size_t Print::printf(const __FlashStringHelper *fmt_P, ...) +{ + PGM_P fmt = reinterpret_cast(fmt_P); + + FILE oStream; //size 12 bytes on Stack + oStream.put = dummy_putchar; + oStream.flags |= __SWR; + oStream.udata = (void*)this; + //---- + oStream.flags |= __SPGM; + // + va_list ap; + va_start( (ap), (fmt_P) ); + size_t i = vfprintf(&oStream, fmt, ap); + va_end(ap); + return i; +} + +//printf end +//---------------------------------------- From 37030cf632fe2888e81e4e09318f1fb38fd93e3e Mon Sep 17 00:00:00 2001 From: u48 Date: Wed, 1 Feb 2017 16:15:00 +0300 Subject: [PATCH 2/2] add "printf" --- hardware/arduino/avr/cores/arduino/Print.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hardware/arduino/avr/cores/arduino/Print.h b/hardware/arduino/avr/cores/arduino/Print.h index 7b53aa4d17e..590bdeca907 100644 --- a/hardware/arduino/avr/cores/arduino/Print.h +++ b/hardware/arduino/avr/cores/arduino/Print.h @@ -79,6 +79,9 @@ class Print size_t println(double, int = 2); size_t println(const Printable&); size_t println(void); + + size_t printf (const char *szFormat, ...); + size_t printf (const __FlashStringHelper *szFormat, ...); }; #endif