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

ArduinoStream.h 3.2KB

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