added Boolean operators to HardwareSerial and CDC to test whether the port is ready to send data.

Mostly useful for Leonardo - simple way to test whether the port is actually opened by an application and ready to receive data.  For Serial objects attached to real UARTs always returns true.
This commit is contained in:
Zach Eveland 2012-04-01 12:54:35 -04:00
parent 1534b2b730
commit a984b581a8
4 changed files with 12 additions and 0 deletions

View File

@ -213,6 +213,12 @@ size_t Serial_::write(uint8_t c)
return 0;
}
Serial_::operator bool() {
if (_usbLineInfo.lineState > 0)
return true;
return false;
}
Serial_ Serial;
#endif

View File

@ -398,6 +398,10 @@ size_t HardwareSerial::write(uint8_t c)
return 1;
}
HardwareSerial::operator bool() {
return true;
}
// Preinstantiate Objects //////////////////////////////////////////////////////
#if defined(UBRRH) && defined(UBRRL)

View File

@ -57,6 +57,7 @@ class HardwareSerial : public Stream
virtual void flush(void);
virtual size_t write(uint8_t);
using Print::write; // pull in write(str) and write(buf, size) from Print
operator bool();
};
#if defined(UBRRH) || defined(UBRR0H)

View File

@ -39,6 +39,7 @@ public:
virtual int read(void);
virtual void flush(void);
virtual size_t write(uint8_t);
operator bool();
};
extern Serial_ Serial;