Index

auto-plow / 52ee505

A wheelchair motor-propelled battery-powered ESP32-driven remote control snow plow.

Latest Commit

{#}TimeHashSubjectAuthor#(+)(-)GPG?
630 Nov 2018 18:364a63c8cInclude Arduino core filesJoshua12660N

Blob @ auto-plow / include / arduino / Print.cpp

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