1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

write(), print(), and println() now return number of bytes written.

The type is long, and negative values indicate errors.  Needs more testing.
http://code.google.com/p/arduino/issues/detail?id=551
This commit is contained in:
David A. Mellis 2011-08-23 19:12:03 -04:00
parent b788ad593f
commit 8059abe581
24 changed files with 290 additions and 174 deletions

View File

@ -352,12 +352,13 @@ void HardwareSerial::flush()
;
}
void HardwareSerial::write(uint8_t c)
long HardwareSerial::write(uint8_t c)
{
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;
// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
// ???: return 0 here instead?
while (i == _tx_buffer->tail)
;
@ -365,6 +366,8 @@ void HardwareSerial::write(uint8_t c)
_tx_buffer->head = i;
sbi(*_ucsrb, _udrie);
return 1;
}
// Preinstantiate Objects //////////////////////////////////////////////////////

View File

@ -55,7 +55,7 @@ class HardwareSerial : public Stream
virtual int peek(void);
virtual int read(void);
virtual void flush(void);
virtual void write(uint8_t);
virtual long write(uint8_t);
using Print::write; // pull in write(str) and write(buf, size) from Print
};

View File

@ -30,167 +30,196 @@
// Public Methods //////////////////////////////////////////////////////////////
/* default implementation: may be overridden */
void Print::write(const char *str)
long Print::write(const char *str)
{
while (*str)
write(*str++);
long n = 0;
while (*str) {
if (write(*str++) <= 0) break;
n++;
}
return n;
}
/* default implementation: may be overridden */
void Print::write(const uint8_t *buffer, size_t size)
long Print::write(const uint8_t *buffer, size_t size)
{
while (size--)
write(*buffer++);
long n = 0;
while (size--) {
if (write(*buffer++) <= 0) break;
n++;
}
return n;
}
void Print::print(const __FlashStringHelper *ifsh)
long Print::print(const __FlashStringHelper *ifsh)
{
const prog_char *p = (const prog_char *)ifsh;
long n = 0;
while (1) {
unsigned char c = pgm_read_byte(p++);
if (c == 0) return;
write(c);
if (c == 0) break;
if (write(c) <= 0) break;
n++;
}
return n;
}
void Print::print(const String &s)
long Print::print(const String &s)
{
long n = 0;
for (int i = 0; i < s.length(); i++) {
write(s[i]);
if (write(s[i]) < 0) break;
n++;
}
return n;
}
void Print::print(const char str[])
long Print::print(const char str[])
{
write(str);
return write(str);
}
void Print::print(char c)
long Print::print(char c)
{
write(c);
return write(c);
}
void Print::print(unsigned char b, int base)
long Print::print(unsigned char b, int base)
{
print((unsigned long) b, base);
return print((unsigned long) b, base);
}
void Print::print(int n, int base)
long Print::print(int n, int base)
{
print((long) n, base);
return print((long) n, base);
}
void Print::print(unsigned int n, int base)
long Print::print(unsigned int n, int base)
{
print((unsigned long) n, base);
return print((unsigned long) n, base);
}
void Print::print(long n, int base)
long Print::print(long n, int base)
{
if (base == 0) {
write(n);
return write(n);
} else if (base == 10) {
if (n < 0) {
print('-');
long t = print('-');
if (t <= 0) return t;
n = -n;
return printNumber(n, 10) + 1;
}
printNumber(n, 10);
return printNumber(n, 10);
} else {
printNumber(n, base);
return printNumber(n, base);
}
}
void Print::print(unsigned long n, int base)
long Print::print(unsigned long n, int base)
{
if (base == 0) write(n);
else printNumber(n, base);
if (base == 0) return write(n);
else return printNumber(n, base);
}
void Print::print(double n, int digits)
long Print::print(double n, int digits)
{
printFloat(n, digits);
return printFloat(n, digits);
}
void Print::println(const __FlashStringHelper *ifsh)
long Print::println(const __FlashStringHelper *ifsh)
{
print(ifsh);
println();
long n = print(ifsh);
if (n >= 0) n += println();
return n;
}
void Print::print(const Printable& x)
long Print::print(const Printable& x)
{
x.printTo(*this);
return x.printTo(*this);
}
void Print::println(void)
long Print::println(void)
{
print('\r');
print('\n');
long t = print('\r');
if (t <= 0) return t;
if (print('\n') <= 0) return 1;
return 2;
}
void Print::println(const String &s)
long Print::println(const String &s)
{
print(s);
println();
long n = print(s);
if (n >= 0) n += println();
return n;
}
void Print::println(const char c[])
long Print::println(const char c[])
{
print(c);
println();
long n = print(c);
if (n >= 0) n += println();
return n;
}
void Print::println(char c)
long Print::println(char c)
{
print(c);
println();
long n = print(c);
if (n > 0) n += println();
return n;
}
void Print::println(unsigned char b, int base)
long Print::println(unsigned char b, int base)
{
print(b, base);
println();
long n = print(b, base);
if (n >= 0) n += println();
return n;
}
void Print::println(int n, int base)
long Print::println(int num, int base)
{
print(n, base);
println();
long n = print(num, base);
if (n >= 0) n += println();
return n;
}
void Print::println(unsigned int n, int base)
long Print::println(unsigned int num, int base)
{
print(n, base);
println();
long n = print(num, base);
if (n >= 0) n += println();
return n;
}
void Print::println(long n, int base)
long Print::println(long num, int base)
{
print(n, base);
println();
long n = print(num, base);
if (n >= 0) n += println();
return n;
}
void Print::println(unsigned long n, int base)
long Print::println(unsigned long num, int base)
{
print(n, base);
println();
long n = print(num, base);
if (n >= 0) n += println();
return n;
}
void Print::println(double n, int digits)
long Print::println(double num, int digits)
{
print(n, digits);
println();
long n = print(num, digits);
if (n >= 0) n += println();
return n;
}
void Print::println(const Printable& x)
long Print::println(const Printable& x)
{
print(x);
println();
long n = print(x);
if (n >= 0) n += println();
return n;
}
// Private Methods /////////////////////////////////////////////////////////////
void Print::printNumber(unsigned long n, uint8_t base) {
long Print::printNumber(unsigned long n, uint8_t base) {
char buf[8 * sizeof(long) + 1]; // Assumes 8-bit chars plus zero byte.
char *str = &buf[sizeof(buf) - 1];
@ -206,15 +235,17 @@ void Print::printNumber(unsigned long n, uint8_t base) {
*--str = c < 10 ? c + '0' : c + 'A' - 10;
} while(n);
write(str);
return write(str);
}
void Print::printFloat(double number, uint8_t digits)
long Print::printFloat(double number, uint8_t digits)
{
long n = 0, t;
// Handle negative numbers
if (number < 0.0)
{
print('-');
if ((n = print('-')) <= 0) return n;
number = -number;
}
@ -228,18 +259,27 @@ void Print::printFloat(double number, uint8_t digits)
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
print(int_part);
if ((t = print(int_part)) < 0) return n;
n += t;
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
print(".");
if (digits > 0) {
t = print(".");
if (t <= 0) return n;
n += t;
}
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
print(toPrint);
t = print(toPrint);
if (t <= 0) return n;
n += t;
remainder -= toPrint;
}
return n;
}

