29
29
// private method to read stream with timeout
30
30
int Stream::timedRead ()
31
31
{
32
- // Serial.println(_timeout);
33
- this ->_startMillis = millis ();
34
- while (millis () - this ->_startMillis < this ->_timeout )
35
- {
36
- if (this ->available () > 0 ) {
37
- return this ->read ();
38
- }
39
- }
32
+ int c;
33
+ _startMillis = millis ();
34
+ do {
35
+ c = read ();
36
+ if (c >= 0 ) return c;
37
+ } while (millis () - _startMillis < _timeout);
38
+ return -1 ; // -1 indicates timeout
39
+ }
40
+
41
+ // private method to peek stream with timeout
42
+ int Stream::timedPeek ()
43
+ {
44
+ int c;
45
+ _startMillis = millis ();
46
+ do {
47
+ c = peek ();
48
+ if (c >= 0 ) return c;
49
+ } while (millis () - _startMillis < _timeout);
40
50
return -1 ; // -1 indicates timeout
41
51
}
42
52
43
- // returns the next digit in the stream or -1 if timeout
53
+ // returns peek of the next digit in the stream or -1 if timeout
44
54
// discards non-numeric characters
45
- int Stream::getNextDigit ()
55
+ int Stream::peekNextDigit ()
46
56
{
47
57
int c;
48
- do {
49
- c = timedRead ();
50
- if ( c < 0 )
51
- return c; // timeout
58
+ while (1 ) {
59
+ c = timedPeek ();
60
+ if (c < 0 ) return c; // timeout
61
+ if (c == ' -' ) return c;
62
+ if (c >= ' 0' && c <= ' 9' ) return c;
63
+ read (); // discard non-numeric
52
64
}
53
- while ( c != ' -' && (c < ' 0' || c > ' 9' ) ) ;
54
-
55
- return c;
56
65
}
57
66
58
67
// Public Methods
@@ -130,7 +139,7 @@ long Stream::parseInt(char skipChar)
130
139
long value = 0 ;
131
140
int c;
132
141
133
- c = getNextDigit ();
142
+ c = peekNextDigit ();
134
143
// ignore non numeric leading characters
135
144
if (c < 0 )
136
145
return 0 ; // zero returned if timeout
@@ -142,9 +151,10 @@ long Stream::parseInt(char skipChar)
142
151
isNegative = true ;
143
152
else if (c >= ' 0' && c <= ' 9' ) // is c a digit?
144
153
value = value * 10 + c - ' 0' ;
145
- c = timedRead ();
154
+ read (); // consume the character we got with peek
155
+ c = timedPeek ();
146
156
}
147
- while ( (c >= ' 0' && c <= ' 9' ) || c == skipChar );
157
+ while ( (c >= ' 0' && c <= ' 9' ) || c == skipChar );
148
158
149
159
if (isNegative)
150
160
value = -value;
@@ -168,7 +178,7 @@ float Stream::parseFloat(char skipChar){
168
178
char c;
169
179
float fraction = 1.0 ;
170
180
171
- c = getNextDigit ();
181
+ c = peekNextDigit ();
172
182
// ignore non numeric leading characters
173
183
if (c < 0 )
174
184
return 0 ; // zero returned if timeout
@@ -185,7 +195,8 @@ float Stream::parseFloat(char skipChar){
185
195
if (isFraction)
186
196
fraction *= 0.1 ;
187
197
}
188
- c = timedRead ();
198
+ read (); // consume the character we got with peek
199
+ c = timedPeek ();
189
200
}
190
201
while ( (c >= ' 0' && c <= ' 9' ) || c == ' .' || c == skipChar );
191
202
0 commit comments