Skip to content

Commit

Permalink
Possible fix for Teensy++ 2.O
Browse files Browse the repository at this point in the history
  • Loading branch information
tttapa committed Jan 31, 2018
1 parent ff983c5 commit cbe25bb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/MIDI_Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "Arduino.h"

#if defined(CORE_TEENSY) && !(defined(USB_MIDI_AUDIO_SERIAL) || defined(USB_MIDI_SERIAL))
#error Please select a 'Serial + MIDI' option in the 'Tools > USB Type' menu.
// #error Please select a 'Serial + MIDI' option in the 'Tools > USB Type' menu.
#endif

#include "./MIDI_Controller/MIDI_Controller_Class.h"
Expand Down
59 changes: 41 additions & 18 deletions src/MIDI_Interfaces/USBMIDI_Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#include "MIDI_Interface.h"

#ifdef CORE_TEENSY
#include <usb_dev.h>
// #define USB_MIDI // Doesn't work
// #include <usb_dev.h>
#include "usb_api.h"
#include "usb_private.h"
#endif

#if defined(USBCON) && !defined(CORE_TEENSY) // If the main MCU has a USB connection but is not a Teensy
Expand All @@ -22,16 +25,8 @@ class USBMIDI_Interface : public MIDI_Interface

bool refresh() // Ignore MIDI input
{
#if defined(CORE_TEENSY) // If it's a Teensy board
if (!usb_configuration) // Check USB configuration
return false;
usb_packet_t *rx_packet = usb_rx(MIDI_RX_ENDPOINT); // Read a new packet from the USB buffer
if (rx_packet == nullptr) // If there's no new packet, return
return false;

usb_free(rx_packet); // Free the packet
return true; // repeat

#if defined(CORE_TEENSY) // If it's a Teensy board
return usbMIDI.read();
#elif defined(USBCON) // If the main MCU has a USB connection but is not a Teensy
return MidiUSB.read().header != 0; // if there's a packet to read, discard it, and read again next time
#endif
Expand All @@ -41,18 +36,46 @@ class USBMIDI_Interface : public MIDI_Interface
void sendImpl(uint8_t m, uint8_t c, uint8_t d1, uint8_t d2)
{
#if defined(CORE_TEENSY) // If it's a Teensy board
usb_midi_write_packed((m >> 4) | ((m | c) << 8) | (d1 << 16) | (d2 << 24));
uint8_t intr_state, timeout;

if (!usb_configuration)
return;
intr_state = SREG;
cli();
UENUM = MIDI_TX_ENDPOINT;
timeout = UDFNUML + 2;
while (1)
{ // are we ready to transmit?
if (UEINTX & (1 << RWAL))
break;
SREG = intr_state;
if (UDFNUML == timeout)
return;
if (!usb_configuration)
return;
intr_state = SREG;
cli();
UENUM = MIDI_TX_ENDPOINT;
}
UEDATX = m >> 4;
UEDATX = m | c;
UEDATX = d1;
UEDATX = d2;
if (!(UEINTX & (1 << RWAL)))
UEINTX = 0x3A;
SREG = intr_state;

#elif defined(USBCON) // If the main MCU has a USB connection but is not a Teensy
midiEventPacket_t msg = {m >> 4, m | c, d1, d2};
MidiUSB.sendMIDI(msg);
MidiUSB.flush();
#endif
}
void sendImpl(uint8_t m, uint8_t c, uint8_t d1)
{
sendImpl(m, c, d1, 0);
}
};
}
void sendImpl(uint8_t m, uint8_t c, uint8_t d1)
{
sendImpl(m, c, d1, 0);
}
};

#else // If the main MCU doesn't have a USB connection

Expand Down

0 comments on commit cbe25bb

Please sign in to comment.