~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to hardware/arduino/cores/arduino/Stream.h

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
  You should have received a copy of the GNU Lesser General Public
16
16
  License along with this library; if not, write to the Free Software
17
17
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 
 
19
  parsing functions based on TextFinder library by Michael Margolis
18
20
*/
19
21
 
20
22
#ifndef Stream_h
23
25
#include <inttypes.h>
24
26
#include "Print.h"
25
27
 
 
28
// compatability macros for testing
 
29
/*
 
30
#define   getInt()            parseInt()
 
31
#define   getInt(skipChar)    parseInt(skipchar)
 
32
#define   getFloat()          parseFloat()
 
33
#define   getFloat(skipChar)  parseFloat(skipChar)
 
34
#define   getString( pre_string, post_string, buffer, length)
 
35
readBytesBetween( pre_string, terminator, buffer, length)
 
36
*/
 
37
 
26
38
class Stream : public Print
27
39
{
 
40
  private:
 
41
    unsigned long _timeout;      // number of milliseconds to wait for the next char before aborting timed read
 
42
    unsigned long _startMillis;  // used for timeout measurement
 
43
    int timedRead();    // private method to read stream with timeout
 
44
    int timedPeek();    // private method to peek stream with timeout
 
45
    int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
 
46
 
28
47
  public:
29
48
    virtual int available() = 0;
30
49
    virtual int read() = 0;
31
50
    virtual int peek() = 0;
32
51
    virtual void flush() = 0;
 
52
 
 
53
    Stream() {_timeout=1000;}
 
54
 
 
55
// parsing methods
 
56
 
 
57
  void setTimeout(unsigned long timeout);  // sets maximum milliseconds to wait for stream data, default is 1 second
 
58
 
 
59
  bool find(char *target);   // reads data from the stream until the target string is found
 
60
  // returns true if target string is found, false if timed out (see setTimeout)
 
61
 
 
62
  bool find(char *target, size_t length);   // reads data from the stream until the target string of given length is found
 
63
  // returns true if target string is found, false if timed out
 
64
 
 
65
  bool findUntil(char *target, char *terminator);   // as find but search ends if the terminator string is found
 
66
 
 
67
  bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen);   // as above but search ends if the terminate string is found
 
68
 
 
69
 
 
70
  long parseInt(); // returns the first valid (long) integer value from the current position.
 
71
  // initial characters that are not digits (or the minus sign) are skipped
 
72
  // integer is terminated by the first character that is not a digit.
 
73
 
 
74
  float parseFloat();               // float version of parseInt
 
75
 
 
76
  size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
 
77
  // terminates if length characters have been read or timeout (see setTimeout)
 
78
  // returns the number of characters placed in the buffer (0 means no valid data found)
 
79
 
 
80
  size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
 
81
  // terminates if length characters have been read, timeout, or if the terminator character  detected
 
82
  // returns the number of characters placed in the buffer (0 means no valid data found)
 
83
 
 
84
  // Arduino String functions to be added here
 
85
 
 
86
  protected:
 
87
  long parseInt(char skipChar); // as above but the given skipChar is ignored
 
88
  // as above but the given skipChar is ignored
 
89
  // this allows format characters (typically commas) in values to be ignored
 
90
 
 
91
  float parseFloat(char skipChar);  // as above but the given skipChar is ignored
33
92
};
34
93
 
35
94
#endif