Skip to content

MIDI: support virtual wires/plug #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 48 additions & 7 deletions src/Adafruit_USBD_MIDI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@
#define EPIN 0x80
#define EPSIZE 64

Adafruit_USBD_MIDI::Adafruit_USBD_MIDI(void)
Adafruit_USBD_MIDI::Adafruit_USBD_MIDI(void) :
_n_cables(1)
{
}

Adafruit_USBD_MIDI::Adafruit_USBD_MIDI(uint8_t n_cables) :
_n_cables(n_cables)
{
}

bool Adafruit_USBD_MIDI::begin(void)
Expand All @@ -47,12 +52,49 @@ bool Adafruit_USBD_MIDI::begin(void)

uint16_t Adafruit_USBD_MIDI::getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize)
{
// usb core will automatically update endpoint number
uint8_t desc[] = { TUD_MIDI_DESCRIPTOR(itfnum, 0, EPOUT, EPIN, EPSIZE) };
uint16_t const len = sizeof(desc);
uint16_t len = 0;

if (bufsize < TUD_MIDI_DESC_HEAD_LEN + TUD_MIDI_DESC_JACK_LEN * _n_cables + TUD_MIDI_DESC_EP_LEN(_n_cables) * 2)
return 0;

{
uint8_t desc[] = { TUD_MIDI_DESC_HEAD(itfnum, 0, _n_cables) };
memcpy(buf + len, desc, sizeof(desc));
len += sizeof(desc);
}

for (uint8_t i = 1; i <= _n_cables; i++) {
uint8_t jack[] = { TUD_MIDI_DESC_JACK(i) };
memcpy(buf + len, jack, sizeof(jack));
len += sizeof(jack);
}

// Endpoint OUT + jack mapping - usb core will automatically update endpoint number
{
uint8_t desc[] = { TUD_MIDI_DESC_EP(EPOUT, EPSIZE, _n_cables) };
memcpy(buf + len, desc, sizeof(desc));
len += sizeof(desc);
}

for (uint8_t i = 1; i <= _n_cables; i++) {
uint8_t jack[] = { TUD_MIDI_JACKID_IN_EMB(i) };
memcpy(buf + len, jack, sizeof(jack));
len += sizeof(jack);
}

// Endpoint IN + jack mapping - usb core will automatically update endpoint number
{
uint8_t desc[] = { TUD_MIDI_DESC_EP(EPIN, EPSIZE, _n_cables) };
memcpy(buf + len, desc, sizeof(desc));
len += sizeof(desc);
}

for (uint8_t i = 1; i <= _n_cables; i++) {
uint8_t jack[] = { TUD_MIDI_JACKID_OUT_EMB(i) };
memcpy(buf + len, jack, sizeof(jack));
len += sizeof(jack);
}

if ( bufsize < len ) return 0;
memcpy(buf, desc, len);
return len;
}

Expand Down Expand Up @@ -85,4 +127,3 @@ void Adafruit_USBD_MIDI::flush (void)

#endif


6 changes: 5 additions & 1 deletion src/Adafruit_USBD_MIDI.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Adafruit_USBD_MIDI : public Stream, Adafruit_USBD_Interface
{
public:
Adafruit_USBD_MIDI(void);
Adafruit_USBD_MIDI(uint8_t n_cables);

bool begin(void);

Expand All @@ -44,8 +45,11 @@ class Adafruit_USBD_MIDI : public Stream, Adafruit_USBD_Interface
virtual int peek ( void );
virtual void flush ( void );

// fron Adafruit_USBD_Interface
// from Adafruit_USBD_Interface
virtual uint16_t getDescriptor(uint8_t itfnum, uint8_t* buf, uint16_t bufsize);

private:
uint8_t _n_cables;
};

#endif /* ADAFRUIT_USBD_MIDI_H_ */