diff options
Diffstat (limited to 'cores/arduino/Print.cpp')
-rwxr-xr-x | cores/arduino/Print.cpp | 207 |
1 files changed, 82 insertions, 125 deletions
diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index ff9b154..fd68942 100755 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -23,213 +23,173 @@ #include <stdio.h> #include <string.h> #include <math.h> -#include "Arduino.h" +#include "wiring.h" #include "Print.h" // Public Methods ////////////////////////////////////////////////////////////// /* default implementation: may be overridden */ -size_t Print::write(const uint8_t *buffer, size_t size) +void Print::write(const char *str) { - size_t n = 0; - while (size--) { - n += write(*buffer++); - } - return n; + while (*str) + write(*str++); } -size_t Print::print(const __FlashStringHelper *ifsh) +/* default implementation: may be overridden */ +void Print::write(const uint8_t *buffer, size_t size) { - const prog_char *p = (const prog_char *)ifsh; - size_t n = 0; - while (1) { - unsigned char c = pgm_read_byte(p++); - if (c == 0) break; - n += write(c); - } - return n; + while (size--) + write(*buffer++); } -size_t Print::print(const String &s) +void Print::print(const String &s) { - size_t n = 0; - for (uint16_t i = 0; i < s.length(); i++) { - n += write(s[i]); + for (unsigned int i = 0; i < s.length(); i++) { + write(s[i]); } - return n; } -size_t Print::print(const char str[]) +void Print::print(const char str[]) { - return write(str); + write(str); } -size_t Print::print(char c) +void Print::print(char c, int base) { - return write(c); + print((long) c, base); } -size_t Print::print(unsigned char b, int base) +void Print::print(unsigned char b, int base) { - return print((unsigned long) b, base); + print((unsigned long) b, base); } -size_t Print::print(int n, int base) +void Print::print(int n, int base) { - return print((long) n, base); + print((long) n, base); } -size_t Print::print(unsigned int n, int base) +void Print::print(unsigned int n, int base) { - return print((unsigned long) n, base); + print((unsigned long) n, base); } -size_t Print::print(long n, int base) +void Print::print(long n, int base) { if (base == 0) { - return write(n); + write(n); } else if (base == 10) { if (n < 0) { - int t = print('-'); + print('-'); n = -n; - return printNumber(n, 10) + t; } - return printNumber(n, 10); + printNumber(n, 10); } else { - return printNumber(n, base); + printNumber(n, base); } } -size_t Print::print(unsigned long n, int base) -{ - if (base == 0) return write(n); - else return printNumber(n, base); -} - -size_t Print::print(double n, int digits) +void Print::print(unsigned long n, int base) { - return printFloat(n, digits); + if (base == 0) write(n); + else printNumber(n, base); } -size_t Print::println(const __FlashStringHelper *ifsh) +void Print::print(double n, int digits) { - size_t n = print(ifsh); - n += println(); - return n; + printFloat(n, digits); } -size_t Print::print(const Printable& x) +void Print::println(void) { - return x.printTo(*this); + print('\r'); + print('\n'); } -size_t Print::println(void) +void Print::println(const String &s) { - size_t n = print('\r'); - n += print('\n'); - return n; + print(s); + println(); } -size_t Print::println(const String &s) +void Print::println(const char c[]) { - size_t n = print(s); - n += println(); - return n; + print(c); + println(); } -size_t Print::println(const char c[]) +void Print::println(char c, int base) { - size_t n = print(c); - n += println(); - return n; + print(c, base); + println(); } -size_t Print::println(char c) +void Print::println(unsigned char b, int base) { - size_t n = print(c); - n += println(); - return n; + print(b, base); + println(); } -size_t Print::println(unsigned char b, int base) +void Print::println(int n, int base) { - size_t n = print(b, base); - n += println(); - return n; + print(n, base); + println(); } -size_t Print::println(int num, int base) +void Print::println(unsigned int n, int base) { - size_t n = print(num, base); - n += println(); - return n; + print(n, base); + println(); } -size_t Print::println(unsigned int num, int base) +void Print::println(long n, int base) { - size_t n = print(num, base); - n += println(); - return n; + print(n, base); + println(); } -size_t Print::println(long num, int base) +void Print::println(unsigned long n, int base) { - size_t n = print(num, base); - n += println(); - return n; + print(n, base); + println(); } -size_t Print::println(unsigned long num, int base) +void Print::println(double n, int digits) { - size_t n = print(num, base); - n += println(); - return n; -} - -size_t Print::println(double num, int digits) -{ - size_t n = print(num, digits); - n += println(); - return n; -} - -size_t Print::println(const Printable& x) -{ - size_t n = print(x); - n += println(); - return n; + print(n, digits); + println(); } // Private Methods ///////////////////////////////////////////////////////////// -size_t Print::printNumber(unsigned long n, uint8_t base) { - char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. - char *str = &buf[sizeof(buf) - 1]; - - *str = '\0'; +void Print::printNumber(unsigned long n, uint8_t base) +{ + unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars. + unsigned long i = 0; - // prevent crash if called with base == 1 - if (base < 2) base = 10; + if (n == 0) { + print('0'); + return; + } - do { - unsigned long m = n; + while (n > 0) { + buf[i++] = n % base; n /= base; - char c = m - base * n; - *--str = c < 10 ? c + '0' : c + 'A' - 10; - } while(n); + } - return write(str); + for (; i > 0; i--) + print((char) (buf[i - 1] < 10 ? + '0' + buf[i - 1] : + 'A' + buf[i - 1] - 10)); } -size_t Print::printFloat(double number, uint8_t digits) +void Print::printFloat(double number, uint8_t digits) { - size_t n = 0; - // Handle negative numbers if (number < 0.0) { - n += print('-'); + print('-'); number = -number; } @@ -243,21 +203,18 @@ size_t Print::printFloat(double number, uint8_t digits) // Extract the integer part of the number and print it unsigned long int_part = (unsigned long)number; double remainder = number - (double)int_part; - n += print(int_part); + print(int_part); // Print the decimal point, but only if there are digits beyond - if (digits > 0) { - n += print("."); - } + if (digits > 0) + print("."); // Extract digits from the remainder one at a time while (digits-- > 0) { remainder *= 10.0; int toPrint = int(remainder); - n += print(toPrint); + print(toPrint); remainder -= toPrint; } - - return n; } |