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