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:
parent
a42326aba2
commit
0681fc1f17
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -2,6 +2,7 @@ extern "C" {
|
|||
#include "types.h"
|
||||
#include "w5100.h"
|
||||
#include "socket.h"
|
||||
#include "string.h"
|
||||
}
|
||||
|
||||
#include "Ethernet.h"
|
||||
|
@ -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();
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue