Dependencies: ChaNFSSD mbed ChaNFS
Diff: USBDevice/USBMIDI/USBMIDI.cpp
- Revision:
- 0:02c293160df3
diff -r 000000000000 -r 02c293160df3 USBDevice/USBMIDI/USBMIDI.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBDevice/USBMIDI/USBMIDI.cpp Wed Nov 09 17:30:56 2011 +0000 @@ -0,0 +1,107 @@ +// USB_MIDI.cpp +// MIDI edvice example +// Copyright (c) 2011 ARM Limited. All rights reserved. + +#include "stdint.h" +#include "USBMIDI.h" +#include "USBBusInterface.h" + + +USBMIDI::USBMIDI(uint16_t vendor_id, uint16_t product_id, uint16_t product_release): USBDevice(vendor_id, product_id, product_release) { + midi_evt = NULL; +} + +void USBMIDI::write(MIDIMessage m) { + USBDevice::write(EPBULK_IN, m.data, 4, MAX_PACKET_SIZE_EPBULK); +} + + +void USBMIDI::attach(void (*fptr)(MIDIMessage)) { + midi_evt = fptr; +} + + +bool USBMIDI::EPBULK_OUT_callback() { + uint8_t buf[64]; + uint16_t len; + read(EPBULK_OUT, buf, &len, 64); + + if (midi_evt != NULL) { + for (int i=0; i<len; i+=4) { + midi_evt(MIDIMessage(buf+i)); + } + } + + // We reactivate the endpoint to receive next characters + readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); + return true; +} + + + +bool USBMIDI::USBCallback_setConfiguration(uint8_t configuration) { + // Called in ISR context + // Set configuration. Return false if the + // configuration is not supported. + if (configuration != DEFAULT_CONFIGURATION) { + return false; + } + + // Configure endpoints > 0 + addEndpoint(EPBULK_IN, MAX_PACKET_SIZE_EPBULK); + addEndpoint(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); + + // We activate the endpoint to be able to receive data + readStart(EPBULK_OUT, MAX_PACKET_SIZE_EPBULK); + return true; +} + + +uint8_t * USBMIDI::stringIinterfaceDesc() { + static uint8_t stringIinterfaceDescriptor[] = { + 0x0c, //bLength + STRING_DESCRIPTOR, //bDescriptorType 0x03 + 'A',0,'u',0,'d',0,'i',0,'o',0 //bString iInterface - Audio + }; + return stringIinterfaceDescriptor; +} + +uint8_t * USBMIDI::stringIproductDesc() { + static uint8_t stringIproductDescriptor[] = { + 0x16, //bLength + STRING_DESCRIPTOR, //bDescriptorType 0x03 + 'M',0,'b',0,'e',0,'d',0,' ',0,'A',0,'u',0,'d',0,'i',0,'o',0 //bString iProduct - Mbed Audio + }; + return stringIproductDescriptor; +} + + +uint8_t * USBMIDI::configurationDesc() { + static uint8_t configDescriptor[] = { + // configuration descriptor + 0x09, 0x02, 0x65, 0x00, 0x02, 0x01, 0x00, 0xc0, 0x50, + + // The Audio Interface Collection + 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, // Standard AC Interface Descriptor + 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, // Class-specific AC Interface Descriptor + 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, // MIDIStreaming Interface Descriptors + 0x07, 0x24, 0x01, 0x00, 0x01, 0x41, 0x00, // Class-Specific MS Interface Header Descriptor + + // MIDI IN JACKS + 0x06, 0x24, 0x02, 0x01, 0x01, 0x00, + 0x06, 0x24, 0x02, 0x02, 0x02, 0x00, + + // MIDI OUT JACKS + 0x09, 0x24, 0x03, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, + 0x09, 0x24, 0x03, 0x02, 0x06, 0x01, 0x01, 0x01, 0x00, + + // OUT endpoint descriptor + 0x09, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, + 0x05, 0x25, 0x01, 0x01, 0x01, + + // IN endpoint descriptor + 0x09, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00, + 0x05, 0x25, 0x01, 0x01, 0x03, + }; + return configDescriptor; +}