View File

@ -34,37 +34,37 @@
class Print
{
private:
void printNumber(unsigned long, uint8_t);
void printFloat(double, uint8_t);
long printNumber(unsigned long, uint8_t);
long printFloat(double, uint8_t);
public:
virtual void write(uint8_t) = 0;
virtual void write(const char *str);
virtual void write(const uint8_t *buffer, size_t size);
virtual long write(uint8_t) = 0;
virtual long write(const char *str);
virtual long write(const uint8_t *buffer, size_t size);
void print(const __FlashStringHelper *);
void print(const String &);
void print(const char[]);
void print(char);
void print(unsigned char, int = DEC);
void print(int, int = DEC);
void print(unsigned int, int = DEC);
void print(long, int = DEC);
void print(unsigned long, int = DEC);
void print(double, int = 2);
void print(const Printable&);
long print(const __FlashStringHelper *);
long print(const String &);
long print(const char[]);
long print(char);
long print(unsigned char, int = DEC);
long print(int, int = DEC);
long print(unsigned int, int = DEC);
long print(long, int = DEC);
long print(unsigned long, int = DEC);
long print(double, int = 2);
long print(const Printable&);
void println(const __FlashStringHelper *);
void println(const String &s);
void println(const char[]);
void println(char);
void println(unsigned char, int = DEC);
void println(int, int = DEC);
void println(unsigned int, int = DEC);
void println(long, int = DEC);
void println(unsigned long, int = DEC);
void println(double, int = 2);
void println(const Printable&);
void println(void);
long println(const __FlashStringHelper *);
long println(const String &s);
long println(const char[]);
long println(char);
long println(unsigned char, int = DEC);
long println(int, int = DEC);
long println(unsigned int, int = DEC);
long println(long, int = DEC);
long println(unsigned long, int = DEC);
long println(double, int = 2);
long println(const Printable&);
long println(void);
};
#endif

