diff options
author | David A. Mellis <d.mellis@arduino.cc> | 2009-04-26 13:10:34 +0000 |
---|---|---|
committer | David A. Mellis <d.mellis@arduino.cc> | 2009-04-26 13:10:34 +0000 |
commit | 0681fc1f177f7c94b4e98bb0931a5efda50f32b0 (patch) | |
tree | 2174000a919ea016385dac3cdb482ce2d21fee9f | |
parent | a42326aba2fd9696a4b2e1239a5a222014056ff5 (diff) |
Adding write(str) and write(buf, size) methods to Print class and Ethernet library Client and Server classes. This allows sending a whole string or buffer at once, reducing the number of ethernet packets.
-rwxr-xr-x | cores/arduino/HardwareSerial.h | 1 | ||||
-rwxr-xr-x | cores/arduino/Print.cpp | 20 | ||||
-rwxr-xr-x | cores/arduino/Print.h | 5 | ||||
-rw-r--r-- | libraries/Ethernet/Client.cpp | 9 | ||||
-rw-r--r-- | libraries/Ethernet/Client.h | 2 | ||||
-rw-r--r-- | libraries/Ethernet/Server.cpp | 15 | ||||
-rw-r--r-- | libraries/Ethernet/Server.h | 2 |
7 files changed, 47 insertions, 7 deletions
diff --git a/cores/arduino/HardwareSerial.h b/cores/arduino/HardwareSerial.h index 55abf07..8610f5a 100755 --- a/cores/arduino/HardwareSerial.h +++ b/cores/arduino/HardwareSerial.h @@ -50,6 +50,7 @@ class HardwareSerial : public Print int read(void); void flush(void); virtual void write(uint8_t); + using Print::write; // pull in write(str) and write(buf, size) from Print }; extern HardwareSerial Serial; diff --git a/cores/arduino/Print.cpp b/cores/arduino/Print.cpp index d4833da..74d0e5b 100755 --- a/cores/arduino/Print.cpp +++ b/cores/arduino/Print.cpp @@ -21,7 +21,6 @@ #include <stdio.h> #include <string.h> -#include <inttypes.h> #include <math.h> #include "wiring.h" @@ -29,6 +28,20 @@ // Public Methods ////////////////////////////////////////////////////////////// +/* default implementation: may be overridden */ +void Print::write(const char *str) +{ + while (*str) + write(*str++); +} + +/* default implementation: may be overridden */ +void Print::write(const uint8_t *buffer, size_t size) +{ + while (size--) + write(*buffer++); +} + void Print::print(uint8_t b) { this->write(b); @@ -39,10 +52,9 @@ void Print::print(char c) print((byte) c); } -void Print::print(const char c[]) +void Print::print(const char str[]) { - while (*c) - print(*c++); + write(str); } void Print::print(int n) diff --git a/cores/arduino/Print.h b/cores/arduino/Print.h index c95a0dc..a69e85d 100755 --- a/cores/arduino/Print.h +++ b/cores/arduino/Print.h @@ -21,6 +21,7 @@ #define Print_h #include <inttypes.h> +#include <stdio.h> // for size_t #define DEC 10 #define HEX 16 @@ -34,7 +35,9 @@ class Print void printNumber(unsigned long, uint8_t); void printFloat(double, uint8_t); public: - virtual void write(uint8_t); + virtual void write(uint8_t) = 0; + virtual void write(const char *str); + virtual void write(const uint8_t *buffer, size_t size); void print(char); void print(const char[]); void print(uint8_t); diff --git a/libraries/Ethernet/Client.cpp b/libraries/Ethernet/Client.cpp index c62e26c..51096db 100644 --- a/libraries/Ethernet/Client.cpp +++ b/libraries/Ethernet/Client.cpp @@ -2,6 +2,7 @@ extern "C" { #include "types.h" #include "w5100.h" #include "socket.h" + #include "string.h" } #include "Ethernet.h" @@ -50,6 +51,14 @@ void Client::write(uint8_t b) { send(_sock, &b, 1); } +void Client::write(const char *str) { + send(_sock, (const uint8_t *)str, strlen(str)); +} + +void Client::write(const uint8_t *buf, size_t size) { + send(_sock, buf, size); +} + int Client::available() { return getSn_RX_RSR(_sock); } diff --git a/libraries/Ethernet/Client.h b/libraries/Ethernet/Client.h index f269e9b..7c0ccdf 100644 --- a/libraries/Ethernet/Client.h +++ b/libraries/Ethernet/Client.h @@ -15,6 +15,8 @@ public: uint8_t status(); uint8_t connect(); virtual void write(uint8_t); + virtual void write(const char *str); + virtual void write(const uint8_t *buf, size_t size); int available(); int read(); void flush(); diff --git a/libraries/Ethernet/Server.cpp b/libraries/Ethernet/Server.cpp index 351f838..d17a5d3 100644 --- a/libraries/Ethernet/Server.cpp +++ b/libraries/Ethernet/Server.cpp @@ -2,6 +2,7 @@ extern "C" { #include "types.h" #include "w5100.h" #include "socket.h" + #include "string.h" } #include "Ethernet.h" @@ -67,6 +68,16 @@ Client Server::available() void Server::write(uint8_t b) { + write(&b, 1); +} + +void Server::write(const char *str) +{ + write((const uint8_t *)str, strlen(str)); +} + +void Server::write(const uint8_t *buffer, size_t size) +{ accept(); for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { @@ -74,7 +85,7 @@ void Server::write(uint8_t b) if (EthernetClass::_server_port[sock] == _port && client.status() == SOCK_ESTABLISHED) { - client.write(b); + client.write(buffer, size); } } -}
\ No newline at end of file +} diff --git a/libraries/Ethernet/Server.h b/libraries/Ethernet/Server.h index d35e691..73d6a5e 100644 --- a/libraries/Ethernet/Server.h +++ b/libraries/Ethernet/Server.h @@ -18,6 +18,8 @@ public: Client available(); void begin(); virtual void write(uint8_t); + virtual void write(const char *str); + virtual void write(const uint8_t *buf, size_t size); }; #endif |