From def86681b7ea753708f50430ecf941a555b5853f Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Fri, 25 Dec 2009 20:20:27 +0000 Subject: [PATCH] Adding precision parameter for printing of floats / doubles. Restructured the print() and println() functions a bit. --- hardware/arduino/cores/arduino/Print.cpp | 95 +++++++++++------------- hardware/arduino/cores/arduino/Print.h | 34 ++++----- readme.txt | 9 ++- 3 files changed, 66 insertions(+), 72 deletions(-) diff --git a/hardware/arduino/cores/arduino/Print.cpp b/hardware/arduino/cores/arduino/Print.cpp index 74d0e5b43..fb5afc11b 100755 --- a/hardware/arduino/cores/arduino/Print.cpp +++ b/hardware/arduino/cores/arduino/Print.cpp @@ -42,58 +42,55 @@ 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::print(double n, int digits) +{ + printFloat(n, digits); } void Print::println(void) @@ -102,46 +99,34 @@ void Print::println(void) print('\n'); } -void Print::println(char c) -{ - print(c); - println(); -} - void Print::println(const char c[]) { print(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(); -} - -void Print::println(unsigned long n) -{ - print(n); - println(); + print(n, base); + println(); } void Print::println(long n, int base) @@ -150,9 +135,15 @@ void Print::println(long n, int base) println(); } -void Print::println(double n) +void Print::println(unsigned long n, int base) { - print(n); + print(n, base); + println(); +} + +void Print::println(double n, int digits) +{ + print(n, digits); println(); } diff --git a/hardware/arduino/cores/arduino/Print.h b/hardware/arduino/cores/arduino/Print.h index a69e85d99..8a1e2b8b3 100755 --- a/hardware/arduino/cores/arduino/Print.h +++ b/hardware/arduino/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 diff --git a/readme.txt b/readme.txt index 7547bc36f..c219137a2 100644 --- a/readme.txt +++ b/readme.txt @@ -50,12 +50,15 @@ UPDATES * No longer disabling interrupts in delayMicroseconds(). * Fixed bug w/ micros() returning incorrect values from within an interrupt. * Added Serial.end() command. +* Added precision parameter for printing of floats / doubles. +* Fixed bug that broke use of analog inputs 8-15 on the Mega. [environment] * Synchronized with the Processing 1.0.9 code base, bringing various fixes, - including a bug causing saving to fail when closing the last sketch. -* Added support for third-party hardware in the SKETCHBOOK/hardware folder. - It mirrors the current structure of the hardware folder in Arduino. + including to a bug causing saving to fail when closing the last sketch. +* Added support for third-party hardware in the SKETCHBOOK/hardware folder, + mirroring the current structure of the hardware folder in Arduino. +* Added Ctrl-Shift-M / Command-Shift-M shortcut for serial monitor. * Hold down shift when pressing the Verify / Compile or Upload toolbar buttons to generate verbose output (including command lines). * Moving build (on upload) from the applet/ sub-folder of the sketch