Skip to content

Commit e32479c

Browse files
committed
refactor for newer PluggableUSB API
1 parent cf3f2bb commit e32479c

File tree

2 files changed

+82
-108
lines changed

2 files changed

+82
-108
lines changed

src/MIDIUSB.cpp

Lines changed: 30 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,11 @@
1818

1919
#define MIDI_BUFFER_SIZE 64
2020

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;
21+
#define MIDI_AC_INTERFACE interface() // MIDI AC Interface
22+
#define MIDI_INTERFACE interface()+1
23+
#define MIDI_FIRST_ENDPOINT endpoint()
24+
#define MIDI_ENDPOINT_OUT endpoint()
25+
#define MIDI_ENDPOINT_IN endpoint()+1
5426

5527
#define MIDI_RX MIDI_ENDPOINT_OUT
5628
#define MIDI_TX MIDI_ENDPOINT_IN
@@ -64,21 +36,37 @@ struct ring_bufferMIDI
6436

6537
ring_bufferMIDI midi_rx_buffer = {{0,0,0,0 }, 0, 0};
6638

67-
static MIDIDescriptor _midiInterface;
39+
MIDI_ MidiUSB;
6840

69-
int MIDI_GetInterface(uint8_t* interfaceNum)
41+
int MIDI_::getInterface(uint8_t* interfaceNum)
7042
{
7143
interfaceNum[0] += 2; // uses 2
44+
_midiInterface =
45+
{
46+
D_IAD(MIDI_AC_INTERFACE, 2, MIDI_AUDIO, MIDI_AUDIO_CONTROL, 0),
47+
D_INTERFACE(MIDI_AC_INTERFACE,0,MIDI_AUDIO,MIDI_AUDIO_CONTROL,0),
48+
D_AC_INTERFACE(0x1, MIDI_INTERFACE),
49+
D_INTERFACE(MIDI_INTERFACE,2, MIDI_AUDIO,MIDI_STREAMING,0),
50+
D_AS_INTERFACE,
51+
D_MIDI_INJACK(MIDI_JACK_EMD, 0x1),
52+
D_MIDI_INJACK(MIDI_JACK_EXT, 0x2),
53+
D_MIDI_OUTJACK(MIDI_JACK_EMD, 0x3, 1, 2, 1),
54+
D_MIDI_OUTJACK(MIDI_JACK_EXT, 0x4, 1, 1, 1),
55+
D_MIDI_JACK_EP(USB_ENDPOINT_OUT(MIDI_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,64),
56+
D_MIDI_AC_JACK_EP(1, 1),
57+
D_MIDI_JACK_EP(USB_ENDPOINT_IN(MIDI_ENDPOINT_IN),USB_ENDPOINT_TYPE_BULK,64),
58+
D_MIDI_AC_JACK_EP (1, 3)
59+
};
7260
return USB_SendControl(0,&_midiInterface,sizeof(_midiInterface));
7361
}
7462

75-
bool MIDI_Setup(USBSetup& setup, uint8_t i)
63+
bool MIDI_::setup(USBSetup& setup, uint8_t i)
7664
{
7765
//Support requests here if needed. Typically these are optional
7866
return false;
7967
}
8068

81-
int MIDI_GetDescriptor(int8_t t)
69+
int MIDI_::getDescriptor(int8_t t)
8270
{
8371
return 0;
8472
}
@@ -189,50 +177,9 @@ void MIDI_::sendMIDI(midiEventPacket_t event)
189177
write(data, 4);
190178
}
191179

192-
MIDI_::MIDI_(void)
180+
MIDI_::MIDI_(void) : PUSBListNode(2, 2, epType)
193181
{
194-
static EPTYPE_DESCRIPTOR_SIZE endpointType[2];
195-
196-
endpointType[0] = EP_TYPE_BULK_OUT_MIDI; // MIDI_ENDPOINT_OUT
197-
endpointType[1] = EP_TYPE_BULK_IN_MIDI; // MIDI_ENDPOINT_IN
198-
199-
static PUSBCallbacks cb = {
200-
.setup = &MIDI_Setup,
201-
.getInterface = &MIDI_GetInterface,
202-
.getDescriptor = &MIDI_GetDescriptor,
203-
.getShortName = &MIDI_GetShortName,
204-
.numEndpoints = 2,
205-
.numInterfaces = 2,
206-
.endpointType = endpointType,
207-
};
208-
209-
static PUSBListNode node(&cb);
210-
211-
MIDI_ENDPOINT_OUT = PUSB_AddFunction(&node, &MIDI_AC_INTERFACE);
212-
MIDI_ENDPOINT_IN = MIDI_ENDPOINT_OUT + 1;
213-
MIDI_INTERFACE = MIDI_AC_INTERFACE + 1;
214-
215-
_midiInterface =
216-
{
217-
D_IAD(MIDI_AC_INTERFACE, 2, MIDI_AUDIO, MIDI_AUDIO_CONTROL, 0),
218-
D_INTERFACE(MIDI_AC_INTERFACE,0,MIDI_AUDIO,MIDI_AUDIO_CONTROL,0),
219-
D_AC_INTERFACE(0x1, MIDI_INTERFACE),
220-
D_INTERFACE(MIDI_INTERFACE,2, MIDI_AUDIO,MIDI_STREAMING,0),
221-
D_AS_INTERFACE,
222-
D_MIDI_INJACK(MIDI_JACK_EMD, 0x1),
223-
D_MIDI_INJACK(MIDI_JACK_EXT, 0x2),
224-
D_MIDI_OUTJACK(MIDI_JACK_EMD, 0x3, 1, 2, 1),
225-
D_MIDI_OUTJACK(MIDI_JACK_EXT, 0x4, 1, 1, 1),
226-
D_MIDI_JACK_EP(USB_ENDPOINT_OUT(MIDI_ENDPOINT_OUT),USB_ENDPOINT_TYPE_BULK,512),
227-
D_MIDI_AC_JACK_EP(1, 1),
228-
D_MIDI_JACK_EP(USB_ENDPOINT_IN(MIDI_ENDPOINT_IN),USB_ENDPOINT_TYPE_BULK,512),
229-
D_MIDI_AC_JACK_EP (1, 3)
230-
};
231-
}
232-
233-
int8_t MIDI_::begin()
234-
{
235-
}
236-
237-
238-
MIDI_ MidiUSB;
182+
epType[0] = EP_TYPE_BULK_OUT_MIDI; // MIDI_ENDPOINT_OUT
183+
epType[1] = EP_TYPE_BULK_IN_MIDI; // MIDI_ENDPOINT_IN
184+
PluggableUSB.plug(this);
185+
}

