ArduinoStream.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * Copyright (c) 2011-2021 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 ArduinoStream_h
  26. #define ArduinoStream_h
  27. /**
  28. * \file
  29. * \brief ArduinoInStream and ArduinoOutStream classes
  30. */
  31. #include "bufstream.h"
  32. //==============================================================================
  33. /**
  34. * \class ArduinoInStream
  35. * \brief Input stream for Arduino Stream objects
  36. */
  37. class ArduinoInStream : public ibufstream {
  38. public:
  39. /**
  40. * Constructor
  41. * \param[in] hws hardware stream
  42. * \param[in] buf buffer for input line
  43. * \param[in] size size of input buffer
  44. */
  45. ArduinoInStream(Stream &hws, char* buf, size_t size) {
  46. m_hw = &hws;
  47. m_line = buf;
  48. m_size = size;
  49. }
  50. /** read a line. */
  51. void readline() {
  52. size_t i = 0;
  53. uint32_t t;
  54. m_line[0] = '\0';
  55. while (!m_hw->available()) {
  56. yield();
  57. }
  58. while (1) {
  59. t = millis();
  60. while (!m_hw->available()) {
  61. if ((millis() - t) > 10) {
  62. goto done;
  63. }
  64. }
  65. if (i >= (m_size - 1)) {
  66. setstate(failbit);
  67. return;
  68. }
  69. m_line[i++] = m_hw->read();
  70. m_line[i] = '\0';
  71. }
  72. done:
  73. init(m_line);
  74. }
  75. protected:
  76. /** Internal - do not use.
  77. * \param[in] off
  78. * \param[in] way
  79. * \return true/false.
  80. */
  81. bool seekoff(off_type off, seekdir way) {
  82. (void)off;
  83. (void)way;
  84. return false;
  85. }
  86. /** Internal - do not use.
  87. * \param[in] pos
  88. * \return true/false.
  89. */
  90. bool seekpos(pos_type pos) {
  91. (void)pos;
  92. return false;
  93. }
  94. private:
  95. char *m_line;
  96. size_t m_size;
  97. Stream* m_hw;
  98. };
  99. //==============================================================================
  100. /**
  101. * \class ArduinoOutStream
  102. * \brief Output stream for Arduino Print objects
  103. */
  104. class ArduinoOutStream : public ostream {
  105. public:
  106. /** constructor
  107. *
  108. * \param[in] pr Print object for this ArduinoOutStream.
  109. */
  110. explicit ArduinoOutStream(print_t& pr) : m_pr(&pr) {}
  111. protected:
  112. /// @cond SHOW_PROTECTED
  113. /**
  114. * Internal do not use
  115. * \param[in] c
  116. */
  117. void putch(char c) {
  118. if (c == '\n') {
  119. m_pr->write('\r');
  120. }
  121. m_pr->write(c);
  122. }
  123. void putstr(const char* str) {
  124. m_pr->write(str);
  125. }
  126. bool seekoff(off_type off, seekdir way) {
  127. (void)off;
  128. (void)way;
  129. return false;
  130. }
  131. bool seekpos(pos_type pos) {
  132. (void)pos;
  133. return false;
  134. }
  135. bool sync() {
  136. return true;
  137. }
  138. pos_type tellpos() {
  139. return 0;
  140. }
  141. /// @endcond
  142. private:
  143. ArduinoOutStream() {}
  144. print_t* m_pr;
  145. };
  146. #endif // ArduinoStream_h