You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.0KB

  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef ArduinoStream_h
  21. #define ArduinoStream_h
  22. /**
  23. * \file
  24. * \brief ArduinoInStream and ArduinoOutStream classes
  25. */
  26. #include <bufstream.h>
  27. //==============================================================================
  28. /**
  29. * \class ArduinoInStream
  30. * \brief Input stream for Arduino Stream objects
  31. */
  32. class ArduinoInStream : public ibufstream {
  33. public:
  34. /**
  35. * Constructor
  36. * \param[in] hws hardware stream
  37. * \param[in] buf buffer for input line
  38. * \param[in] size size of input buffer
  39. */
  40. ArduinoInStream(Stream &hws, char* buf, size_t size) {
  41. m_hw = &hws;
  42. m_line = buf;
  43. m_size = size;
  44. }
  45. /** read a line. */
  46. void readline() {
  47. size_t i = 0;
  48. uint32_t t;
  49. m_line[0] = '\0';
  50. while (!m_hw->available()) {}
  51. while (1) {
  52. t = millis();
  53. while (!m_hw->available()) {
  54. if ((millis() - t) > 10) goto done;
  55. }
  56. if (i >= (m_size - 1)) {
  57. setstate(failbit);
  58. return;
  59. }
  60. m_line[i++] = m_hw->read();
  61. m_line[i] = '\0';
  62. }
  63. done:
  64. init(m_line);
  65. }
  66. protected:
  67. /** Internal - do not use.
  68. * \param[in] off
  69. * \param[in] way
  70. * \return true/false.
  71. */
  72. bool seekoff(off_type off, seekdir way) {return false;}
  73. /** Internal - do not use.
  74. * \param[in] pos
  75. * \return true/false.
  76. */
  77. bool seekpos(pos_type pos) {return false;}
  78. private:
  79. char *m_line;
  80. size_t m_size;
  81. Stream* m_hw;
  82. };
  83. //==============================================================================
  84. /**
  85. * \class ArduinoOutStream
  86. * \brief Output stream for Arduino Print objects
  87. */
  88. class ArduinoOutStream : public ostream {
  89. public:
  90. /** constructor
  91. *
  92. * \param[in] pr Print object for this ArduinoOutStream.
  93. */
  94. explicit ArduinoOutStream(Print& pr) : m_pr(&pr) {}
  95. protected:
  96. /// @cond SHOW_PROTECTED
  97. /**
  98. * Internal do not use
  99. * \param[in] c
  100. */
  101. void putch(char c) {
  102. if (c == '\n') m_pr->write('\r');
  103. m_pr->write(c);
  104. }
  105. void putstr(const char* str) {m_pr->write(str);}
  106. bool seekoff(off_type off, seekdir way) {return false;}
  107. bool seekpos(pos_type pos) {return false;}
  108. bool sync() {return true;}
  109. pos_type tellpos() {return 0;}
  110. /// @endcond
  111. private:
  112. ArduinoOutStream() {}
  113. Print* m_pr;
  114. };
  115. #endif // ArduinoStream_h