Teensy 4.1 core updated for C++20
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.

Print.h 7.2KB

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