View File

@ -30,7 +30,7 @@ class Print;
class Printable
{
public:
virtual void printTo(Print& p) const = 0;
virtual long printTo(Print& p) const = 0;
};
#endif

View File

@ -0,0 +1,61 @@
#include <ArduinoTestSuite.h>
void Test_Equal(long actual, long expected)
{
char buf[100];
boolean b = expected == actual;
ATS_PrintTestStatus("", b);
if (!b) {
Serial.print("expected '");
Serial.print(expected);
Serial.print("', actual '");
Serial.print(actual);
Serial.println("'");
}
}
void setup()
{
byte buf[5] = { 65, 66, 67, 0, 69 };
ATS_begin("Arduino", "Write & Print Return Values Test");
Test_Equal(Serial.write('a'), 1);
Test_Equal(Serial.write(byte(0)), 1);
Test_Equal(Serial.write("abc"), 3);
Test_Equal(Serial.write(""), 0);
Test_Equal(Serial.write(buf, 5), 5);
Test_Equal(Serial.print(0), 1);
Test_Equal(Serial.print(""), 0);
Test_Equal(Serial.print("abc"), 3);
Test_Equal(Serial.print(0), 1);
Test_Equal(Serial.print(1), 1);
Test_Equal(Serial.print(11), 2);
Test_Equal(Serial.print(12345), 5);
Test_Equal(Serial.print(-1), 2);
Test_Equal(Serial.print(-123), 4);
Test_Equal(Serial.println(), 2);
Test_Equal(Serial.println(""), 2);
Test_Equal(Serial.println("abc"), 5);
Test_Equal(Serial.println(0), 3);
Test_Equal(Serial.println(1), 3);
Test_Equal(Serial.println(11), 4);
Test_Equal(Serial.println(12345), 7);
Test_Equal(Serial.println(-1), 4);
Test_Equal(Serial.println(-123), 6);
ATS_end();
}
void loop() {}

View File

@ -70,19 +70,18 @@ int Client::connect(IPAddress ip, uint16_t port) {
return 1;
}
void Client::write(uint8_t b) {
if (_sock != MAX_SOCK_NUM)
send(_sock, &b, 1);
long Client::write(uint8_t b) {
return write(&b, 1);
}
void Client::write(const char *str) {
if (_sock != MAX_SOCK_NUM)
send(_sock, (const uint8_t *)str, strlen(str));
long Client::write(const char *str) {
return write((const uint8_t *) str, strlen(str));
}
void Client::write(const uint8_t *buf, size_t size) {
if (_sock != MAX_SOCK_NUM)
send(_sock, buf, size);
long Client::write(const uint8_t *buf, size_t size) {
if (_sock == MAX_SOCK_NUM) return -1;
if (!send(_sock, buf, size)) return -2;
return size;
}
int Client::available() {

View File

@ -12,9 +12,9 @@ public:
uint8_t status();
int connect(IPAddress ip, uint16_t port);
int connect(const char *host, uint16_t port);
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
virtual long write(uint8_t);
virtual long write(const char *str);
virtual long write(const uint8_t *buf, size_t size);
virtual int available();
virtual int read();
virtual int read(uint8_t *buf, size_t size);

View File

@ -42,13 +42,15 @@ bool IPAddress::operator==(const uint8_t* addr)
return memcmp(addr, _address, sizeof(_address)) == 0;
}
void IPAddress::printTo(Print& p) const
long IPAddress::printTo(Print& p) const
{
long n = 0, t;
for (int i =0; i < 3; i++)
{
p.print(_address[i], DEC);
p.print('.');
if ((t = p.print(_address[i], DEC)) > 0) n += t;
if ((t = p.print('.')) > 0) n+= t;
}
p.print(_address[3], DEC);
if ((t = p.print(_address[3], DEC)) > 0) n += t;
return n;
}

View File

@ -60,7 +60,7 @@ public:
IPAddress& operator=(const uint8_t *address);
IPAddress& operator=(uint32_t address);
virtual void printTo(Print& p) const;
virtual long printTo(Print& p) const;
friend class EthernetClass;
friend class UDP;

View File

@ -67,18 +67,20 @@ Client Server::available()
return Client(MAX_SOCK_NUM);
}
void Server::write(uint8_t b)
long Server::write(uint8_t b)
{
write(&b, 1);
}
void Server::write(const char *str)
long Server::write(const char *str)
{
write((const uint8_t *)str, strlen(str));
}
void Server::write(const uint8_t *buffer, size_t size)
long Server::write(const uint8_t *buffer, size_t size)
{
long n = 0;
accept();
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
@ -86,7 +88,9 @@ void Server::write(const uint8_t *buffer, size_t size)
if (EthernetClass::_server_port[sock] == _port &&
client.status() == SnSR::ESTABLISHED) {
client.write(buffer, size);
n += client.write(buffer, size);
}
}
return n;
}

View File

@ -14,9 +14,9 @@ public:
Server(uint16_t);
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);
virtual long write(uint8_t);
virtual long write(const char *str);
virtual long write(const uint8_t *buf, size_t size);
};
#endif

