Skip to content

Commit 53944cf

Browse files
majenkotechfacchinm
authored andcommitted
Fixed slowdown with repeated CDC bool operator test
Rebase and squash of arduino/Arduino#3624
1 parent fb7c2cc commit 53944cf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

cores/arduino/CDC.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef struct
2929
u8 bParityType;
3030
u8 bDataBits;
3131
u8 lineState;
32+
u8 prevLineState;
3233
} LineInfo;
3334

3435
static volatile LineInfo _usbLineInfo = { 57600, 0x00, 0x00, 0x00, 0x00 };
@@ -221,7 +222,7 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
221222
// TODO - ZE - check behavior on different OSes and test what happens if an
222223
// open connection isn't broken cleanly (cable is yanked out, host dies
223224
// or locks up, or host virtual serial port hangs)
224-
if (_usbLineInfo.lineState > 0) {
225+
if (this) {
225226
int r = USB_Send(CDC_TX,buffer,size);
226227
if (r > 0) {
227228
return r;
@@ -240,12 +241,18 @@ size_t Serial_::write(const uint8_t *buffer, size_t size)
240241
// setup() before printing to ensure that an application on the host is
241242
// actually ready to receive and display the data.
242243
// We add a short delay before returning to fix a bug observed by Federico
243-
// where the port is configured (lineState != 0) but not quite opened.
244+
// where the port is configured (lineState != 0) but not quite opened. This
245+
// only happens when the line state transitions from ==0 to >0 otherwise you
246+
// get a nasty delay when one isn't needed.
244247
Serial_::operator bool() {
245248
bool result = false;
246-
if (_usbLineInfo.lineState > 0)
249+
if (_usbLineInfo.lineState > 0) {
247250
result = true;
248-
delay(10);
251+
if (_usbLineInfo.prevLineState == 0) {
252+
delay(10);
253+
}
254+
}
255+
_usbLineInfo.prevLineState = _usbLineInfo.lineState;
249256
return result;
250257
}
251258

0 commit comments

Comments
 (0)