ArduinoStream.h 3.7 KB

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