Skip to content

Commit b360489

Browse files
odbolfacchinm
authored andcommitted
Fixed write() freezing entire Sketch unless MIDI port is opened by PC
multiple architecture version fixes #4
1 parent e17a367 commit b360489

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/MIDIUSB.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,23 @@ size_t MIDI_::write(const uint8_t *buffer, size_t size)
153153
// open connection isn't broken cleanly (cable is yanked out, host dies
154154
// or locks up, or host virtual serial port hangs)
155155

156-
int r = USB_Send(MIDI_TX, buffer, size);
157-
158-
if (r > 0)
159-
{
160-
return r;
161-
} else
156+
// first, check the TX buffer to see if it's ready for writing.
157+
// USB_Send() may block if there's no one listening on the other end.
158+
// in that case, we don't want to block waiting for someone to connect,
159+
// because that would freeze the whole sketch
160+
// instead, we'll just drop the packets and hope the caller figures it out.
161+
if (is_write_enabled(MIDI_TX))
162162
{
163-
return 0;
163+
164+
int r = USB_Send(MIDI_TX, buffer, size);
165+
166+
if (r > 0)
167+
{
168+
return r;
169+
} else
170+
{
171+
return 0;
172+
}
164173
}
165174
return 0;
166175
}

src/MIDIUSB.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ typedef struct
3333
#define EP_TYPE_BULK_OUT_MIDI EP_TYPE_BULK_OUT
3434
#define EP_TYPE_BULK_IN_MIDI EP_TYPE_BULK_IN
3535
#define MIDI_BUFFER_SIZE 64
36+
#define is_write_enabled(x) (1)
3637

3738
#else
3839

@@ -41,11 +42,12 @@ typedef struct
4142
#define MIDI_BUFFER_SIZE 512
4243

4344
#if defined(ARDUINO_ARCH_SAM)
44-
#define USB_SendControl USBD_SendControl
45-
#define USB_Available USBD_Available
46-
#define USB_Recv USBD_Recv
47-
#define USB_Send USBD_Send
48-
#define USB_Flush USBD_Flush
45+
#define USB_SendControl USBD_SendControl
46+
#define USB_Available USBD_Available
47+
#define USB_Recv USBD_Recv
48+
#define USB_Send USBD_Send
49+
#define USB_Flush USBD_Flush
50+
#define is_write_enabled(x) Is_udd_write_enabled(x)
4951

5052
#define EP_TYPE_BULK_IN_MIDI (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | \
5153
UOTGHS_DEVEPTCFG_EPDIR_IN | \
@@ -62,11 +64,12 @@ typedef struct
6264
#endif
6365

6466
#if defined(__SAMD21G18A__)
65-
#define USB_SendControl USBDevice.sendControl
66-
#define USB_Available USBDevice.available
67-
#define USB_Recv USBDevice.recv
68-
#define USB_Send USBDevice.send
69-
#define USB_Flush USBDevice.flush
67+
#define USB_SendControl USBDevice.sendControl
68+
#define USB_Available USBDevice.available
69+
#define USB_Recv USBDevice.recv
70+
#define USB_Send USBDevice.send
71+
#define USB_Flush USBDevice.flush
72+
#define is_write_enabled(x) (1)
7073

7174
#define EP_TYPE_BULK_IN_MIDI USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0);
7275
#define EP_TYPE_BULK_OUT_MIDI USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_OUT(0);

0 commit comments

Comments
 (0)