Add availableForWrite() to HardwareSerial

This commit is contained in:
PaulStoffregen 2014-07-18 07:01:26 -07:00
parent cb4ae51b42
commit bcc5488cbc
2 changed files with 16 additions and 0 deletions

View File

@ -176,6 +176,21 @@ int HardwareSerial::read(void)
}
}
int HardwareSerial::availableForWrite(void)
{
#if (SERIAL_TX_BUFFER_SIZE>256)
uint8_t oldSREG = SREG;
cli();
#endif
tx_buffer_index_t head = _tx_buffer_head;
tx_buffer_index_t tail = _tx_buffer_tail;
#if (SERIAL_TX_BUFFER_SIZE>256)
SREG = oldSREG;
#endif
if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail;
return tail - head - 1;
}
void HardwareSerial::flush()
{
// If we have never written a byte, no need to flush. This special

View File

@ -112,6 +112,7 @@ class HardwareSerial : public Stream
virtual int available(void);
virtual int peek(void);
virtual int read(void);
int availableForWrite(void);
virtual void flush(void);
virtual size_t write(uint8_t);
inline size_t write(unsigned long n) { return write((uint8_t)n); }