1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-03-14 11:29:26 +01:00

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.

This commit is contained in:
David A. Mellis 2009-04-26 13:10:34 +00:00
parent a0b3e6334d
commit 9dd3491ab3
7 changed files with 47 additions and 7 deletions

View File

@ -50,6 +50,7 @@ class HardwareSerial : public Print
int read(void); int read(void);
void flush(void); void flush(void);
virtual void write(uint8_t); virtual void write(uint8_t);
using Print::write; // pull in write(str) and write(buf, size) from Print
}; };
extern HardwareSerial Serial; extern HardwareSerial Serial;

View File

@ -21,7 +21,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <inttypes.h>
#include <math.h> #include <math.h>
#include "wiring.h" #include "wiring.h"
@ -29,6 +28,20 @@
// Public Methods ////////////////////////////////////////////////////////////// // 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) void Print::print(uint8_t b)
{ {
this->write(b); this->write(b);
@ -39,10 +52,9 @@ void Print::print(char c)
print((byte) c); print((byte) c);
} }
void Print::print(const char c[]) void Print::print(const char str[])
{ {
while (*c) write(str);
print(*c++);
} }
void Print::print(int n) void Print::print(int n)

View File

@ -21,6 +21,7 @@
#define Print_h #define Print_h
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> // for size_t
#define DEC 10 #define DEC 10
#define HEX 16 #define HEX 16
@ -34,7 +35,9 @@ class Print
void printNumber(unsigned long, uint8_t); void printNumber(unsigned long, uint8_t);
void printFloat(double, uint8_t); void printFloat(double, uint8_t);
public: 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(char);
void print(const char[]); void print(const char[]);
void print(uint8_t); void print(uint8_t);

View File

@ -2,6 +2,7 @@ extern "C" {
#include "types.h" #include "types.h"
#include "w5100.h" #include "w5100.h"
#include "socket.h" #include "socket.h"
#include "string.h"
} }
#include "Ethernet.h" #include "Ethernet.h"
@ -50,6 +51,14 @@ void Client::write(uint8_t b) {
send(_sock, &b, 1); 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() { int Client::available() {
return getSn_RX_RSR(_sock); return getSn_RX_RSR(_sock);
} }

View File

@ -15,6 +15,8 @@ public:
uint8_t status(); uint8_t status();
uint8_t connect(); uint8_t connect();
virtual void write(uint8_t); virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
int available(); int available();
int read(); int read();
void flush(); void flush();

View File

@ -2,6 +2,7 @@ extern "C" {
#include "types.h" #include "types.h"
#include "w5100.h" #include "w5100.h"
#include "socket.h" #include "socket.h"
#include "string.h"
} }
#include "Ethernet.h" #include "Ethernet.h"
@ -66,6 +67,16 @@ Client Server::available()
} }
void Server::write(uint8_t b) 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(); accept();
@ -74,7 +85,7 @@ void Server::write(uint8_t b)
if (EthernetClass::_server_port[sock] == _port && if (EthernetClass::_server_port[sock] == _port &&
client.status() == SOCK_ESTABLISHED) { client.status() == SOCK_ESTABLISHED) {
client.write(b); client.write(buffer, size);
} }
} }
} }

View File

@ -18,6 +18,8 @@ public:
Client available(); Client available();
void begin(); void begin();
virtual void write(uint8_t); virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
}; };
#endif #endif