Stream.h
Go to the documentation of this file.
1 /* $Id: Stream.h 1151 2011-06-06 21:13:05Z bhagman $
2 ||
3 || @author Brett Hagman <bhagman@wiring.org.co>
4 || @url http://wiring.org.co/
5 || @contribution Alexander Brevig <abrevig@wiring.org.co>
6 || @contribution David A. Mellis
7 ||
8 || @description
9 || | Base class for streams.
10 || |
11 || | Wiring Common API
12 || #
13 ||
14 || @notes
15 || | Originally discussed here:
16 || |
17 || | http://code.google.com/p/arduino/issues/detail?id=60
18 || #
19 ||
20 || @license Please see cores/Common/License.txt.
21 ||
22 */
23 
24 #pragma once
25 
26 #include "Print.h"
27 
32 class Stream : public Print
33 {
34  protected:
35  unsigned long receiveTimeout = 1000; // number of milliseconds to wait for the next char before aborting timed read
36  unsigned long startMillis = 0; // used for timeout measurement
37  int timedRead(); // private method to read stream with timeout
38  int timedPeek(); // private method to peek stream with timeout
39  int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
40 
41  public:
42  virtual int available() = 0;
43  virtual int peek() = 0;
44  virtual int read() = 0;
45  virtual void flush() = 0;
46 
47  Stream() { }
48 
49  // parsing methods
50 
51  void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
52 
53  bool find(char *target); // reads data from the stream until the target string is found
54  // returns true if target string is found, false if timed out (see setTimeout)
55 
56  bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
57  // returns true if target string is found, false if timed out
58 
59  bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
60 
61  bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
62 
63 
64  long parseInt(); // returns the first valid (long) integer value from the current position.
65  // initial characters that are not digits (or the minus sign) are skipped
66  // integer is terminated by the first character that is not a digit.
67 
68  float parseFloat(); // float version of parseInt
69 
70  size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
71  // terminates if length characters have been read or timeout (see setTimeout)
72  // returns the number of characters placed in the buffer (0 means no valid data found)
73 
74  size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
75  // terminates if length characters have been read, timeout, or if the terminator character detected
76  // returns the number of characters placed in the buffer (0 means no valid data found)
77 
78  // Wiring String functions to be added here
80  String readStringUntil(char terminator);
81 
82  /*
83  * @brief Returns the location of the searched character
84  * @param char c - character to search for
85  * @retval int -1 if not found 0 or positive number otherwise
86  */
87  virtual int indexOf(char c) {
88  return -1;
89  }
90 
91  protected:
92  long parseInt(char skipChar); // as above but the given skipChar is ignored
93  // as above but the given skipChar is ignored
94  // this allows format characters (typically commas) in values to be ignored
95 
96  float parseFloat(char skipChar); // as above but the given skipChar is ignored
97 };
unsigned long startMillis
Definition: Stream.h:36
size_t readBytesUntil(char terminator, char *buffer, size_t length)
virtual int available()=0
virtual int indexOf(char c)
Definition: Stream.h:87
int peekNextDigit()
long parseInt()
int timedRead()
unsigned long receiveTimeout
Definition: Stream.h:35
bool findUntil(char *target, char *terminator)
The String class.
Definition: WString.h:136
Provides formatted output to stream.
Definition: Print.h:36
bool find(char *target)
String readString()
virtual int read()=0
void setTimeout(unsigned long timeout)
size_t readBytes(char *buffer, size_t length)
String readStringUntil(char terminator)
int timedPeek()
float parseFloat()
Stream()
Definition: Stream.h:47
virtual int peek()=0
virtual void flush()=0
Base Stream class.
Definition: Stream.h:32