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.

11 vuotta sitten
11 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. class Print
  35. {
  36. public:
  37. constexpr Print() : write_error(0) {}
  38. virtual size_t write(uint8_t b) = 0;
  39. size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }
  40. virtual size_t write(const uint8_t *buffer, size_t size);
  41. virtual int availableForWrite(void) { return 0; }
  42. virtual void flush() { }
  43. size_t write(const char *buffer, size_t size) { return write((const uint8_t *)buffer, size); }
  44. size_t print(const String &s);
  45. size_t print(char c) { return write((uint8_t)c); }
  46. size_t print(const char s[]) { return write(s); }
  47. size_t print(const __FlashStringHelper *f);
  48. size_t print(uint8_t b) { return printNumber(b, 0, 10); }
  49. size_t print(int n) { return print((long)n); }
  50. size_t print(unsigned int n) { return printNumber(n, 0, 10); }
  51. size_t print(long n);
  52. size_t print(unsigned long n) { return printNumber(n, 0, 10); }
  53. size_t print(int64_t n);
  54. size_t print(uint64_t n) { return printNumber64(n, 10, 0); }
  55. size_t print(unsigned char n, int base) { return printNumber(n, 0, base); }
  56. size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
  57. size_t print(unsigned int n, int base) { return printNumber(n, 0, base); }
  58. size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
  59. size_t print(unsigned long n, int base) { return printNumber(n, 0, base); }
  60. size_t print(int64_t n, int base) { return (base == 10) ? print(n) : printNumber64(n, base, 0); }
  61. size_t print(uint64_t n, int base) { return printNumber64(n, base, 0); }
  62. size_t print(double n, int digits = 2) { return printFloat(n, digits); }
  63. size_t print(const Printable &obj) { return obj.printTo(*this); }
  64. size_t println(void);
  65. size_t println(const String &s) { return print(s) + println(); }
  66. size_t println(char c) { return print(c) + println(); }
  67. size_t println(const char s[]) { return print(s) + println(); }
  68. size_t println(const __FlashStringHelper *f) { return print(f) + println(); }
  69. size_t println(uint8_t b) { return print(b) + println(); }
  70. size_t println(int n) { return print(n) + println(); }
  71. size_t println(unsigned int n) { return print(n) + println(); }
  72. size_t println(long n) { return print(n) + println(); }
  73. size_t println(unsigned long n) { return print(n) + println(); }
  74. size_t println(int64_t n) { return print(n) + println(); }
  75. size_t println(uint64_t n) { return print(n) + println(); }
  76. size_t println(unsigned char n, int base) { return print(n, base) + println(); }
  77. size_t println(int n, int base) { return print(n, base) + println(); }
  78. size_t println(unsigned int n, int base) { return print(n, base) + println(); }
  79. size_t println(long n, int base) { return print(n, base) + println(); }
  80. size_t println(unsigned long n, int base) { return print(n, base) + println(); }
  81. size_t println(int64_t n, int base) { return print(n, base) + println(); }
  82. size_t println(uint64_t n, int base) { return print(n, base) + println(); }
  83. size_t println(double n, int digits = 2) { return print(n, digits) + println(); }
  84. size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
  85. int getWriteError() { return write_error; }
  86. void clearWriteError() { setWriteError(0); }
  87. int printf(const char *format, ...);
  88. int printf(const __FlashStringHelper *format, ...);
  89. protected:
  90. void setWriteError(int err = 1) { write_error = err; }
  91. private:
  92. char write_error;
  93. size_t printNumberDec(unsigned long n, uint8_t sign);
  94. size_t printNumberHex(unsigned long n);
  95. size_t printNumberBin(unsigned long n);
  96. size_t printNumberAny(unsigned long n, uint8_t base);
  97. inline size_t printNumber(unsigned long n, uint8_t sign, uint8_t base) __attribute__((always_inline)) {
  98. // when "base" is a constant (pretty much always), the
  99. // compiler optimizes this to a single function call.
  100. if (base == 0) return write((uint8_t)n);
  101. if (base == 10 || base < 2) return printNumberDec(n, sign);
  102. //if (base == 10 || base < 2) return printNumberAny(n, 10); // testing only
  103. if (base == 16) return printNumberHex(n);
  104. if (base == 2) return printNumberBin(n);
  105. return printNumberAny(n, base);
  106. }
  107. size_t printNumber64(uint64_t n, uint8_t base, uint8_t sign);
  108. size_t printFloat(double n, uint8_t digits);
  109. };
  110. #endif