sanguino/cores/arduino/Stream.h

Fri, 17 Nov 2017 10:13:31 +0100

author
mdd
date
Fri, 17 Nov 2017 10:13:31 +0100
changeset 3
770b218a4931
parent 2
b373b0288715
permissions
-rw-r--r--

proper configuration, homing and planner optimization

2
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
1 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
2 Stream.h - base class for character-based streams.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
3 Copyright (c) 2010 David A. Mellis. All right reserved.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
4
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
6 modify it under the terms of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
7 License as published by the Free Software Foundation; either
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
9
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
13 Lesser General Public License for more details.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
14
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
15 You should have received a copy of the GNU Lesser General Public
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
16 License along with this library; if not, write to the Free Software
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
18
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
19 parsing functions based on TextFinder library by Michael Margolis
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
20 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
21
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
22 #ifndef Stream_h
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
23 #define Stream_h
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
24
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
25 #include <inttypes.h>
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
26 #include "Print.h"
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
27
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
28 // compatability macros for testing
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
29 /*
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
30 #define getInt() parseInt()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
31 #define getInt(skipChar) parseInt(skipchar)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
32 #define getFloat() parseFloat()
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
33 #define getFloat(skipChar) parseFloat(skipChar)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
34 #define getString( pre_string, post_string, buffer, length)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
35 readBytesBetween( pre_string, terminator, buffer, length)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
36 */
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
37
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
38 class Stream : public Print
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
39 {
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
40 private:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
41 unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
42 unsigned long _startMillis; // used for timeout measurement
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
43 int timedRead(); // private method to read stream with timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
44 int timedPeek(); // private method to peek stream with timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
45 int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
46
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
47 public:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
48 virtual int available() = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
49 virtual int read() = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
50 virtual int peek() = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
51 virtual void flush() = 0;
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
52
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
53 Stream() {_timeout=1000;}
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
54
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
55 // parsing methods
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
56
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
57 void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
58
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
59 bool find(char *target); // reads data from the stream until the target string is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
60 // returns true if target string is found, false if timed out (see setTimeout)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
61
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
62 bool find(char *target, size_t length); // reads data from the stream until the target string of given length is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
63 // returns true if target string is found, false if timed out
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
64
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
65 bool findUntil(char *target, char *terminator); // as find but search ends if the terminator string is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
66
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
67 bool findUntil(char *target, size_t targetLen, char *terminate, size_t termLen); // as above but search ends if the terminate string is found
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
68
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
69
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
70 long parseInt(); // returns the first valid (long) integer value from the current position.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
71 // initial characters that are not digits (or the minus sign) are skipped
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
72 // integer is terminated by the first character that is not a digit.
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
73
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
74 float parseFloat(); // float version of parseInt
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
75
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
76 size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
77 // terminates if length characters have been read or timeout (see setTimeout)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
78 // returns the number of characters placed in the buffer (0 means no valid data found)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
79
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
80 size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
81 // terminates if length characters have been read, timeout, or if the terminator character detected
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
82 // returns the number of characters placed in the buffer (0 means no valid data found)
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
83
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
84 // Arduino String functions to be added here
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
85 String readString();
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
86 String readStringUntil(char terminator);
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
87
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
88 protected:
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
89 long parseInt(char skipChar); // as above but the given skipChar is ignored
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
90 // as above but the given skipChar is ignored
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
91 // this allows format characters (typically commas) in values to be ignored
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
92
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
93 float parseFloat(char skipChar); // as above but the given skipChar is ignored
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
94 };
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
95
b373b0288715 added missing sanguino files
mbayer
parents:
diff changeset
96 #endif

mercurial