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.

168 lines
6.9KB

  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 "core_id.h"
  21. #include "WString.h"
  22. #include "Printable.h"
  23. #define DEC 10
  24. #define HEX 16
  25. #define OCT 8
  26. #define BIN 2
  27. #define BYTE 0
  28. class __FlashStringHelper;
  29. #if ARDUINO >= 100
  30. class Print
  31. {
  32. public:
  33. Print() : write_error(0) {}
  34. virtual size_t write(uint8_t b);
  35. size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }
  36. virtual size_t write(const uint8_t *buffer, size_t size);
  37. size_t print(const String &s);
  38. size_t print(char c) { return write((uint8_t)c); }
  39. size_t print(const char s[]) { return write(s); }
  40. size_t print(const __FlashStringHelper *f);
  41. size_t print(uint8_t b) { return printNumber(b, 0, 10); }
  42. size_t print(int n) { return print((long)n); }
  43. size_t print(unsigned int n) { return printNumber(n, 0, 10); }
  44. size_t print(long n);
  45. size_t print(unsigned long n) { return printNumber(n, 0, 10); }
  46. size_t print(unsigned char n, int base) { return printNumber(n, 0, base); }
  47. size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
  48. size_t print(unsigned int n, int base) { return printNumber(n, 0, base); }
  49. size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
  50. size_t print(unsigned long n, int base) { return printNumber(n, 0, base); }
  51. size_t print(double n, int digits = 2) { return printFloat(n, digits); }
  52. size_t print(const Printable &obj) { return obj.printTo(*this); }
  53. size_t println(void);
  54. size_t println(const String &s) { return print(s) + println(); }
  55. size_t println(char c) { return print(c) + println(); }
  56. size_t println(const char s[]) { return print(s) + println(); }
  57. size_t println(const __FlashStringHelper *f) { return print(f) + println(); }
  58. size_t println(uint8_t b) { return print(b) + println(); }
  59. size_t println(int n) { return print(n) + println(); }
  60. size_t println(unsigned int n) { return print(n) + println(); }
  61. size_t println(long n) { return print(n) + println(); }
  62. size_t println(unsigned long n) { return print(n) + println(); }
  63. size_t println(unsigned char n, int base) { return print(n, base) + println(); }
  64. size_t println(int n, int base) { return print(n, base) + println(); }
  65. size_t println(unsigned int n, int base) { return print(n, base) + println(); }
  66. size_t println(long n, int base) { return print(n, base) + println(); }
  67. size_t println(unsigned long n, int base) { return print(n, base) + println(); }
  68. size_t println(double n, int digits = 2) { return print(n, digits) + println(); }
  69. size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
  70. int getWriteError() { return write_error; }
  71. void clearWriteError() { setWriteError(0); }
  72. protected:
  73. void setWriteError(int err = 1) { write_error = err; }
  74. private:
  75. char write_error;
  76. size_t printNumberDec(unsigned long n, uint8_t sign);
  77. size_t printNumberHex(unsigned long n);
  78. size_t printNumberBin(unsigned long n);
  79. size_t printNumberAny(unsigned long n, uint8_t base);
  80. inline size_t printNumber(unsigned long n, uint8_t sign, uint8_t base) __attribute__((always_inline)) {
  81. // when "base" is a constant (pretty much always), the
  82. // compiler optimizes this to a single function call.
  83. if (base == 0) return write((uint8_t)n);
  84. if (base == 10 || base < 2) return printNumberDec(n, sign);
  85. //if (base == 10 || base < 2) return printNumberAny(n, 10); // testing only
  86. if (base == 16) return printNumberHex(n);
  87. if (base == 2) return printNumberBin(n);
  88. return printNumberAny(n, base);
  89. }
  90. size_t printFloat(double n, uint8_t digits);
  91. };
  92. #else
  93. class Print
  94. {
  95. public:
  96. virtual void write(uint8_t);
  97. virtual void write(const char *str);
  98. virtual void write(const uint8_t *buffer, size_t size);
  99. void print(const String &s);
  100. void print(char c) { write((uint8_t)c); }
  101. void print(const char s[]) { write(s); }
  102. void print(const __FlashStringHelper *f);
  103. void print(uint8_t b) { write(b); }
  104. void print(int n) { print((long)n); }
  105. void print(unsigned int n) { printNumber(n, 0, 10); }
  106. void print(long n);
  107. void print(unsigned long n) { printNumber(n, 0, 10); }
  108. void print(unsigned char n, int base) { printNumber(n, 0, base); }
  109. void print(int n, int base) { (base == 10) ? print(n) : printNumber(n, 0, base); }
  110. void print(unsigned int n, int base) { printNumber(n, 0, base); }
  111. void print(long n, int base) { (base == 10) ? print(n) : printNumber(n, 0, base); }
  112. void print(unsigned long n, int base) { printNumber(n, 0, base); }
  113. void print(double n, int digits = 2) { printFloat(n, digits); }
  114. void println(void);
  115. void println(const String &s) { print(s); println(); }
  116. void println(char c) { print(c); println(); }
  117. void println(const char s[]) { print(s); println(); }
  118. void println(const __FlashStringHelper *f) { print(f); println(); }
  119. void println(uint8_t b) { print(b); println(); }
  120. void println(int n) { print(n); println(); }
  121. void println(unsigned int n) { print(n); println(); }
  122. void println(long n) { print(n); println(); }
  123. void println(unsigned long n) { print(n); println(); }
  124. void println(unsigned char n, int base) { print(n, base); println(); }
  125. void println(int n, int base) { print(n, base); println(); }
  126. void println(unsigned int n, int base) { print(n, base); println(); }
  127. void println(long n, int base) { print(n, base); println(); }
  128. void println(unsigned long n, int base) { print(n, base); println(); }
  129. void println(double n, int digits = 2) { print(n, digits); println(); }
  130. private:
  131. void printNumberDec(unsigned long n, uint8_t sign);
  132. void printNumberHex(unsigned long n);
  133. void printNumberBin(unsigned long n);
  134. void printNumberAny(unsigned long n, uint8_t base);
  135. inline size_t printNumber(unsigned long n, uint8_t sign, uint8_t base) __attribute__((always_inline)) {
  136. if (base == 0) { write((uint8_t)n); return; }
  137. if (base == 10 || base < 2) { printNumberDec(n, sign); return; }
  138. if (base == 16) { printNumberHex(n); return; }
  139. if (base == 2) { printNumberBin(n); return; }
  140. printNumberAny(n, base);
  141. }
  142. void printFloat(double n, uint8_t digits);
  143. };
  144. #endif
  145. #endif