AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Loading...
Searching...
No Matches
Reader.h
Go to the documentation of this file.
1/*
2 Reader.cpp - a base class for reading data
3 For AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
4 This is part of the AVRTools library.
5 Copyright (c) 2014 Igor Mikolic-Torreira. All right reserved.
6 Functions readLong() and readFloat() adapted from Arduino code that
7 is Copyright (c) 2008 David A. Mellis and licensed under LGPL.
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21*/
22
32#ifndef Reader_h
33#define Reader_h
34
35
36#include <stddef.h>
37#include <stdint.h>
38
39
40#ifndef SERIAL_INPUT_EOL
41#define SERIAL_INPUT_EOL '\n'
42#endif
43
44
65class Reader
66{
67
68public:
69
70
75 Reader();
76
77
78
79 // Virtual methods (pure -- need to be implmented by derived classes)
80
87 virtual int read() = 0;
88
89
96 virtual int peek() = 0;
97
98
105 virtual bool available() = 0;
106
107
108
109
110 // Parsing methods
111
117 void setTimeout( unsigned long milliseconds )
118 { mTimeOut = milliseconds; }
119
120
128 bool find( const char *target )
129 { return findUntil( target, 0 ); }
130
131
141 bool find( const char *target, size_t length )
142 { return findUntil( target, length, NULL, 0 ); }
143
144
157 bool findUntil( const char *target, const char *terminator );
158
159
174 bool findUntil( const char *target, size_t targetLen, const char *terminate, size_t termLen );
175
176
187 bool readLong( long* result );
188
189
200 bool readFloat( float* result );
201
202
217 bool readLong( long* result, char skipChar );
218
219
234 bool readFloat( float* result, char skipChar );
235
236
247 size_t readBytes( char *buffer, size_t length );
248
249
262 size_t readBytesUntil( char terminator, char* buffer, size_t length );
263
264
275 size_t readBytes( uint8_t* buffer, size_t length )
276 { return readBytes( reinterpret_cast<char*>(buffer), length ); }
277
278
279
292 size_t readBytesUntil( uint8_t terminator, uint8_t* buffer, size_t length )
293 { return readBytesUntil( static_cast<char>(terminator), reinterpret_cast<char*>(buffer), length ); }
294
295
307 size_t readLine( char *buffer, size_t length );
308
309
314 void consumeWhiteSpace();
315
316
317private:
318
319 // Number of milliseconds to wait for the next char before aborting timed read
320 unsigned long mTimeOut;
321
322 int timedRead(); // private method to read stream with timeout
323 int timedPeek(); // private method to peek stream with timeout
324 int peekNextDigit(); // returns the next numeric digit in the stream or -1 if timeout
325};
326
327
328#endif
This is an abstract class defining a generic interface to read numbers and strings from a sequential ...
Definition Reader.h:66
bool findUntil(const char *target, const char *terminator)
Read data from the stream until the target string is found, or the terminator string is found,...
Definition Reader.cpp:156
size_t readBytes(char *buffer, size_t length)
Read characters from the input stream into a buffer, terminating if length characters have been read ...
Definition Reader.cpp:371
bool readLong(long *result)
Return the first valid long integer value from the stream.
Definition Reader.cpp:212
size_t readBytes(uint8_t *buffer, size_t length)
Read bytes (uint8_t) from the input stream into a buffer, terminating if length bytes have been read ...
Definition Reader.h:275
size_t readBytesUntil(char terminator, char *buffer, size_t length)
Read characters from the input stream into a buffer, terminating when the terminator charactor is enc...
Definition Reader.cpp:398
void consumeWhiteSpace()
Consumes whitespace characters until the first non-whitespace character is encountered or the functio...
Definition Reader.cpp:449
bool find(const char *target, size_t length)
Read data from the stream until the target string of given length is found.
Definition Reader.h:141
size_t readBytesUntil(uint8_t terminator, uint8_t *buffer, size_t length)
Read bytes (uint8_t) from the input stream into a buffer, terminating when the terminator byte is enc...
Definition Reader.h:292
size_t readLine(char *buffer, size_t length)
Read characters from the input stream into a buffer, until it reaches EOL, or if length characters ha...
Definition Reader.cpp:424
bool find(const char *target)
Read data from the input stream until the target string is found.
Definition Reader.h:128
virtual int peek()=0
Pure virtual function that examines the next byte from the input stream, without removing it.
virtual int read()=0
Pure virtual function that reads and removes the next byte from the input stream.
void setTimeout(unsigned long milliseconds)
Sets maximum milliseconds to wait for stream data, default is 1 second.
Definition Reader.h:117
virtual bool available()=0
Pure virtual function that determines if data is available in the input stream.
Reader()
Constructor. It sets the default timeout to 1 second.
Definition Reader.cpp:70
bool readFloat(float *result)
Return the first valid float value from the stream.
Definition Reader.cpp:280