Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitea00d59

Browse files
committed
Added Print API
1 parent2d738cd commitea00d59

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
#defineARDUINO_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_tPrint::write(constuint8_t *buffer,size_t size)
30+
{
31+
size_t n =0;
32+
while (size--) {
33+
if (write(*buffer++)) n++;
34+
elsebreak;
35+
}
36+
return n;
37+
}
38+
39+
size_tPrint::print(const __FlashStringHelper *ifsh)
40+
{
41+
returnprint(reinterpret_cast<constchar *>(ifsh));
42+
}
43+
44+
size_tPrint::print(const String &s)
45+
{
46+
returnwrite(s.c_str(), s.length());
47+
}
48+
49+
size_tPrint::print(constchar str[])
50+
{
51+
returnwrite(str);
52+
}
53+
54+
size_tPrint::print(char c)
55+
{
56+
returnwrite(c);
57+
}
58+
59+
size_tPrint::print(unsignedchar b,int base)
60+
{
61+
returnprint((unsignedlong) b, base);
62+
}
63+
64+
size_tPrint::print(int n,int base)
65+
{
66+
returnprint((long) n, base);
67+
}
68+
69+
size_tPrint::print(unsignedint n,int base)
70+
{
71+
returnprint((unsignedlong) n, base);
72+
}
73+
74+
size_tPrint::print(long n,int base)
75+
{
76+
if (base ==0) {
77+
returnwrite(n);
78+
}elseif (base ==10) {
79+
if (n <0) {
80+
int t =print('-');
81+
n = -n;
82+
returnprintNumber(n,10) + t;
83+
}
84+
returnprintNumber(n,10);
85+
}else {
86+
returnprintNumber(n, base);
87+
}
88+
}
89+
90+
size_tPrint::print(unsignedlong n,int base)
91+
{
92+
if (base ==0)returnwrite(n);
93+
elsereturnprintNumber(n, base);
94+
}
95+
96+
size_tPrint::print(double n,int digits)
97+
{
98+
returnprintFloat(n, digits);
99+
}
100+
101+
size_tPrint::println(const __FlashStringHelper *ifsh)
102+
{
103+
size_t n =print(ifsh);
104+
n +=println();
105+
return n;
106+
}
107+
108+
size_tPrint::print(const Printable& x)
109+
{
110+
return x.printTo(*this);
111+
}
112+
113+
size_tPrint::println(void)
114+
{
115+
returnwrite("\r\n");
116+
}
117+
118+
size_tPrint::println(const String &s)
119+
{
120+
size_t n =print(s);
121+
n +=println();
122+
return n;
123+
}
124+
125+
size_tPrint::println(constchar c[])
126+
{
127+
size_t n =print(c);
128+
n +=println();
129+
return n;
130+
}
131+
132+
size_tPrint::println(char c)
133+
{
134+
size_t n =print(c);
135+
n +=println();
136+
return n;
137+
}
138+
139+
size_tPrint::println(unsignedchar b,int base)
140+
{
141+
size_t n =print(b, base);
142+
n +=println();
143+
return n;
144+
}
145+
146+
size_tPrint::println(int num,int base)
147+
{
148+
size_t n =print(num, base);
149+
n +=println();
150+
return n;
151+
}
152+
153+
size_tPrint::println(unsignedint num,int base)
154+
{
155+
size_t n =print(num, base);
156+
n +=println();
157+
return n;
158+
}
159+
160+
size_tPrint::println(long num,int base)
161+
{
162+
size_t n =print(num, base);
163+
n +=println();
164+
return n;
165+
}
166+
167+
size_tPrint::println(unsignedlong num,int base)
168+
{
169+
size_t n =print(num, base);
170+
n +=println();
171+
return n;
172+
}
173+
174+
size_tPrint::println(double num,int digits)
175+
{
176+
size_t n =print(num, digits);
177+
n +=println();
178+
return n;
179+
}
180+
181+
size_tPrint::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_tPrint::printNumber(unsignedlong 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+
returnwrite(str);
208+
}
209+
210+
size_tPrint::printFloat(double number,uint8_t digits)
211+
{
212+
size_t n =0;
213+
214+
if (isnan(number))returnprint("nan");
215+
if (isinf(number))returnprint("inf");
216+
if (number >4294967040.0)returnprint ("ovf");// constant determined empirically
217+
if (number <-4294967040.0)returnprint ("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+
unsignedlong int_part = (unsignedlong)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+
unsignedint toPrint = (unsignedint)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+
#defineDEC10
28+
#defineHEX16
29+
#defineOCT8
30+
#defineBIN2
31+
32+
classPrint
33+
{
34+
private:
35+
int write_error;
36+
size_tprintNumber(unsignedlong,uint8_t);
37+
size_tprintFloat(double,uint8_t);
38+
protected:
39+
voidsetWriteError(int err =1) { write_error = err; }
40+
public:
41+
Print() : write_error(0) {}
42+
43+
intgetWriteError() {return write_error; }
44+
voidclearWriteError() {setWriteError(0); }
45+
46+
virtualsize_twrite(uint8_t) = 0;
47+
size_twrite(constchar *str) {
48+
if (str ==NULL)return0;
49+
returnwrite((constuint8_t *)str,strlen(str));
50+
}
51+
virtualsize_twrite(constuint8_t *buffer,size_t size);
52+
size_twrite(constchar *buffer,size_t size) {
53+
returnwrite((constuint8_t *)buffer, size);
54+
}
55+
56+
size_tprint(const __FlashStringHelper *);
57+
size_tprint(const String &);
58+
size_tprint(constchar[]);
59+
size_tprint(char);
60+
size_tprint(unsignedchar,int = DEC);
61+
size_tprint(int,int = DEC);
62+
size_tprint(unsignedint,int = DEC);
63+
size_tprint(long,int = DEC);
64+
size_tprint(unsignedlong,int = DEC);
65+
size_tprint(double,int =2);
66+
size_tprint(const Printable&);
67+
68+
size_tprintln(const __FlashStringHelper *);
69+
size_tprintln(const String &s);
70+
size_tprintln(constchar[]);
71+
size_tprintln(char);
72+
size_tprintln(unsignedchar,int = DEC);
73+
size_tprintln(int,int = DEC);
74+
size_tprintln(unsignedint,int = DEC);
75+
size_tprintln(long,int = DEC);
76+
size_tprintln(unsignedlong,int = DEC);
77+
size_tprintln(double,int =2);
78+
size_tprintln(const Printable&);
79+
size_tprintln(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+
classPrint;
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+
classPrintable
32+
{
33+
public:
34+
virtualsize_tprintTo(Print& p)const = 0;
35+
};
36+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp