Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 286 lines (238 sloc) 5.686 kB
1a89112 @damellis Adding the Print class.
damellis authored
1 /*
2 Print.cpp - Base class that provides print() and println()
3 Copyright (c) 2008 David A. Mellis. All right 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 Modified 23 November 2006 by David A. Mellis
20 */
21
9343126 @damellis Adding support for printing Strings to the Print class.
damellis authored
22 #include <stdlib.h>
1a89112 @damellis Adding the Print class.
damellis authored
23 #include <stdio.h>
24 #include <string.h>
2661608 @damellis Adding support for printing floats and doubles (defaulting to 2 decim…
damellis authored
25 #include <math.h>
bb7a044 @damellis Moving wiring.h contents into Arduino.h.
damellis authored
26 #include "Arduino.h"
1a89112 @damellis Adding the Print class.
damellis authored
27
28 #include "Print.h"
29
30 // Public Methods //////////////////////////////////////////////////////////////
31
9dd3491 @damellis Adding write(str) and write(buf, size) methods to Print class and Eth…
damellis authored
32 /* default implementation: may be overridden */
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
33 long Print::write(const char *str)
9dd3491 @damellis Adding write(str) and write(buf, size) methods to Print class and Eth…
damellis authored
34 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
35 long n = 0;
36 while (*str) {
37 if (write(*str++) <= 0) break;
38 n++;
39 }
40 return n;
9dd3491 @damellis Adding write(str) and write(buf, size) methods to Print class and Eth…
damellis authored
41 }
42
43 /* default implementation: may be overridden */
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
44 long Print::write(const uint8_t *buffer, size_t size)
9dd3491 @damellis Adding write(str) and write(buf, size) methods to Print class and Eth…
damellis authored
45 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
46 long n = 0;
47 while (size--) {
48 if (write(*buffer++) <= 0) break;
49 n++;
50 }
51 return n;
9dd3491 @damellis Adding write(str) and write(buf, size) methods to Print class and Eth…
damellis authored
52 }
53
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
54 long Print::print(const __FlashStringHelper *ifsh)
0ac0dcf @damellis Adding F("foo") syntax for flash strings.
damellis authored
55 {
56 const prog_char *p = (const prog_char *)ifsh;
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
57 long n = 0;
0ac0dcf @damellis Adding F("foo") syntax for flash strings.
damellis authored
58 while (1) {
59 unsigned char c = pgm_read_byte(p++);
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
60 if (c == 0) break;
61 if (write(c) <= 0) break;
62 n++;
0ac0dcf @damellis Adding F("foo") syntax for flash strings.
damellis authored
63 }
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
64 return n;
0ac0dcf @damellis Adding F("foo") syntax for flash strings.
damellis authored
65 }
66
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
67 long Print::print(const String &s)
9343126 @damellis Adding support for printing Strings to the Print class.
damellis authored
68 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
69 long n = 0;
722b93d @damellis Changing String::toCharArray() and getBytes() to accept a buffer, rat…
damellis authored
70 for (int i = 0; i < s.length(); i++) {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
71 if (write(s[i]) < 0) break;
72 n++;
722b93d @damellis Changing String::toCharArray() and getBytes() to accept a buffer, rat…
damellis authored
73 }
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
74 return n;
9343126 @damellis Adding support for printing Strings to the Print class.
damellis authored
75 }
76
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
77 long Print::print(const char str[])
1a89112 @damellis Adding the Print class.
damellis authored
78 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
79 return write(str);
1a89112 @damellis Adding the Print class.
damellis authored
80 }
81
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
82 long Print::print(char c)
1a89112 @damellis Adding the Print class.
damellis authored
83 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
84 return write(c);
1a89112 @damellis Adding the Print class.
damellis authored
85 }
86
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
87 long Print::print(unsigned char b, int base)
1a89112 @damellis Adding the Print class.
damellis authored
88 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
89 return print((unsigned long) b, base);
1a89112 @damellis Adding the Print class.
damellis authored
90 }
91
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
92 long Print::print(int n, int base)
1a89112 @damellis Adding the Print class.
damellis authored
93 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
94 return print((long) n, base);
1a89112 @damellis Adding the Print class.
damellis authored
95 }
96
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
97 long Print::print(unsigned int n, int base)
1a89112 @damellis Adding the Print class.
damellis authored
98 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
99 return print((unsigned long) n, base);
1a89112 @damellis Adding the Print class.
damellis authored
100 }
101
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
102 long Print::print(long n, int base)
1a89112 @damellis Adding the Print class.
damellis authored
103 {
def8668 @damellis Adding precision parameter for printing of floats / doubles. Restruc…
damellis authored
104 if (base == 0) {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
105 return write(n);
def8668 @damellis Adding precision parameter for printing of floats / doubles. Restruc…
damellis authored
106 } else if (base == 10) {
107 if (n < 0) {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
108 long t = print('-');
109 if (t <= 0) return t;
def8668 @damellis Adding precision parameter for printing of floats / doubles. Restruc…
damellis authored
110 n = -n;
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
111 return printNumber(n, 10) + 1;
def8668 @damellis Adding precision parameter for printing of floats / doubles. Restruc…
damellis authored
112 }
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
113 return printNumber(n, 10);
def8668 @damellis Adding precision parameter for printing of floats / doubles. Restruc…
damellis authored
114 } else {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
115 return printNumber(n, base);
def8668 @damellis Adding precision parameter for printing of floats / doubles. Restruc…
damellis authored
116 }
1a89112 @damellis Adding the Print class.
damellis authored
117 }
118
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
119 long Print::print(unsigned long n, int base)
2661608 @damellis Adding support for printing floats and doubles (defaulting to 2 decim…
damellis authored
120 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
121 if (base == 0) return write(n);
122 else return printNumber(n, base);
2661608 @damellis Adding support for printing floats and doubles (defaulting to 2 decim…
damellis authored
123 }
124
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
125 long Print::print(double n, int digits)
1a89112 @damellis Adding the Print class.
damellis authored
126 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
127 return printFloat(n, digits);
1a89112 @damellis Adding the Print class.
damellis authored
128 }
129
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
130 long Print::println(const __FlashStringHelper *ifsh)
0ac0dcf @damellis Adding F("foo") syntax for flash strings.
damellis authored
131 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
132 long n = print(ifsh);
133 if (n >= 0) n += println();
134 return n;
0ac0dcf @damellis Adding F("foo") syntax for flash strings.
damellis authored
135 }
136
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
137 long Print::print(const Printable& x)
87a04fc @damellis Added Printable interface class to allow printing of classes such as …
damellis authored
138 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
139 return x.printTo(*this);
87a04fc @damellis Added Printable interface class to allow printing of classes such as …
damellis authored
140 }
141
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
142 long Print::println(void)
1a89112 @damellis Adding the Print class.
damellis authored
143 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
144 long t = print('\r');
145 if (t <= 0) return t;
146 if (print('\n') <= 0) return 1;
147 return 2;
1a89112 @damellis Adding the Print class.
damellis authored
148 }
149
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
150 long Print::println(const String &s)
9343126 @damellis Adding support for printing Strings to the Print class.
damellis authored
151 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
152 long n = print(s);
153 if (n >= 0) n += println();
154 return n;
9343126 @damellis Adding support for printing Strings to the Print class.
damellis authored
155 }
156
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
157 long Print::println(const char c[])
1a89112 @damellis Adding the Print class.
damellis authored
158 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
159 long n = print(c);
160 if (n >= 0) n += println();
161 return n;
1a89112 @damellis Adding the Print class.
damellis authored
162 }
163
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
164 long Print::println(char c)
1a89112 @damellis Adding the Print class.
damellis authored
165 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
166 long n = print(c);
167 if (n > 0) n += println();
168 return n;
1a89112 @damellis Adding the Print class.
damellis authored
169 }
170
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
171 long Print::println(unsigned char b, int base)
1a89112 @damellis Adding the Print class.
damellis authored
172 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
173 long n = print(b, base);
174 if (n >= 0) n += println();
175 return n;
1a89112 @damellis Adding the Print class.
damellis authored
176 }
177
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
178 long Print::println(int num, int base)
1a89112 @damellis Adding the Print class.
damellis authored
179 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
180 long n = print(num, base);
181 if (n >= 0) n += println();
182 return n;
1a89112 @damellis Adding the Print class.
damellis authored
183 }
184
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
185 long Print::println(unsigned int num, int base)
1a89112 @damellis Adding the Print class.
damellis authored
186 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
187 long n = print(num, base);
188 if (n >= 0) n += println();
189 return n;
1a89112 @damellis Adding the Print class.
damellis authored
190 }
191
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
192 long Print::println(long num, int base)
1a89112 @damellis Adding the Print class.
damellis authored
193 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
194 long n = print(num, base);
195 if (n >= 0) n += println();
196 return n;
1a89112 @damellis Adding the Print class.
damellis authored
197 }
198
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
199 long Print::println(unsigned long num, int base)
1a89112 @damellis Adding the Print class.
damellis authored
200 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
201 long n = print(num, base);
202 if (n >= 0) n += println();
203 return n;
1a89112 @damellis Adding the Print class.
damellis authored
204 }
205
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
206 long Print::println(double num, int digits)
2661608 @damellis Adding support for printing floats and doubles (defaulting to 2 decim…
damellis authored
207 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
208 long n = print(num, digits);
209 if (n >= 0) n += println();
210 return n;
2661608 @damellis Adding support for printing floats and doubles (defaulting to 2 decim…
damellis authored
211 }
212
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
213 long Print::println(const Printable& x)
87a04fc @damellis Added Printable interface class to allow printing of classes such as …
damellis authored
214 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
215 long n = print(x);
216 if (n >= 0) n += println();
217 return n;
87a04fc @damellis Added Printable interface class to allow printing of classes such as …
damellis authored
218 }
219
1a89112 @damellis Adding the Print class.
damellis authored
220 // Private Methods /////////////////////////////////////////////////////////////
221
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
222 long Print::printNumber(unsigned long n, uint8_t base) {
1747292 @damellis Optimizing printing of numbers (writing a single buffer).
damellis authored
223 char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
224 char *str = &buf[sizeof(buf) - 1];
1a89112 @damellis Adding the Print class.
damellis authored
225
1747292 @damellis Optimizing printing of numbers (writing a single buffer).
damellis authored
226 *str = '\0';
227
228 // prevent crash if called with base == 1
229 if (base < 2) base = 10;
1a89112 @damellis Adding the Print class.
damellis authored
230
1747292 @damellis Optimizing printing of numbers (writing a single buffer).
damellis authored
231 do {
232 unsigned long m = n;
1a89112 @damellis Adding the Print class.
damellis authored
233 n /= base;
1747292 @damellis Optimizing printing of numbers (writing a single buffer).
damellis authored
234 char c = m - base * n;
235 *--str = c < 10 ? c + '0' : c + 'A' - 10;
236 } while(n);
1a89112 @damellis Adding the Print class.
damellis authored
237
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
238 return write(str);
1a89112 @damellis Adding the Print class.
damellis authored
239 }
2661608 @damellis Adding support for printing floats and doubles (defaulting to 2 decim…
damellis authored
240
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
241 long Print::printFloat(double number, uint8_t digits)
2661608 @damellis Adding support for printing floats and doubles (defaulting to 2 decim…
damellis authored
242 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
243 long n = 0, t;
244
854c69d @damellis fixing / improving printFloat() from Mikal Hart
damellis authored
245 // Handle negative numbers
246 if (number < 0.0)
247 {
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
248 if ((n = print('-')) <= 0) return n;
854c69d @damellis fixing / improving printFloat() from Mikal Hart
damellis authored
249 number = -number;
250 }
251
252 // Round correctly so that print(1.999, 2) prints as "2.00"
253 double rounding = 0.5;
254 for (uint8_t i=0; i<digits; ++i)
255 rounding /= 10.0;
256
257 number += rounding;
258
259 // Extract the integer part of the number and print it
260 unsigned long int_part = (unsigned long)number;
261 double remainder = number - (double)int_part;
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
262 if ((t = print(int_part)) < 0) return n;
263
264 n += t;
854c69d @damellis fixing / improving printFloat() from Mikal Hart
damellis authored
265
266 // Print the decimal point, but only if there are digits beyond
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
267 if (digits > 0) {
268 t = print(".");
269 if (t <= 0) return n;
270 n += t;
271 }
854c69d @damellis fixing / improving printFloat() from Mikal Hart
damellis authored
272
273 // Extract digits from the remainder one at a time
274 while (digits-- > 0)
275 {
276 remainder *= 10.0;
277 int toPrint = int(remainder);
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
278 t = print(toPrint);
279 if (t <= 0) return n;
280 n += t;
854c69d @damellis fixing / improving printFloat() from Mikal Hart
damellis authored
281 remainder -= toPrint;
2661608 @damellis Adding support for printing floats and doubles (defaulting to 2 decim…
damellis authored
282 }
8059abe @damellis write(), print(), and println() now return number of bytes written.
damellis authored
283
284 return n;
854c69d @damellis fixing / improving printFloat() from Mikal Hart
damellis authored
285 }
Something went wrong with that request. Please try again.