iCar
Print.h
1 /*
2  Print.h - Base class that provides print() and println()
3  Copyright (c) 2008 David A. Mellis. All right reserved.
4  Copyright (C) 2020 Kernel Software Engineering.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #ifndef Print_h
22 #define Print_h
23 
24 #include <inttypes.h>
25 #include <stdio.h> // for size_t
26 
27 #include "WString.h"
28 #include "Printable.h"
29 
30 #define DEC 10
31 #define HEX 16
32 #define OCT 8
33 #ifdef BIN // Prevent warnings if BIN is previously defined in "iotnx4.h" or similar
34 #undef BIN
35 #endif
36 #define BIN 2
37 
38 class Print
39 {
40  private:
41  int write_error;
42  size_t printNumber(unsigned long, uint8_t);
43  size_t printFloat(double, uint8_t);
44  protected:
45  void setWriteError(int err = 1) { write_error = err; }
46  public:
47  Print() : write_error(0) {}
48 
49  int getWriteError() { return write_error; }
50  void clearWriteError() { setWriteError(0); }
51 
52  virtual size_t write(uint8_t) = 0;
53  size_t write(const char *str) {
54  if (str == NULL) return 0;
55  return write((const uint8_t *)str, strlen(str));
56  }
57  virtual size_t write(const uint8_t *buffer, size_t size);
58  size_t write(const char *buffer, size_t size) {
59  return write((const uint8_t *)buffer, size);
60  }
61 
62  // default to zero, meaning "a single write may block"
63  // should be overriden by subclasses with buffering
64  virtual int availableForWrite() { return 0; }
65 
66  size_t print(const String &);
67  size_t print(const char[]);
68  size_t print(char);
69  size_t print(unsigned char, int = DEC);
70  size_t print(int, int = DEC);
71  size_t print(unsigned int, int = DEC);
72  size_t print(long, int = DEC);
73  size_t print(unsigned long, int = DEC);
74  size_t print(double, int = 2);
75  size_t print(const Printable&);
76 
77  size_t println(const String &s);
78  size_t println(const char[]);
79  size_t println(char);
80  size_t println(unsigned char, int = DEC);
81  size_t println(int, int = DEC);
82  size_t println(unsigned int, int = DEC);
83  size_t println(long, int = DEC);
84  size_t println(unsigned long, int = DEC);
85  size_t println(double, int = 2);
86  size_t println(const Printable&);
87  size_t println(void);
88 
89  virtual void flush() { /* Empty implementation for backward compatibility */ }
90 };
91 
92 #endif
Definition: Printable.h:33
Definition: Print.h:38
Definition: WString.h:39