Browse Source

Add MIDIUSB.h compatibility for Teensy 2.0

teensy4-core
PaulStoffregen 7 years ago
parent
commit
834bb53e4f
3 changed files with 116 additions and 0 deletions
  1. +53
    -0
      teensy/MIDIUSB.h
  2. +60
    -0
      usb_midi/usb_api.cpp
  3. +3
    -0
      usb_midi/usb_api.h

+ 53
- 0
teensy/MIDIUSB.h View File

@@ -0,0 +1,53 @@
#ifndef MIDIUSB_h
#define MIDIUSB_h

// For compatibility with Arduino's MIDIUSB library

#ifdef __cplusplus
#if !defined(USB_MIDI)
#error "Please select MIDI in Tools > USB Type to use MIDIUSB.h"
#endif

#include "usb_api.h"

typedef struct {
struct {
uint8_t header;
uint8_t byte1;
uint8_t byte2;
uint8_t byte3;
};
} midiEventPacket_t;


class MIDI_
{
public:
constexpr MIDI_(void) { }
uint32_t available(void) {
return usbMIDI.midiusb_available();
}
midiEventPacket_t read(void) {
midiEventPacket_t event;
usbMIDI.midiusb_read((uint8_t *)&event);
return event;
}
void flush(void) {
usbMIDI.send_now();
}
void sendMIDI(midiEventPacket_t event) {
usbMIDI.send_raw(event.header, event.byte1, event.byte2, event.byte3);
}
size_t write(const uint8_t *buffer, size_t size) {
// TODO - is this really needed?
return 0;
}
operator bool() {
// TODO - is this really needed?
return true;
}
};
extern MIDI_ MidiUSB;

#endif // __cplusplus
#endif // MIDIUSB_h

+ 60
- 0
usb_midi/usb_api.cpp View File

@@ -167,7 +167,67 @@ uint8_t usb_midi_class::analog2velocity(uint16_t val, uint8_t range)



uint32_t usb_midi_class::midiusb_available()
{
uint8_t c, intr_state;

intr_state = SREG;
cli();
if (!usb_configuration) {
SREG = intr_state;
return 0;
}
UENUM = MIDI_RX_ENDPOINT;
retry:
c = UEINTX;
if (!(c & (1<<RWAL))) {
if (c & (1<<RXOUTI)) {
UEINTX = 0x6B;
goto retry;
}
SREG = intr_state;
return 0;
}
SREG = intr_state;
return 4;
}

void usb_midi_class::midiusb_read(uint8_t *buf)
{
uint8_t c, intr_state;

intr_state = SREG;
cli();
if (!usb_configuration) {
SREG = intr_state;
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
return;
}
UENUM = MIDI_RX_ENDPOINT;
retry:
c = UEINTX;
if (!(c & (1<<RWAL))) {
if (c & (1<<RXOUTI)) {
UEINTX = 0x6B;
goto retry;
}
SREG = intr_state;
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
return;
}
buf[0] = UEDATX;
buf[1] = UEDATX;
buf[2] = UEDATX;
buf[3] = UEDATX;
if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
SREG = intr_state;
}


bool usb_midi_class::read(uint8_t channel)

+ 3
- 0
usb_midi/usb_api.h View File

@@ -258,6 +258,8 @@ public:
}
private:
void send_raw(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3);
uint32_t midiusb_available();
void midiusb_read(uint8_t *buf);
void sendSysEx_BufferHasTerm(uint16_t length, const uint8_t *data);
void sendSysEx_AddTermBytes(uint16_t length, const uint8_t *data);
void read_sysex_byte(uint8_t b);
@@ -287,6 +289,7 @@ private:
void (*handleActiveSensing)(void);
void (*handleSystemReset)(void);
void (*handleRealTimeSystem)(uint8_t rtb);
friend class MIDI_;
};

extern usb_midi_class usbMIDI;

Loading…
Cancel
Save