Wiring/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 public:
35  virtual int available() = 0;
36  virtual int peek() = 0;
37  virtual int read() = 0;
38  virtual void flush() = 0;
39 
41  {
42  }
43 
44  // parsing methods
45 
49  void setTimeout(unsigned long timeout)
50  {
51  receiveTimeout = timeout;
52  }
53 
59  bool find(const char* target)
60  {
61  return findUntil(target, "");
62  }
63 
70  bool find(const char* target, size_t length)
71  {
72  return findUntil(target, length, nullptr, 0);
73  }
74 
78  bool findUntil(const char* target, const char* terminator);
79 
83  bool findUntil(const char* target, size_t targetLen, const char* terminate, size_t termLen);
84 
91  long parseInt();
92 
96  float parseFloat();
97 
106  virtual size_t readBytes(char* buffer, size_t length);
107 
108  size_t readBytes(uint8_t* buffer, size_t length)
109  {
110  return readBytes(reinterpret_cast<char*>(buffer), length);
111  }
112 
120  size_t readBytesUntil(char terminator, char* buffer, size_t length);
121 
127  virtual String readString(size_t maxLen);
128 
129  String readStringUntil(char terminator);
130 
131  /*
132  * @brief Returns the location of the searched character
133  * @param c Character to search for
134  * @retval int -1 if not found 0 or positive number otherwise
135  */
136  virtual int indexOf(char c)
137  {
138  return -1;
139  }
140 
141 protected:
142  int timedRead();
143  int timedPeek();
144 
149 
155  long parseInt(char skipChar);
156 
160  float parseFloat(char skipChar);
161 
162  uint16_t receiveTimeout = 1000;
163 };
Provides formatted output to stream.
Definition: Print.h:37
Base Stream class.
Definition: Wiring/Stream.h:33
bool find(const char *target)
Read data from the stream until the target string is found.
Definition: Wiring/Stream.h:59
float parseFloat(char skipChar)
Like parseInt(skipChar) for float.
size_t readBytes(uint8_t *buffer, size_t length)
Definition: Wiring/Stream.h:108
virtual int peek()=0
int timedRead()
long parseInt(char skipChar)
Like regular parseInt() but the given skipChar is ignored.
long parseInt()
Returns the first valid (long) integer value from the current position.
uint16_t receiveTimeout
number of milliseconds to wait for the next char before aborting timed read
Definition: Wiring/Stream.h:162
float parseFloat()
float version of parseInt
virtual String readString(size_t maxLen)
Like readBytes but place content into a String
String readStringUntil(char terminator)
bool find(const char *target, size_t length)
Read data from the stream until the target string of given length is found.
Definition: Wiring/Stream.h:70
void setTimeout(unsigned long timeout)
Set maximum milliseconds to wait for stream data, default is 1 second.
Definition: Wiring/Stream.h:49
Stream()
Definition: Wiring/Stream.h:40
virtual int available()=0
virtual void flush()=0
virtual int indexOf(char c)
Definition: Wiring/Stream.h:136
virtual size_t readBytes(char *buffer, size_t length)
Read chars from stream into buffer.
bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen)
As findUntil(const char*, const char*) but search ends if the terminate string is found.
int peekNextDigit()
returns the next numeric digit in the stream or -1 if timeout
bool findUntil(const char *target, const char *terminator)
As find() but search ends if the terminator string is found.
int timedPeek()
virtual int read()=0
size_t readBytesUntil(char terminator, char *buffer, size_t length)
As readBytes() with terminator character.
The String class.
Definition: WString.h:137