Skip to content

Commit 3540d92

Browse files
committed
Added Printable interface class to allow printing of classes such as IPAddress
1 parent 7f18110 commit 3540d92

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

hardware/arduino/cores/arduino/Print.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ void Print::print(double n, int digits)
101101
printFloat(n, digits);
102102
}
103103

104+
void Print::print(const Printable& x)
105+
{
106+
x.printTo(*this);
107+
}
108+
104109
void Print::println(void)
105110
{
106111
print('\r');
@@ -161,6 +166,12 @@ void Print::println(double n, int digits)
161166
println();
162167
}
163168

169+
void Print::println(const Printable& x)
170+
{
171+
print(x);
172+
println();
173+
}
174+
164175
// Private Methods /////////////////////////////////////////////////////////////
165176

166177
void Print::printNumber(unsigned long n, uint8_t base)

hardware/arduino/cores/arduino/Print.h

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <stdio.h> // for size_t
2525

2626
#include "WString.h"
27+
#include "Printable.h"
2728

2829
#define DEC 10
2930
#define HEX 16
@@ -50,6 +51,7 @@ class Print
5051
void print(long, int = DEC);
5152
void print(unsigned long, int = DEC);
5253
void print(double, int = 2);
54+
void print(const Printable&);
5355

5456
void println(const String &s);
5557
void println(const char[]);
@@ -60,6 +62,7 @@ class Print
6062
void println(long, int = DEC);
6163
void println(unsigned long, int = DEC);
6264
void println(double, int = 2);
65+
void println(const Printable&);
6366
void println(void);
6467
};
6568

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Printable.h - Interface class that allows printing of complex types
3+
Copyright (c) 2011 Adrian McEwen. 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+
20+
#ifndef Printable_h
21+
#define Printable_h
22+
23+
class Print;
24+
25+
class Printable
26+
{
27+
public:
28+
virtual void printTo(Print& p) const =0;
29+
};
30+
31+
#endif
32+

libraries/Ethernet/IPAddress.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,13 @@ bool IPAddress::operator==(const uint8_t* addr)
4242
return memcmp(addr, _address, sizeof(_address)) == 0;
4343
}
4444

45+
void IPAddress::printTo(Print& p) const
46+
{
47+
for (int i =0; i < 3; i++)
48+
{
49+
p.print(_address[i], DEC);
50+
p.print('.');
51+
}
52+
p.print(_address[3], DEC);
53+
}
54+

libraries/Ethernet/IPAddress.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
#ifndef IPAddress_h
2727
#define IPAddress_h
2828

29+
#include <Printable.h>
30+
2931
// A class to make it easier to handle and pass around IP addresses
3032

31-
class IPAddress {
33+
class IPAddress : public Printable {
3234
private:
3335
uint8_t _address[4]; // IPv4 address
3436
// Access the raw byte array containing the address. Because this returns a pointer
@@ -58,6 +60,8 @@ class IPAddress {
5860
IPAddress& operator=(const uint8_t *address);
5961
IPAddress& operator=(uint32_t address);
6062

63+
virtual void printTo(Print& p) const;
64+
6165
friend class EthernetClass;
6266
friend class UDP;
6367
friend class Client;

0 commit comments

Comments
 (0)