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 | */
|
---|
21 |
|
---|
22 | #ifndef Stream_h
|
---|
23 | #define Stream_h
|
---|
24 |
|
---|
25 | #include <inttypes.h>
|
---|
26 | #include "Print.h"
|
---|
27 |
|
---|
28 |
|
---|
29 | class Stream : public Print
|
---|
30 | {
|
---|
31 | protected:
|
---|
32 | unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
|
---|
33 | unsigned long _startMillis; // used for timeout measurement
|
---|
34 | int timedRead(); // private method to read stream with timeout
|
---|
35 | int timedPeek(); // private method to peek stream with timeout
|
---|
36 | int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
|
---|
37 |
|
---|
38 | public:
|
---|
39 | virtual int available() = 0;
|
---|
40 | virtual int read() = 0;
|
---|
41 | virtual int peek() = 0;
|
---|
42 | virtual void flush() = 0;
|
---|
43 |
|
---|
44 | Stream() {_timeout=1000;}
|
---|
45 |
|
---|
46 | // parsing methods
|
---|
47 |
|
---|
48 | void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
|
---|
49 |
|
---|
50 | bool find(char *target); // reads data from the stream until the target string is found
|
---|
51 | bool find(uint8_t *target) { return find ((char *)target); }
|
---|
52 | // returns true if target string is found, false if timed out (see setTimeout)
|
---|
53 |
|
---|
54 | bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
|
---|
55 | bool find(uint8_t *target, size_t length) { return find ((char *)target, length); }
|
---|
56 | // returns true if target string is found, false if timed out
|
---|
57 |
|
---|
58 | bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
|
---|
59 | bool findUntil(uint8_t *target, char *terminator) { return findUntil((char *)target, terminator); }
|
---|
60 |
|
---|
61 | bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
|
---|
62 | bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) {return findUntil((char *)target, targetLen, terminate, termLen); }
|
---|
63 |
|
---|
64 |
|
---|
65 | long parseInt(); // returns the first valid (long) integer value from the current position.
|
---|
66 | // initial characters that are not digits (or the minus sign) are skipped
|
---|
67 | // integer is terminated by the first character that is not a digit.
|
---|
68 |
|
---|
69 | float parseFloat(); // float version of parseInt
|
---|
70 |
|
---|
71 | size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
|
---|
72 | size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
|
---|
73 | // terminates if length characters have been read or timeout (see setTimeout)
|
---|
74 | // returns the number of characters placed in the buffer (0 means no valid data found)
|
---|
75 |
|
---|
76 | size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
|
---|
77 | size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
|
---|
78 | // terminates if length characters have been read, timeout, or if the terminator character detected
|
---|
79 | // returns the number of characters placed in the buffer (0 means no valid data found)
|
---|
80 |
|
---|
81 | // Arduino String functions to be added here
|
---|
82 | String readString();
|
---|
83 | String readStringUntil(char terminator);
|
---|
84 |
|
---|
85 | protected:
|
---|
86 | long parseInt(char skipChar); // as above but the given skipChar is ignored
|
---|
87 | // as above but the given skipChar is ignored
|
---|
88 | // this allows format characters (typically commas) in values to be ignored
|
---|
89 |
|
---|
90 | float parseFloat(char skipChar); // as above but the given skipChar is ignored
|
---|
91 | };
|
---|
92 |
|
---|
93 | #endif
|
---|