From f282cbaf968f7142ef5abb68a92e970c3d5eea35 Mon Sep 17 00:00:00 2001 From: "David A. Mellis" Date: Tue, 23 Aug 2011 19:12:03 -0400 Subject: write(), print(), and println() now return number of bytes written. The type is long, and negative values indicate errors. Needs more testing. http://code.google.com/p/arduino/issues/detail?id=551 --- cores/arduino/HardwareSerial.cpp | 5 +- cores/arduino/HardwareSerial.h | 2 +- cores/arduino/Print.cpp | 194 +++++++++++++++++++++++---------------- cores/arduino/Print.h | 56 +++++------ cores/arduino/Printable.h | 2 +- 5 files changed, 151 insertions(+), 108 deletions(-) (limited to 'cores') diff --git a/cores/arduino/HardwareSerial.cpp b/cores/arduino/HardwareSerial.cpp index db6b149..a200da5 100644 --- a/cores/arduino/HardwareSerial.cpp +++ b/cores/arduino/HardwareSerial.cpp @@ -352,12 +352,13 @@ void HardwareSerial::flush() ; } -void HardwareSerial::write(uint8_t c) +long HardwareSerial::write(uint8_t c) { int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE; // If the output buffer is full, there's nothing for it other than to // wait for the interrupt handler to empty it a bit + // ???: return 0 here instead? while (i == _tx_buffer->tail) ; @@ -365,6 +366,8 @@ void HardwareSerial::write(uint8_t c) _tx_buffer->head = i; sbi(*_ucsrb, _udrie); + + return 1; } // Preinstantiate Objects ////////////////////////////////////////////////////// diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index eefdcbe..4af8c59 100644 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -55,7 +55,7 @@ class HardwareSerial : public Stream virtual int peek(void); virtual int read(void); virtual void flush(void); - virtual void write(uint8_t); + virtual long write(uint8_t); using Print::write; // pull in write(str) and write(buf, size) from Print }; diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index 06ac52a..58b1032 100755 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -30,167 +30,196 @@ // Public Methods ////////////////////////////////////////////////////////////// /* default implementation: may be overridden */ -void Print::write(const char *str) +long Print::write(const char *str) { - while (*str) - write(*str++); + long n = 0; + while (*str) { + if (write(*str++) <= 0) break; + n++; + } + return n; } /* default implementation: may be overridden */ -void Print::write(const uint8_t *buffer, size_t size) +long Print::write(const uint8_t *buffer, size_t size) { - while (size--) - write(*buffer++); + long n = 0; + while (size--) { + if (write(*buffer++) <= 0) break; + n++; + } + return n; } -void Print::print(const __FlashStringHelper *ifsh) +long Print::print(const __FlashStringHelper *ifsh) { const prog_char *p = (const prog_char *)ifsh; + long n = 0; while (1) { unsigned char c = pgm_read_byte(p++); - if (c == 0) return; - write(c); + if (c == 0) break; + if (write(c) <= 0) break; + n++; } + return n; } -void Print::print(const String &s) +long Print::print(const String &s) { + long n = 0; for (int i = 0; i < s.length(); i++) { - write(s[i]); + if (write(s[i]) < 0) break; + n++; } + return n; } -void Print::print(const char str[]) +long Print::print(const char str[]) { - write(str); + return write(str); } -void Print::print(char c) +long Print::print(char c) { - write(c); + return write(c); } -void Print::print(unsigned char b, int base) +long 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) +long Print::print(int n, int base) { - print((long) n, base); + return print((long) n, base); } -void Print::print(unsigned int n, int base) +long 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) +long Print::print(long n, int base) { if (base == 0) { - write(n); + return write(n); } else if (base == 10) { if (n < 0) { - print('-'); + long t = print('-'); + if (t <= 0) return t; n = -n; + return printNumber(n, 10) + 1; } - printNumber(n, 10); + return printNumber(n, 10); } else { - printNumber(n, base); + return printNumber(n, base); } } -void Print::print(unsigned long n, int base) +long 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) +long Print::print(double n, int digits) { - printFloat(n, digits); + return printFloat(n, digits); } -void Print::println(const __FlashStringHelper *ifsh) +long Print::println(const __FlashStringHelper *ifsh) { - print(ifsh); - println(); + long n = print(ifsh); + if (n >= 0) n += println(); + return n; } -void Print::print(const Printable& x) +long Print::print(const Printable& x) { - x.printTo(*this); + return x.printTo(*this); } -void Print::println(void) +long Print::println(void) { - print('\r'); - print('\n'); + long t = print('\r'); + if (t <= 0) return t; + if (print('\n') <= 0) return 1; + return 2; } -void Print::println(const String &s) +long Print::println(const String &s) { - print(s); - println(); + long n = print(s); + if (n >= 0) n += println(); + return n; } -void Print::println(const char c[]) +long Print::println(const char c[]) { - print(c); - println(); + long n = print(c); + if (n >= 0) n += println(); + return n; } -void Print::println(char c) +long Print::println(char c) { - print(c); - println(); + long n = print(c); + if (n > 0) n += println(); + return n; } -void Print::println(unsigned char b, int base) +long Print::println(unsigned char b, int base) { - print(b, base); - println(); + long n = print(b, base); + if (n >= 0) n += println(); + return n; } -void Print::println(int n, int base) +long Print::println(int num, int base) { - print(n, base); - println(); + long n = print(num, base); + if (n >= 0) n += println(); + return n; } -void Print::println(unsigned int n, int base) +long Print::println(unsigned int num, int base) { - print(n, base); - println(); + long n = print(num, base); + if (n >= 0) n += println(); + return n; } -void Print::println(long n, int base) +long Print::println(long num, int base) { - print(n, base); - println(); + long n = print(num, base); + if (n >= 0) n += println(); + return n; } -void Print::println(unsigned long n, int base) +long Print::println(unsigned long num, int base) { - print(n, base); - println(); + long n = print(num, base); + if (n >= 0) n += println(); + return n; } -void Print::println(double n, int digits) +long Print::println(double num, int digits) { - print(n, digits); - println(); + long n = print(num, digits); + if (n >= 0) n += println(); + return n; } -void Print::println(const Printable& x) +long Print::println(const Printable& x) { - print(x); - println(); + long n = print(x); + if (n >= 0) n += println(); + return n; } // Private Methods ///////////////////////////////////////////////////////////// -void Print::printNumber(unsigned long n, uint8_t base) { +long 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]; @@ -206,15 +235,17 @@ void Print::printNumber(unsigned long n, uint8_t base) { *--str = c < 10 ? c + '0' : c + 'A' - 10; } while(n); - write(str); + return write(str); } -void Print::printFloat(double number, uint8_t digits) +long Print::printFloat(double number, uint8_t digits) { + long n = 0, t; + // Handle negative numbers if (number < 0.0) { - print('-'); + if ((n = print('-')) <= 0) return n; number = -number; } @@ -228,18 +259,27 @@ 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); + if ((t = print(int_part)) < 0) return n; + + n += t; // Print the decimal point, but only if there are digits beyond - if (digits > 0) - print("."); + if (digits > 0) { + t = print("."); + if (t <= 0) return n; + n += t; + } // Extract digits from the remainder one at a time while (digits-- > 0) { remainder *= 10.0; int toPrint = int(remainder); - print(toPrint); + t = print(toPrint); + if (t <= 0) return n; + n += t; remainder -= toPrint; } + + return n; } diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index bf10b14..d5b02ff 100755 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -34,37 +34,37 @@ class Print { private: - void printNumber(unsigned long, uint8_t); - void printFloat(double, uint8_t); + long printNumber(unsigned long, uint8_t); + long printFloat(double, uint8_t); public: - virtual void write(uint8_t) = 0; - virtual void write(const char *str); - virtual void write(const uint8_t *buffer, size_t size); + virtual long write(uint8_t) = 0; + virtual long write(const char *str); + virtual long write(const uint8_t *buffer, size_t size); - void print(const __FlashStringHelper *); - void print(const String &); - void print(const char[]); - void print(char); - void print(unsigned char, int = DEC); - 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 print(const Printable&); + long print(const __FlashStringHelper *); + long print(const String &); + long print(const char[]); + long print(char); + long print(unsigned char, int = DEC); + long print(int, int = DEC); + long print(unsigned int, int = DEC); + long print(long, int = DEC); + long print(unsigned long, int = DEC); + long print(double, int = 2); + long print(const Printable&); - void println(const __FlashStringHelper *); - void println(const String &s); - void println(const char[]); - void println(char); - void println(unsigned char, int = DEC); - 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(const Printable&); - void println(void); + long println(const __FlashStringHelper *); + long println(const String &s); + long println(const char[]); + long println(char); + long println(unsigned char, int = DEC); + long println(int, int = DEC); + long println(unsigned int, int = DEC); + long println(long, int = DEC); + long println(unsigned long, int = DEC); + long println(double, int = 2); + long println(const Printable&); + long println(void); }; #endif diff --git a/cores/arduino/Printable.h b/cores/arduino/Printable.h index d332aad..6814ee4 100644 --- a/cores/arduino/Printable.h +++ b/cores/arduino/Printable.h @@ -30,7 +30,7 @@ class Print; class Printable { public: - virtual void printTo(Print& p) const = 0; + virtual long printTo(Print& p) const = 0; }; #endif -- cgit v1.2.3-18-g5258