|
@ -1,21 +1,21 @@ |
|
|
/*
|
|
|
/*
|
|
|
Print.cpp - Base class that provides print() and println() |
|
|
Print.cpp - Base class that provides print() and println() |
|
|
Copyright (c) 2008 David A. Mellis. All right reserved. |
|
|
Copyright (c) 2008 David A. Mellis. All right reserved. |
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or |
|
|
This library is free software; you can redistribute it and/or |
|
|
modify it under the terms of the GNU Lesser General Public |
|
|
modify it under the terms of the GNU Lesser General Public |
|
|
License as published by the Free Software Foundation; either |
|
|
License as published by the Free Software Foundation; either |
|
|
version 2.1 of the License, or (at your option) any later version. |
|
|
version 2.1 of the License, or (at your option) any later version. |
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful, |
|
|
This library is distributed in the hope that it will be useful, |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
|
Lesser General Public License for more details. |
|
|
Lesser General Public License for more details. |
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public |
|
|
You should have received a copy of the GNU Lesser General Public |
|
|
License along with this library; if not, write to the Free Software |
|
|
License along with this library; if not, write to the Free Software |
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
|
|
|
|
|
|
|
Modified 23 November 2006 by David A. Mellis |
|
|
Modified 23 November 2006 by David A. Mellis |
|
|
Modified 03 August 2015 by Chuck Todd |
|
|
Modified 03 August 2015 by Chuck Todd |
|
|
*/ |
|
|
*/ |
|
@ -37,7 +37,7 @@ typedef signed long sint32_t; |
|
|
/* default implementation: may be overridden */ |
|
|
/* default implementation: may be overridden */ |
|
|
size_t Print::write(const uint8_t *buffer, size_t size) |
|
|
size_t Print::write(const uint8_t *buffer, size_t size) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
size_t n = 0; |
|
|
size_t n = 0; |
|
|
while (size--) { |
|
|
while (size--) { |
|
|
if (write(*buffer++)) n++; |
|
|
if (write(*buffer++)) n++; |
|
@ -49,7 +49,7 @@ size_t Print::write(const uint8_t *buffer, size_t size) |
|
|
|
|
|
|
|
|
size_t Print::print(const char str[]) |
|
|
size_t Print::print(const char str[]) |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
//while(1);
|
|
|
//while(1);
|
|
|
return write(str); |
|
|
return write(str); |
|
|
} |
|
|
} |
|
@ -195,15 +195,15 @@ size_t Print::printNumber(unsigned long n, uint8_t base) { |
|
|
return write(str); |
|
|
return write(str); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
size_t Print::printFloat(double number, uint8_t digits) |
|
|
size_t Print::printFloat(double number, uint8_t digits) |
|
|
{ |
|
|
{ |
|
|
size_t n = 0; |
|
|
size_t n = 0; |
|
|
|
|
|
|
|
|
if (isnan(number)) return print("nan"); |
|
|
if (isnan(number)) return print("nan"); |
|
|
if (isinf(number)) return print("inf"); |
|
|
if (isinf(number)) return print("inf"); |
|
|
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
|
|
|
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
|
|
|
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
|
|
|
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
|
|
|
|
|
|
|
|
|
// Handle negative numbers
|
|
|
// Handle negative numbers
|
|
|
if (number < 0.0) |
|
|
if (number < 0.0) |
|
|
{ |
|
|
{ |
|
@ -215,7 +215,7 @@ size_t Print::printFloat(double number, uint8_t digits) |
|
|
double rounding = 0.5; |
|
|
double rounding = 0.5; |
|
|
for (uint8_t i=0; i<digits; ++i) |
|
|
for (uint8_t i=0; i<digits; ++i) |
|
|
rounding /= 10.0; |
|
|
rounding /= 10.0; |
|
|
|
|
|
|
|
|
number += rounding; |
|
|
number += rounding; |
|
|
|
|
|
|
|
|
// Extract the integer part of the number and print it
|
|
|
// Extract the integer part of the number and print it
|
|
@ -225,7 +225,7 @@ size_t Print::printFloat(double number, uint8_t digits) |
|
|
|
|
|
|
|
|
// Print the decimal point, but only if there are digits beyond
|
|
|
// Print the decimal point, but only if there are digits beyond
|
|
|
if (digits > 0) { |
|
|
if (digits > 0) { |
|
|
n += print("."); |
|
|
n += print("."); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Extract digits from the remainder one at a time
|
|
|
// Extract digits from the remainder one at a time
|
|
@ -234,14 +234,14 @@ size_t Print::printFloat(double number, uint8_t digits) |
|
|
remainder *= 10.0; |
|
|
remainder *= 10.0; |
|
|
int toPrint = int(remainder); |
|
|
int toPrint = int(remainder); |
|
|
n += print(toPrint); |
|
|
n += print(toPrint); |
|
|
remainder -= toPrint; |
|
|
remainder -= toPrint; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return n; |
|
|
return n; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if (PrintfEnable == 1) |
|
|
#if (PrintfEnable == 1) |
|
|
size_t Print::printf(const char *argList, ...) |
|
|
size_t Print::printf(const char *argList, ...) |
|
|
{ |
|
|
{ |
|
|
const char *ptr; |
|
|
const char *ptr; |
|
@ -279,7 +279,7 @@ size_t Print::printf(const char *argList, ...) |
|
|
else |
|
|
else |
|
|
{ |
|
|
{ |
|
|
numOfDigits = 0xff; |
|
|
numOfDigits = 0xff; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch(ch) /* Decode the type of the argument */ |
|
|
switch(ch) /* Decode the type of the argument */ |
|
@ -297,14 +297,14 @@ size_t Print::printf(const char *argList, ...) |
|
|
case 'D': |
|
|
case 'D': |
|
|
num_s32 = va_arg(argp, int); |
|
|
num_s32 = va_arg(argp, int); |
|
|
print(num_s32, 10); |
|
|
print(num_s32, 10); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'u': |
|
|
case 'u': |
|
|
case 'U': /* Argument type is of integer, hence read 32bit unsigend data */ |
|
|
case 'U': /* Argument type is of integer, hence read 32bit unsigend data */ |
|
|
num_u32 = va_arg(argp, uint32_t); |
|
|
num_u32 = va_arg(argp, uint32_t); |
|
|
print(num_u32, 10); |
|
|
print(num_u32, 10); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -312,21 +312,21 @@ size_t Print::printf(const char *argList, ...) |
|
|
case 'x': |
|
|
case 'x': |
|
|
case 'X': /* Argument type is of hex, hence hexadecimal data from the argp */ |
|
|
case 'X': /* Argument type is of hex, hence hexadecimal data from the argp */ |
|
|
num_u32 = va_arg(argp, uint32_t); |
|
|
num_u32 = va_arg(argp, uint32_t); |
|
|
print(num_u32, 16); |
|
|
print(num_u32, 16); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'b': |
|
|
case 'b': |
|
|
case 'B': /* Argument type is of binary,Read int and convert to binary */ |
|
|
case 'B': /* Argument type is of binary,Read int and convert to binary */ |
|
|
num_u32 = va_arg(argp, uint32_t); |
|
|
num_u32 = va_arg(argp, uint32_t); |
|
|
print(num_u32, 2); |
|
|
print(num_u32, 2); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'F': |
|
|
case 'F': |
|
|
case 'f': /* Argument type is of float, hence read double data from the argp */ |
|
|
case 'f': /* Argument type is of float, hence read double data from the argp */ |
|
|
floatNum_f32 = va_arg(argp, double); |
|
|
floatNum_f32 = va_arg(argp, double); |
|
|
printFloat(floatNum_f32,10); |
|
|
printFloat(floatNum_f32,10); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
@ -335,7 +335,7 @@ size_t Print::printf(const char *argList, ...) |
|
|
case 'S': |
|
|
case 'S': |
|
|
case 's': /* Argument type is of string, hence get the pointer to sting passed */ |
|
|
case 's': /* Argument type is of string, hence get the pointer to sting passed */ |
|
|
str = va_arg(argp, char *); |
|
|
str = va_arg(argp, char *); |
|
|
print(str); |
|
|
print(str); |
|
|
break; |
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -356,4 +356,4 @@ size_t Print::printf(const char *argList, ...) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif |
|
|
#endif |
|
|