1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-29 18:52:13 +01:00

Moving write errors out of return value into separate API methods.

write(), print(), println() now return size_t (and don't use negative values to signal errors).
Print adds writeError() for checking for write errors, clearWriteError() to reset the flag to false, and a protected setWriteError() for signalling errors.

http://code.google.com/p/arduino/issues/detail?id=598
This commit is contained in:
David A. Mellis 2011-08-26 16:08:14 -04:00
parent 929597375b
commit b73cf39d94
23 changed files with 184 additions and 173 deletions

View File

@ -352,7 +352,7 @@ void HardwareSerial::flush()
; ;
} }
ssize_t HardwareSerial::write(uint8_t c) size_t HardwareSerial::write(uint8_t c)
{ {
int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE; int i = (_tx_buffer->head + 1) % SERIAL_BUFFER_SIZE;

View File

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

View File

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

View File

@ -23,8 +23,6 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> // for size_t #include <stdio.h> // for size_t
typedef int ssize_t;
#include "WString.h" #include "WString.h"
#include "Printable.h" #include "Printable.h"
@ -36,37 +34,45 @@ typedef int ssize_t;
class Print class Print
{ {
private: private:
ssize_t printNumber(unsigned long, uint8_t); int write_error;
ssize_t printFloat(double, uint8_t); size_t printNumber(unsigned long, uint8_t);
size_t printFloat(double, uint8_t);
protected:
void setWriteError(int err = 1) { write_error = err; }
public: public:
virtual ssize_t write(uint8_t) = 0; Print() : write_error(0) {}
virtual ssize_t write(const char *str);
virtual ssize_t write(const uint8_t *buffer, size_t size); int writeError() { return write_error; }
void clearWriteError() { setWriteError(0); }
virtual size_t write(uint8_t) = 0;
virtual size_t write(const char *str);
virtual size_t write(const uint8_t *buffer, size_t size);
ssize_t print(const __FlashStringHelper *); size_t print(const __FlashStringHelper *);
ssize_t print(const String &); size_t print(const String &);
ssize_t print(const char[]); size_t print(const char[]);
ssize_t print(char); size_t print(char);
ssize_t print(unsigned char, int = DEC); size_t print(unsigned char, int = DEC);
ssize_t print(int, int = DEC); size_t print(int, int = DEC);
ssize_t print(unsigned int, int = DEC); size_t print(unsigned int, int = DEC);
ssize_t print(long, int = DEC); size_t print(long, int = DEC);
ssize_t print(unsigned long, int = DEC); size_t print(unsigned long, int = DEC);
ssize_t print(double, int = 2); size_t print(double, int = 2);
ssize_t print(const Printable&); size_t print(const Printable&);
ssize_t println(const __FlashStringHelper *); size_t println(const __FlashStringHelper *);
ssize_t println(const String &s); size_t println(const String &s);
ssize_t println(const char[]); size_t println(const char[]);
ssize_t println(char); size_t println(char);
ssize_t println(unsigned char, int = DEC); size_t println(unsigned char, int = DEC);
ssize_t println(int, int = DEC); size_t println(int, int = DEC);
ssize_t println(unsigned int, int = DEC); size_t println(unsigned int, int = DEC);
ssize_t println(long, int = DEC); size_t println(long, int = DEC);
ssize_t println(unsigned long, int = DEC); size_t println(unsigned long, int = DEC);
ssize_t println(double, int = 2); size_t println(double, int = 2);
ssize_t println(const Printable&); size_t println(const Printable&);
ssize_t println(void); size_t println(void);
}; };
#endif #endif

View File

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

View File

@ -70,17 +70,23 @@ int Client::connect(IPAddress ip, uint16_t port) {
return 1; return 1;
} }
ssize_t Client::write(uint8_t b) { size_t Client::write(uint8_t b) {
return write(&b, 1); return write(&b, 1);
} }
ssize_t Client::write(const char *str) { size_t Client::write(const char *str) {
return write((const uint8_t *) str, strlen(str)); return write((const uint8_t *) str, strlen(str));
} }
ssize_t Client::write(const uint8_t *buf, size_t size) { size_t Client::write(const uint8_t *buf, size_t size) {
if (_sock == MAX_SOCK_NUM) return -1; if (_sock == MAX_SOCK_NUM) {
if (!send(_sock, buf, size)) return -2; setWriteError();
return 0;
}
if (!send(_sock, buf, size)) {
setWriteError();
return 0;
}
return size; return size;
} }

View File

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

View File

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

View File

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

View File

@ -67,19 +67,19 @@ Client Server::available()
return Client(MAX_SOCK_NUM); return Client(MAX_SOCK_NUM);
} }
ssize_t Server::write(uint8_t b) size_t Server::write(uint8_t b)
{ {
write(&b, 1); write(&b, 1);
} }
ssize_t Server::write(const char *str) size_t Server::write(const char *str)
{ {
write((const uint8_t *)str, strlen(str)); write((const uint8_t *)str, strlen(str));
} }
ssize_t Server::write(const uint8_t *buffer, size_t size) size_t Server::write(const uint8_t *buffer, size_t size)
{ {
ssize_t n = 0; size_t n = 0;
accept(); accept();

View File

@ -14,9 +14,9 @@ public:
Server(uint16_t); Server(uint16_t);
Client available(); Client available();
void begin(); void begin();
virtual ssize_t write(uint8_t); virtual size_t write(uint8_t);
virtual ssize_t write(const char *str); virtual size_t write(const char *str);
virtual ssize_t write(const uint8_t *buf, size_t size); virtual size_t write(const uint8_t *buf, size_t size);
}; };
#endif #endif

