From 5f25a97cea1050760834ae022ffb296b293c24a8 Mon Sep 17 00:00:00 2001
From: Jacob Abel <thatcadguy@gmail.com>
Date: Thu, 27 Aug 2015 08:47:15 -0700
Subject: [PATCH] Add isFlushed() method to serial classes

Add isFlushed() to HardwareSerial class for AVR and to UARTClass class
for SAM. This provides a non-blocking way to check if the transmit
buffer is flushed.
---
 cores/arduino/UARTClass.cpp | 5 +++++
 cores/arduino/UARTClass.h   | 1 +
 2 files changed, 6 insertions(+)

diff --git a/cores/arduino/UARTClass.cpp b/cores/arduino/UARTClass.cpp
index 36de1358..ef228675 100644
--- a/cores/arduino/UARTClass.cpp
+++ b/cores/arduino/UARTClass.cpp
@@ -142,6 +142,11 @@ void UARTClass::flush( void )
    ;
 }
 
+bool UARTClass::isFlushed( void )
+{
+  return ((_pUart->UART_SR & UART_SR_TXRDY) == UART_SR_TXRDY) && (_tx_buffer->_iTail == _tx_buffer->_iHead);
+}
+
 size_t UARTClass::write( const uint8_t uc_data )
 {
   // Is the hardware currently busy?
diff --git a/cores/arduino/UARTClass.h b/cores/arduino/UARTClass.h
index 3747d8be..42052001 100644
--- a/cores/arduino/UARTClass.h
+++ b/cores/arduino/UARTClass.h
@@ -52,6 +52,7 @@ class UARTClass : public HardwareSerial
     int peek(void);
     int read(void);
     void flush(void);
+    bool isFlushed(void);
     size_t write(const uint8_t c);
     using Print::write; // pull in write(str) and write(buf, size) from Print