PrintBasic.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Copyright (c) 2011-2020 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef PrintBasic_h
  26. #define PrintBasic_h
  27. /**
  28. * \file
  29. * \brief Stream/Print like replacement for non-Arduino systems.
  30. */
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. #include <string.h>
  34. #include "../SdFatConfig.h"
  35. #ifndef F
  36. #if defined(__AVR__)
  37. #include <avr/pgmspace.h>
  38. class __FlashStringHelper;
  39. #define F(string_literal) \
  40. (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))
  41. #else // defined(__AVR__)
  42. #define F(str) (str)
  43. #endif // defined(__AVR__)
  44. #endif // F
  45. #ifdef BIN
  46. #undef BIN
  47. #endif // BIN
  48. #define BIN 2
  49. #define OCT 8
  50. #define DEC 10
  51. #define HEX 16
  52. class PrintBasic {
  53. public:
  54. PrintBasic() : m_error(0) {}
  55. void clearWriteError() { setWriteError(0); }
  56. int getWriteError() { return m_error; }
  57. size_t print(char c) { return write(c); }
  58. size_t print(const char *str) { return write(str); }
  59. size_t print(const __FlashStringHelper *str) {
  60. #ifdef __AVR__
  61. PGM_P p = reinterpret_cast<PGM_P>(str);
  62. size_t n = 0;
  63. for (uint8_t c; (c = pgm_read_byte(p + n)) && write(c); n++) {
  64. }
  65. return n;
  66. #else // __AVR__
  67. return print(reinterpret_cast<const char *>(str));
  68. #endif // __AVR__
  69. }
  70. size_t println(const __FlashStringHelper *str) {
  71. #ifdef __AVR__
  72. return print(str) + println();
  73. #else // __AVR__
  74. return println(reinterpret_cast<const char *>(str));
  75. #endif // __AVR__
  76. }
  77. size_t print(double n, uint8_t prec = 2) { return printDouble(n, prec); }
  78. size_t print(signed char n, uint8_t base = 10) {
  79. return print((long)n, base);
  80. }
  81. size_t print(unsigned char n, uint8_t base = 10) {
  82. return print((unsigned long)n, base);
  83. }
  84. size_t print(int n, uint8_t base = 10) { return print((long)n, base); }
  85. size_t print(unsigned int n, uint8_t base = 10) {
  86. return print((unsigned long)n, base);
  87. }
  88. size_t print(long n, uint8_t base = 10);
  89. size_t print(unsigned long n, uint8_t base = 10) { return printNum(n, base); }
  90. size_t println() { return write("\r\n"); }
  91. size_t println(char c) { return write(c) + println(); }
  92. size_t println(const char *str) { return print(str) + println(); }
  93. size_t println(double n, uint8_t prec = 2) {
  94. return print(n, prec) + println();
  95. }
  96. size_t println(signed char n, uint8_t base = 10) {
  97. return print(n, base) + println();
  98. }
  99. size_t println(unsigned char n, uint8_t base = 10) {
  100. return print(n, base) + println();
  101. }
  102. size_t println(int n, uint8_t base = 10) {
  103. return print(n, base) + println();
  104. }
  105. size_t println(unsigned int n, uint8_t base = 10) {
  106. return print(n, base) + println();
  107. }
  108. size_t println(long n, uint8_t base = 10) {
  109. return print(n, base) + println();
  110. }
  111. size_t println(unsigned long n, uint8_t base = 10) {
  112. return print(n, base) + println();
  113. }
  114. size_t write(const char *str) { return write(str, strlen(str)); }
  115. virtual size_t write(uint8_t b) = 0;
  116. virtual size_t write(const uint8_t *buffer, size_t size) {
  117. size_t i;
  118. for (i = 0; i < size; i++) {
  119. if (!write(buffer[i])) break;
  120. }
  121. return i;
  122. }
  123. size_t write(const char *buffer, size_t size) {
  124. return write(reinterpret_cast<const uint8_t *>(buffer), size);
  125. }
  126. protected:
  127. void setWriteError(int err = 1) { m_error = err; }
  128. private:
  129. size_t printDouble(double n, uint8_t prec);
  130. size_t printNum(unsigned long n, uint8_t base);
  131. int m_error;
  132. };
  133. //------------------------------------------------------------------------------
  134. class StreamBasic : public PrintBasic {
  135. public:
  136. virtual int available() = 0;
  137. virtual int peek() = 0;
  138. virtual int read() = 0;
  139. };
  140. #endif // PrintBasic_h