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 
40  Stream() = default;
41 
42  // parsing methods
43 
47  void setTimeout(unsigned long timeout)
48  {
49  receiveTimeout = timeout;
50  }
51 
57  bool find(const char* target)
58  {
59  return findUntil(target, "");
60  }
61 
68  bool find(const char* target, size_t length)
69  {
70  return findUntil(target, length, nullptr, 0);
71  }
72 
76  bool findUntil(const char* target, const char* terminator);
77 
81  bool findUntil(const char* target, size_t targetLen, const char* terminate, size_t termLen);
82 
89  long parseInt();
90 
94  float parseFloat();
95 
104  virtual size_t readBytes(char* buffer, size_t length);
105 
106  size_t readBytes(uint8_t* buffer, size_t length)
107  {
108  return readBytes(reinterpret_cast<char*>(buffer), length);
109  }
110 
118  size_t readBytesUntil(char terminator, char* buffer, size_t length);
119 
125  virtual String readString(size_t maxLen);
126 
127  String readStringUntil(char terminator);
128 
129  /*
130  * @brief Returns the location of the searched character
131  * @param c Character to search for
132  * @retval int -1 if not found 0 or positive number otherwise
133  */
134  virtual int indexOf(char)
135  {
136  return -1;
137  }
138 
139 protected:
140  int timedRead();
141  int timedPeek();
142 
147 
153  long parseInt(char skipChar);
154 
158  float parseFloat(char skipChar);
159 
160  uint16_t receiveTimeout = 1000;
161 };
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:57
float parseFloat(char skipChar)
Like parseInt(skipChar) for float.
size_t readBytes(uint8_t *buffer, size_t length)
Definition: Wiring/Stream.h:106
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:160
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:68
void setTimeout(unsigned long timeout)
Set maximum milliseconds to wait for stream data, default is 1 second.
Definition: Wiring/Stream.h:47
Stream()=default
virtual int available()=0
virtual void flush()=0
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
virtual int indexOf(char)
Definition: Wiring/Stream.h:134
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:133