選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

123 行
3.0KB

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