src/MIDIUSB.h

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@ typedef struct
1919
}midiEventPacket_t;
2020

2121
#if defined(ARDUINO_ARCH_AVR)
22+
23+
#include "PluggableUSB.h"
24+
#define EPTYPE_DESCRIPTOR_SIZE uint8_t
2225
#define EP_TYPE_BULK_OUT_MIDI EP_TYPE_BULK_OUT
2326
#define EP_TYPE_BULK_IN_MIDI EP_TYPE_BULK_IN
24-
#endif
27+
28+
#else
29+
30+
#include "USB/PluggableUSB.h"
31+
#define EPTYPE_DESCRIPTOR_SIZE uint32_t
2532

2633
#if defined(ARDUINO_ARCH_SAM)
34+
#define USB_SendControl USBD_SendControl
35+
#define USB_Available USBD_Available
36+
#define USB_Recv USBD_Recv
37+
#define USB_Send USBD_Send
38+
#define USB_Flush USBD_Flush
39+
2740
#define EP_TYPE_BULK_IN_MIDI (UOTGHS_DEVEPTCFG_EPSIZE_64_BYTE | \
2841
UOTGHS_DEVEPTCFG_EPDIR_IN | \
2942
UOTGHS_DEVEPTCFG_EPTYPE_BLK | \
@@ -39,29 +52,17 @@ typedef struct
3952
#endif
4053

4154
#if defined(__SAMD21G18A__)
55+
#define USB_SendControl USBDevice.sendControl
56+
#define USB_Available USBDevice.available
57+
#define USB_Recv USBDevice.recv
58+
#define USB_Send USBDevice.send
59+
#define USB_Flush USBDevice.flush
60+
4261
#define EP_TYPE_BULK_IN_MIDI USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_IN(0);
4362
#define EP_TYPE_BULK_OUT_MIDI USB_ENDPOINT_TYPE_BULK | USB_ENDPOINT_OUT(0);
44-
#endif
4563

46-
class MIDI_
47-
{
48-
// private:
49-
// RingBuffer *_midi_rx_buffer;
50-
private:
51-
void accept(void);
52-
public:
53-
MIDI_(void);
54-
55-
int8_t begin();
56-
57-
uint32_t available(void);
58-
midiEventPacket_t read(void);
59-
void flush(void);
60-
void sendMIDI(midiEventPacket_t event);
61-
size_t write(const uint8_t *buffer, size_t size);
62-
operator bool();
63-
};
64-
extern MIDI_ MidiUSB;
64+
#endif
65+
#endif
6566

6667
#define MIDI_AUDIO 0x01
6768
#define MIDI_AUDIO_CONTROL 0x01
@@ -71,7 +72,7 @@ extern MIDI_ MidiUSB;
7172
#define MIDI_JACK_EMD 0x01
7273
#define MIDI_JACK_EXT 0X02
7374

74-
#pragma pack(1)
75+
_Pragma("pack(1)")
7576

7677
typedef struct
7778
{
@@ -176,9 +177,35 @@ typedef struct
176177
#define D_CDCCS(_subtype,_d0,_d1) { 5, 0x24, _subtype, _d0, _d1 }
177178
#define D_CDCCS4(_subtype,_d0) { 4, 0x24, _subtype, _d0 }
178179

179-
#pragma pack()
180+
_Pragma("pack()")
180181

181182
#define WEAK __attribute__ ((weak))
182183

183-
#endif
184-
#endif
184+
class MIDI_ : public PUSBListNode
185+
{
186+
// private:
187+
// RingBuffer *_midi_rx_buffer;
188+
private:
189+
void accept(void);
190+
EPTYPE_DESCRIPTOR_SIZE epType[2];
191+
MIDIDescriptor _midiInterface;
192+
193+
protected:
194+
// Implementation of the PUSBListNode
195+
int getInterface(uint8_t* interfaceNum);
196+
int getDescriptor(int8_t t);
197+
bool setup(USBSetup& setup, uint8_t i);
198+
199+
public:
200+
MIDI_(void);
201+
uint32_t available(void);
202+
midiEventPacket_t read(void);
203+
void flush(void);
204+
void sendMIDI(midiEventPacket_t event);
205+
size_t write(const uint8_t *buffer, size_t size);
206+
operator bool();
207+
};
208+
extern MIDI_ MidiUSB;
209+
210+
#endif /* USBCON */
211+
#endif /* MIDIUSB_h */

0 commit comments

Comments
 (0)