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