aboutsummaryrefslogtreecommitdiff
path: root/cores/arduino/Print.cpp
diff options
context:
space:
mode:
authorZach Eveland <zeveland@blacklabel-development.com>2011-10-27 11:45:13 -0400
committerZach Eveland <zeveland@blacklabel-development.com>2011-10-27 11:45:13 -0400
commit3524f2ff0b07338cd0939d9c695a6bcbb2af39dd (patch)
tree3a1ced5ef4198e9e40e06bc15a4cca5e106f5226 /cores/arduino/Print.cpp
parentec93cd74a187a58f0e2bbe496c38edd7d9f33f30 (diff)
Revert "Merge branch 'master' of github.com:arduino/Arduino into diskloader_reboot"
This reverts commit df9835efafd13685251749bc210c0b96a18a96a5, reversing changes made to ec45af8bfa9222a807c075dd1db4b5aa798bba03. Conflicts: hardware/arduino/variants/mega/pins_arduino.h libraries/Ethernet/examples/PachubeClientString/PachubeClientString.ino
Diffstat (limited to 'cores/arduino/Print.cpp')
-rwxr-xr-xcores/arduino/Print.cpp207
1 files changed, 125 insertions, 82 deletions
diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp
index fd68942..ff9b154 100755
--- a/cores/arduino/Print.cpp
+++ b/cores/arduino/Print.cpp
@@ -23,173 +23,213 @@
#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 uint8_t *buffer, size_t size)
{
- while (*str)
- write(*str++);
+ size_t n = 0;
+ while (size--) {
+ n += write(*buffer++);
+ }
+ return n;
}
-/* default implementation: may be overridden */
-void Print::write(const uint8_t *buffer, size_t size)
+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)
{
- for (unsigned int i = 0; i < s.length(); i++) {
- write(s[i]);
+ size_t n = 0;
+ for (uint16_t i = 0; i < s.length(); 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;
+}
- while (n > 0) {
- buf[i++] = n % base;
+// 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';
+
+ // 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 +243,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;
}