1616
1717#include " MIDIUSB.h"
1818
19- #define MIDI_BUFFER_SIZE 64
20-
21- #if defined(ARDUINO_ARCH_AVR)
22-
23- #include " PluggableUSB.h"
24- #define EPTYPE_DESCRIPTOR_SIZE uint8_t
25-
26- #else
27-
28- #include " USB/PluggableUSB.h"
29- #define EPTYPE_DESCRIPTOR_SIZE uint32_t
30-
31- #if defined(ARDUINO_ARCH_SAM)
32- #define USB_SendControl USBD_SendControl
33- #define USB_Available USBD_Available
34- #define USB_Recv USBD_Recv
35- #define USB_Send USBD_Send
36- #define USB_Flush USBD_Flush
37- #endif
38-
39- #if defined(__SAMD21G18A__)
40- #define USB_SendControl USBDevice.sendControl
41- #define USB_Available USBDevice.available
42- #define USB_Recv USBDevice.recv
43- #define USB_Send USBDevice.send
44- #define USB_Flush USBDevice.flush
45- #endif
46-
47- #endif
48-
49- static uint8_t MIDI_AC_INTERFACE; // MIDI AC Interface
50- static uint8_t MIDI_INTERFACE;
51- static uint8_t MIDI_FIRST_ENDPOINT;
52- static uint8_t MIDI_ENDPOINT_OUT;
53- static uint8_t MIDI_ENDPOINT_IN;
19+ #define MIDI_AC_INTERFACE pluggedInterface // MIDI AC Interface
20+ #define MIDI_INTERFACE pluggedInterface+1
21+ #define MIDI_FIRST_ENDPOINT pluggedEndpoint
22+ #define MIDI_ENDPOINT_OUT pluggedEndpoint
23+ #define MIDI_ENDPOINT_IN pluggedEndpoint+1
5424
5525#define MIDI_RX MIDI_ENDPOINT_OUT
5626#define MIDI_TX MIDI_ENDPOINT_IN
@@ -64,25 +34,47 @@ struct ring_bufferMIDI
6434
6535ring_bufferMIDI midi_rx_buffer = {{0 ,0 ,0 ,0 }, 0 , 0 };
6636
67- static MIDIDescriptor _midiInterface ;
37+ MIDI_ MidiUSB ;
6838
69- int MIDI_GetInterface (uint8_t * interfaceNum)
39+ int MIDI_::getInterface (uint8_t * interfaceNum)
7040{
71- interfaceNum[0 ] += 2 ; // uses 2
72- return USB_SendControl (0 ,&_midiInterface,sizeof (_midiInterface));
41+ interfaceNum[0 ] += 2 ; // uses 2 interfaces
42+ MIDIDescriptor _midiInterface =
43+ {
44+ D_IAD (MIDI_AC_INTERFACE, 2 , MIDI_AUDIO, MIDI_AUDIO_CONTROL, 0 ),
45+ D_INTERFACE (MIDI_AC_INTERFACE,0 ,MIDI_AUDIO,MIDI_AUDIO_CONTROL,0 ),
46+ D_AC_INTERFACE (0x1 , MIDI_INTERFACE),
47+ D_INTERFACE (MIDI_INTERFACE,2 , MIDI_AUDIO,MIDI_STREAMING,0 ),
48+ D_AS_INTERFACE,
49+ D_MIDI_INJACK (MIDI_JACK_EMD, 0x1 ),
50+ D_MIDI_INJACK (MIDI_JACK_EXT, 0x2 ),
51+ D_MIDI_OUTJACK (MIDI_JACK_EMD, 0x3 , 1 , 2 , 1 ),
52+ D_MIDI_OUTJACK (MIDI_JACK_EXT, 0x4 , 1 , 1 , 1 ),
53+ D_MIDI_JACK_EP (USB_ENDPOINT_OUT (MIDI_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,MIDI_BUFFER_SIZE),
54+ D_MIDI_AC_JACK_EP (1 , 1 ),
55+ D_MIDI_JACK_EP (USB_ENDPOINT_IN (MIDI_ENDPOINT_IN),USB_ENDPOINT_TYPE_BULK,MIDI_BUFFER_SIZE),
56+ D_MIDI_AC_JACK_EP (1 , 3 )
57+ };
58+ return USB_SendControl (0 , &_midiInterface, sizeof (_midiInterface));
7359}
7460
75- bool MIDI_Setup (USBSetup& setup, uint8_t i )
61+ bool MIDI_::setup (USBSetup& setup __attribute__ ((unused)) )
7662{
7763 // Support requests here if needed. Typically these are optional
7864 return false ;
7965}
8066
81- int MIDI_GetDescriptor ( int8_t t )
67+ int MIDI_::getDescriptor (USBSetup& setup __attribute__ ((unused)) )
8268{
8369 return 0 ;
8470}
8571
72+ uint8_t MIDI_::getShortName (char * name)
73+ {
74+ memcpy (name, " MIDI" , 4 );
75+ return 4 ;
76+ }
77+
8678void MIDI_::accept (void )
8779{
8880 ring_bufferMIDI *buffer = &midi_rx_buffer;
@@ -183,49 +175,9 @@ void MIDI_::sendMIDI(midiEventPacket_t event)
183175 write (data, 4 );
184176}
185177
186- MIDI_::MIDI_ (void )
187- {
188- static EPTYPE_DESCRIPTOR_SIZE endpointType[2 ];
189-
190- endpointType[0 ] = EP_TYPE_BULK_OUT_MIDI; // MIDI_ENDPOINT_OUT
191- endpointType[1 ] = EP_TYPE_BULK_IN_MIDI; // MIDI_ENDPOINT_IN
192-
193- static PUSBCallbacks cb = {
194- .setup = &MIDI_Setup,
195- .getInterface = &MIDI_GetInterface,
196- .getDescriptor = &MIDI_GetDescriptor,
197- .numEndpoints = 2 ,
198- .numInterfaces = 2 ,
199- .endpointType = endpointType,
200- };
201-
202- static PUSBListNode node (&cb);
203-
204- MIDI_ENDPOINT_OUT = PUSB_AddFunction (&node, &MIDI_AC_INTERFACE);
205- MIDI_ENDPOINT_IN = MIDI_ENDPOINT_OUT + 1 ;
206- MIDI_INTERFACE = MIDI_AC_INTERFACE + 1 ;
207-
208- _midiInterface =
209- {
210- D_IAD (MIDI_AC_INTERFACE, 2 , MIDI_AUDIO, MIDI_AUDIO_CONTROL, 0 ),
211- D_INTERFACE (MIDI_AC_INTERFACE,0 ,MIDI_AUDIO,MIDI_AUDIO_CONTROL,0 ),
212- D_AC_INTERFACE (0x1 , MIDI_INTERFACE),
213- D_INTERFACE (MIDI_INTERFACE,2 , MIDI_AUDIO,MIDI_STREAMING,0 ),
214- D_AS_INTERFACE,
215- D_MIDI_INJACK (MIDI_JACK_EMD, 0x1 ),
216- D_MIDI_INJACK (MIDI_JACK_EXT, 0x2 ),
217- D_MIDI_OUTJACK (MIDI_JACK_EMD, 0x3 , 1 , 2 , 1 ),
218- D_MIDI_OUTJACK (MIDI_JACK_EXT, 0x4 , 1 , 1 , 1 ),
219- D_MIDI_JACK_EP (USB_ENDPOINT_OUT (MIDI_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,512 ),
220- D_MIDI_AC_JACK_EP (1 , 1 ),
221- D_MIDI_JACK_EP (USB_ENDPOINT_IN (MIDI_ENDPOINT_IN),USB_ENDPOINT_TYPE_BULK,512 ),
222- D_MIDI_AC_JACK_EP (1 , 3 )
223- };
224- }
225-
226- int8_t MIDI_::begin ()
178+ MIDI_::MIDI_ (void ) : PluggableUSBModule(2 , 2 , epType)
227179{
228- }
229-
230-
231- MIDI_ MidiUSB;
180+ epType[ 0 ] = EP_TYPE_BULK_OUT_MIDI; // MIDI_ENDPOINT_OUT
181+ epType[ 1 ] = EP_TYPE_BULK_IN_MIDI; // MIDI_ENDPOINT_IN
182+ PluggableUSB (). plug ( this );
183+ }
0 commit comments