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

144 lines
6.3KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #ifndef Print_h
  31. #define Print_h
  32. #include <inttypes.h>
  33. #include <stdio.h> // for size_t - gives sprintf and other stuff to all sketches & libs
  34. #include <stdarg.h>
  35. #include "core_id.h"
  36. #include "WString.h"
  37. #include "Printable.h"
  38. #define DEC 10
  39. #define HEX 16
  40. #define OCT 8
  41. #define BIN 2
  42. // BYTE was defined in very old versions of Arduino
  43. // maybe this now causes more trouble than it's worth?
  44. //#ifndef BYTE
  45. //#define BYTE 0
  46. //#endif
  47. class __FlashStringHelper;
  48. class Print
  49. {
  50. public:
  51. constexpr Print() : write_error(0) {}
  52. virtual size_t write(uint8_t b) = 0;
  53. size_t write(const char *str) { return write((const uint8_t *)str, strlen(str)); }
  54. virtual size_t write(const uint8_t *buffer, size_t size);
  55. virtual int availableForWrite(void) { return 0; }
  56. virtual void flush() { }
  57. size_t write(const char *buffer, size_t size) { return write((const uint8_t *)buffer, size); }
  58. size_t print(const String &s);
  59. size_t print(char c) { return write((uint8_t)c); }
  60. size_t print(const char s[]) { return write(s); }
  61. size_t print(const __FlashStringHelper *f) { return write((const char *)f); }
  62. size_t print(uint8_t b) { return printNumber(b, 10, 0); }
  63. size_t print(int n) { return print((long)n); }
  64. size_t print(unsigned int n) { return printNumber(n, 10, 0); }
  65. size_t print(long n);
  66. size_t print(unsigned long n) { return printNumber(n, 10, 0); }
  67. size_t print(int64_t n);
  68. size_t print(uint64_t n) { return printNumber64(n, 10, 0); }
  69. size_t print(unsigned char n, int base) { return printNumber(n, base, 0); }
  70. size_t print(int n, int base) { return (base == 10) ? print(n) : printNumber(n, base, 0); }
  71. size_t print(unsigned int n, int base) { return printNumber(n, base, 0); }
  72. size_t print(long n, int base) { return (base == 10) ? print(n) : printNumber(n, base, 0); }
  73. size_t print(unsigned long n, int base) { return printNumber(n, base, 0); }
  74. size_t print(int64_t n, int base) { return (base == 10) ? print(n) : printNumber64(n, base, 0); }
  75. size_t print(uint64_t n, int base) { return printNumber64(n, base, 0); }
  76. size_t print(double n, int digits = 2) { return printFloat(n, digits); }
  77. size_t print(const Printable &obj) { return obj.printTo(*this); }
  78. size_t println(void);
  79. size_t println(const String &s) { return print(s) + println(); }
  80. size_t println(char c) { return print(c) + println(); }
  81. size_t println(const char s[]) { return print(s) + println(); }
  82. size_t println(const __FlashStringHelper *f) { return print(f) + println(); }
  83. size_t println(uint8_t b) { return print(b) + println(); }
  84. size_t println(int n) { return print(n) + println(); }
  85. size_t println(unsigned int n) { return print(n) + println(); }
  86. size_t println(long n) { return print(n) + println(); }
  87. size_t println(unsigned long n) { return print(n) + println(); }
  88. size_t println(int64_t n) { return print(n) + println(); }
  89. size_t println(uint64_t n) { return print(n) + println(); }
  90. size_t println(unsigned char n, int base) { return print(n, base) + println(); }
  91. size_t println(int n, int base) { return print(n, base) + println(); }
  92. size_t println(unsigned int n, int base) { return print(n, base) + println(); }
  93. size_t println(long n, int base) { return print(n, base) + println(); }
  94. size_t println(unsigned long n, int base) { return print(n, base) + println(); }
  95. size_t println(int64_t n, int base) { return print(n, base) + println(); }
  96. size_t println(uint64_t n, int base) { return print(n, base) + println(); }
  97. size_t println(double n, int digits = 2) { return print(n, digits) + println(); }
  98. size_t println(const Printable &obj) { return obj.printTo(*this) + println(); }
  99. int getWriteError() { return write_error; }
  100. void clearWriteError() { setWriteError(0); }
  101. // format warnings are too pedantic - disable until newer toolchain offers better...
  102. // https://forum.pjrc.com/threads/62473?p=256873&viewfull=1#post256873
  103. int printf(const char *format, ...) /*__attribute__ ((format (printf, 2, 3)))*/;
  104. int printf(const __FlashStringHelper *format, ...);
  105. protected:
  106. void setWriteError(int err = 1) { write_error = err; }
  107. private:
  108. char write_error;
  109. size_t printFloat(double n, uint8_t digits);
  110. #ifdef __MKL26Z64__
  111. size_t printNumberDec(unsigned long n, uint8_t sign);
  112. size_t printNumberHex(unsigned long n);
  113. size_t printNumberBin(unsigned long n);
  114. size_t printNumberAny(unsigned long n, uint8_t base);
  115. inline size_t printNumber(unsigned long n, uint8_t base, uint8_t sign) __attribute__((always_inline)) {
  116. // when "base" is a constant (pretty much always), the
  117. // compiler optimizes this to a single function call.
  118. if (base == 0) return write((uint8_t)n);
  119. if (base == 10 || base < 2) return printNumberDec(n, sign);
  120. if (base == 16) return printNumberHex(n);
  121. if (base == 2) return printNumberBin(n);
  122. return printNumberAny(n, base);
  123. }
  124. #else
  125. size_t printNumber(unsigned long n, uint8_t base, uint8_t sign);
  126. #endif
  127. size_t printNumber64(uint64_t n, uint8_t base, uint8_t sign);
  128. };
  129. #endif