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