26 #define PARSE_TIMEOUT 1000 // default number of milli-seconds to wait
27 #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
30 int Stream::timedRead()
33 _startMillis = millis();
37 }
while(millis() - _startMillis < _timeout);
42 int Stream::timedPeek()
45 _startMillis = millis();
49 }
while(millis() - _startMillis < _timeout);
55 int Stream::peekNextDigit()
61 if (c ==
'-')
return c;
62 if (c >=
'0' && c <=
'9')
return c;
70 void Stream::setTimeout(
unsigned long timeout)
76 bool Stream::find(
char *target)
78 return findUntil(target, NULL);
83 bool Stream::find(
char *target,
size_t length)
85 return findUntil(target, length, NULL, 0);
89 bool Stream::findUntil(
char *target,
char *terminator)
91 return findUntil(target, strlen(target), terminator, strlen(terminator));
97 bool Stream::findUntil(
char *target,
size_t targetLen,
char *terminator,
size_t termLen)
100 size_t termIndex = 0;
105 while( (c = timedRead()) > 0){
107 if(c != target[index])
110 if( c == target[index]){
112 if(++index >= targetLen){
117 if(termLen > 0 && c == terminator[termIndex]){
118 if(++termIndex >= termLen)
131 long Stream::parseInt()
133 return parseInt(NO_SKIP_CHAR);
138 long Stream::parseInt(
char skipChar)
140 boolean isNegative =
false;
154 else if(c >=
'0' && c <=
'9')
155 value = value * 10 + c -
'0';
159 while( (c >=
'0' && c <=
'9') || c == skipChar );
168 float Stream::parseFloat()
170 return parseFloat(NO_SKIP_CHAR);
175 float Stream::parseFloat(
char skipChar){
176 boolean isNegative =
false;
177 boolean isFraction =
false;
180 float fraction = 1.0;
194 else if(c >=
'0' && c <=
'9') {
195 value = value * 10 + c -
'0';
202 while( (c >=
'0' && c <=
'9') || c ==
'.' || c == skipChar );
207 return value * fraction;
217 size_t Stream::readBytes(
char *buffer,
size_t length)
220 while (
count < length) {
234 size_t Stream::readBytesUntil(
char terminator,
char *buffer,
size_t length)
236 if (length < 1)
return 0;
238 while (index < length) {
240 if (c < 0 || c == terminator)
break;
247 String Stream::readString()
259 String Stream::readStringUntil(
char terminator)
263 while (c >= 0 && c != terminator)