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.

преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
преди 10 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) {
  57. goto done;
  58. }
  59. }
  60. if (i >= (m_size - 1)) {
  61. setstate(failbit);
  62. return;
  63. }
  64. m_line[i++] = m_hw->read();
  65. m_line[i] = '\0';
  66. }
  67. done:
  68. init(m_line);
  69. }
  70. protected:
  71. /** Internal - do not use.
  72. * \param[in] off
  73. * \param[in] way
  74. * \return true/false.
  75. */
  76. bool seekoff(off_type off, seekdir way) {
  77. return false;
  78. }
  79. /** Internal - do not use.
  80. * \param[in] pos
  81. * \return true/false.
  82. */
  83. bool seekpos(pos_type pos) {
  84. return false;
  85. }
  86. private:
  87. char *m_line;
  88. size_t m_size;
  89. Stream* m_hw;
  90. };
  91. //==============================================================================
  92. /**
  93. * \class ArduinoOutStream
  94. * \brief Output stream for Arduino Print objects
  95. */
  96. class ArduinoOutStream : public ostream {
  97. public:
  98. /** constructor
  99. *
  100. * \param[in] pr Print object for this ArduinoOutStream.
  101. */
  102. explicit ArduinoOutStream(Print& pr) : m_pr(&pr) {}
  103. protected:
  104. /// @cond SHOW_PROTECTED
  105. /**
  106. * Internal do not use
  107. * \param[in] c
  108. */
  109. void putch(char c) {
  110. if (c == '\n') {
  111. m_pr->write('\r');
  112. }
  113. m_pr->write(c);
  114. }
  115. void putstr(const char* str) {
  116. m_pr->write(str);
  117. }
  118. bool seekoff(off_type off, seekdir way) {
  119. return false;
  120. }
  121. bool seekpos(pos_type pos) {
  122. return false;
  123. }
  124. bool sync() {
  125. return true;
  126. }
  127. pos_type tellpos() {
  128. return 0;
  129. }
  130. /// @endcond
  131. private:
  132. ArduinoOutStream() {}
  133. Print* m_pr;
  134. };
  135. #endif // ARDUINO
  136. #endif // ArduinoStream_h