Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
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.

Stream.h 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. Stream.h - base class for character-based streams.
  3. Copyright (c) 2010 David A. Mellis. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. parsing functions based on TextFinder library by Michael Margolis
  16. Modified August 2016 by Marcelo Aquino <marceloaqno@gmail.org> for MySensors use
  17. */
  18. #ifndef Stream_h
  19. #define Stream_h
  20. #include <inttypes.h>
  21. #include <string>
  22. #include "Print.h"
  23. // compatability macros for testing
  24. /*
  25. #define getInt() parseInt()
  26. #define getInt(skipChar) parseInt(skipchar)
  27. #define getFloat() parseFloat()
  28. #define getFloat(skipChar) parseFloat(skipChar)
  29. #define getString( pre_string, post_string, buffer, length)
  30. readBytesBetween( pre_string, terminator, buffer, length)
  31. */
  32. #if !DOXYGEN
  33. class Stream: public Print
  34. {
  35. protected:
  36. unsigned long
  37. _timeout; // number of milliseconds to wait for the next char before aborting timed read
  38. unsigned long _startMillis; // used for timeout measurement
  39. int timedRead(); // private method to read stream with timeout
  40. int timedPeek(); // private method to peek stream with timeout
  41. int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
  42. public:
  43. virtual int available() = 0;
  44. virtual int read() = 0;
  45. virtual int peek() = 0;
  46. virtual void flush() = 0;
  47. Stream()
  48. {
  49. _timeout = 1000;
  50. _startMillis = 0;
  51. }
  52. // parsing methods
  53. void setTimeout(unsigned long
  54. timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
  55. bool find(const char *target); // reads data from the stream until the target string is found
  56. bool find(uint8_t *target)
  57. {
  58. return find((char *) target);
  59. }
  60. // returns true if target string is found, false if timed out (see setTimeout)
  61. bool find(const char *target, size_t
  62. length); // reads data from the stream until the target string of given length is found
  63. bool find(const uint8_t *target, size_t length)
  64. {
  65. return find((char *) target, length);
  66. }
  67. // returns true if target string is found, false if timed out
  68. bool find(char target)
  69. {
  70. return find (&target, 1);
  71. }
  72. bool findUntil(const char *target,
  73. const char *terminator); // as find but search ends if the terminator string is found
  74. bool findUntil(const uint8_t *target, const char *terminator)
  75. {
  76. return findUntil((char *) target, terminator);
  77. }
  78. bool findUntil(const char *target, size_t targetLen, const char *terminate,
  79. size_t termLen); // as above but search ends if the terminate string is found
  80. bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen)
  81. {
  82. return findUntil((char *) target, targetLen, terminate, termLen);
  83. }
  84. long parseInt(); // returns the first valid (long) integer value from the current position.
  85. // initial characters that are not digits (or the minus sign) are skipped
  86. // integer is terminated by the first character that is not a digit.
  87. float parseFloat(); // float version of parseInt
  88. virtual size_t readBytes(char *buffer, size_t length); // read chars from stream into buffer
  89. virtual size_t readBytes(uint8_t *buffer, size_t length)
  90. {
  91. return readBytes((char *) buffer, length);
  92. }
  93. // terminates if length characters have been read or timeout (see setTimeout)
  94. // returns the number of characters placed in the buffer (0 means no valid data found)
  95. size_t readBytesUntil(char terminator, char *buffer,
  96. size_t length); // as readBytes with terminator character
  97. size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length)
  98. {
  99. return readBytesUntil(terminator, (char *) buffer, length);
  100. }
  101. // terminates if length characters have been read, timeout, or if the terminator character detected
  102. // returns the number of characters placed in the buffer (0 means no valid data found)
  103. // Arduino String functions to be added here
  104. std::string readString();
  105. std::string readStringUntil(char terminator);
  106. protected:
  107. long parseInt(char skipChar); // as above but the given skipChar is ignored
  108. // as above but the given skipChar is ignored
  109. // this allows format characters (typically commas) in values to be ignored
  110. float parseFloat(char skipChar); // as above but the given skipChar is ignored
  111. };
  112. #endif
  113. #endif