From a7f44696d3a31630b5769b567e867f9c98b0ca67 Mon Sep 17 00:00:00 2001
From: Matt Jenkins <matt@majenko.co.uk>
Date: Tue, 4 Aug 2015 13:51:36 +0100
Subject: [PATCH 1/2] Fixed slowdown with repeated CDC bool operator test

---
 hardware/arduino/avr/cores/arduino/CDC.cpp | 23 ++++++++++++++--------
 1 file changed, 15 insertions(+), 8 deletions(-)

diff --git a/hardware/arduino/avr/cores/arduino/CDC.cpp b/hardware/arduino/avr/cores/arduino/CDC.cpp
index d694a2d2ca8..365eecd5fe1 100644
--- a/hardware/arduino/avr/cores/arduino/CDC.cpp
+++ b/hardware/arduino/avr/cores/arduino/CDC.cpp
@@ -28,9 +28,10 @@ typedef struct
 	u8 	bParityType;
 	u8 	bDataBits;
 	u8	lineState;
+    u8  prevLineState;
 } LineInfo;
 
-static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
+static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00, 0x00 };
 
 #define WEAK __attribute__ ((weak))
 
@@ -177,7 +178,7 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
 	// TODO - ZE - check behavior on different OSes and test what happens if an
 	// open connection isn't broken cleanly (cable is yanked out, host dies
 	// or locks up, or host virtual serial port hangs)
-	if (_usbLineInfo.lineState > 0)	{
+	if (this) {
 		int r = USB_Send(CDC_TX,buffer,size);
 		if (r > 0) {
 			return r;
@@ -196,13 +197,19 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
 // setup() before printing to ensure that an application on the host is
 // actually ready to receive and display the data.
 // We add a short delay before returning to fix a bug observed by Federico
-// where the port is configured (lineState != 0) but not quite opened.
+// where the port is configured (lineState != 0) but not quite opened.  This
+// only happens when the line state transitions from ==0 to >0 otherwise you
+// get a nasty delay when one isn't needed.
 Serial_::operator bool() {
-	bool result = false;
-	if (_usbLineInfo.lineState > 0) 
-		result = true;
-	delay(10);
-	return result;
+    bool res = false;
+    if (_usbLineInfo.lineState > 0) {
+        res = true;
+        if (_usbLineInfo.prevLineState == 0) {
+            delay(10);
+        }
+    }
+    _usbLineInfo.prevLineState = _usbLineInfo.lineState;
+    return res;
 }
 
 Serial_ Serial;

From 1f614f63ee197f5bae109ce788708a97cdb32453 Mon Sep 17 00:00:00 2001
From: Matt Jenkins <matt@majenko.co.uk>
Date: Tue, 18 Aug 2015 22:54:39 +0100
Subject: [PATCH 2/2] Rename res to result

---
 hardware/arduino/avr/cores/arduino/CDC.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/hardware/arduino/avr/cores/arduino/CDC.cpp b/hardware/arduino/avr/cores/arduino/CDC.cpp
index 365eecd5fe1..d924109aabb 100644
--- a/hardware/arduino/avr/cores/arduino/CDC.cpp
+++ b/hardware/arduino/avr/cores/arduino/CDC.cpp
@@ -201,15 +201,15 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
 // only happens when the line state transitions from ==0 to >0 otherwise you
 // get a nasty delay when one isn't needed.
 Serial_::operator bool() {
-    bool res = false;
+    bool result = false;
     if (_usbLineInfo.lineState > 0) {
-        res = true;
+        result = true;
         if (_usbLineInfo.prevLineState == 0) {
             delay(10);
         }
     }
     _usbLineInfo.prevLineState = _usbLineInfo.lineState;
-    return res;
+    return result;
 }
 
 Serial_ Serial;