Skip to content

Commit

Permalink
Adding write(str) and write(buf, size) methods to Print class and Eth…
Browse files Browse the repository at this point in the history
…ernet library Client and Server classes. This allows sending a whole string or buffer at once, reducing the number of ethernet packets.
  • Loading branch information
damellis committed Apr 26, 2009
1 parent a0b3e63 commit 9dd3491
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions hardware/cores/arduino/HardwareSerial.h
Expand Up @@ -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;
Expand Down
20 changes: 16 additions & 4 deletions hardware/cores/arduino/Print.cpp
Expand Up @@ -21,14 +21,27 @@

#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include "wiring.h"

#include "Print.h"

// 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);
Expand All @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion hardware/cores/arduino/Print.h
Expand Up @@ -21,6 +21,7 @@
#define Print_h

#include <inttypes.h>
#include <stdio.h> // for size_t

#define DEC 10
#define HEX 16
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions hardware/libraries/Ethernet/Client.cpp
Expand Up @@ -2,6 +2,7 @@ extern "C" {
#include "types.h"
#include "w5100.h"
#include "socket.h"
#include "string.h"
}

#include "Ethernet.h"
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions hardware/libraries/Ethernet/Client.h
Expand Up @@ -15,6 +15,8 @@ class Client : public Print {
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();
Expand Down
15 changes: 13 additions & 2 deletions hardware/libraries/Ethernet/Server.cpp
Expand Up @@ -2,6 +2,7 @@ extern "C" {
#include "types.h"
#include "w5100.h"
#include "socket.h"
#include "string.h"
}

#include "Ethernet.h"
Expand Down Expand Up @@ -66,6 +67,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();

Expand All @@ -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);
}
}
}
}
2 changes: 2 additions & 0 deletions hardware/libraries/Ethernet/Server.h
Expand Up @@ -18,6 +18,8 @@ class Server : public Print {
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

0 comments on commit 9dd3491

Please sign in to comment.