Closed
Description
So I added this issue as a comment to Arduino core, but putting here for visibility (and ultimately this is where we'd apply the fix).
The whole Arduino sketch will freeze if you try using it after the USB is connected, but before an app opens the MIDI port and starts listening to the MIDI sent from the Arduino.
Try with this test sketch: https://github.com/odbol/Arduino-MIDI-Test-Sketch
I've narrowed it down to this:
- When you plug the USB into a computer, the SerialUSB port initializes.
- The sketch calls
MidiUSB.sendMIDI()
, which freezes the whole sketch. (LED stops blinking) - The reason, is that the
USBD_Send()
function blocks until there is a connection. - Once you open a MIDI app on the PC (such as MIDI Monitor or Garageband), it opens the MIDI port, and the sketch unfreezes (
USBD_Send()
stops blocking). - Sending over SerialUSB works: the
SerialUSB.write()
function does not block if there is nothing connected. See theif (_usbLineInfo.lineState > 0)
line in CDC.cpp for how they do this. - We need to figure out a way to tell if
USBD_Send()
will block (i.e. the MIDI port is not yet opened on the other end), and avoid calling it if it will block (just drop the messages if there's nothing listening).
I've attempted to duplicate the _usbLineInfo.lineState
technique in CDC.cpp, but couldn't get it to work:
https://github.com/odbol/Arduino/tree/midi-fixserial
Any advice or help would be much appreciated!
Metadata
Metadata
Assignees
Labels
No labels