ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
ExtStreaming.h
Go to the documentation of this file.
1 /*
2 Streaming.h - Arduino library for supporting the << streaming operator
3 Copyright (c) 2010-2012 Mikal Hart. All rights reserved.
4 
5 ExtStreaming.h by Jean-Michel Gallego is a copy of Streaming.h.
6 endl had been removed and replaced by eol for compatibility with SdFat.
7 
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12 
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 
23 #ifndef EXT_STREAMING_H
24 #define EXT_STREAMING_H
25 
26 #if defined(ARDUINO) && ARDUINO >= 100
27 #include "Arduino.h"
28 #else
29 #include "WProgram.h"
30 #endif
31 
32 #define STREAMING_LIBRARY_VERSION 5
33 
34 // Generic template
35 template<class T>
36 inline Print &operator <<(Print &stream, T arg)
37 {
38  stream.print(arg);
39  return stream;
40 }
41 
42 struct _BASED {
43  long val;
44  int base;
45  _BASED(long v, int b): val(v), base(b)
46  {}
47 };
48 
49 #if ARDUINO >= 100
50 
51 struct _BYTE_CODE {
52  byte val;
53  _BYTE_CODE(byte v) : val(v)
54  {}
55 };
56 #define _BYTE(a) _BYTE_CODE(a)
57 
58 inline Print &operator <<(Print &obj, const _BYTE_CODE &arg)
59 {
60  obj.write(arg.val);
61  return obj;
62 }
63 
64 #else
65 
66 #define _BYTE(a) _BASED(a, BYTE)
67 
68 #endif
69 
70 #define _HEX(a) _BASED(a, HEX)
71 #define _DEC(a) _BASED(a, DEC)
72 #define _OCT(a) _BASED(a, OCT)
73 #define _BIN(a) _BASED(a, BIN)
74 
75 // Specialization for class _BASED
76 // Thanks to Arduino forum user Ben Combee who suggested this
77 // clever technique to allow for expressions like
78 // Serial << _HEX(a);
79 
80 inline Print &operator <<(Print &obj, const _BASED &arg)
81 {
82  obj.print(arg.val, arg.base);
83  return obj;
84 }
85 
86 #if ARDUINO >= 18
87 // Specialization for class _FLOAT
88 // Thanks to Michael Margolis for suggesting a way
89 // to accommodate Arduino 0018's floating point precision
90 // feature like this:
91 // Serial << _FLOAT(gps_latitude, 6); // 6 digits of precision
92 
93 struct _FLOAT {
94  float val;
95  int digits;
96  _FLOAT(double v, int d): val(v), digits(d)
97  {}
98 };
99 
100 inline Print &operator <<(Print &obj, const _FLOAT &arg)
101 {
102  obj.print(arg.val, arg.digits);
103  return obj;
104 }
105 #endif
106 
107 // Specialization for enum _EndLineCode
108 // Thanks to Arduino forum user Paul V. who suggested this
109 // clever technique to allow for expressions like
110 // Serial << "Hello!" << endl;
111 
112 /*
113 enum _EndLineCode { endl };
114 
115 inline Print &operator <<(Print &obj, _EndLineCode arg)
116 { obj.println(); return obj; }
117 */
118 
119 enum _EndLineCode { eol };
120 
121 inline Print &operator <<(Print &obj, _EndLineCode arg)
122 {
123  obj.print( "\r\n" );
124  return obj;
125 }
126 
127 #endif // EXT_STREAMING_H
operator<<
Print & operator<<(Print &stream, T arg)
Definition: ExtStreaming.h:36
_BASED
Definition: ExtStreaming.h:42
eol
@ eol
Definition: ExtStreaming.h:119
_BASED::base
int base
Definition: ExtStreaming.h:44
_BASED::_BASED
_BASED(long v, int b)
Definition: ExtStreaming.h:45
_EndLineCode
_EndLineCode
Definition: ExtStreaming.h:119
_BASED::val
long val
Definition: ExtStreaming.h:43