Skip to content

Fixed write() freezing entire Sketch unless MIDI port is opened by PC… #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/MIDIUSB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,23 @@ size_t MIDI_::write(const uint8_t *buffer, size_t size)
// open connection isn't broken cleanly (cable is yanked out, host dies
// or locks up, or host virtual serial port hangs)

int r = USB_Send(MIDI_TX, buffer, size);

if (r > 0)
{
return r;
} else
// first, check the TX buffer to see if it's ready for writing.
// USB_Send() may block if there's no one listening on the other end.
// in that case, we don't want to block waiting for someone to connect,
// because that would freeze the whole sketch
// instead, we'll just drop the packets and hope the caller figures it out.
if (Is_udd_write_enabled(MIDI_TX))
{
return 0;

int r = USB_Send(MIDI_TX, buffer, size);

if (r > 0)
{
return r;
} else
{
return 0;
}
}
return 0;
}
Expand Down