|
1 |
| -/* |
2 |
| -Streaming.h - Arduino library for supporting the << streaming operator |
3 |
| -Copyright (c) 2010-2012 Mikal Hart. All rights 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 |
| - |
20 |
| -#ifndef ARDUINO_STREAMING |
21 |
| -#define ARDUINO_STREAMING |
22 |
| - |
| 1 | +/* |
| 2 | + Streaming.h - Arduino library for supporting the << streaming operator |
| 3 | + Copyright (c) 2010-2012 Mikal Hart. All rights 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 | + |
| 20 | +#ifndef ARDUINO_STREAMING |
| 21 | +#define ARDUINO_STREAMING |
| 22 | + |
23 | 23 | #if defined(ARDUINO) && ARDUINO >= 100
|
24 |
| -#include "Arduino.h" |
25 |
| -#else |
26 |
| -#include "WProgram.h" |
27 |
| -#endif |
28 |
| - |
29 |
| -#define STREAMING_LIBRARY_VERSION 5 |
30 |
| - |
31 |
| -// Generic template |
32 |
| -template<class T> |
33 |
| -inline Print &operator <<(Print &stream, T arg) |
34 |
| -{ stream.print(arg); return stream; } |
35 |
| - |
36 |
| -struct _BASED |
37 |
| -{ |
38 |
| - long val; |
39 |
| - int base; |
40 |
| - _BASED(long v, int b): val(v), base(b) |
41 |
| - {} |
42 |
| -}; |
43 |
| - |
44 |
| -#if ARDUINO >= 100 |
45 |
| - |
46 |
| -struct _BYTE_CODE |
47 |
| -{ |
48 |
| - byte val; |
49 |
| - _BYTE_CODE(byte v) : val(v) |
50 |
| - {} |
51 |
| -}; |
52 |
| -#define _BYTE(a) _BYTE_CODE(a) |
53 |
| - |
54 |
| -inline Print &operator <<(Print &obj, const _BYTE_CODE &arg) |
55 |
| -{ obj.write(arg.val); return obj; } |
56 |
| - |
57 |
| -#else |
58 |
| - |
59 |
| -#define _BYTE(a) _BASED(a, BYTE) |
60 |
| - |
61 |
| -#endif |
62 |
| - |
63 |
| -#define _HEX(a) _BASED(a, HEX) |
64 |
| -#define _DEC(a) _BASED(a, DEC) |
65 |
| -#define _OCT(a) _BASED(a, OCT) |
66 |
| -#define _BIN(a) _BASED(a, BIN) |
67 |
| - |
68 |
| -// Specialization for class _BASED |
69 |
| -// Thanks to Arduino forum user Ben Combee who suggested this |
70 |
| -// clever technique to allow for expressions like |
71 |
| -// Serial << _HEX(a); |
72 |
| - |
73 |
| -inline Print &operator <<(Print &obj, const _BASED &arg) |
74 |
| -{ obj.print(arg.val, arg.base); return obj; } |
75 |
| - |
76 |
| -#if ARDUINO >= 18 |
77 |
| -// Specialization for class _FLOAT |
78 |
| -// Thanks to Michael Margolis for suggesting a way |
79 |
| -// to accommodate Arduino 0018's floating point precision |
80 |
| -// feature like this: |
81 |
| -// Serial << _FLOAT(gps_latitude, 6); // 6 digits of precision |
82 |
| - |
83 |
| -struct _FLOAT |
84 |
| -{ |
85 |
| - float val; |
86 |
| - int digits; |
87 |
| - _FLOAT(double v, int d): val(v), digits(d) |
88 |
| - {} |
89 |
| -}; |
90 |
| - |
91 |
| -inline Print &operator <<(Print &obj, const _FLOAT &arg) |
92 |
| -{ obj.print(arg.val, arg.digits); return obj; } |
93 |
| -#endif |
94 |
| - |
95 |
| -// Specialization for enum _EndLineCode |
96 |
| -// Thanks to Arduino forum user Paul V. who suggested this |
97 |
| -// clever technique to allow for expressions like |
98 |
| -// Serial << "Hello!" << endl; |
99 |
| - |
100 |
| -enum _EndLineCode { endl }; |
101 |
| - |
102 |
| -inline Print &operator <<(Print &obj, _EndLineCode arg) |
103 |
| -{ obj.println(); return obj; } |
104 |
| - |
105 |
| -#endif |
| 24 | +#include "Arduino.h" |
| 25 | +#else |
| 26 | +#include "WProgram.h" |
| 27 | +#endif |
| 28 | + |
| 29 | +#define STREAMING_LIBRARY_VERSION 5 |
| 30 | + |
| 31 | +// Generic template |
| 32 | +template<class T> |
| 33 | +inline Print &operator <<(Print &stream, T arg) |
| 34 | +{ stream.print(arg); return stream; } |
| 35 | + |
| 36 | +struct _BASED |
| 37 | +{ |
| 38 | + long val; |
| 39 | + int base; |
| 40 | + _BASED(long v, int b): val(v), base(b) |
| 41 | + {} |
| 42 | +}; |
| 43 | + |
| 44 | +#if ARDUINO >= 100 |
| 45 | + |
| 46 | +struct _BYTE_CODE |
| 47 | +{ |
| 48 | + byte val; |
| 49 | + _BYTE_CODE(byte v) : val(v) |
| 50 | + {} |
| 51 | +}; |
| 52 | +#define _BYTE(a) _BYTE_CODE(a) |
| 53 | + |
| 54 | +inline Print &operator <<(Print &obj, const _BYTE_CODE &arg) |
| 55 | +{ obj.write(arg.val); return obj; } |
| 56 | + |
| 57 | +#else |
| 58 | + |
| 59 | +#define _BYTE(a) _BASED(a, BYTE) |
| 60 | + |
| 61 | +#endif |
| 62 | + |
| 63 | +#define _HEX(a) _BASED(a, HEX) |
| 64 | +#define _DEC(a) _BASED(a, DEC) |
| 65 | +#define _OCT(a) _BASED(a, OCT) |
| 66 | +#define _BIN(a) _BASED(a, BIN) |
| 67 | + |
| 68 | +// Specialization for class _BASED |
| 69 | +// Thanks to Arduino forum user Ben Combee who suggested this |
| 70 | +// clever technique to allow for expressions like |
| 71 | +// Serial << _HEX(a); |
| 72 | + |
| 73 | +inline Print &operator <<(Print &obj, const _BASED &arg) |
| 74 | +{ obj.print(arg.val, arg.base); return obj; } |
| 75 | + |
| 76 | +#if ARDUINO >= 18 |
| 77 | +// Specialization for class _FLOAT |
| 78 | +// Thanks to Michael Margolis for suggesting a way |
| 79 | +// to accommodate Arduino 0018's floating point precision |
| 80 | +// feature like this: |
| 81 | +// Serial << _FLOAT(gps_latitude, 6); // 6 digits of precision |
| 82 | + |
| 83 | +struct _FLOAT |
| 84 | +{ |
| 85 | + float val; |
| 86 | + int digits; |
| 87 | + _FLOAT(double v, int d): val(v), digits(d) |
| 88 | + {} |
| 89 | +}; |
| 90 | + |
| 91 | +inline Print &operator <<(Print &obj, const _FLOAT &arg) |
| 92 | +{ obj.print(arg.val, arg.digits); return obj; } |
| 93 | +#endif |
| 94 | + |
| 95 | +// Specialization for enum _EndLineCode |
| 96 | +// Thanks to Arduino forum user Paul V. who suggested this |
| 97 | +// clever technique to allow for expressions like |
| 98 | +// Serial << "Hello!" << endl; |
| 99 | + |
| 100 | +enum _EndLineCode { endl }; |
| 101 | + |
| 102 | +inline Print &operator <<(Print &obj, _EndLineCode arg) |
| 103 | +{ obj.println(); return obj; } |
| 104 | + |
| 105 | +#endif |
0 commit comments