You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. Print.h - Base class that provides print() and println()
  3. Copyright (c) 2008 David A. Mellis. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #ifndef Print_h
  17. #define Print_h
  18. #include <inttypes.h>
  19. #include <stdio.h> // for size_t
  20. #include <stdarg.h>
  21. #include "core_id.h"
  22. #include "WString.h"
  23. #include "Printable.h"
  24. #define DEC 10
  25. #define HEX 16
  26. #define OCT 8
  27. #define BIN 2
  28. #define BYTE 0
  29. class __FlashStringHelper;
  30. #if ARDUINO >= 100
  31. class Print
  32. {
  33. public:
  34. Print() : write_error(0) {}
  35. virtual size_t write(uint8_t b);
  36. size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }
  37. virtual size_t write(const uint8_t *buffer, size_t size);
  38. size_t write(const char *buffer, size_t size) { return write((const uint8_t *)buffer, size); }
  39. size_t print(const String &s);
  40. size_t print(char c) { return write((uint8_t)c); }
  41. size_t print(const char s[]) { return write(s); }
  42. size_t print(const __FlashStringHelper *f);
  43. size_t print(uint8_t b) { return printNumber(b, 0, 10); }
  44. size_t print(int n) { return print((long)n); }
  45. size_t print(unsigned int n) { return printNumber(n, 0, 10); }
  46. size_t print(long n);
  47. size_t print(unsigned long n) { return printNumber(n, 0, 10); }
  48. size_t print(unsigned char n, int base) { return printNumber(n, 0, base); }
  49. size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
  50. size_t print(unsigned int n, int base) { return printNumber(n, 0, base); }
  51. size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
  52. size_t print(unsigned long n, int base) { return printNumber(n, 0, base); }
  53. size_t print(double n, int digits = 2) { return printFloat(n, digits); }
  54. size_t print(const Printable &obj) { return obj.printTo(*this); }
  55. size_t println(void);
  56. size_t println(const String &s) { return print(s) + println(); }
  57. size_t println(char c) { return print(c) + println(); }
  58. size_t println(const char s[]) { return print(s) + println(); }
  59. size_t println(const __FlashStringHelper *f) { return print(f) + println(); }
  60. size_t println(uint8_t b) { return print(b) + println(); }
  61. size_t println(int n) { return print(n) + println(); }
  62. size_t println(unsigned int n) { return print(n) + println(); }
  63. size_t println(long n) { return print(n) + println(); }
  64. size_t println(unsigned long n) { return print(n) + println(); }
  65. size_t println(unsigned char n, int base) { return print(n, base) + println(); }
  66. size_t println(int n, int base) { return print(n, base) + println(); }
  67. size_t println(unsigned int n, int base) { return print(n, base) + println(); }
  68. size_t println(long n, int base) { return print(n, base) + println(); }
  69. size_t println(unsigned long n, int base) { return print(n, base) + println(); }
  70. size_t println(double n, int digits = 2) { return print(n, digits) + println(); }
  71. size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
  72. int getWriteError() { return write_error; }
  73. void clearWriteError() { setWriteError(0); }
  74. int printf(const char *format, ...);
  75. int printf(const __FlashStringHelper *format, ...);
  76. protected:
  77. void setWriteError(int err = 1) { write_error = err; }
  78. private:
  79. char write_error;
  80. size_t printNumberDec(unsigned long n, uint8_t sign);
  81. size_t printNumberHex(unsigned long n);
  82. size_t printNumberBin(unsigned long n);
  83. size_t printNumberAny(unsigned long n, uint8_t base);
  84. inline size_t printNumber(unsigned long n, uint8_t sign, uint8_t base) __attribute__((always_inline)) {
  85. // when "base" is a constant (pretty much always), the
  86. // compiler optimizes this to a single function call.
  87. if (base == 0) return write((uint8_t)n);
  88. if (base == 10 || base < 2) return printNumberDec(n, sign);
  89. //if (base == 10 || base < 2) return printNumberAny(n, 10); // testing only
  90. if (base == 16) return printNumberHex(n);
  91. if (base == 2) return printNumberBin(n);
  92. return printNumberAny(n, base);
  93. }
  94. size_t printFloat(double n, uint8_t digits);
  95. };
  96. #else
  97. class Print
  98. {
  99. public:
  100. virtual void write(uint8_t);
  101. virtual void write(const char *str);
  102. virtual void write(const uint8_t *buffer, size_t size);
  103. void print(const String &s);
  104. void print(char c) { write((uint8_t)c); }
  105. void print(const char s[]) { write(s); }
  106. void print(const __FlashStringHelper *f);
  107. void print(uint8_t b) { write(b); }
  108. void print(int n) { print((long)n); }
  109. void print(unsigned int n) { printNumber(n, 0, 10); }
  110. void print(long n);
  111. void print(unsigned long n) { printNumber(n, 0, 10); }
  112. void print(unsigned char n, int base) { printNumber(n, 0, base); }
  113. void print(int n, int base) { (base == 10) ? print(n) : printNumber(n, 0, base); }
  114. void print(unsigned int n, int base) { printNumber(n, 0, base); }
  115. void print(long n, int base) { (base == 10) ? print(n) : printNumber(n, 0, base); }
  116. void print(unsigned long n, int base) { printNumber(n, 0, base); }
  117. void print(double n, int digits = 2) { printFloat(n, digits); }
  118. void println(void);
  119. void println(const String &s) { print(s); println(); }
  120. void println(char c) { print(c); println(); }
  121. void println(const char s[]) { print(s); println(); }
  122. void println(const __FlashStringHelper *f) { print(f); println(); }
  123. void println(uint8_t b) { print(b); println(); }
  124. void println(int n) { print(n); println(); }
  125. void println(unsigned int n) { print(n); println(); }
  126. void println(long n) { print(n); println(); }
  127. void println(unsigned long n) { print(n); println(); }
  128. void println(unsigned char n, int base) { print(n, base); println(); }
  129. void println(int n, int base) { print(n, base); println(); }
  130. void println(unsigned int n, int base) { print(n, base); println(); }
  131. void println(long n, int base) { print(n, base); println(); }
  132. void println(unsigned long n, int base) { print(n, base); println(); }
  133. void println(double n, int digits = 2) { print(n, digits); println(); }
  134. private:
  135. void printNumberDec(unsigned long n, uint8_t sign);
  136. void printNumberHex(unsigned long n);
  137. void printNumberBin(unsigned long n);
  138. void printNumberAny(unsigned long n, uint8_t base);
  139. inline size_t printNumber(unsigned long n, uint8_t sign, uint8_t base) __attribute__((always_inline)) {
  140. if (base == 0) { write((uint8_t)n); return; }
  141. if (base == 10 || base < 2) { printNumberDec(n, sign); return; }
  142. if (base == 16) { printNumberHex(n); return; }
  143. if (base == 2) { printNumberBin(n); return; }
  144. printNumberAny(n, base);
  145. }
  146. void printFloat(double n, uint8_t digits);
  147. };
  148. #endif
  149. #endif