Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

Print.h 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(unsigned char n, int base) { return printNumber(n, 0, base); }
  54. size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
  55. size_t print(unsigned int n, int base) { return printNumber(n, 0, base); }
  56. size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, 0, base); }
  57. size_t print(unsigned long n, int base) { return printNumber(n, 0, base); }
  58. size_t print(double n, int digits = 2) { return printFloat(n, digits); }
  59. size_t print(const Printable &obj) { return obj.printTo(*this); }
  60. size_t println(void);
  61. size_t println(const String &s) { return print(s) + println(); }
  62. size_t println(char c) { return print(c) + println(); }
  63. size_t println(const char s[]) { return print(s) + println(); }
  64. size_t println(const __FlashStringHelper *f) { return print(f) + println(); }
  65. size_t println(uint8_t b) { return print(b) + println(); }
  66. size_t println(int n) { return print(n) + println(); }
  67. size_t println(unsigned int n) { return print(n) + println(); }
  68. size_t println(long n) { return print(n) + println(); }
  69. size_t println(unsigned long n) { return print(n) + println(); }
  70. size_t println(unsigned char n, int base) { return print(n, base) + println(); }
  71. size_t println(int n, int base) { return print(n, base) + println(); }
  72. size_t println(unsigned int n, int base) { return print(n, base) + println(); }
  73. size_t println(long n, int base) { return print(n, base) + println(); }
  74. size_t println(unsigned long n, int base) { return print(n, base) + println(); }
  75. size_t println(double n, int digits = 2) { return print(n, digits) + println(); }
  76. size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
  77. int getWriteError() { return write_error; }
  78. void clearWriteError() { setWriteError(0); }
  79. int printf(const char *format, ...);
  80. int printf(const __FlashStringHelper *format, ...);
  81. protected:
  82. void setWriteError(int err = 1) { write_error = err; }
  83. private:
  84. char write_error;
  85. size_t printNumberDec(unsigned long n, uint8_t sign);
  86. size_t printNumberHex(unsigned long n);
  87. size_t printNumberBin(unsigned long n);
  88. size_t printNumberAny(unsigned long n, uint8_t base);
  89. inline size_t printNumber(unsigned long n, uint8_t sign, uint8_t base) __attribute__((always_inline)) {
  90. // when "base" is a constant (pretty much always), the
  91. // compiler optimizes this to a single function call.
  92. if (base == 0) return write((uint8_t)n);
  93. if (base == 10 || base < 2) return printNumberDec(n, sign);
  94. //if (base == 10 || base < 2) return printNumberAny(n, 10); // testing only
  95. if (base == 16) return printNumberHex(n);
  96. if (base == 2) return printNumberBin(n);
  97. return printNumberAny(n, base);
  98. }
  99. size_t printFloat(double n, uint8_t digits);
  100. };
  101. #endif