Suga koubou / Mbed 2 deprecated USB_CDC_MSD_Hello

Dependencies:   ChaNFSSD mbed ChaNFS

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBMIDI.cpp Source File

USBMIDI.cpp

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #include "stdint.h"
00020 #include "USBMIDI.h"
00021 #include "USBBusInterface.h"
00022 
00023 
00024 USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) {
00025     midi_evt = NULL;
00026     USBDevice::connect();
00027 }
00028 
00029 void USBMIDI::write(MIDIMessage m) {
00030     USBDevice::write(EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK);
00031 }
00032 
00033 
00034 void USBMIDI::attach(void (*fptr)(MIDIMessage)) {
00035     midi_evt = fptr;
00036 }
00037 
00038 
00039 bool USBMIDI::EP2_OUT_callback() {
00040     uint8_t buf[64];
00041     uint16_t len;
00042     readEP(EPBULK_OUT, buf, &len, 64);
00043 
00044     if (midi_evt != NULL) {
00045         for (int i=0; i<len; i+=4) {
00046             midi_evt(MIDIMessage(buf+i));
00047         }
00048     }
00049 
00050     // We reactivate the endpoint to receive next characters
00051     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00052     return true;
00053 }
00054 
00055 
00056 
00057 // Called in ISR context
00058 // Set configuration. Return false if the
00059 // configuration is not supported.
00060 bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) {
00061     if (configuration != DEFAULT_CONFIGURATION) {
00062         return false;
00063     }
00064 
00065     // Configure endpoints > 0
00066     addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK);
00067     addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00068 
00069     // We activate the endpoint to be able to receive data
00070     readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK);
00071     return true;
00072 }
00073 
00074 
00075 uint8_t * USBMIDI::stringIinterfaceDesc() {
00076     static uint8_t stringIinterfaceDescriptor[] = {
00077         0x0c,                           //bLength
00078         STRING_DESCRIPTOR,              //bDescriptorType 0x03
00079         'A',0,'u',0,'d',0,'i',0,'o',0   //bString iInterface - Audio
00080     };
00081     return stringIinterfaceDescriptor;
00082 }
00083 
00084 uint8_t * USBMIDI::stringIproductDesc() {
00085     static uint8_t stringIproductDescriptor[] = {
00086         0x16,                                                       //bLength
00087         STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
00088         'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio
00089     };
00090     return stringIproductDescriptor;
00091 }
00092 
00093 
00094 uint8_t * USBMIDI::configurationDesc() {
00095     static uint8_t configDescriptor[] = {
00096         // configuration descriptor
00097         0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
00098 
00099         // The Audio Interface Collection
00100         0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
00101         0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
00102         0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
00103         0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00,             // Class-Specific MS Interface Header Descriptor
00104 
00105         // MIDI IN JACKS
00106         0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
00107         0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
00108 
00109         // MIDI OUT JACKS
00110         0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
00111         0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
00112 
00113         // OUT endpoint descriptor
00114         0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
00115         0x05, 0x25, 0x01, 0x01, 0x01,
00116 
00117         // IN endpoint descriptor
00118         0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
00119         0x05, 0x25, 0x01, 0x01, 0x03,
00120     };
00121     return configDescriptor;
00122 }