aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRalf Kistner <ralf@embarkmobile.com>2014-04-27 14:36:16 +0200
committerRalf Kistner <ralf@embarkmobile.com>2014-12-18 16:25:50 +0200
commit64411fdda7c47df6ed72d51833142e5876f91abb (patch)
tree822ccb0538781564cc42e9d51724e76b083981df
parent8fed01fe372173d6d859dcb59716bf3d7211be61 (diff)
Only send MIDI nodes when we can.
This prevents the Arduino from hanging or slowing down when it is not connected to an application.
-rw-r--r--avr/cores/arcore/MIDIUSB.cpp14
-rw-r--r--avr/cores/arcore/USBAPI.h1
-rw-r--r--avr/cores/arcore/USBCore.cpp5
3 files changed, 14 insertions, 6 deletions
diff --git a/avr/cores/arcore/MIDIUSB.cpp b/avr/cores/arcore/MIDIUSB.cpp
index cef81f7..741e7f6 100644
--- a/avr/cores/arcore/MIDIUSB.cpp
+++ b/avr/cores/arcore/MIDIUSB.cpp
@@ -150,10 +150,13 @@ void MIDIUSB_::flush(void)
size_t MIDIUSB_::write(MIDIEvent c)
{
// TODO: only try to send bytes if the connection is open
-
- int r = USB_Send(MIDI_TX,&c,4);
- if (r > 0) {
- return r;
+ if(USB_Ready(MIDI_TX)) {
+ int r = USB_Send(MIDI_TX,&c,4);
+ if (r > 0) {
+ return r;
+ } else {
+ return 0;
+ }
} else {
return 0;
}
@@ -164,9 +167,8 @@ size_t MIDIUSB_::write(MIDIEvent c)
// to just being connected to the host). It can be used, for example, in
// setup() before printing to ensure that an application on the host is
// actually ready to receive and display the data.
-// This is not actually implemented yet (always returns true).
MIDIUSB_::operator bool() {
- return true;
+ return USB_Ready(MIDI_TX);
}
MIDIUSB_ MIDIUSB;
diff --git a/avr/cores/arcore/USBAPI.h b/avr/cores/arcore/USBAPI.h
index 4c1852f..8cf85ee 100644
--- a/avr/cores/arcore/USBAPI.h
+++ b/avr/cores/arcore/USBAPI.h
@@ -235,6 +235,7 @@ int USB_SendControl(uint8_t flags, const void* d, int len);
int USB_RecvControl(void* d, int len);
uint8_t USB_Available(uint8_t ep);
+uint8_t USB_Ready(uint8_t ep);
int USB_Send(uint8_t ep, const void* data, int len); // blocking
int USB_Recv(uint8_t ep, void* data, int len); // non-blocking
int USB_Recv(uint8_t ep); // non-blocking
diff --git a/avr/cores/arcore/USBCore.cpp b/avr/cores/arcore/USBCore.cpp
index 44969b7..ff0147f 100644
--- a/avr/cores/arcore/USBCore.cpp
+++ b/avr/cores/arcore/USBCore.cpp
@@ -271,6 +271,11 @@ u8 USB_SendSpace(u8 ep)
return 64 - FifoByteCount();
}
+u8 USB_Ready(u8 ep) {
+ LockEP lock(ep);
+ return ReadWriteAllowed();
+}
+
// Blocking Send of data to an endpoint
int USB_Send(u8 ep, const void* d, int len)
{