]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/USBDevice/USBMIDI/USBMIDI.cpp
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / USBDevice / USBMIDI / USBMIDI.cpp
1 /* Copyright (c) 2010-2011 mbed.org, MIT License
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4 * and associated documentation files (the "Software"), to deal in the Software without
5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
7 * Software is furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in all copies or
10 * substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17 */
18
19 #include "stdint.h"
20 #include "USBMIDI.h"
21
22
23 USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release)
24 : USBDevice(vendor_id, product_id, product_release), cur_data(0), data_end(true)
25 {
26 midi_evt = NULL;
27 USBDevice::connect();
28 }
29
30 // write plain MIDIMessage that will be converted to USBMidi event packet
31 void USBMIDI::write(MIDIMessage m) {
32 // first byte keeped for retro-compatibility
33 for(int p=1; p < m.length; p+=3) {
34 uint8_t buf[4];
35 // Midi message to USBMidi event packet
36 buf[0]=m.data[1] >> 4;
37 // SysEx
38 if(buf[0] == 0xF) {
39 if((m.length - p) > 3) {
40 // SysEx start or continue
41 buf[0]=0x4;
42 } else {
43 switch(m.length - p) {
44 case 1:
45 // SysEx end with one byte
46 buf[0]=0x5;
47 break;
48 case 2:
49 // SysEx end with two bytes
50 buf[0]=0x6;
51 break;
52 case 3:
53 // SysEx end with three bytes
54 buf[0]=0x7;
55 break;
56 }
57 }
58 }
59 buf[1]=m.data[p];
60
61 if(p+1 < m.length)
62 buf[2]=m.data[p+1];
63 else
64 buf[2]=0;
65
66 if(p+2 < m.length)
67 buf[3]=m.data[p+2];
68 else
69 buf[3]=0;
70
71 USBDevice::write(EPBULK_IN, buf, 4, MAX_PACKET_SIZE_EPBULK);
72 }
73 }
74
75
76 void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
77 midi_evt = fptr;
78 }
79
80 bool USBMIDI::EPBULK_OUT_callback() {
81 uint8_t buf[64];
82 uint32_t len;
83 readEP(EPBULK_OUT, buf, &len, 64);
84
85 if (midi_evt != NULL) {
86 for (uint32_t i=0; i<len; i+=4) {
87 uint8_t data_read;
88 data_end=true;
89 switch(buf[i]) {
90 case 0x2:
91 // Two-bytes System Common Message - undefined in USBMidi 1.0
92 data_read=2;
93 break;
94 case 0x4:
95 // SysEx start or continue
96 data_end=false;
97 data_read=3;
98 break;
99 case 0x5:
100 // Single-byte System Common Message or SysEx end with one byte
101 data_read=1;
102 break;
103 case 0x6:
104 // SysEx end with two bytes
105 data_read=2;
106 break;
107 case 0xC:
108 // Program change
109 data_read=2;
110 break;
111 case 0xD:
112 // Channel pressure
113 data_read=2;
114 break;
115 case 0xF:
116 // Single byte
117 data_read=1;
118 break;
119 default:
120 // Others three-bytes messages
121 data_read=3;
122 break;
123 }
124
125 for(uint8_t j=1;j<data_read+1;j++) {
126 data[cur_data]=buf[i+j];
127 cur_data++;
128 }
129
130 if(data_end) {
131 midi_evt(MIDIMessage(data,cur_data));
132 cur_data=0;
133 }
134 }
135 }
136
137 // We reactivate the endpoint to receive next characters
138 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
139 return true;
140 }
141
142 // Called in ISR context
143 // Set configuration. Return false if the
144 // configuration is not supported.
145 bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
146 if (configuration != DEFAULT_CONFIGURATION) {
147 return false;
148 }
149
150 // Configure endpoints > 0
151 addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
152 addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
153
154 // We activate the endpoint to be able to receive data
155 readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
156 return true;
157 }
158
159
160 uint8_t * USBMIDI::stringIinterfaceDesc() {
161 static uint8_t stringIinterfaceDescriptor[] = {
162 0x0c, //bLength
163 STRING_DESCRIPTOR, //bDescriptorType 0x03
164 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio
165 };
166 return stringIinterfaceDescriptor;
167 }
168
169 uint8_t * USBMIDI::stringIproductDesc() {
170 static uint8_t stringIproductDescriptor[] = {
171 0x16, //bLength
172 STRING_DESCRIPTOR, //bDescriptorType 0x03
173 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
174 };
175 return stringIproductDescriptor;
176 }
177
178
179 uint8_t * USBMIDI::configurationDesc() {
180 static uint8_t configDescriptor[] = {
181 // configuration descriptor
182 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
183
184 // The Audio Interface Collection
185 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
186 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
187 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
188 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor
189
190 // MIDI IN JACKS
191 0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
192 0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
193
194 // MIDI OUT JACKS
195 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
196 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
197
198 // OUT endpoint descriptor
199 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
200 0x05, 0x25, 0x01, 0x01, 0x01,
201
202 // IN endpoint descriptor
203 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
204 0x05, 0x25, 0x01, 0x01, 0x03,
205 };
206 return configDescriptor;
207 }
Imprint / Impressum