From 0606612e4090983d6cdf42137a24eeacebb2fbff Mon Sep 17 00:00:00 2001 From: Pierre Bouchet Date: Tue, 6 Mar 2012 17:44:06 +0100 Subject: [PATCH 1/2] Made Stream::timedRead() and Stream::timedPeek() protected instead of private. --- hardware/arduino/cores/arduino/Stream.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hardware/arduino/cores/arduino/Stream.h b/hardware/arduino/cores/arduino/Stream.h index 13f11bee022..a516c26f08e 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: @@ -84,6 +82,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 From 4cd29d075c52c6d5554d917877ea05a781d249f3 Mon Sep 17 00:00:00 2001 From: Pierre Bouchet Date: Wed, 7 Mar 2012 11:29:45 +0100 Subject: [PATCH 2/2] Added Stream::find(char) --- hardware/arduino/cores/arduino/Stream.cpp | 13 +++++++++++++ hardware/arduino/cores/arduino/Stream.h | 3 +++ 2 files changed, 16 insertions(+) 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 a516c26f08e..bc03877eb3e 100644 --- a/hardware/arduino/cores/arduino/Stream.h +++ b/hardware/arduino/cores/arduino/Stream.h @@ -54,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)