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