Hi,
I would like to be able to use formatted print for both Serial and LCD prints. I followed the path described in Arduino Playground - Printf .
This suggested to change the Print.h files for AVR {installdir}/hardware/arduino/cores/arduino to include the code listed below at the end of the public: section in the Print.h file.
I did this to get many compilation errors:
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:26:0,
from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29,
from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:232,
from C:\Users\klong\AppData\Local\Temp\arduino_build_651782\sketch\test_prtf.ino.cpp:1:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Print.h:93:9: error: 'void Print::printf(const char*, ...)' cannot be overloaded
void printf(const char *format, ...) {
^
This just a few, and suggests that I've missed something basic.
Below I listed the modified Print.h file.
What did I do wrong?
Thanks,
Keith
#ifndef Print_h
#define Print_h
#include <stdint.h>
#include <stddef.h>
#include "WString.h"
#include "Printable.h"
#define DEC 10
#define HEX 16
#define OCT 8
#define BIN 2
class Print {
private:
int write_error;
size_t printNumber(unsigned long, uint8_t);
size_t printFloat(double, uint8_t);
protected:
void setWriteError(int err = 1) {
write_error = err;
}
public:
Print() :
write_error(0) {
}
int getWriteError() {
return write_error;
}
void clearWriteError() {
setWriteError(0);
}
virtual size_t write(uint8_t) = 0;
size_t write(const char *str) {
if(str == NULL)
return 0;
return write((const uint8_t *) str, strlen(str));
}
virtual size_t write(const uint8_t *buffer, size_t size);
size_t write(const char *buffer, size_t size) {
return write((const uint8_t *) buffer, size);
}
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));
size_t print(const __FlashStringHelper *);
size_t print(const String &);
size_t print(const char[]);
size_t print(char);
size_t print(unsigned char, int = DEC);
size_t print(int, int = DEC);
size_t print(unsigned int, int = DEC);
size_t print(long, int = DEC);
size_t print(unsigned long, int = DEC);
size_t print(double, int = 2);
size_t print(const Printable&);
size_t println(const __FlashStringHelper *);
size_t println(const String &s);
size_t println(const char[]);
size_t println(char);
size_t println(unsigned char, int = DEC);
size_t println(int, int = DEC);
size_t println(unsigned int, int = DEC);
size_t println(long, int = DEC);
size_t println(unsigned long, int = DEC);
size_t println(double, int = 2);
size_t println(const Printable&);
size_t println(void);
virtual void flush() {/* Empty implementation for backward compatibility */}
#include <stdarg.h>
#define PRINTF_BUF 80 // define the tmp buffer size (change if desired)
void printf(const char *format, ...) {
char buf[PRINTF_BUF];
va_list ap;
va_start(ap, format);
vsnprintf(buf, sizeof(buf), format, ap);
for(char *p = &buf[0]; *p; p++) // emulate cooked mode for newlines {
if(*p == '\n')
write('\r');
write(*p);
}
va_end(ap);
}
#ifdef F // check to see if F() macro is available
void printf(const __FlashStringHelper *format, ...) {
char buf[PRINTF_BUF];
va_list ap;
va_start(ap, format);
#ifdef __AVR__
vsnprintf_P(buf, sizeof(buf), (const char *)format, ap); // progmem for AVR
#else
vsnprintf(buf, sizeof(buf), (const char *)format, ap); // for the rest of the world
#endif
for(char *p = &buf[0]; *p; p++) // emulate cooked mode for newlines {
if(*p == '\n')
write('\r');
write(*p);
}
va_end(ap);
}
#endif
};
#endif