From 85de48989da4009dced261ac326c70bad0ba0684 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 25 Dec 2009 20:20:27 +0000 Subject: Adding precision parameter for printing of floats / doubles. Restructured the print() and println() functions a bit. --- cores/arduino/Print.cpp | 91 ++++++++++++++++++++++--------------------------- cores/arduino/Print.h | 34 +++++++++--------- 2 files changed, 58 insertions(+), 67 deletions(-) (limited to 'cores/arduino') diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index 74d0e5b..fb5afc1 100755 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -42,70 +42,61 @@ void Print::write(const uint8_t *buffer, size_t size) write(*buffer++); } -void Print::print(uint8_t b) -{ - this->write(b); -} - -void Print::print(char c) -{ - print((byte) c); -} - void Print::print(const char str[]) { write(str); } -void Print::print(int n) +void Print::print(char c, int base) { - print((long) n); + print((long) c, base); } -void Print::print(unsigned int n) +void Print::print(unsigned char b, int base) { - print((unsigned long) n); + print((unsigned long) b, base); } -void Print::print(long n) +void Print::print(int n, int base) { - if (n < 0) { - print('-'); - n = -n; - } - printNumber(n, 10); + print((long) n, base); } -void Print::print(unsigned long n) +void Print::print(unsigned int n, int base) { - printNumber(n, 10); + print((unsigned long) n, base); } void Print::print(long n, int base) { - if (base == 0) - print((char) n); - else if (base == 10) - print(n); - else + if (base == 0) { + write(n); + } else if (base == 10) { + if (n < 0) { + print('-'); + n = -n; + } + printNumber(n, 10); + } else { printNumber(n, base); + } } -void Print::print(double n) +void Print::print(unsigned long n, int base) { - printFloat(n, 2); + if (base == 0) write(n); + else printNumber(n, base); } -void Print::println(void) +void Print::print(double n, int digits) { - print('\r'); - print('\n'); + printFloat(n, digits); } -void Print::println(char c) +void Print::println(void) { - print(c); - println(); + print('\r'); + print('\n'); } void Print::println(const char c[]) @@ -114,45 +105,45 @@ void Print::println(const char c[]) println(); } -void Print::println(uint8_t b) +void Print::println(char c, int base) { - print(b); + print(c, base); println(); } -void Print::println(int n) +void Print::println(unsigned char b, int base) { - print(n); + print(b, base); println(); } -void Print::println(unsigned int n) +void Print::println(int n, int base) { - print(n); + print(n, base); println(); } -void Print::println(long n) +void Print::println(unsigned int n, int base) { - print(n); - println(); + print(n, base); + println(); } -void Print::println(unsigned long n) +void Print::println(long n, int base) { - print(n); - println(); + print(n, base); + println(); } -void Print::println(long n, int base) +void Print::println(unsigned long n, int base) { print(n, base); println(); } -void Print::println(double n) +void Print::println(double n, int digits) { - print(n); + print(n, digits); println(); } diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index a69e85d..8a1e2b8 100755 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -38,25 +38,25 @@ class Print virtual void write(uint8_t) = 0; virtual void write(const char *str); virtual void write(const uint8_t *buffer, size_t size); - void print(char); + void print(const char[]); - void print(uint8_t); - void print(int); - void print(unsigned int); - void print(long); - void print(unsigned long); - void print(long, int); - void print(double); - void println(void); - void println(char); + void print(char, int = BYTE); + void print(unsigned char, int = BYTE); + void print(int, int = DEC); + void print(unsigned int, int = DEC); + void print(long, int = DEC); + void print(unsigned long, int = DEC); + void print(double, int = 2); + void println(const char[]); - void println(uint8_t); - void println(int); - void println(unsigned int); - void println(long); - void println(unsigned long); - void println(long, int); - void println(double); + void println(char, int = BYTE); + void println(unsigned char, int = BYTE); + void println(int, int = DEC); + void println(unsigned int, int = DEC); + void println(long, int = DEC); + void println(unsigned long, int = DEC); + void println(double, int = 2); + void println(void); }; #endif -- cgit v1.2.3-18-g5258