Thu, 07 Jul 2016 12:23:34 +0200
added missing sanguino files
2 | 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 | ||
22 | #include <stdlib.h> | |
23 | #include <stdio.h> | |
24 | #include <string.h> | |
25 | #include <math.h> | |
26 | #include "Arduino.h" | |
27 | ||
28 | #include "Print.h" | |
29 | ||
30 | // Public Methods ////////////////////////////////////////////////////////////// | |
31 | ||
32 | /* default implementation: may be overridden */ | |
33 | size_t Print::write(const uint8_t *buffer, size_t size) | |
34 | { | |
35 | size_t n = 0; | |
36 | while (size--) { | |
37 | n += write(*buffer++); | |
38 | } | |
39 | return n; | |
40 | } | |
41 | ||
42 | size_t Print::print(const __FlashStringHelper *ifsh) | |
43 | { | |
44 | const char PROGMEM *p = (const char PROGMEM *)ifsh; | |
45 | size_t n = 0; | |
46 | while (1) { | |
47 | unsigned char c = pgm_read_byte(p++); | |
48 | if (c == 0) break; | |
49 | n += write(c); | |
50 | } | |
51 | return n; | |
52 | } | |
53 | ||
54 | size_t Print::print(const String &s) | |
55 | { | |
56 | size_t n = 0; | |
57 | for (uint16_t i = 0; i < s.length(); i++) { | |
58 | n += write(s[i]); | |
59 | } | |
60 | return n; | |
61 | } | |
62 | ||
63 | size_t Print::print(const char str[]) | |
64 | { | |
65 | return write(str); | |
66 | } | |
67 | ||
68 | size_t Print::print(char c) | |
69 | { | |
70 | return write(c); | |
71 | } | |
72 | ||
73 | size_t Print::print(unsigned char b, int base) | |
74 | { | |
75 | return print((unsigned long) b, base); | |
76 | } | |
77 | ||
78 | size_t Print::print(int n, int base) | |
79 | { | |
80 | return print((long) n, base); | |
81 | } | |
82 | ||
83 | size_t Print::print(unsigned int n, int base) | |
84 | { | |
85 | return print((unsigned long) n, base); | |
86 | } | |
87 | ||
88 | size_t Print::print(long n, int base) | |
89 | { | |
90 | if (base == 0) { | |
91 | return write(n); | |
92 | } else if (base == 10) { | |
93 | if (n < 0) { | |
94 | int t = print('-'); | |
95 | n = -n; | |
96 | return printNumber(n, 10) + t; | |
97 | } | |
98 | return printNumber(n, 10); | |
99 | } else { | |
100 | return printNumber(n, base); | |
101 | } | |
102 | } | |
103 | ||
104 | size_t Print::print(unsigned long n, int base) | |
105 | { | |
106 | if (base == 0) return write(n); | |
107 | else return printNumber(n, base); | |
108 | } | |
109 | ||
110 | size_t Print::print(double n, int digits) | |
111 | { | |
112 | return printFloat(n, digits); | |
113 | } | |
114 | ||
115 | size_t Print::println(const __FlashStringHelper *ifsh) | |
116 | { | |
117 | size_t n = print(ifsh); | |
118 | n += println(); | |
119 | return n; | |
120 | } | |
121 | ||
122 | size_t Print::print(const Printable& x) | |
123 | { | |
124 | return x.printTo(*this); | |
125 | } | |
126 | ||
127 | size_t Print::println(void) | |
128 | { | |
129 | size_t n = print('\r'); | |
130 | n += print('\n'); | |
131 | return n; | |
132 | } | |
133 | ||
134 | size_t Print::println(const String &s) | |
135 | { | |
136 | size_t n = print(s); | |
137 | n += println(); | |
138 | return n; | |
139 | } | |
140 | ||
141 | size_t Print::println(const char c[]) | |
142 | { | |
143 | size_t n = print(c); | |
144 | n += println(); | |
145 | return n; | |
146 | } | |
147 | ||
148 | size_t Print::println(char c) | |
149 | { | |
150 | size_t n = print(c); | |
151 | n += println(); | |
152 | return n; | |
153 | } | |
154 | ||
155 | size_t Print::println(unsigned char b, int base) | |
156 | { | |
157 | size_t n = print(b, base); | |
158 | n += println(); | |
159 | return n; | |
160 | } | |
161 | ||
162 | size_t Print::println(int num, int base) | |
163 | { | |
164 | size_t n = print(num, base); | |
165 | n += println(); | |
166 | return n; | |
167 | } | |
168 | ||
169 | size_t Print::println(unsigned int num, int base) | |
170 | { | |
171 | size_t n = print(num, base); | |
172 | n += println(); | |
173 | return n; | |
174 | } | |
175 | ||
176 | size_t Print::println(long num, int base) | |
177 | { | |
178 | size_t n = print(num, base); | |
179 | n += println(); | |
180 | return n; | |
181 | } | |
182 | ||
183 | size_t Print::println(unsigned long num, int base) | |
184 | { | |
185 | size_t n = print(num, base); | |
186 | n += println(); | |
187 | return n; | |
188 | } | |
189 | ||
190 | size_t Print::println(double num, int digits) | |
191 | { | |
192 | size_t n = print(num, digits); | |
193 | n += println(); | |
194 | return n; | |
195 | } | |
196 | ||
197 | size_t Print::println(const Printable& x) | |
198 | { | |
199 | size_t n = print(x); | |
200 | n += println(); | |
201 | return n; | |
202 | } | |
203 | ||
204 | // Private Methods ///////////////////////////////////////////////////////////// | |
205 | ||
206 | size_t Print::printNumber(unsigned long n, uint8_t base) { | |
207 | char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte. | |
208 | char *str = &buf[sizeof(buf) - 1]; | |
209 | ||
210 | *str = '\0'; | |
211 | ||
212 | // prevent crash if called with base == 1 | |
213 | if (base < 2) base = 10; | |
214 | ||
215 | do { | |
216 | unsigned long m = n; | |
217 | n /= base; | |
218 | char c = m - base * n; | |
219 | *--str = c < 10 ? c + '0' : c + 'A' - 10; | |
220 | } while(n); | |
221 | ||
222 | return write(str); | |
223 | } | |
224 | ||
225 | size_t Print::printFloat(double number, uint8_t digits) | |
226 | { | |
227 | size_t n = 0; | |
228 | ||
229 | // Handle negative numbers | |
230 | if (number < 0.0) | |
231 | { | |
232 | n += print('-'); | |
233 | number = -number; | |
234 | } | |
235 | ||
236 | // Round correctly so that print(1.999, 2) prints as "2.00" | |
237 | double rounding = 0.5; | |
238 | for (uint8_t i=0; i<digits; ++i) | |
239 | rounding /= 10.0; | |
240 | ||
241 | number += rounding; | |
242 | ||
243 | // Extract the integer part of the number and print it | |
244 | unsigned long int_part = (unsigned long)number; | |
245 | double remainder = number - (double)int_part; | |
246 | n += print(int_part); | |
247 | ||
248 | // Print the decimal point, but only if there are digits beyond | |
249 | if (digits > 0) { | |
250 | n += print("."); | |
251 | } | |
252 | ||
253 | // Extract digits from the remainder one at a time | |
254 | while (digits-- > 0) | |
255 | { | |
256 | remainder *= 10.0; | |
257 | int toPrint = int(remainder); | |
258 | n += print(toPrint); | |
259 | remainder -= toPrint; | |
260 | } | |
261 | ||
262 | return n; | |
263 | } |