diff --git a/hardware/arduino/cores/arduino/Stream.cpp b/hardware/arduino/cores/arduino/Stream.cpp index 3d5b9052911..14e4872362d 100644 --- a/hardware/arduino/cores/arduino/Stream.cpp +++ b/hardware/arduino/cores/arduino/Stream.cpp @@ -72,6 +72,19 @@ void Stream::setTimeout(unsigned long timeout) // sets the maximum number of mi _timeout = timeout; } +// find returns true if the target character is found +bool Stream::find(char target) +{ + int c = 0; + do { + c = timedRead(); + if ((char)c == target) + return true; + } while (c >= 0); + // if c is -1, it means a timeout occured + return false; +} + // find returns true if the target string is found bool Stream::find(char *target) { diff --git a/hardware/arduino/cores/arduino/Stream.h b/hardware/arduino/cores/arduino/Stream.h index 13f11bee022..bc03877eb3e 100644 --- a/hardware/arduino/cores/arduino/Stream.h +++ b/hardware/arduino/cores/arduino/Stream.h @@ -40,8 +40,6 @@ class Stream : public Print private: unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read unsigned long _startMillis; // used for timeout measurement - int timedRead(); // private method to read stream with 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: @@ -56,6 +54,9 @@ class Stream : public Print void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second + bool find(char target); // reads data from the stream until the target character is found + // returns true if target character is found, false if timed out (see setTimeout) + bool find(char *target); // reads data from the stream until the target string is found // returns true if target string is found, false if timed out (see setTimeout) @@ -84,6 +85,8 @@ class Stream : public Print // Arduino String functions to be added here protected: + int timedRead(); // private method to read stream with timeout + int timedPeek(); // private method to peek stream with timeout long parseInt(char skipChar); // as above but the given skipChar is ignored // as above but the given skipChar is ignored // this allows format characters (typically commas) in values to be ignored