Prusa MINI Firmware overview
Stream Class Referenceabstract

#include <Stream.h>

Inheritance diagram for Stream:
Collaboration diagram for Stream:

Classes

struct  MultiTarget
 

Public Member Functions

virtual int available ()=0
 
virtual int read ()=0
 
virtual int peek ()=0
 
virtual void flush ()=0
 
 Stream ()
 
void setTimeout (unsigned long timeout)
 
unsigned long getTimeout (void)
 
bool find (const char *target)
 
bool find (const uint8_t *target)
 
bool find (const char *target, size_t length)
 
bool find (const uint8_t *target, size_t length)
 
bool find (char target)
 
bool findUntil (const char *target, const char *terminator)
 
bool findUntil (const uint8_t *target, const char *terminator)
 
bool findUntil (const char *target, size_t targetLen, const char *terminate, size_t termLen)
 
bool findUntil (const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen)
 
long parseInt (LookaheadMode lookahead=SKIP_ALL, char ignore=NO_IGNORE_CHAR)
 
float parseFloat (LookaheadMode lookahead=SKIP_ALL, char ignore=NO_IGNORE_CHAR)
 
virtual size_t readBytes (char *buffer, size_t length)
 
size_t readBytes (uint8_t *buffer, size_t length)
 
virtual size_t readBytesUntil (char terminator, char *buffer, size_t length)
 
size_t readBytesUntil (char terminator, uint8_t *buffer, size_t length)
 
String readString ()
 
String readStringUntil (char terminator)
 
- Public Member Functions inherited from Print
 Print ()
 
int getWriteError ()
 
void clearWriteError ()
 
virtual size_t write (uint8_t)=0
 
size_t write (const char *str)
 
virtual size_t write (const uint8_t *buffer, size_t size)
 
size_t write (const char *buffer, size_t size)
 
size_t print (const __FlashStringHelper *)
 
size_t print (const String &)
 
size_t print (const char[])
 
size_t print (char)
 
size_t print (unsigned char, int=DEC)
 
size_t print (int, int=DEC)
 
size_t print (unsigned int, int=DEC)
 
size_t print (long, int=DEC)
 
size_t print (unsigned long, int=DEC)
 
size_t print (double, int=2)
 
size_t print (const Printable &)
 
size_t println (const __FlashStringHelper *)
 
size_t println (const String &s)
 
size_t println (const char[])
 
size_t println (char)
 
size_t println (unsigned char, int=DEC)
 
size_t println (int, int=DEC)
 
size_t println (unsigned int, int=DEC)
 
size_t println (long, int=DEC)
 
size_t println (unsigned long, int=DEC)
 
size_t println (double, int=2)
 
size_t println (const Printable &)
 
size_t println (void)
 
void println (int64_t, uint8_t=DEC)
 
void print (int64_t, uint8_t=DEC)
 
void println (uint64_t, uint8_t=DEC)
 
void print (uint64_t, uint8_t=DEC)
 

Protected Member Functions

int timedRead ()
 
int timedPeek ()
 
int peekNextDigit (LookaheadMode lookahead, bool detectDecimal)
 
long parseInt (char ignore)
 
float parseFloat (char ignore)
 
int findMulti (struct MultiTarget *targets, int tCount)
 
- Protected Member Functions inherited from Print
void setWriteError (int err=1)
 

Protected Attributes

unsigned long _timeout
 
unsigned long _startMillis
 

Constructor & Destructor Documentation

◆ Stream()

Stream::Stream ( )
63  {
64  _timeout = 1000;
65  }

Member Function Documentation

◆ timedRead()

