forked from greiman/SdFat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BufferedPrint.h
269 lines (266 loc) · 8.13 KB
/
BufferedPrint.h
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
/**
* Copyright (c) 2011-2021 Bill Greiman
* This file is part of the SdFat library for SD memory cards.
*
* MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifndef BufferedPrint_h
#define BufferedPrint_h
/**
* \file
* \brief Fast buffered print.
*/
#include "common/FmtNumber.h"
/**
* \class BufferedPrint
* \brief Fast buffered print template.
*/
template<typename WriteClass, uint8_t BUF_DIM>
class BufferedPrint {
public:
BufferedPrint() : m_wr(nullptr), m_in(0) {}
/** BufferedPrint constructor.
* \param[in] wr Print destination.
*/
explicit BufferedPrint(WriteClass* wr) : m_wr(wr), m_in(0) {}
/** Initialize the BuffedPrint class.
* \param[in] wr Print destination.
*/
void begin(WriteClass* wr) {
m_wr = wr;
m_in = 0;
}
/** Flush the buffer - same as sync() with no status return. */
void flush() {sync();}
/** Print a character followed by a field terminator.
* \param[in] c character to print.
* \param[in] term The field terminator. Use '\\n' for CR LF.
* \return true for success or false if an error occurs.
*/
size_t printField(char c, char term) {
char buf[3];
char* str = buf + sizeof(buf);
if (term) {
*--str = term;
if (term == '\n') {
*--str = '\r';
}
}
*--str = c;
return write(str, buf + sizeof(buf) - str);
}
/** Print a string stored in AVR flash followed by a field terminator.
* \param[in] fsh string to print.
* \param[in] term The field terminator. Use '\\n' for CR LF.
* \return true for success or false if an error occurs.
*/
size_t printField(const __FlashStringHelper *fsh, char term) {
#ifdef __AVR__
size_t rtn = 0;
PGM_P p = reinterpret_cast<PGM_P>(fsh);
char c;
while ((c = pgm_read_byte(p++))) {
if (!write(&c, 1)) {
return 0;
}
rtn++;
}
if (term) {
char buf[2];
char* str = buf + sizeof(buf);
*--str = term;
if (term == '\n') {
*--str = '\r';
}
rtn += write(str, buf + sizeof(buf) - str);
}
return rtn;
#else // __AVR__
return printField(reinterpret_cast<const char *>(fsh), term);
#endif // __AVR__
}
/** Print a string followed by a field terminator.
* \param[in] str string to print.
* \param[in] term The field terminator. Use '\\n' for CR LF.
* \return true for success or false if an error occurs.
*/
size_t printField(const char* str, char term) {
size_t rtn = write(str, strlen(str));
if (term) {
char buf[2];
char* ptr = buf + sizeof(buf);
*--ptr = term;
if (term == '\n') {
*--ptr = '\r';
}
rtn += write(ptr, buf + sizeof(buf) - ptr);
}
return rtn;
}
/** Print a double followed by a field terminator.
* \param[in] d The number to be printed.
* \param[in] term The field terminator. Use '\\n' for CR LF.
* \param[in] prec Number of digits after decimal point.
* \return true for success or false if an error occurs.
*/
size_t printField(double d, char term, uint8_t prec = 2) {
char buf[24];
char* str = buf + sizeof(buf);
if (term) {
*--str = term;
if (term == '\n') {
*--str = '\r';
}
}
str = fmtDouble(str, d, prec, false);
return write(str, buf + sizeof(buf) - str);
}
/** Print a float followed by a field terminator.
* \param[in] f The number to be printed.
* \param[in] term The field terminator. Use '\\n' for CR LF.
* \param[in] prec Number of digits after decimal point.
* \return true for success or false if an error occurs.
*/
size_t printField(float f, char term, uint8_t prec = 2) {
return printField(static_cast<double>(f), term, prec);
}
/** Print an integer value for 8, 16, and 32 bit signed and unsigned types.
* \param[in] n The value to print.
* \param[in] term The field terminator. Use '\\n' for CR LF.
* \return true for success or false if an error occurs.
*/
template<typename Type>
size_t printField(Type n, char term) {
const uint8_t DIM = sizeof(Type) <= 2 ? 8 : 13;
char buf[DIM];
char* str = buf + sizeof(buf);
if (term) {
*--str = term;
if (term == '\n') {
*--str = '\r';
}
}
Type p = n < 0 ? -n : n;
if (sizeof(Type) <= 2) {
str = fmtBase10(str, (uint16_t)p);
} else {
str = fmtBase10(str, (uint32_t)p);
}
if (n < 0) {
*--str = '-';
}
return write(str, buf + sizeof(buf) - str);
}
/** Print CR LF.
* \return true for success or false if an error occurs.
*/
size_t println() {
char buf[2];
buf[0] = '\r';
buf[1] = '\n';
return write(buf, 2);
}
/** Print a double.
* \param[in] d The number to be printed.
* \param[in] prec Number of digits after decimal point.
* \return true for success or false if an error occurs.
*/
size_t print(double d, uint8_t prec = 2) {
return printField(d, 0, prec);
}
/** Print a double followed by CR LF.
* \param[in] d The number to be printed.
* \param[in] prec Number of digits after decimal point.
* \return true for success or false if an error occurs.
*/
size_t println(double d, uint8_t prec = 2) {
return printField(d, '\n', prec);
}
/** Print a float.
* \param[in] f The number to be printed.
* \param[in] prec Number of digits after decimal point.
* \return true for success or false if an error occurs.
*/
size_t print(float f, uint8_t prec = 2) {
return printField(static_cast<double>(f), 0, prec);
}
/** Print a float followed by CR LF.
* \param[in] f The number to be printed.
* \param[in] prec Number of digits after decimal point.
* \return true for success or false if an error occurs.
*/
size_t println(float f, uint8_t prec) {
return printField(static_cast<double>(f), '\n', prec);
}
/** Print character, string, or number.
* \param[in] v item to print.
* \return true for success or false if an error occurs.
*/
template<typename Type>
size_t print(Type v) {
return printField(v, 0);
}
/** Print character, string, or number followed by CR LF.
* \param[in] v item to print.
* \return true for success or false if an error occurs.
*/
template<typename Type>
size_t println(Type v) {
return printField(v, '\n');
}
/** Flush the buffer.
* \return true for success or false if an error occurs.
*/
bool sync() {
if (!m_wr || m_wr->write(m_buf, m_in) != m_in) {
return false;
}
m_in = 0;
return true;
}
/** Write data to an open file.
* \param[in] src Pointer to the location of the data to be written.
*
* \param[in] n Number of bytes to write.
*
* \return For success write() returns the number of bytes written, always
* \a n.
*/
size_t write(const void* src, size_t n) {
if ((m_in + n) > sizeof(m_buf)) {
if (!sync()) {
return 0;
}
if (n >= sizeof(m_buf)) {
return n == m_wr->write((const uint8_t*)src, n) ? n : 0;
}
}
memcpy(m_buf + m_in, src, n);
m_in += n;
return n;
}
private:
WriteClass* m_wr;
uint8_t m_in;
// Insure room for double.
uint8_t m_buf[BUF_DIM < 24 ? 24 : BUF_DIM]; // NOLINT
};
#endif // BufferedPrint_h