Setcom Corporation / USBDevice

Dependents:   shaun_larada

Fork of USBDevice by mbed official

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 
00022 
00023 USBMIDI::USBMIDI( uint16_t vendor_id, uint16_t product_id, uint16_t product_release ): USBDevice( vendor_id, product_id, product_release )
00024 {
00025     midi_evt = NULL;
00026     USBDevice::connect();
00027 }
00028 
00029 void USBMIDI::write( MIDIMessage m )
00030 {
00031     USBDevice::write( EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK );
00032 }
00033 
00034 
00035 void USBMIDI::attach( void ( *fptr )( MIDIMessage ) )
00036 {
00037     midi_evt = fptr;
00038 }
00039 
00040 
00041 bool USBMIDI::EP2_OUT_callback()
00042 {
00043     uint8_t buf[64];
00044     uint32_t len;
00045     readEP( EPBULK_OUT, buf, &len, 64 );
00046 
00047     if ( midi_evt != NULL )
00048     {
00049         for ( int i = 0; i < len; i += 4 )
00050         {
00051             midi_evt( MIDIMessage( buf + i ) );
00052         }
00053     }
00054 
00055     // We reactivate the endpoint to receive next characters
00056     readStart( EPBULK_OUT, MAX_PACKET_SIZE_EPBULK );
00057     return true;
00058 }
00059 
00060 
00061 
00062 // Called in ISR context
00063 // Set configuration. Return false if the
00064 // configuration is not supported.
00065 bool USBMIDI::USBCallback_setConfiguration( uint8_t configuration )
00066 {
00067     if ( configuration != DEFAULT_CONFIGURATION )
00068     {
00069         return false;
00070     }
00071 
00072     // Configure endpoints > 0
00073     addEndpoint( EPBULK_IN, MAX_PACKET_SIZE_EPBULK );
00074     addEndpoint( EPBULK_OUT, MAX_PACKET_SIZE_EPBULK );
00075     // We activate the endpoint to be able to receive data
00076     readStart( EPBULK_OUT, MAX_PACKET_SIZE_EPBULK );
00077     return true;
00078 }
00079 
00080 
00081 uint8_t *USBMIDI::stringIinterfaceDesc()
00082 {
00083     static uint8_t stringIinterfaceDescriptor[] =
00084     {
00085         0x0c,                           //bLength
00086         STRING_DESCRIPTOR,              //bDescriptorType 0x03
00087         'A', 0, 'u', 0, 'd', 0, 'i', 0, 'o', 0 //bString iInterface - Audio
00088     };
00089     return stringIinterfaceDescriptor;
00090 }
00091 
00092 uint8_t *USBMIDI::stringIproductDesc()
00093 {
00094     static uint8_t stringIproductDescriptor[] =
00095     {
00096         0x16,                                                       //bLength
00097         STRING_DESCRIPTOR,                                          //bDescriptorType 0x03
00098         'M', 0, 'b', 0, 'e', 0, 'd', 0, ' ', 0, 'A', 0, 'u', 0, 'd', 0, 'i', 0, 'o', 0 //bString iProduct - Mbed Audio
00099     };
00100     return stringIproductDescriptor;
00101 }
00102 
00103 
00104 uint8_t *USBMIDI::configurationDesc()
00105 {
00106     static uint8_t configDescriptor[] =
00107     {
00108         // configuration descriptor
00109         0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50,
00110 
00111         // The Audio Interface Collection
00112         0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor
00113         0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor
00114         0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors
00115         0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00,             // Class-Specific MS Interface Header Descriptor
00116 
00117         // MIDI IN JACKS
00118         0x06, 0x24, 0x02, 0x01, 0x01, 0x00,
00119         0x06, 0x24, 0x02, 0x02, 0x02, 0x00,
00120 
00121         // MIDI OUT JACKS
00122         0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00,
00123         0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00,
00124 
00125         // OUT endpoint descriptor
00126         0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
00127         0x05, 0x25, 0x01, 0x01, 0x01,
00128 
00129         // IN endpoint descriptor
00130         0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
00131         0x05, 0x25, 0x01, 0x01, 0x03,
00132     };
00133     return configDescriptor;
00134 }
00135