Merge branch 'new-extension' of https://github.com/arduino/Arduino into new-extension

This commit is contained in:
Zach Eveland 2011-09-16 10:27:11 -04:00
commit bf38f17c0e
2 changed files with 36 additions and 24 deletions

View File

@ -29,30 +29,39 @@
// private method to read stream with timeout // private method to read stream with timeout
int Stream::timedRead() int Stream::timedRead()
{ {
//Serial.println(_timeout); int c;
this->_startMillis = millis(); _startMillis = millis();
while(millis() - this->_startMillis < this->_timeout) do {
{ c = read();
if (this->available() > 0) { if (c >= 0) return c;
return this->read(); } while(millis() - _startMillis < _timeout);
}
}
return -1; // -1 indicates timeout return -1; // -1 indicates timeout
} }
// returns the next digit in the stream or -1 if timeout // private method to peek stream with timeout
// discards non-numeric characters int Stream::timedPeek()
int Stream::getNextDigit()
{ {
int c; int c;
_startMillis = millis();
do { do {
c = timedRead(); c = peek();
if( c < 0) if (c >= 0) return c;
return c; // timeout } while(millis() - _startMillis < _timeout);
return -1; // -1 indicates timeout
} }
while( c != '-' && (c < '0' || c > '9') ) ;
return c; // returns peek of the next digit in the stream or -1 if timeout
// discards non-numeric characters
int Stream::peekNextDigit()
{
int c;
while (1) {
c = timedPeek();
if (c < 0) return c; // timeout
if (c == '-') return c;
if (c >= '0' && c <= '9') return c;
read(); // discard non-numeric
}
} }
// Public Methods // Public Methods
@ -130,7 +139,7 @@ long Stream::parseInt(char skipChar)
long value = 0; long value = 0;
int c; int c;
c = getNextDigit(); c = peekNextDigit();
// ignore non numeric leading characters // ignore non numeric leading characters
if(c < 0) if(c < 0)
return 0; // zero returned if timeout return 0; // zero returned if timeout
@ -142,7 +151,8 @@ long Stream::parseInt(char skipChar)
isNegative = true; isNegative = true;
else if(c >= '0' && c <= '9') // is c a digit? else if(c >= '0' && c <= '9') // is c a digit?
value = value * 10 + c - '0'; value = value * 10 + c - '0';
c = timedRead(); read(); // consume the character we got with peek
c = timedPeek();
} }
while( (c >= '0' && c <= '9') || c == skipChar ); while( (c >= '0' && c <= '9') || c == skipChar );
@ -168,7 +178,7 @@ float Stream::parseFloat(char skipChar){
char c; char c;
float fraction = 1.0; float fraction = 1.0;
c = getNextDigit(); c = peekNextDigit();
// ignore non numeric leading characters // ignore non numeric leading characters
if(c < 0) if(c < 0)
return 0; // zero returned if timeout return 0; // zero returned if timeout
@ -185,7 +195,8 @@ float Stream::parseFloat(char skipChar){
if(isFraction) if(isFraction)
fraction *= 0.1; fraction *= 0.1;
} }
c = timedRead(); read(); // consume the character we got with peek
c = timedPeek();
} }
while( (c >= '0' && c <= '9') || c == '.' || c == skipChar ); while( (c >= '0' && c <= '9') || c == '.' || c == skipChar );

View File

@ -41,7 +41,8 @@ class Stream : public Print
long _timeout; // number of milliseconds to wait for the next char before aborting timed read long _timeout; // number of milliseconds to wait for the next char before aborting timed read
long _startMillis; // used for timeout measurement long _startMillis; // used for timeout measurement
int timedRead(); // private method to read stream with timeout int timedRead(); // private method to read stream with timeout
int getNextDigit(); // returns the next numeric digit in the stream or -1 if timeout int timedPeek(); // private method to peek stream with timeout
int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
public: public:
virtual int available() = 0; virtual int available() = 0;