Radio Junk Box / Mbed 2 deprecated KAMUI_USBHOST_MIDI-CV_Example

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MidiUSB.cpp Source File

MidiUSB.cpp

00001 
00002 /*
00003 Copyright (c) 2012 RadioJunkBox
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011 
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014 
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "mbed.h"
00025 #include "USBHost.h"
00026 #include "Utils.h"
00027 
00028 #define MIDIEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
00029 u8 ms_buf[255];  // MIDI Streaming Buffer
00030 
00031 // Ring Buffer
00032 #define BUFSIZE        32  // size of ring buffer (ex 4,8,16,32...)
00033 extern int             gPtr_buf_in, gPtr_buf_out;
00034 extern unsigned char   gRxBuf[];
00035 
00036 //  Received MIDI Steram
00037 void MidiEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
00038 {
00039     int i;
00040     int ptr = 0;
00041     
00042     while((data[ptr] != 0) && (ptr < len))
00043     {        
00044         // printf("MIDI %02X %02X %02X",device,status,len);
00045         // printfBytes("",data,4);
00046         for( i = 1; i<4; i++)
00047         {
00048             gPtr_buf_in++;
00049             gPtr_buf_in &= (BUFSIZE - 1);
00050             gRxBuf[gPtr_buf_in] = data[ptr+i];
00051         }
00052         ptr+=4;
00053     }
00054     USBBulkTransfer(device,endpoint,data,len,MidiEventCallback,userData);
00055 }
00056 
00057 //  Add MIDI Interface
00058 void AddMidiInterface(int device, InterfaceDescriptor* id, EndpointDescriptor* ed,int len)
00059 {
00060     if ((ed->bmAttributes & 3) != ENDPOINT_BULK || !(ed->bEndpointAddress & 0x80))
00061         return;
00062     
00063     // Make bulk enpoints for MIDI devices
00064     u32 evt = MIDIEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
00065     u8* dst = ms_buf;
00066 
00067     if(dst)
00068     {
00069         // printf("Add Midi Interface %02X %08X\r\n",ed->bEndpointAddress,evt);
00070         USBBulkTransfer(device,ed->bEndpointAddress,dst,len,MidiEventCallback,(void*)evt);
00071     }
00072 }
00073 
00074 //  Detected Midi Device
00075 int OnMidiInsert(int device)
00076 {
00077     u8 buffer[255];
00078     int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
00079     if (err < 0)
00080         return err;
00081 
00082     int len = buffer[2] | (buffer[3] << 8);
00083     u8* d = buffer;
00084     u8* end = d + len;
00085 
00086     // printf("OnMidiInsert %02X %02X\r\n",device,len);
00087 
00088     while (d < end)
00089     {
00090         if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
00091         {
00092             InterfaceDescriptor* id = (InterfaceDescriptor*)d;
00093             d += d[0];
00094             while (d < end)
00095             {
00096                     if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
00097                     {
00098                         // printfBytes("MIDI Endpoint",d,d[0]);
00099                         AddMidiInterface(device,id,(EndpointDescriptor*)d,d[4]);
00100                     }
00101                     d += d[0];
00102             }
00103         }
00104         d += d[0];
00105     }
00106     return 0;
00107 }