dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Committer:
cyberjoey
Date:
Sat Oct 22 01:31:58 2016 +0000
Revision:
9:6bb35cef007d
Parent:
1:55a6170b404f
WORKING

Who changed what in which revision?

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