BleSerialPeripheralRK
spark_wiring_stream.h
Go to the documentation of this file.
1 
27 #ifndef __SPARK_WIRING_STREAM_H
28 #define __SPARK_WIRING_STREAM_H
29 
30 #include "spark_wiring_string.h"
31 #include "spark_wiring_print.h"
32 #include "system_tick_hal.h"
33 
34 // compatability macros for testing
35 /*
36 #define getInt() parseInt()
37 #define getInt(skipChar) parseInt(skipchar)
38 #define getFloat() parseFloat()
39 #define getFloat(skipChar) parseFloat(skipChar)
40 #define getString( pre_string, post_string, buffer, length)
41 readBytesBetween( pre_string, terminator, buffer, length)
42 */
43 
49 class Stream : public Print
50 {
51  protected:
52 #ifndef DOXYGEN_DO_NOT_DOCUMENT
53  system_tick_t _timeout; // number of milliseconds to wait for the next char before aborting timed read
54  system_tick_t _startMillis; // used for timeout measurement
55  int timedRead(); // private method to read stream with timeout
56  int timedPeek(); // private method to peek stream with timeout
57  int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
58 #endif
59 
60  public:
66  virtual int available() = 0;
67 
73  virtual int read() = 0;
74 
82  virtual int peek() = 0;
83 
87  virtual void flush() = 0;
88 
89 #ifndef DOXYGEN_DO_NOT_DOCUMENT
90  Stream() {_timeout=1000;}
91 #endif
92 
93 // parsing methods
94 
100  void setTimeout(system_tick_t timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
101 
109  bool find(char *target);
110 
120  bool find(char *target, size_t length);
130  bool findUntil(char *target, char *terminator);
131 
145  bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
146 
147 
154  long parseInt();
155 
156 
164  float parseFloat();
165 
173  size_t readBytes( char *buffer, size_t length);
174 
185  size_t readBytesUntil( char terminator, char *buffer, size_t length);
186 
187  // Arduino String functions to be added here
188 
192  String readString();
193 
199  String readStringUntil(char terminator);
200 
201  protected:
202 #ifndef DOXYGEN_DO_NOT_DOCUMENT
203  long parseInt(char skipChar); // as above but the given skipChar is ignored
204  // as above but the given skipChar is ignored
205  // this allows format characters (typically commas) in values to be ignored
206 
207  float parseFloat(char skipChar); // as above but the given skipChar is ignored
208 #endif
209 };
210 
211 #endif
String readStringUntil(char terminator)
Reads the remainder of the file into a string or until terminator is found.
Arduino/Wiring Stream Class.
Definition: spark_wiring_stream.h:49
size_t readBytesUntil(char terminator, char *buffer, size_t length)
Read chars from stream into buffer until the character terminator is found.
long parseInt()
returns the first valid (long) integer value from the current position
String readString()
Reads the remainder of the file into a string.
virtual void flush()=0
For output streams, writes any unwritten buffered data.
bool find(char *target)
Reads data from the stream until the target string is found.
size_t readBytes(char *buffer, size_t length)
Read chars from stream into buffer.
bool findUntil(char *target, char *terminator)
Reads data from the stream until the target string is found or the terminator string is found...
Class for printing to a stream or file.
Definition: spark_wiring_print.h:50
float parseFloat()
returns the first valid float value from the current position
void setTimeout(system_tick_t timeout)
Sets the read timeout (default: 1000 milliseconds)
Header for spark_wiring_print.c module.
virtual int read()=0
Read a byte from the stream.
virtual int peek()=0
Read a byte from the stream but do not remove it so read will return the same byte next time...
virtual int available()=0
Returns the number of a bytes available to read right now.