AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
Writer.h
Go to the documentation of this file.
1 /*
2  Writer.h - a base class for writing 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 printNumber() and printFloat() 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 
23 
34 #ifndef Writer_h
35 #define Writer_h
36 
37 #include <stdint.h>
38 #include <stddef.h>
39 
40 
41 
42 #ifndef SERIAL_OUTPUT_EOL
43 #define SERIAL_OUTPUT_EOL '\n'
44 #endif
45 
46 
47 
48 
63 class Writer
64 {
65 public:
66 
67 
74  {
75  kBin = 2,
76  kOct = 8,
77  kDec = 10,
78  kHex = 16
79  };
80 
81 
82 
90  virtual size_t write( char c ) = 0;
91 
92 
100  virtual size_t write( const char* str ) = 0;
101 
102 
111  virtual size_t write( const char* buffer, size_t size ) = 0;
112 
113 
122  virtual size_t write( const uint8_t* buffer, size_t size ) = 0;
123 
124 
129  virtual void flush() = 0;
130 
131 
132 
133 
145  size_t print( const char* str, bool addLn = false );
146 
147 
160  size_t print( const uint8_t* buf, size_t size, bool addLn = false );
161 
162 
174  size_t print( char c, bool addLn = false );
175 
176 
190  size_t print( int8_t n, int base = kDec, bool addLn = false );
191 
192 
206  size_t print( uint8_t n, int base = kDec, bool addLn = false )
207  { return print( static_cast<unsigned long>( n ), base, addLn ); }
208 
222  size_t print( int n, int base = kDec, bool addLn = false );
223 
224 
238  size_t print( unsigned int n, int base = kDec, bool addLn = false )
239  { return print( static_cast<unsigned long>( n ), base, addLn ); }
240 
241 
255  size_t print( long n, int base = kDec, bool addLn = false );
256 
257 
271  size_t print( unsigned long n, int base = kDec, bool addLn = false );
272 
273 
286  size_t print( double d, int digits = 2, bool addLn = false );
287 
288 
297  size_t println( const char* str ) { return print( str, true ); }
298 
308  size_t println( const uint8_t* buf, size_t size ) { return print( buf, size, true ); }
309 
318  size_t println( char c ) { return print( c, true ); }
319 
332  size_t println( int8_t n, int base = kDec ) { return print( n, base, true ); }
333 
346  size_t println( uint8_t n, int base = kDec ) { return print( n, base, true ); }
347 
360  size_t println( int n, int base = kDec ) { return print( n, base, true ); }
361 
374  size_t println( unsigned int n, int base = kDec ) { return print( n, base, true ); }
375 
388  size_t println( long n, int base = kDec ) { return print( n, base, true ); }
389 
402  size_t println( unsigned long n, int base = kDec ) { return print( n, base, true ); }
403 
415  size_t println( double d, int digits = 2 ) { return print( d, digits, true ); }
416 
420  size_t println();
421 
422 private:
423 
424  size_t printNumber( unsigned long n, uint8_t base );
425  size_t printFloat( double d, uint8_t digits );
426 };
427 
428 #endif
429 
This is an abstract class defining a generic interface to write numbers and strings to a sequential s...
Definition: Writer.h:63
IntegerOutputBase
An enumeration that defines the number that will be used as the base for representing integer quantit...
Definition: Writer.h:73
size_t println(long n, int base=kDec)
Print a long integer to the output stream, adding a new line character at the end.
Definition: Writer.h:388
size_t println(int n, int base=kDec)
Print an integer to the output stream, adding a new line character at the end.
Definition: Writer.h:360
size_t println(const char *str)
Print a null-terminated string to the output stream, adding a new line character at the end...
Definition: Writer.h:297
size_t println(char c)
Print a single character to the output stream, adding a new line character at the end...
Definition: Writer.h:318
size_t print(unsigned int n, int base=kDec, bool addLn=false)
Print an unsigned integer to the output stream, with or without adding a new line character at the en...
Definition: Writer.h:238
size_t println(const uint8_t *buf, size_t size)
Print a number of bytes to the output stream, adding a new line character at the end.
Definition: Writer.h:308
size_t println()
Print a new line to the output stream.
Definition: Writer.cpp:191
size_t print(const char *str, bool addLn=false)
Print a null-terminated string to the output stream, with or without adding a new line character at t...
Definition: Writer.cpp:73
virtual size_t write(char c)=0
Pure virtual function that writes a single character to the output stream.
Definition: Writer.cpp:36
size_t print(uint8_t n, int base=kDec, bool addLn=false)
Print an 8-bit unsigned integer to the output stream, with or without adding a new line character at ...
Definition: Writer.h:206
size_t println(unsigned int n, int base=kDec)
Print an unsigned integer to the output stream, adding a new line character at the end...
Definition: Writer.h:374
size_t println(int8_t n, int base=kDec)
Print an 8-bit integer to the output stream, adding a new line character at the end.
Definition: Writer.h:332
Produce a decimal representation of integers (e.g., 11 is output as 11.
Definition: Writer.h:77
size_t println(double d, int digits=2)
Print a floating point number to the output stream, adding a new line character at the end...
Definition: Writer.h:415
Produce a hexadecimal representation of integers (e.g., 11 is output as 0x0b)
Definition: Writer.h:78
size_t println(uint8_t n, int base=kDec)
Print an 8-bit unsigned integer to the output stream, adding a new line character at the end...
Definition: Writer.h:346
Produce an octal representation of integers (e.g, 11 is output as 013)
Definition: Writer.h:76
size_t println(unsigned long n, int base=kDec)
Print an unsigned long integer to the output stream, adding a new line character at the end...
Definition: Writer.h:402
virtual void flush()=0
Pure virtual function to flush the output stream. When this function returns, all previously written ...
Definition: Writer.cpp:64
Produce a binary representation of integers (e.g., 11 is output as 0b1011)
Definition: Writer.h:75