Skip to content

Commit ea00d59

Browse files
committed
Added Print API
1 parent 2d738cd commit ea00d59

File tree

4 files changed

+374
-0
lines changed

4 files changed

+374
-0
lines changed

api/ArduinoAPI.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
#define ARDUINO_API_VERSION 10000
2525

2626
#include "Binary.h"
27+
28+
#ifdef __cplusplus
29+
#include "Print.h"
2730
#include "String.h"
31+
#endif
2832

2933
#endif

api/Print.cpp

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
/*
2+
Copyright (c) 2014 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#include <stdlib.h>
20+
#include <stdio.h>
21+
#include <string.h>
22+
#include <math.h>
23+
24+
#include "Print.h"
25+
26+
// Public Methods //////////////////////////////////////////////////////////////
27+
28+
/* default implementation: may be overridden */
29+
size_t Print::write(const uint8_t *buffer, size_t size)
30+
{
31+
size_t n = 0;
32+
while (size--) {
33+
if (write(*buffer++)) n++;
34+
else break;
35+
}
36+
return n;
37+
}
38+
39+
size_t Print::print(const __FlashStringHelper *ifsh)
40+
{
41+
return print(reinterpret_cast<const char *>(ifsh));
42+
}
43+
44+
size_t Print::print(const String &s)
45+
{
46+
return write(s.c_str(), s.length());
47+
}
48+
49+
size_t Print::print(const char str[])
50+
{
51+
return write(str);
52+
}
53+
54+
size_t Print::print(char c)
55+
{
56+
return write(c);
57+
}
58+
59+
size_t Print::print(unsigned char b, int base)
60+
{
61+
return print((unsigned long) b, base);
62+
}
63+
64+
size_t Print::print(int n, int base)
65+
{
66+
return print((long) n, base);
67+
}
68+
69+
size_t Print::print(unsigned int n, int base)
70+
{
71+
return print((unsigned long) n, base);
72+
}
73+
74+
size_t Print::print(long n, int base)
75+
{
76+
if (base == 0) {
77+
return write(n);
78+
} else if (base == 10) {
79+
if (n < 0) {
80+
int t = print('-');
81+
n = -n;
82+
return printNumber(n, 10) + t;
83+
}
84+
return printNumber(n, 10);
85+
} else {
86+
return printNumber(n, base);
87+
}
88+
}
89+
90+
size_t Print::print(unsigned long n, int base)
91+
{
92+
if (base == 0) return write(n);
93+
else return printNumber(n, base);
94+
}
95+
96+
size_t Print::print(double n, int digits)
97+
{
98+
return printFloat(n, digits);
99+
}
100+
101+
size_t Print::println(const __FlashStringHelper *ifsh)
102+
{
103+
size_t n = print(ifsh);
104+
n += println();
105+
return n;
106+
}
107+
108+
size_t Print::print(const Printable& x)
109+
{
110+
return x.printTo(*this);
111+
}
112+
113+
size_t Print::println(void)
114+
{
115+
return write("\r\n");
116+
}
117+
118+
size_t Print::println(const String &s)
119+
{
120+
size_t n = print(s);
121+
n += println();
122+
return n;
123+
}
124+
125+
size_t Print::println(const char c[])
126+
{
127+
size_t n = print(c);
128+
n += println();
129+
return n;
130+
}
131+
132+
size_t Print::println(char c)
133+
{
134+
size_t n = print(c);
135+
n += println();
136+
return n;
137+
}
138+
139+
size_t Print::println(unsigned char b, int base)
140+
{
141+
size_t n = print(b, base);
142+
n += println();
143+
return n;
144+
}
145+
146+
size_t Print::println(int num, int base)
147+
{
148+
size_t n = print(num, base);
149+
n += println();
150+
return n;
151+
}
152+
153+
size_t Print::println(unsigned int num, int base)
154+
{
155+
size_t n = print(num, base);
156+
n += println();
157+
return n;
158+
}
159+
160+
size_t Print::println(long num, int base)
161+
{
162+
size_t n = print(num, base);
163+
n += println();
164+
return n;
165+
}
166+
167+
size_t Print::println(unsigned long num, int base)
168+
{
169+
size_t n = print(num, base);
170+
n += println();
171+
return n;
172+
}
173+
174+
size_t Print::println(double num, int digits)
175+
{
176+
size_t n = print(num, digits);
177+
n += println();
178+
return n;
179+
}
180+
181+
size_t Print::println(const Printable& x)
182+
{
183+
size_t n = print(x);
184+
n += println();
185+
return n;
186+
}
187+
188+
// Private Methods /////////////////////////////////////////////////////////////
189+
190+
size_t Print::printNumber(unsigned long n, uint8_t base)
191+
{
192+
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
193+
char *str = &buf[sizeof(buf) - 1];
194+
195+
*str = '\0';
196+
197+
// prevent crash if called with base == 1
198+
if (base < 2) base = 10;
199+
200+
do {
201+
char c = n % base;
202+
n /= base;
203+
204+
*--str = c < 10 ? c + '0' : c + 'A' - 10;
205+
} while(n);
206+
207+
return write(str);
208+
}
209+
210+
size_t Print::printFloat(double number, uint8_t digits)
211+
{
212+
size_t n = 0;
213+
214+
if (isnan(number)) return print("nan");
215+
if (isinf(number)) return print("inf");
216+
if (number > 4294967040.0) return print ("ovf"); // constant determined empirically
217+
if (number <-4294967040.0) return print ("ovf"); // constant determined empirically
218+
219+
// Handle negative numbers
220+
if (number < 0.0)
221+
{
222+
n += print('-');
223+
number = -number;
224+
}
225+
226+
// Round correctly so that print(1.999, 2) prints as "2.00"
227+
double rounding = 0.5;
228+
for (uint8_t i=0; i<digits; ++i)
229+
rounding /= 10.0;
230+
231+
number += rounding;
232+
233+
// Extract the integer part of the number and print it
234+
unsigned long int_part = (unsigned long)number;
235+
double remainder = number - (double)int_part;
236+
n += print(int_part);
237+
238+
// Print the decimal point, but only if there are digits beyond
239+
if (digits > 0) {
240+
n += print(".");
241+
}
242+
243+
// Extract digits from the remainder one at a time
244+
while (digits-- > 0)
245+
{
246+
remainder *= 10.0;
247+
unsigned int toPrint = (unsigned int)remainder;
248+
n += print(toPrint);
249+
remainder -= toPrint;
250+
}
251+
252+
return n;
253+
}