View File

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

View File

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

View File

@ -258,7 +258,7 @@ inline void LiquidCrystal::command(uint8_t value) {
send(value, LOW); send(value, LOW);
} }
inline ssize_t LiquidCrystal::write(uint8_t value) { inline size_t LiquidCrystal::write(uint8_t value) {
send(value, HIGH); send(value, HIGH);
return 1; // assume sucess return 1; // assume sucess
} }

View File

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

View File

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

View File

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

View File

@ -141,7 +141,7 @@ class SdFile : public Print {
* Set writeError to false before calling print() and/or write() and check * Set writeError to false before calling print() and/or write() and check
* for true after calls to print() and/or write(). * for true after calls to print() and/or write().
*/ */
bool writeError; //bool writeError;
/** /**
* Cancel unbuffered reads for this file. * Cancel unbuffered reads for this file.
* See setUnbufferedRead() * See setUnbufferedRead()
@ -283,9 +283,9 @@ class SdFile : public Print {
} }
/** \return SdVolume that contains this file. */ /** \return SdVolume that contains this file. */
SdVolume* volume(void) const {return vol_;} SdVolume* volume(void) const {return vol_;}
ssize_t write(uint8_t b); size_t write(uint8_t b);
ssize_t write(const void* buf, uint16_t nbyte); size_t write(const void* buf, uint16_t nbyte);
ssize_t write(const char* str); size_t write(const char* str);
void write_P(PGM_P str); void write_P(PGM_P str);
void writeln_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. * for a read-only file, device is full, a corrupt file system or an I/O error.
* *
*/ */
ssize_t SdFile::write(const void* buf, uint16_t nbyte) { size_t SdFile::write(const void* buf, uint16_t nbyte) {
// convert void* to uint8_t* - must be before goto statements // convert void* to uint8_t* - must be before goto statements
const uint8_t* src = reinterpret_cast<const uint8_t*>(buf); const uint8_t* src = reinterpret_cast<const uint8_t*>(buf);
@ -1210,8 +1210,9 @@ ssize_t SdFile::write(const void* buf, uint16_t nbyte) {
writeErrorReturn: writeErrorReturn:
// return for write error // return for write error
writeError = true; //writeError = true;
return -1; setWriteError();
return 0;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
/** /**
@ -1219,7 +1220,7 @@ ssize_t SdFile::write(const void* buf, uint16_t nbyte) {
* *
* Use SdFile::writeError to check for errors. * Use SdFile::writeError to check for errors.
*/ */
ssize_t SdFile::write(uint8_t b) { size_t SdFile::write(uint8_t b) {
return write(&b, 1); return write(&b, 1);
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -1228,7 +1229,7 @@ ssize_t SdFile::write(uint8_t b) {
* *
* Use SdFile::writeError to check for errors. * Use SdFile::writeError to check for errors.
*/ */
ssize_t SdFile::write(const char* str) { size_t SdFile::write(const char* str) {
return write(str, strlen(str)); return write(str, strlen(str));
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -440,10 +440,12 @@ int SoftwareSerial::available()
return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF; return (_receive_buffer_tail + _SS_MAX_RX_BUFF - _receive_buffer_head) % _SS_MAX_RX_BUFF;
} }
ssize_t SoftwareSerial::write(uint8_t b) size_t SoftwareSerial::write(uint8_t b)
{ {
if (_tx_delay == 0) if (_tx_delay == 0) {
return -1; setWriteError();
return 0;
}
uint8_t oldSREG = SREG; uint8_t oldSREG = SREG;
cli(); // turn off interrupts for a clean txmit cli(); // turn off interrupts for a clean txmit

View File

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

View File

@ -124,13 +124,14 @@ uint8_t TwoWire::endTransmission(void)
// must be called in: // must be called in:
// slave tx event callback // slave tx event callback
// or after beginTransmission(address) // or after beginTransmission(address)
ssize_t TwoWire::write(uint8_t data) size_t TwoWire::write(uint8_t data)
{ {
if(transmitting){ if(transmitting){
// in master transmitter mode // in master transmitter mode
// don't bother if buffer is full // don't bother if buffer is full
if(txBufferLength >= BUFFER_LENGTH){ if(txBufferLength >= BUFFER_LENGTH){
return -1; setWriteError();
return 0;
} }
// put byte in tx buffer // put byte in tx buffer
txBuffer[txBufferIndex] = data; txBuffer[txBufferIndex] = data;
@ -148,12 +149,12 @@ ssize_t TwoWire::write(uint8_t data)
// must be called in: // must be called in:
// slave tx event callback // slave tx event callback
// or after beginTransmission(address) // or after beginTransmission(address)
ssize_t TwoWire::write(const uint8_t *data, size_t quantity) size_t TwoWire::write(const uint8_t *data, size_t quantity)
{ {
if(transmitting){ if(transmitting){
// in master transmitter mode // in master transmitter mode
for(size_t i = 0; i < quantity; ++i){ for(size_t i = 0; i < quantity; ++i){
if (write(data[i]) < 0) return i; write(data[i]);
} }
}else{ }else{
// in slave send mode // in slave send mode
@ -166,7 +167,7 @@ ssize_t TwoWire::write(const uint8_t *data, size_t quantity)
// must be called in: // must be called in:
// slave tx event callback // slave tx event callback
// or after beginTransmission(address) // or after beginTransmission(address)
ssize_t TwoWire::write(const char *data) size_t TwoWire::write(const char *data)
{ {
return write((uint8_t*)data, strlen(data)); return write((uint8_t*)data, strlen(data));
} }

View File

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