Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added Printable interface class to allow printing of classes such as …

…IPAddress
  • Loading branch information...
commit 3540d92eb2a8066eb4e8553f6eb72bd632335411 1 parent 7f18110
Adrian McEwen authored April 01, 2011
11  hardware/arduino/cores/arduino/Print.cpp
@@ -101,6 +101,11 @@ void Print::print(double n, int digits)
101 101
   printFloat(n, digits);
102 102
 }
103 103
 
  104
+void Print::print(const Printable& x)
  105
+{
  106
+  x.printTo(*this);
  107
+}
  108
+
104 109
 void Print::println(void)
105 110
 {
106 111
   print('\r');
@@ -161,6 +166,12 @@ void Print::println(double n, int digits)
161 166
   println();
162 167
 }
163 168
 
  169
+void Print::println(const Printable& x)
  170
+{
  171
+  print(x);
  172
+  println();
  173
+}
  174
+
164 175
 // Private Methods /////////////////////////////////////////////////////////////
165 176
 
166 177
 void Print::printNumber(unsigned long n, uint8_t base)
3  hardware/arduino/cores/arduino/Print.h
@@ -24,6 +24,7 @@
24 24
 #include <stdio.h> // for size_t
25 25
 
26 26
 #include "WString.h"
  27
+#include "Printable.h"
27 28
 
28 29
 #define DEC 10
29 30
 #define HEX 16
@@ -50,6 +51,7 @@ class Print
50 51
     void print(long, int = DEC);
51 52
     void print(unsigned long, int = DEC);
52 53
     void print(double, int = 2);
  54
+    void print(const Printable&);
53 55
 
54 56
     void println(const String &s);
55 57
     void println(const char[]);
@@ -60,6 +62,7 @@ class Print
60 62
     void println(long, int = DEC);
61 63
     void println(unsigned long, int = DEC);
62 64
     void println(double, int = 2);
  65
+    void println(const Printable&);
63 66
     void println(void);
64 67
 };
65 68
 
32  hardware/arduino/cores/arduino/Printable.h
... ...
@@ -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
+
10  libraries/Ethernet/IPAddress.cpp
@@ -42,3 +42,13 @@ bool IPAddress::operator==(const uint8_t* addr)
42 42
     return memcmp(addr, _address, sizeof(_address)) == 0;
43 43
 }
44 44
 
  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
+
6  libraries/Ethernet/IPAddress.h
@@ -26,9 +26,11 @@
26 26
 #ifndef IPAddress_h
27 27
 #define IPAddress_h
28 28
 
  29
+#include <Printable.h>
  30
+
29 31
 // A class to make it easier to handle and pass around IP addresses
30 32
 
31  
-class IPAddress {
  33
+class IPAddress : public Printable {
32 34
 private:
33 35
     uint8_t _address[4];  // IPv4 address
34 36
     // Access the raw byte array containing the address.  Because this returns a pointer
@@ -58,6 +60,8 @@ class IPAddress {
58 60
     IPAddress& operator=(const uint8_t *address);
59 61
     IPAddress& operator=(uint32_t address);
60 62
 
  63
+    virtual void printTo(Print& p) const;
  64
+
61 65
     friend class EthernetClass;
62 66
     friend class UDP;
63 67
     friend class Client;

0 notes on commit 3540d92

Please sign in to comment.
Something went wrong with that request. Please try again.