api/Print.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Copyright (c) 2016 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#pragma once
20+
21+
#include <inttypes.h>
22+
#include <stdio.h> // for size_t
23+
24+
#include "String.h"
25+
#include "Printable.h"
26+
27+
#define DEC 10
28+
#define HEX 16
29+
#define OCT 8
30+
#define BIN 2
31+
32+
class Print
33+
{
34+
private:
35+
int write_error;
36+
size_t printNumber(unsigned long, uint8_t);
37+
size_t printFloat(double, uint8_t);
38+
protected:
39+
void setWriteError(int err = 1) { write_error = err; }
40+
public:
41+
Print() : write_error(0) {}
42+
43+
int getWriteError() { return write_error; }
44+
void clearWriteError() { setWriteError(0); }
45+
46+
virtual size_t write(uint8_t) = 0;
47+
size_t write(const char *str) {
48+
if (str == NULL) return 0;
49+
return write((const uint8_t *)str, strlen(str));
50+
}
51+
virtual size_t write(const uint8_t *buffer, size_t size);
52+
size_t write(const char *buffer, size_t size) {
53+
return write((const uint8_t *)buffer, size);
54+
}
55+
56+
size_t print(const __FlashStringHelper *);
57+
size_t print(const String &);
58+
size_t print(const char[]);
59+
size_t print(char);
60+
size_t print(unsigned char, int = DEC);
61+
size_t print(int, int = DEC);
62+
size_t print(unsigned int, int = DEC);
63+
size_t print(long, int = DEC);
64+
size_t print(unsigned long, int = DEC);
65+
size_t print(double, int = 2);
66+
size_t print(const Printable&);
67+
68+
size_t println(const __FlashStringHelper *);
69+
size_t println(const String &s);
70+
size_t println(const char[]);
71+
size_t println(char);
72+
size_t println(unsigned char, int = DEC);
73+
size_t println(int, int = DEC);
74+
size_t println(unsigned int, int = DEC);
75+
size_t println(long, int = DEC);
76+
size_t println(unsigned long, int = DEC);
77+
size_t println(double, int = 2);
78+
size_t println(const Printable&);
79+
size_t println(void);
80+
};
81+

api/Printable.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright (c) 2016 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#pragma once
20+
21+
#include <stdlib.h>
22+
23+
class Print;
24+
25+
/** The Printable class provides a way for new classes to allow themselves to be printed.
26+
By deriving from Printable and implementing the printTo method, it will then be possible
27+
for users to print out instances of this class by passing them into the usual
28+
Print::print and Print::println methods.
29+
*/
30+
31+
class Printable
32+
{
33+
public:
34+
virtual size_t printTo(Print& p) const = 0;
35+
};
36+

0 commit comments

Comments
 (0)