|
40 | 40 | // or a time out occurs due to lack of input.
|
41 | 41 | enum LookaheadMode
|
42 | 42 | {
|
43 |
| - SKIP_ALL, // All invalid characters are ignored. |
44 |
| - SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. |
45 |
| - SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. |
| 43 | + SKIP_ALL, // All invalid characters are ignored. |
| 44 | + SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid. |
| 45 | + SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped. |
46 | 46 | };
|
47 | 47 |
|
48 | 48 | #define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
|
49 | 49 |
|
50 | 50 | class Stream : public Print
|
51 | 51 | {
|
52 |
| - protected: |
53 |
| - unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read |
54 |
| - unsigned long _startMillis = 0; // used for timeout measurement |
55 |
| - int timedRead(); // read stream with timeout |
56 |
| - int timedPeek(); // peek stream with timeout |
57 |
| - int peekNextDigit(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout |
58 |
| - |
59 |
| - public: |
60 |
| - virtual int available() = 0; |
61 |
| - virtual int read() = 0; |
62 |
| - virtual int peek() = 0; |
63 |
| - |
64 |
| - Stream() |
65 |
| - { |
66 |
| - _timeout = 1000; |
67 |
| - } |
68 |
| - |
69 |
| - // parsing methods |
70 |
| - |
71 |
| - void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second |
72 |
| - unsigned long getTimeout(void) |
73 |
| - { |
74 |
| - return _timeout; |
75 |
| - } |
76 |
| - |
77 |
| - bool find(char *target); // reads data from the stream until the target string is found |
78 |
| - bool find(uint8_t *target) |
79 |
| - { |
80 |
| - return find ((char *)target); |
81 |
| - } |
82 |
| - // returns true if target string is found, false if timed out (see setTimeout) |
83 |
| - |
84 |
| - bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found |
85 |
| - bool find(uint8_t *target, size_t length) |
86 |
| - { |
87 |
| - return find ((char *)target, length); |
88 |
| - } |
89 |
| - // returns true if target string is found, false if timed out |
90 |
| - |
91 |
| - bool find(char target) |
92 |
| - { |
93 |
| - return find (&target, 1); |
94 |
| - } |
95 |
| - |
96 |
| - bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found |
97 |
| - bool findUntil(uint8_t *target, char *terminator) |
98 |
| - { |
99 |
| - return findUntil((char *)target, terminator); |
100 |
| - } |
101 |
| - |
102 |
| - bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found |
103 |
| - bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) |
104 |
| - { |
105 |
| - return findUntil((char *)target, targetLen, terminate, termLen); |
106 |
| - } |
107 |
| - |
108 |
| - long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); |
109 |
| - // returns the first valid (long) integer value from the current position. |
110 |
| - // lookahead determines how parseInt looks ahead in the stream. |
111 |
| - // See LookaheadMode enumeration at the top of the file. |
112 |
| - // Lookahead is terminated by the first character that is not a valid part of an integer. |
113 |
| - // Once parsing commences, 'ignore' will be skipped in the stream. |
114 |
| - |
115 |
| - float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); |
116 |
| - // float version of parseInt |
117 |
| - |
118 |
| - size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer |
119 |
| - size_t readBytes( uint8_t *buffer, size_t length) |
120 |
| - { |
121 |
| - return readBytes((char *)buffer, length); |
122 |
| - } |
123 |
| - // terminates if length characters have been read or timeout (see setTimeout) |
124 |
| - // returns the number of characters placed in the buffer (0 means no valid data found) |
125 |
| - |
126 |
| - size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character |
127 |
| - size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) |
128 |
| - { |
129 |
| - return readBytesUntil(terminator, (char *)buffer, length); |
130 |
| - } |
131 |
| - // terminates if length characters have been read, timeout, or if the terminator character detected |
132 |
| - // returns the number of characters placed in the buffer (0 means no valid data found) |
133 |
| - |
134 |
| - // Arduino String functions to be added here |
135 |
| - String readString(); |
136 |
| - String readStringUntil(char terminator); |
137 |
| - |
138 |
| - protected: |
139 |
| - long parseInt(char ignore) |
140 |
| - { |
141 |
| - return parseInt(SKIP_ALL, ignore); |
142 |
| - } |
143 |
| - float parseFloat(char ignore) |
144 |
| - { |
145 |
| - return parseFloat(SKIP_ALL, ignore); |
146 |
| - } |
147 |
| - // These overload exists for compatibility with any class that has derived |
148 |
| - // Stream and used parseFloat/Int with a custom ignore character. To keep |
149 |
| - // the public API simple, these overload remains protected. |
150 |
| - |
151 |
| - struct MultiTarget |
152 |
| - { |
153 |
| - const char *str; // string you're searching for |
154 |
| - size_t len; // length of string you're searching for |
155 |
| - size_t index; // index used by the search routine. |
156 |
| - }; |
157 |
| - |
158 |
| - // This allows you to search for an arbitrary number of strings. |
159 |
| - // Returns index of the target that is found first or -1 if timeout occurs. |
160 |
| - int findMulti(struct MultiTarget *targets, int tCount); |
| 52 | + protected: |
| 53 | + unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read |
| 54 | + unsigned long _startMillis = 0; // used for timeout measurement |
| 55 | + int timedRead(); // read stream with timeout |
| 56 | + int timedPeek(); // peek stream with timeout |
| 57 | + int peekNextDigit(LookaheadMode lookahead, |
| 58 | + bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout |
| 59 | + |
| 60 | + public: |
| 61 | + virtual int available() = 0; |
| 62 | + virtual int read() = 0; |
| 63 | + virtual int peek() = 0; |
| 64 | + |
| 65 | + Stream() |
| 66 | + { |
| 67 | + _timeout = 1000; |
| 68 | + } |
| 69 | + |
| 70 | + // parsing methods |
| 71 | + |
| 72 | + void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second |
| 73 | + unsigned long getTimeout(void) |
| 74 | + { |
| 75 | + return _timeout; |
| 76 | + } |
| 77 | + |
| 78 | + bool find(char *target); // reads data from the stream until the target string is found |
| 79 | + bool find(uint8_t *target) |
| 80 | + { |
| 81 | + return find ((char *)target); |
| 82 | + } |
| 83 | + // returns true if target string is found, false if timed out (see setTimeout) |
| 84 | + |
| 85 | + bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found |
| 86 | + bool find(uint8_t *target, size_t length) |
| 87 | + { |
| 88 | + return find ((char *)target, length); |
| 89 | + } |
| 90 | + // returns true if target string is found, false if timed out |
| 91 | + |
| 92 | + bool find(char target) |
| 93 | + { |
| 94 | + return find (&target, 1); |
| 95 | + } |
| 96 | + |
| 97 | + bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found |
| 98 | + bool findUntil(uint8_t *target, char *terminator) |
| 99 | + { |
| 100 | + return findUntil((char *)target, terminator); |
| 101 | + } |
| 102 | + |
| 103 | + bool findUntil(char *target, size_t targetLen, char *terminate, |
| 104 | + size_t termLen); // as above but search ends if the terminate string is found |
| 105 | + bool findUntil(uint8_t *target, size_t targetLen, char *terminate, size_t termLen) |
| 106 | + { |
| 107 | + return findUntil((char *)target, targetLen, terminate, termLen); |
| 108 | + } |
| 109 | + |
| 110 | + long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); |
| 111 | + // returns the first valid (long) integer value from the current position. |
| 112 | + // lookahead determines how parseInt looks ahead in the stream. |
| 113 | + // See LookaheadMode enumeration at the top of the file. |
| 114 | + // Lookahead is terminated by the first character that is not a valid part of an integer. |
| 115 | + // Once parsing commences, 'ignore' will be skipped in the stream. |
| 116 | + |
| 117 | + float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR); |
| 118 | + // float version of parseInt |
| 119 | + |
| 120 | + size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer |
| 121 | + size_t readBytes( uint8_t *buffer, size_t length) |
| 122 | + { |
| 123 | + return readBytes((char *)buffer, length); |
| 124 | + } |
| 125 | + // terminates if length characters have been read or timeout (see setTimeout) |
| 126 | + // returns the number of characters placed in the buffer (0 means no valid data found) |
| 127 | + |
| 128 | + size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character |
| 129 | + size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) |
| 130 | + { |
| 131 | + return readBytesUntil(terminator, (char *)buffer, length); |
| 132 | + } |
| 133 | + // terminates if length characters have been read, timeout, or if the terminator character detected |
| 134 | + // returns the number of characters placed in the buffer (0 means no valid data found) |
| 135 | + |
| 136 | + // Arduino String functions to be added here |
| 137 | + String readString(); |
| 138 | + String readStringUntil(char terminator); |
| 139 | + |
| 140 | + protected: |
| 141 | + long parseInt(char ignore) |
| 142 | + { |
| 143 | + return parseInt(SKIP_ALL, ignore); |
| 144 | + } |
| 145 | + float parseFloat(char ignore) |
| 146 | + { |
| 147 | + return parseFloat(SKIP_ALL, ignore); |
| 148 | + } |
| 149 | + // These overload exists for compatibility with any class that has derived |
| 150 | + // Stream and used parseFloat/Int with a custom ignore character. To keep |
| 151 | + // the public API simple, these overload remains protected. |
| 152 | + |
| 153 | + struct MultiTarget |
| 154 | + { |
| 155 | + const char *str; // string you're searching for |
| 156 | + size_t len; // length of string you're searching for |
| 157 | + size_t index; // index used by the search routine. |
| 158 | + }; |
| 159 | + |
| 160 | + // This allows you to search for an arbitrary number of strings. |
| 161 | + // Returns index of the target that is found first or -1 if timeout occurs. |
| 162 | + int findMulti(struct MultiTarget *targets, int tCount); |
161 | 163 | };
|
162 | 164 |
|
163 | 165 | #undef NO_IGNORE_CHAR
|
|
0 commit comments