Arduino Core for STM32  1.0
Stream.h
Go to the documentation of this file.
1 
36 #ifndef Stream_h
37 #define Stream_h
38 
39 #include <inttypes.h>
40 #include "Print.h"
41 
42 
43 #define NO_SKIP_CHAR 1 // a magic char not found in a valid ASCII numeric field
44 #ifndef YIELD
45 #define YIELD
46 #endif
47 
48 #define F(x) (x)
49 
56 class Stream: public Print {
57  protected:
61  unsigned long _timeout = 1000;
62 
66  unsigned long _startMillis;
67 
72  int timedRead();
73 
78  int timedPeek();
79 
84  int peekNextDigit(); //
85 
86  public:
92  virtual int available() = 0;
93 
99  virtual int read() = 0;
100 
106  virtual int peek() = 0;
107 
111  Stream() {}
112 
113  //Input operators - they are experimental and can be buggy
119  Stream &operator >>(char& c);
120 
127  Stream &operator >>(char* buffer);
128 
135  Stream &operator >>(uint8_t* buffer);
136 
137  //Integer types
145  val = parseNum<T>();
146  return *this;
147  }
148  //Floating point types
156  val = parseNum<T>();
157  return *this;
158  }
159 
160 // parsing methods
161 
166  void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
167 
173  virtual void width(size_t width_);
174 
180  bool find(const char *target);
181 
187  bool find(uint8_t *target) {
188  return find((char *) target);
189  }
190 
197  bool find(const char *target, size_t length);
198 
205  bool find(const uint8_t *target, size_t length) {
206  return find((char *) target, length);
207  }
208 
214  bool find(char target) { return find (&target, 1); }
215 
222  bool findUntil(const char *target, const char *terminator); // as find but search ends if the terminator string is found
223 
230  bool findUntil(const uint8_t *target, const char *terminator) {
231  return findUntil((char *) target, terminator);
232  }
233 
242  bool findUntil(const char *target, size_t targetLen, const char *terminate, size_t termLen); // as above but search ends if the terminate string is found
243 
252  bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen) {
253  return findUntil((char *) target, targetLen, terminate, termLen);
254  }
255 
262  long inline parseInt(){
263  return parseInt(NO_SKIP_CHAR); // terminate on first non-digit character (or timeout)
264  }
265 
273  __INTEGER_ONLY__(T,T) inline parseNum(){
274  return parseNum<T>(NO_SKIP_CHAR);
275  }
276 
283  float inline parseFloat(){// float version of parseInt
284  return parseNum<float>(NO_SKIP_CHAR);
285  }
286 
294  __FLOATING_ONLY__(T,T) inline parseNum(){
295  return parseNum<T>(NO_SKIP_CHAR);
296  }
297 
305  virtual size_t readBytes(char *buffer, size_t length);
306 
314  virtual size_t readBytes(uint8_t *buffer, size_t length) {
315  return readBytes((char *) buffer, length);
316  }
317 
328  size_t readBytesUntil(char terminator, char *buffer, size_t length); // as readBytes with terminator character
329 
340  size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length) {
341  return readBytesUntil(terminator, (char *) buffer, length);
342  }
343  // Arduino String functions to be added here
344 #ifdef String_class_h
345 
349  virtual String readString();
350 
358  String readStringUntil(char terminator);
359 #endif
360 
361  protected:
369  long inline parseInt(char skipChar){
370  return parseNum<long>(skipChar);
371  }
372 
381  __INTEGER_ONLY__(T,T) parseNum(char skipChar){
382  bool isNegative = false;
383  T value = 0;
384  int c;
385 
386  c = peekNextDigit();
387  // ignore non numeric leading characters
388  if(c < 0)
389  return 0; // zero returned if timeout
390 
391  do {
392  if(c == skipChar)
393  ; // ignore this character
394  else if(c == '-')
395  isNegative = true;
396  else if(c >= '0' && c <= '9') // is c a digit?
397  value = value * 10 + c - '0';
398  read(); // consume the character we got with peek
399  c = timedPeek();
400  } while((c >= '0' && c <= '9') || c == skipChar);
401 
402  if(isNegative)
403  value = -value;
404  return value;
405  }
406 
414  float inline parseFloat(char skipChar){ // as above but the given skipChar is ignored
415  return parseFloat(NO_SKIP_CHAR);
416  }
417 
426  __FLOATING_ONLY__(T,T) parseNum(char skipChar){
427  bool isNegative = false;
428  bool isFraction = false;
429  long value = 0;
430  int c;
431  T fraction = 1.0f;
432 
433  c = peekNextDigit();
434  // ignore non numeric leading characters
435  if(c < 0)
436  return 0; // zero returned if timeout
437 
438  do {
439  if(c == skipChar)
440  ; // ignore
441  else if(c == '-')
442  isNegative = true;
443  else if(c == '.')
444  isFraction = true;
445  else if(c >= '0' && c <= '9') { // is c a digit?
446  value = value * 10 + c - '0';
447  if(isFraction)
448  fraction *= 0.1f;
449  }
450  read(); // consume the character we got with peek
451  c = timedPeek();
452  } while((c >= '0' && c <= '9') || c == '.' || c == skipChar);
453 
454  if(isNegative)
455  value = -value;
456  if(isFraction)
457  return value * fraction;
458  else
459  return value;
460  }
461 
467  virtual size_t widthCheck(size_t width_);
468 
472  size_t _width = INT32_MAX;
473 };
474 
475 #endif
Stream::timedRead
int timedRead()
Private method to read stream with timeout.
Definition: Stream.cpp:15
Stream::__INTEGER_ONLY__
__INTEGER_ONLY__(T, Stream &) operator>>(T &val)
This operator (>>) applied to an input stream is known as extraction operator, it can be used same as...
Definition: Stream.h:144
Stream::find
bool find(const char *target)
Reads and removes data from the stream until the target string is found.
Definition: Stream.cpp:153
Stream::readBytes
virtual size_t readBytes(char *buffer, size_t length)
Reads characters from the stream into a buffer.
Definition: Stream.cpp:303
Stream::width
virtual void width(size_t width_)
Sets receiving width, that is applied when using operators >> (same as cin.width() method).
Definition: Stream.cpp:132
Stream::findUntil
bool findUntil(const char *target, const char *terminator)
Reads data from the stream until a target string or terminator string is found.
Definition: Stream.cpp:164
Stream::readBytes
virtual size_t readBytes(uint8_t *buffer, size_t length)
Reads characters from the stream into a buffer.
Definition: Stream.h:314
Stream::find
bool find(uint8_t *target)
Reads and removes data from the stream until the target string is found.
Definition: Stream.h:187
Stream::readBytesUntil
size_t readBytesUntil(char terminator, uint8_t *buffer, size_t length)
Reads characters from the stream into a buffer.
Definition: Stream.h:340
Stream::timedPeek
int timedPeek()
Private method to peek stream with timeout.
Definition: Stream.cpp:30
Stream::__FLOATING_ONLY__
__FLOATING_ONLY__(T, T) inline parseNum()
Parses the first valid floating point number from the stream.
Definition: Stream.h:294
NO_SKIP_CHAR
#define NO_SKIP_CHAR
Definition: Stream.h:43
Stream::__INTEGER_ONLY__
__INTEGER_ONLY__(T, T) inline parseNum()
Parses the first valid integer number from the stream.
Definition: Stream.h:273
Stream::parseFloat
float parseFloat(char skipChar)
Parses the first valid floating point number from the stream.
Definition: Stream.h:414
Stream::widthCheck
virtual size_t widthCheck(size_t width_)
Checks if the correct width in width() method was set.
Definition: Stream.cpp:142
Stream::parseInt
long parseInt()
Parses the first valid (long) integer number from the stream.
Definition: Stream.h:262
Stream::peek
virtual int peek()=0
Reads one byte from receiving buffer and without removing it from buffer.
Stream::read
virtual int read()=0
Reads one byte from receiving buffer and removes it from buffer.
Stream::parseInt
long parseInt(char skipChar)
Parses the first valid (long) integer number from the stream.
Definition: Stream.h:369
Stream::parseFloat
float parseFloat()
Parses the first valid floating point number from the stream.
Definition: Stream.h:283
Stream::readBytesUntil
size_t readBytesUntil(char terminator, char *buffer, size_t length)
Reads characters from the stream into a buffer.
Definition: Stream.cpp:319
Stream::operator>>
Stream & operator>>(char &c)
This operator (>>) applied to an input stream is known as extraction operator, it can be used same as...
Definition: Stream.cpp:64
Stream::find
bool find(const uint8_t *target, size_t length)
Reads and removes data from the stream until the target string is found.
Definition: Stream.h:205
Stream::_startMillis
unsigned long _startMillis
Used for timeout measurement.
Definition: Stream.h:66
Stream
This class is for receiving and transmitting data.
Definition: Stream.h:56
Stream::Stream
Stream()
Constructor.
Definition: Stream.h:111
Stream::__FLOATING_ONLY__
__FLOATING_ONLY__(T, Stream &) operator>>(T &val)
This operator (>>) applied to an input stream is known as extraction operator, it can be used same as...
Definition: Stream.h:155
Print.h
This file contains class with methods, that are used for to print formatted output.
Stream::peekNextDigit
int peekNextDigit()
Gets the next numeric digit in the stream or -1 if timeout.
Definition: Stream.cpp:46
Print
The stream class is derived from this class. This class contains methods, that are used for to print ...
Definition: Print.h:130
Stream::setTimeout
void setTimeout(unsigned long timeout)
Sets the maximum milliseconds to wait for data transmitting or receiving.
Definition: Stream.cpp:127
Stream::available
virtual int available()=0
Checks how many bytes are available in receiving buffer.
Stream::__INTEGER_ONLY__
__INTEGER_ONLY__(T, T) parseNum(char skipChar)
Parses the first valid integer number from the stream.
Definition: Stream.h:381
Stream::find
bool find(char target)
Reads and removes data from the stream until the target character is found.
Definition: Stream.h:214
Stream::__FLOATING_ONLY__
__FLOATING_ONLY__(T, T) parseNum(char skipChar)
Parses the first valid floating point number from the stream.
Definition: Stream.h:426
Stream::findUntil
bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen)
Reads data from the stream until a target string or terminator string is found.
Definition: Stream.h:252
Stream::_timeout
unsigned long _timeout
Number of milliseconds to wait for the next char before aborting timed read.
Definition: Stream.h:61
Stream::_width
size_t _width
Maximum operator >> read width.
Definition: Stream.h:472
Stream::findUntil
bool findUntil(const uint8_t *target, const char *terminator)
Reads data from the stream until a target string or terminator string is found.
Definition: Stream.h:230