int Stream::timedRead ( )
protected
31  {
32  int c;
33  _startMillis = millis();
34  do {
35  c = read();
36  if (c >= 0) {
37  return c;
38  }
39  } while (millis() - _startMillis < _timeout);
40  return -1; // -1 indicates timeout
41 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ timedPeek()

int Stream::timedPeek ( )
protected
44  {
45  int c;
46  _startMillis = millis();
47  do {
48  c = peek();
49  if (c >= 0) {
50  return c;
51  }
52  } while (millis() - _startMillis < _timeout);
53  return -1; // -1 indicates timeout
54 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ peekNextDigit()

int Stream::peekNextDigit ( LookaheadMode  lookahead,
bool  detectDecimal 
)
protected
58  {
59  int c;
60  while (1) {
61  c = timedPeek();
62 
63  if (c < 0 || c == '-' || (c >= '0' && c <= '9') || (detectDecimal && c == '.')) {
64  return c;
65  }
66 
67  switch (lookahead) {
68  case SKIP_NONE:
69  return -1; // Fail code.
70  case SKIP_WHITESPACE:
71  switch (c) {
72  case ' ':
73  case '\t':
74  case '\r':
75  case '\n':
76  break;
77  default:
78  return -1; // Fail code.
79  }
80  case SKIP_ALL:
81  break;
82  }
83  read(); // discard non-numeric
84  }
85 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ available()

virtual int Stream::available ( )
pure virtual

◆ read()

virtual int Stream::read ( )
pure virtual

Implemented in TwoWire, HardwareSerial, USBSerial, and WebSocketSerial.

Here is the caller graph for this function:

◆ peek()

virtual int Stream::peek ( )
pure virtual

Implemented in TwoWire, HardwareSerial, USBSerial, and WebSocketSerial.

Here is the caller graph for this function:

◆ flush()

virtual void Stream::flush ( )
pure virtual

◆ setTimeout()

void Stream::setTimeout ( unsigned long  timeout)
91 {
92  _timeout = timeout;
93 }

◆ getTimeout()

unsigned long Stream::getTimeout ( void  )
70  {
71  return _timeout;
72  }

◆ find() [1/5]

bool Stream::find ( const char *  target)
96  {
97  return findUntil(target, strlen(target), NULL, 0);
98 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ find() [2/5]

bool Stream::find ( const uint8_t target)
75  {
76  return find((const char *)target);
77  }
Here is the call graph for this function:

◆ find() [3/5]

bool Stream::find ( const char *  target,
size_t  length 
)
102  {
103  return findUntil(target, length, NULL, 0);
104 }
Here is the call graph for this function:

◆ find() [4/5]

bool Stream::find ( const uint8_t target,
size_t  length 
)
81  {
82  return find((const char *)target, length);
83  }
Here is the call graph for this function:

◆ find() [5/5]

bool Stream::find ( char  target)
86  {
87  return find(&target, 1);
88  }
Here is the call graph for this function:

◆ findUntil() [1/4]

bool Stream::findUntil ( const char *  target,
const char *  terminator 
)
107  {
108  return findUntil(target, strlen(target), terminator, strlen(terminator));
109 }
Here is the caller graph for this function:

◆ findUntil() [2/4]

bool Stream::findUntil ( const uint8_t target,
const char *  terminator 
)
91  {
92  return findUntil((const char *)target, terminator);
93  }
Here is the call graph for this function:

◆ findUntil() [3/4]

bool Stream::findUntil ( const char *  target,
size_t  targetLen,
const char *  terminate,
size_t  termLen 
)
114  {
115  if (terminator == NULL) {
116  MultiTarget t[1] = { { target, targetLen, 0 } };
117  return findMulti(t, 1) == 0 ? true : false;
118  } else {
119  MultiTarget t[2] = { { target, targetLen, 0 }, { terminator, termLen, 0 } };
120  return findMulti(t, 2) == 0 ? true : false;
121  }
122 }
Here is the call graph for this function:

◆ findUntil() [4/4]

bool Stream::findUntil ( const uint8_t target,
size_t  targetLen,
const char *  terminate,
size_t  termLen 
)
96  {
97  return findUntil((const char *)target, targetLen, terminate, termLen);
98  }
Here is the call graph for this function:

◆ parseInt() [1/2]

long Stream::parseInt ( LookaheadMode  lookahead = SKIP_ALL,
char  ignore = NO_IGNORE_CHAR 
)
129  {
130  bool isNegative = false;
131  long value = 0;
132  int c;
133 
134  c = peekNextDigit(lookahead, false);
135  // ignore non numeric leading characters
136  if (c < 0) {
137  return 0; // zero returned if timeout
138  }
139 
140  do {
141  if (c == ignore)
142  ; // ignore this character
143  else if (c == '-') {
144  isNegative = true;
145  } else if (c >= '0' && c <= '9') { // is c a digit?
146  value = value * 10 + c - '0';
147  }
148  read(); // consume the character we got with peek
149  c = timedPeek();
150  } while ((c >= '0' && c <= '9') || c == ignore);
151 
152  if (isNegative) {
153  value = -value;
154  }
155  return value;
156 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseFloat() [1/2]

float Stream::parseFloat ( LookaheadMode  lookahead = SKIP_ALL,
char  ignore = NO_IGNORE_CHAR 
)
159  {
160  bool isNegative = false;
161  bool isFraction = false;
162  long value = 0;
163  int c;
164  float fraction = 1.0;
165 
166  c = peekNextDigit(lookahead, true);
167  // ignore non numeric leading characters
168  if (c < 0) {
169  return 0; // zero returned if timeout
170  }
171 
172  do {
173  if (c == ignore)
174  ; // ignore
175  else if (c == '-') {
176  isNegative = true;
177  } else if (c == '.') {
178  isFraction = true;
179  } else if (c >= '0' && c <= '9') { // is c a digit?
180  value = value * 10 + c - '0';
181  if (isFraction) {
182  fraction *= 0.1;
183  }
184  }
185  read(); // consume the character we got with peek
186  c = timedPeek();
187  } while ((c >= '0' && c <= '9') || (c == '.' && !isFraction) || c == ignore);
188 
189  if (isNegative) {
190  value = -value;
191  }
192  if (isFraction) {
193  return value * fraction;
194  } else {
195  return value;
196  }
197 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readBytes() [1/2]

size_t Stream::readBytes ( char *  buffer,
size_t  length 
)
virtual

Reimplemented in USBSerial.

204  {
205  size_t count = 0;
206  while (count < length) {
207  int c = timedRead();
208  if (c < 0) {
209  break;
210  }
211  *buffer++ = (char)c;
212  count++;
213  }
214  return count;
215 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readBytes() [2/2]

size_t Stream::readBytes ( uint8_t buffer,
size_t  length 
)
111  {
112  return readBytes((char *)buffer, length);
113  }
Here is the call graph for this function:

◆ readBytesUntil() [1/2]

size_t Stream::readBytesUntil ( char  terminator,
char *  buffer,
size_t  length 
)
virtual

Reimplemented in USBSerial.

221  {
222  if (length < 1) {
223  return 0;
224  }
225  size_t index = 0;
226  while (index < length) {
227  int c = timedRead();
228  if (c < 0 || c == terminator) {
229  break;
230  }
231  *buffer++ = (char)c;
232  index++;
233  }
234  return index; // return number of characters, not including null terminator
235 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ readBytesUntil() [2/2]

size_t Stream::readBytesUntil ( char  terminator,
uint8_t buffer,
size_t  length 
)
118  {
119  return readBytesUntil(terminator, (char *)buffer, length);
120  }
Here is the call graph for this function:

◆ readString()

String Stream::readString ( )
237  {
238  String ret;
239  int c = timedRead();
240  while (c >= 0) {
241  ret += (char)c;
242  c = timedRead();
243  }
244  return ret;
245 }
Here is the call graph for this function:

◆ readStringUntil()

String Stream::readStringUntil ( char  terminator)
247  {
248  String ret;
249  int c = timedRead();
250  while (c >= 0 && c != terminator) {
251  ret += (char)c;
252  c = timedRead();
253  }
254  return ret;
255 }
Here is the call graph for this function:

◆ parseInt() [2/2]

long Stream::parseInt ( char  ignore)
protected
129  {
130  return parseInt(SKIP_ALL, ignore);
131  }
Here is the call graph for this function:

◆ parseFloat() [2/2]

float Stream::parseFloat ( char  ignore)
protected
132  {
133  return parseFloat(SKIP_ALL, ignore);
134  }
Here is the call graph for this function:

◆ findMulti()

int Stream::findMulti ( struct MultiTarget targets,
int  tCount 
)
protected
257  {
258  // any zero length target string automatically matches and would make
259  // a mess of the rest of the algorithm.
260  for (struct MultiTarget *t = targets; t < targets + tCount; ++t) {
261  if (t->len <= 0) {
262  return t - targets;
263  }
264  }
265 
266  while (1) {
267  int c = timedRead();
268  if (c < 0) {
269  return -1;
270  }
271 
272  for (struct MultiTarget *t = targets; t < targets + tCount; ++t) {
273  // the simple case is if we match, deal with that first.
274  if (c == t->str[t->index]) {
275  if (++t->index == t->len) {
276  return t - targets;
277  } else {
278  continue;
279  }
280  }
281 
282  // if not we need to walk back and see if we could have matched further
283  // down the stream (ie '1112' doesn't match the first position in '11112'
284  // but it will match the second position so we can't just reset the current
285  // index to 0 when we find a mismatch.
286  if (t->index == 0) {
287  continue;
288  }
289 
290  int origIndex = t->index;
291  do {
292  --t->index;
293  // first check if current char works against the new current index
294  if (c != t->str[t->index]) {
295  continue;
296  }
297 
298  // if it's the only char then we're good, nothing more to check
299  if (t->index == 0) {
300  t->index++;
301  break;
302  }
303 
304  // otherwise we need to check the rest of the found string
305  int diff = origIndex - t->index;
306  size_t i;
307  for (i = 0; i < t->index; ++i) {
308  if (t->str[i] != t->str[i + diff]) {
309  break;
310  }
311  }
312 
313  // if we successfully got through the previous loop then our current
314  // index is good.
315  if (i == t->index) {
316  t->index++;
317  break;
318  }
319 
320  // otherwise we just try the next index
321  } while (t->index);
322  }
323  }
324  // unreachable
325  return -1;
326 }
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _timeout

unsigned long Stream::_timeout
protected

◆ _startMillis

unsigned long Stream::_startMillis
protected
Stream::timedRead
int timedRead()
Definition: Stream.cpp:31
Stream::find
bool find(const char *target)
Definition: Stream.cpp:96
Stream::readBytes
virtual size_t readBytes(char *buffer, size_t length)
Definition: Stream.cpp:204
Stream::parseInt
long parseInt(LookaheadMode lookahead=SKIP_ALL, char ignore=NO_IGNORE_CHAR)
Definition: Stream.cpp:129
SKIP_ALL
Definition: Stream.h:42
Stream::findUntil
bool findUntil(const char *target, const char *terminator)
Definition: Stream.cpp:107
SKIP_WHITESPACE
Definition: Stream.h:44
Stream::peekNextDigit
int peekNextDigit(LookaheadMode lookahead, bool detectDecimal)
Definition: Stream.cpp:58
i
uint8_t i
Definition: screen_test_graph.c:72
millis
uint32_t millis(void)
Definition: wiring_time.c:29
Stream::timedPeek
int timedPeek()
Definition: Stream.cpp:44
NULL
#define NULL
Definition: usbd_def.h:53
Stream::peek
virtual int peek()=0
Stream::read
virtual int read()=0
Stream::readBytesUntil
virtual size_t readBytesUntil(char terminator, char *buffer, size_t length)
Definition: Stream.cpp:221
Stream::_startMillis
unsigned long _startMillis
Definition: Stream.h:52
Stream::parseFloat
float parseFloat(LookaheadMode lookahead=SKIP_ALL, char ignore=NO_IGNORE_CHAR)
Definition: Stream.cpp:159
Stream::findMulti
int findMulti(struct MultiTarget *targets, int tCount)
Definition: Stream.cpp:257
length
png_uint_32 length
Definition: png.c:2247
Stream::_timeout
unsigned long _timeout
Definition: Stream.h:51
SKIP_NONE
Definition: Stream.h:43