View File

@ -102,21 +102,22 @@ int UDP::endPacket()
return sendUDP(_sock);
}
void UDP::write(uint8_t byte)
long UDP::write(uint8_t byte)
{
write(&byte, 1);
return write(&byte, 1);
}
void UDP::write(const char *str)
long UDP::write(const char *str)
{
size_t len = strlen(str);
write((const uint8_t *)str, len);
return write((const uint8_t *)str, len);
}
void UDP::write(const uint8_t *buffer, size_t size)
long UDP::write(const uint8_t *buffer, size_t size)
{
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
_offset += bytes_written;
return bytes_written;
}
int UDP::parsePacket()

View File

@ -67,11 +67,11 @@ public:
// Returns 1 if the packet was sent successfully, 0 if there was an error
int endPacket();
// Write a single byte into the packet
virtual void write(uint8_t);
virtual long write(uint8_t);
// Write a string of characters into the packet
virtual void write(const char *str);
virtual long write(const char *str);
// Write size bytes from buffer into the packet
virtual void write(const uint8_t *buffer, size_t size);
virtual long write(const uint8_t *buffer, size_t size);
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available

View File

@ -258,8 +258,9 @@ inline void LiquidCrystal::command(uint8_t value) {
send(value, LOW);
}
inline void LiquidCrystal::write(uint8_t value) {
inline long LiquidCrystal::write(uint8_t value) {
send(value, HIGH);
return 1; // assume sucess
}
/************ low level data pushing commands **********/

View File

@ -79,7 +79,7 @@ public:
void createChar(uint8_t, uint8_t[]);
void setCursor(uint8_t, uint8_t);
virtual void write(uint8_t);
virtual long write(uint8_t);
void command(uint8_t);
private:
void send(uint8_t, uint8_t);

View File

@ -58,19 +58,20 @@ boolean File::isDirectory(void) {
}
void File::write(uint8_t val) {
if (_file)
_file->write(val);
long File::write(uint8_t val) {
return write(&val, 1);
}
void File::write(const char *str) {
if (_file)
_file->write(str);
long File::write(const char *str) {
return write((const uint8_t *) str, strlen(str));
}
void File::write(const uint8_t *buf, size_t size) {
if (_file)
_file->write(buf, size);
long File::write(const uint8_t *buf, size_t size) {
long t;
if (!_file) return -1;
t = _file->write(buf, size);
if (t < 0) return t - 1;
return t;
}
int File::peek() {

View File

@ -32,9 +32,9 @@ public:
File(SdFile f, char *name); // wraps an underlying SdFile
File(void); // 'empty' constructor
~File(void); // destructor
virtual void write(uint8_t);
virtual void write(const char *str);
virtual void write(const uint8_t *buf, size_t size);
virtual long write(uint8_t);
virtual long write(const char *str);
virtual long write(const uint8_t *buf, size_t size);
virtual int read();
virtual int peek();
virtual int available();

View File

@ -283,9 +283,9 @@ class SdFile : public Print {
}
/** \return SdVolume that contains this file. */
SdVolume* volume(void) const {return vol_;}
void write(uint8_t b);
int16_t write(const void* buf, uint16_t nbyte);
void write(const char* str);
long write(uint8_t b);
long write(const void* buf, uint16_t nbyte);
long write(const char* str);
void write_P(PGM_P str);
void writeln_P(PGM_P str);
//------------------------------------------------------------------------------

View File

@ -1121,7 +1121,7 @@ uint8_t SdFile::truncate(uint32_t length) {
* for a read-only file, device is full, a corrupt file system or an I/O error.
*
*/
int16_t SdFile::write(const void* buf, uint16_t nbyte) {
long SdFile::write(const void* buf, uint16_t nbyte) {
// convert void* to uint8_t* - must be before goto statements
const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
@ -1219,8 +1219,8 @@ int16_t SdFile::write(const void* buf, uint16_t nbyte) {
*
* Use SdFile::writeError to check for errors.
*/
void SdFile::write(uint8_t b) {
write(&b, 1);
long SdFile::write(uint8_t b) {
return write(&b, 1);
}
//------------------------------------------------------------------------------
/**
@ -1228,8 +1228,8 @@ void SdFile::write(uint8_t b) {
*
* Use SdFile::writeError to check for errors.
*/
void SdFile::write(const char* str) {
write(str, strlen(str));
long SdFile::write(const char* str) {
return write(str, strlen(str));
}
//------------------------------------------------------------------------------
/**

View File

@ -440,10 +440,10 @@ int SoftwareSerial::available()
return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
}
void SoftwareSerial::write(uint8_t b)
long SoftwareSerial::write(uint8_t b)
{
if (_tx_delay == 0)
return;
return -1;
uint8_t oldSREG = SREG;
cli(); // turn off interrupts for a clean txmit
@ -484,6 +484,8 @@ void SoftwareSerial::write(uint8_t b)
SREG = oldSREG; // turn interrupts back on
tunedDelay(_tx_delay);
return 1;
}
void SoftwareSerial::flush()

View File

@ -89,7 +89,7 @@ public:
bool overflow() { bool ret = _buffer_overflow; _buffer_overflow = false; return ret; }
int peek();
virtual void write(uint8_t byte);
virtual long write(uint8_t byte);
virtual int read();
virtual int available();
virtual void flush();

View File

@ -124,13 +124,13 @@ uint8_t TwoWire::endTransmission(void)
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void TwoWire::write(uint8_t data)
long TwoWire::write(uint8_t data)
{
if(transmitting){
// in master transmitter mode
// don't bother if buffer is full
if(txBufferLength >= BUFFER_LENGTH){
return;
return -1;
}
// put byte in tx buffer
txBuffer[txBufferIndex] = data;
@ -142,31 +142,33 @@ void TwoWire::write(uint8_t data)
// reply to master
twi_transmit(&data, 1);
}
return 1;
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void TwoWire::write(const uint8_t *data, size_t quantity)
long TwoWire::write(const uint8_t *data, size_t quantity)
{
if(transmitting){
// in master transmitter mode
for(size_t i = 0; i < quantity; ++i){
write(data[i]);
if (write(data[i]) < 0) return i;
}
}else{
// in slave send mode
// reply to master
twi_transmit(data, quantity);
}
return quantity;
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
void TwoWire::write(const char *data)
long TwoWire::write(const char *data)
{
write((uint8_t*)data, strlen(data));
return write((uint8_t*)data, strlen(data));
}
// must be called in:

View File

@ -52,9 +52,9 @@ class TwoWire : public Stream
uint8_t endTransmission(void);
uint8_t requestFrom(uint8_t, uint8_t);
uint8_t requestFrom(int, int);
virtual void write(uint8_t);
virtual void write(const char *);
virtual void write(const uint8_t *, size_t);
virtual long write(uint8_t);
virtual long write(const char *);
virtual long write(const uint8_t *, size_t);
virtual int available(void);
virtual int read(void);
virtual int peek(void);