forked from scottdky/Streaming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreaming.h
More file actions
274 lines (233 loc) · 5.16 KB
/
Streaming.h
File metadata and controls
274 lines (233 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Streaming.h - Arduino library for supporting the << streaming operator
Copyright (c) 2010-2012 Mikal Hart. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef ARDUINO_STREAMING
#define ARDUINO_STREAMING
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define STREAMING_LIBRARY_VERSION 5
#define cout Serial
#define tabl '\t'
// Generic template
template<class T>
inline Print &operator <<(Print &stream, T arg)
{ stream.print(arg); return stream; }
struct _BASED
{
long val;
int base;
_BASED(long v, int b): val(v), base(b)
{}
};
#if ARDUINO >= 100
struct _BYTE_CODE
{
byte val;
_BYTE_CODE(byte v) : val(v)
{}
};
#define _BYTE(a) _BYTE_CODE(a)
inline Print &operator <<(Print &obj, const _BYTE_CODE &arg)
{ obj.write(arg.val); return obj; }
#else
#define _BYTE(a) _BASED(a, BYTE)
#endif
#define _HEX(a) _BASED(a, HEX)
#define _DEC(a) _BASED(a, DEC)
#define _OCT(a) _BASED(a, OCT)
#define _BIN(a) _BASED(a, BIN)
// Specialization for class _BASED
// Thanks to Arduino forum user Ben Combee who suggested this
// clever technique to allow for expressions like
// Serial << _HEX(a);
inline Print &operator <<(Print &obj, const _BASED &arg)
{ obj.print(arg.val, arg.base); return obj; }
#if ARDUINO >= 18
// Specialization for class _FLOAT
// Thanks to Michael Margolis for suggesting a way
// to accommodate Arduino 0018's floating point precision
// feature like this:
// Serial << _FLOAT(gps_latitude, 6); // 6 digits of precision
struct _FLOAT
{
float val;
int digits;
_FLOAT(double v, int d): val(v), digits(d)
{}
};
inline Print &operator <<(Print &obj, const _FLOAT &arg)
{ obj.print(arg.val, arg.digits); return obj; }
#endif
#if ARDUINO >= 18
// Specialization for class _FIXED
// a fixed point number
// output val/(10^d)
// n=1234
// Serial << _FIXED(n, 2); // output 12.34
struct _FIXED
{
long val;
int digits;
_FIXED(long v, int d): val(v), digits(d)
{}
};
Print &operator <<(Print &obj, const _FIXED &arg)
{
if(arg.digits<=0){
obj.print(arg.val);
}
else{
char buf[12];
char *str = &buf[sizeof(buf) - 1];
unsigned long n;
int i = arg.digits;
if(arg.val<0){
n = -arg.val;
}
else{
n = arg.val;
}
*str = '\0';
do {
unsigned long m = n;
n /= 10;
char c = m - 10 * n;
*--str = c + '0';
i--;
if(i==0){*--str='.';}
} while(n);
while(i>=0){
if(i==0){
*--str='.';
*--str='0';
}
else{
*--str='0';
}
i--;
}
if(arg.val<0){*--str='-';}
obj.print(str);
}
return obj;
}
#endif
#if ARDUINO >= 18
// Specialization for class _DYNAMIC(v,d,s)
// 输出一个定位小数(10进制),但有长度s限制,输出尽量多的有效位(舍去的小数部分四舍五入)
// output v/(10^d)
//
// Serial << _DYNAMIC(12345, 2,4); // output 123.5
struct _DYNAMIC
{
long val;
int digits;
_DYNAMIC(long v, int d,size_t s)
{
long b=1;
val=v;
if (d<0){
digits=0;
}
else{
digits=d;
}
if(s<=0){
return;
}
if (v<0){
s--;
}
while(s>0){
b *= 10;
s--;
}
while(digits>0 && abs(val)>=b){
val =(val+5)/10;
digits--;
}
}
};
Print &operator <<(Print &obj, const _DYNAMIC &arg)
{
if(arg.digits<=0){
obj.print(arg.val);
}
else{
obj <<_FIXED(arg.val,arg.digits);
}
return obj;
}
#endif
#if ARDUINO >= 18
// Specialization for class _LEADING0(v,d,s)
// 输出一个定位小数(10进制),显示前度零 ,长度s
// output v/(10^d)
// n=1234
// Serial << _DYNAMIC(n, 2,5); // output 012.34
struct _LEADING0
{
long val;
int digits;
size_t size;
_LEADING0(long v, int d,size_t s):val(v),size(s)
{if (d<0 || d >=s){
digits=0;
}else{
digits=d;
}}
};
Print &operator <<(Print &obj, const _LEADING0 &arg)
{
int32_t v,b=1L;
uint8_t s=0;
while(s<arg.digits){
b *= 10;
s++;
}
v=arg.val;
if(v<0){ //负数
v=-v;
s++;
obj <<"-";
}
if(v<b){s++;b *=10;} //纯小数
while(s<arg.size){
if(v<b){
obj <<"0";
}
s++;
b *=10;
}
if(arg.digits<=0){
obj.print(v);
}
else{
obj <<_FIXED(v,arg.digits);
}
return obj;
}
#endif
// Specialization for enum _EndLineCode
// Thanks to Arduino forum user Paul V. who suggested this
// clever technique to allow for expressions like
// Serial << "Hello!" << endl;
enum _EndLineCode { endl };
inline Print &operator <<(Print &obj, _EndLineCode arg)
{ obj.println(); return obj; }
#endif