Skip to content

Commit 1f6205e

Browse files
committed
Added Printable interface class to allow printing of classes such as IPAddress
1 parent e06b349 commit 1f6205e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

IPAddress.cpp

Lines changed: 10 additions & 0 deletions
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+

IPAddress.h

Lines changed: 5 additions & 1 deletion
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)