35
35
#define EPIN 0x80
36
36
#define EPSIZE 64
37
37
38
- Adafruit_USBD_MIDI::Adafruit_USBD_MIDI (void ) : _n_cables(1 ) {}
38
+ // TODO multiple instances
39
+ static Adafruit_USBD_MIDI *_midi_dev = NULL ;
40
+
41
+
42
+ #ifdef ARDUINO_ARCH_ESP32
43
+ static uint16_t midi_load_descriptor (uint8_t * dst, uint8_t * itf)
44
+ {
45
+ // uint8_t str_index = tinyusb_add_string_descriptor("TinyUSB HID");
46
+ uint8_t str_index = 0 ;
47
+
48
+ uint8_t ep_in = tinyusb_get_free_in_endpoint ();
49
+ uint8_t ep_out = tinyusb_get_free_out_endpoint ();
50
+ TU_VERIFY (ep_in && ep_out);
51
+ ep_in |= 0x80 ;
52
+
53
+ uint16_t desc_len = _midi_dev->getInterfaceDescriptor (0 , NULL , 0 );
54
+
55
+ desc_len = _midi_dev->makeItfDesc (*itf, dst, desc_len, ep_in, ep_out);
56
+
57
+ *itf+=2 ;
58
+ return desc_len;
59
+ }
60
+ #endif
61
+
62
+ Adafruit_USBD_MIDI::Adafruit_USBD_MIDI (uint8_t n_cables){
63
+ _n_cables = n_cables;
64
+
65
+ #ifdef ARDUINO_ARCH_ESP32
66
+ // ESP32 requires setup configuration descriptor within constructor
67
+ _midi_dev = this ;
68
+ uint16_t const desc_len = getInterfaceDescriptor (0 , NULL , 0 );
69
+ tinyusb_enable_interface (USB_INTERFACE_MIDI, desc_len, midi_load_descriptor);
70
+ #endif
71
+ }
39
72
40
73
void Adafruit_USBD_MIDI::setCables (uint8_t n_cables) { _n_cables = n_cables; }
41
74
42
75
bool Adafruit_USBD_MIDI::begin (void ) {
43
- if (!TinyUSBDevice.addInterface (*this ))
76
+ if (!TinyUSBDevice.addInterface (*this )) {
44
77
return false ;
78
+ }
45
79
80
+ _midi_dev = this ;
46
81
return true ;
47
82
}
48
83
49
- uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor (uint8_t itfnum,
50
- uint8_t *buf,
51
- uint16_t bufsize) {
52
- uint16_t len = 0 ;
84
+ uint16_t Adafruit_USBD_MIDI::makeItfDesc (uint8_t itfnum, uint8_t *buf, uint16_t bufsize, uint8_t ep_in, uint8_t ep_out) {
85
+ uint16_t const desc_len = TUD_MIDI_DESC_HEAD_LEN + TUD_MIDI_DESC_JACK_LEN * _n_cables + 2 * TUD_MIDI_DESC_EP_LEN (_n_cables);
86
+
87
+ // null buf is for length only
88
+ if (!buf) {
89
+ return desc_len;
90
+ }
53
91
54
- if (bufsize < TUD_MIDI_DESC_HEAD_LEN + TUD_MIDI_DESC_JACK_LEN * _n_cables +
55
- TUD_MIDI_DESC_EP_LEN (_n_cables) * 2 )
92
+ if (bufsize < desc_len) {
56
93
return 0 ;
94
+ }
57
95
96
+ uint16_t len = 0 ;
97
+
98
+ // Header
58
99
{
59
100
uint8_t desc[] = {TUD_MIDI_DESC_HEAD (itfnum, 0 , _n_cables)};
60
101
memcpy (buf + len, desc, sizeof (desc));
61
102
len += sizeof (desc);
62
103
}
63
104
105
+ // Jack
64
106
for (uint8_t i = 1 ; i <= _n_cables; i++) {
65
107
uint8_t jack[] = {TUD_MIDI_DESC_JACK (i)};
66
108
memcpy (buf + len, jack, sizeof (jack));
67
109
len += sizeof (jack);
68
110
}
69
111
70
- // Endpoint OUT + jack mapping - usb core will automatically update endpoint
71
- // number
112
+ // Endpoint OUT + jack mapping
72
113
{
73
- uint8_t desc[] = {TUD_MIDI_DESC_EP (EPOUT , EPSIZE, _n_cables)};
114
+ uint8_t desc[] = {TUD_MIDI_DESC_EP (ep_out , EPSIZE, _n_cables)};
74
115
memcpy (buf + len, desc, sizeof (desc));
75
116
len += sizeof (desc);
76
117
}
@@ -81,10 +122,9 @@ uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor(uint8_t itfnum,
81
122
len += sizeof (jack);
82
123
}
83
124
84
- // Endpoint IN + jack mapping - usb core will automatically update endpoint
85
- // number
125
+ // Endpoint IN + jack mapping
86
126
{
87
- uint8_t desc[] = {TUD_MIDI_DESC_EP (EPIN , EPSIZE, _n_cables)};
127
+ uint8_t desc[] = {TUD_MIDI_DESC_EP (ep_in , EPSIZE, _n_cables)};
88
128
memcpy (buf + len, desc, sizeof (desc));
89
129
len += sizeof (desc);
90
130
}
@@ -95,7 +135,18 @@ uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor(uint8_t itfnum,
95
135
len += sizeof (jack);
96
136
}
97
137
98
- return len;
138
+ if ( len != desc_len ) {
139
+ // TODO should throw an error message
140
+ return 0 ;
141
+ }
142
+
143
+ return desc_len;
144
+ }
145
+
146
+ uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor (uint8_t itfnum,
147
+ uint8_t *buf,
148
+ uint16_t bufsize) {
149
+ return makeItfDesc (itfnum, buf, bufsize, EPIN, EPOUT);
99
150
}
100
151
101
152
int Adafruit_USBD_MIDI::read (void ) {
0 commit comments