arduino-emulator
Loading...
Searching...
No Matches
Stream.h
1/*
2 Stream.h - base class for character-based streams.
3 Copyright (c) 2010 David A. Mellis. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19 parsing functions based on TextFinder library by Michael Margolis
20*/
21
22#pragma once
23
24#include <inttypes.h>
25#include "Print.h"
26
27// compatibility macros for testing
28/*
29#define getInt() parseInt()
30#define getInt(ignore) parseInt(ignore)
31#define getFloat() parseFloat()
32#define getFloat(ignore) parseFloat(ignore)
33#define getString( pre_string, post_string, buffer, length)
34readBytesBetween( pre_string, terminator, buffer, length)
35*/
36
37namespace arduino {
38
39// This enumeration provides the lookahead options for parseInt(), parseFloat()
40// The rules set out here are used until either the first valid character is found
41// or a time out occurs due to lack of input.
42enum LookaheadMode{
43 SKIP_ALL, // All invalid characters are ignored.
44 SKIP_NONE, // Nothing is skipped, and the stream is not touched unless the first waiting character is valid.
45 SKIP_WHITESPACE // Only tabs, spaces, line feeds & carriage returns are skipped.
46};
47
48#define NO_IGNORE_CHAR '\x01' // a char not found in a valid ASCII numeric field
49
50class Stream : public Print
51{
52 protected:
53 unsigned long _timeout; // number of milliseconds to wait for the next char before aborting timed read
54 unsigned long _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(LookaheadMode lookahead, bool detectDecimal); // returns the next numeric digit in the stream or -1 if timeout
58
59 public:
60 virtual int available() = 0;
61 virtual int read() = 0;
62 virtual int peek() = 0;
63
64 Stream() {_timeout=1000;}
65
66// parsing methods
67
68 void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
69 unsigned long getTimeout(void) { return _timeout; }
70
71 bool find(const char *target); // reads data from the stream until the target string is found
72 bool find(const uint8_t *target) { return find ((const char *)target); }
73 // returns true if target string is found, false if timed out (see setTimeout)
74
75 bool find(const char *target, size_t length); // reads data from the stream until the target string of given length is found
76 bool find(const uint8_t *target, size_t length) { return find ((const char *)target, length); }
77 // returns true if target string is found, false if timed out
78
79 bool find(char target) { return find (&target, 1); }
80
81 bool findUntil(const char *target, const char *terminator); // as find but search ends if the terminator string is found
82 bool findUntil(const uint8_t *target, const char *terminator) { return findUntil((const char *)target, terminator); }
83
84 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
85 bool findUntil(const uint8_t *target, size_t targetLen, const char *terminate, size_t termLen) {return findUntil((const char *)target, targetLen, terminate, termLen); }
86
87 long parseInt(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
88 // returns the first valid (long) integer value from the current position.
89 // lookahead determines how parseInt looks ahead in the stream.
90 // See LookaheadMode enumeration at the top of the file.
91 // Lookahead is terminated by the first character that is not a valid part of an integer.
92 // Once parsing commences, 'ignore' will be skipped in the stream.
93
94 float parseFloat(LookaheadMode lookahead = SKIP_ALL, char ignore = NO_IGNORE_CHAR);
95 // float version of parseInt
96
97 size_t readBytes( char *buffer, size_t length); // read chars from stream into buffer
98 size_t readBytes( uint8_t *buffer, size_t length) { return readBytes((char *)buffer, length); }
99 // terminates if length characters have been read or timeout (see setTimeout)
100 // returns the number of characters placed in the buffer (0 means no valid data found)
101
102 size_t readBytesUntil( char terminator, char *buffer, size_t length); // as readBytes with terminator character
103 size_t readBytesUntil( char terminator, uint8_t *buffer, size_t length) { return readBytesUntil(terminator, (char *)buffer, length); }
104 // terminates if length characters have been read, timeout, or if the terminator character detected
105 // returns the number of characters placed in the buffer (0 means no valid data found)
106
107 // Arduino String functions to be added here
108 String readString();
109 String readStringUntil(char terminator);
110
111 protected:
112 long parseInt(char ignore) { return parseInt(SKIP_ALL, ignore); }
113 float parseFloat(char ignore) { return parseFloat(SKIP_ALL, ignore); }
114 // These overload exists for compatibility with any class that has derived
115 // Stream and used parseFloat/Int with a custom ignore character. To keep
116 // the public API simple, these overload remains protected.
117
118 struct MultiTarget {
119 const char *str; // string you're searching for
120 size_t len; // length of string you're searching for
121 size_t index; // index used by the search routine.
122 };
123
124 // This allows you to search for an arbitrary number of strings.
125 // Returns index of the target that is found first or -1 if timeout occurs.
126 int findMulti(struct MultiTarget *targets, int tCount);
127};
128
129#undef NO_IGNORE_CHAR
130
131}
132
133using arduino::Stream;
Definition DMAPool.h:103
Definition Print.h:36
Definition Stream.h:51
Definition String.h:53
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31
Definition Stream.h:118