Peter Barrett氏のBlueUSBにMIDI USB HOST機能を加えたサンプルプログラムです。KORG nanoKEYなどのUSB MIDIストリームをシリアルMIDI(Serial TX p9)にブリッジします。動作確認はKORG nanoKEY、AKAI LPK-25、EDIROL PC-50のみです。

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AutoEvents.cpp Source File

AutoEvents.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Peter Barrett
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 /* 
00025 May 11 2012 RadioJunkBox : added MIDI USB support
00026 */
00027 
00028 #include "mbed.h"
00029 #include "USBHost.h"
00030 #include "Utils.h"
00031 
00032 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
00033 #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1)
00034 #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2)
00035 
00036 u8 auto_mouse[4];       // buttons,dx,dy,scroll
00037 u8 auto_keyboard[8];    // modifiers,reserved,keycode1..keycode6
00038 u8 auto_joystick[4];    // x,y,buttons,throttle
00039 
00040 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
00041 {
00042     int evt = (int)userData;
00043     switch (evt)
00044     {
00045         case AUTO_KEYBOARD:
00046             printf("AUTO_KEYBOARD ");
00047             break;
00048         case AUTO_MOUSE:
00049             printf("AUTO_MOUSE ");
00050             break;
00051         default:
00052             printf("HUH ");
00053     }
00054     printfBytes("data",data,len);
00055     USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData);
00056 }
00057 
00058 //  Establish transfers for interrupt events
00059 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed)
00060 {
00061     if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80))
00062         return;
00063     
00064     // Make automatic interrupt enpoints for known devices
00065     u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
00066     u8* dst = 0;
00067     int len;
00068     switch (evt)
00069     {
00070         case AUTO_MOUSE:
00071             dst = auto_mouse;
00072             len = sizeof(auto_mouse);
00073             break;
00074         case AUTO_KEYBOARD:
00075             dst = auto_keyboard;
00076             len = sizeof(auto_keyboard);
00077             break;
00078         default:
00079             printf("Interrupt endpoint %02X %08X\n",ed->bEndpointAddress,evt);
00080             break;
00081     }
00082     if (dst)
00083     {
00084         printf("Auto Event for %02X %08X\n",ed->bEndpointAddress,evt);
00085         USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt);
00086     }
00087 }
00088 
00089 void PrintString(int device, int i)
00090 {
00091     u8 buffer[256];
00092     int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255);
00093     if (le < 0)
00094          return;
00095     char* dst = (char*)buffer;
00096     for (int j = 2; j < le; j += 2)
00097         *dst++ = buffer[j];
00098     *dst = 0;
00099     printf("%d:%s\n",i,(const char*)buffer);
00100  }
00101  
00102 //  Walk descriptors and create endpoints for a given device
00103 int StartAutoEvent(int device, int configuration, int interfaceNumber)
00104 {
00105     u8 buffer[255];
00106     int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
00107     if (err < 0)
00108         return err;
00109 
00110     int len = buffer[2] | (buffer[3] << 8);
00111     u8* d = buffer;
00112     u8* end = d + len;
00113     while (d < end)
00114     {
00115         if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
00116         {
00117             InterfaceDescriptor* id = (InterfaceDescriptor*)d;
00118             if (id->bInterfaceNumber == interfaceNumber)
00119             {
00120                  d += d[0];
00121                 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
00122                 {
00123                     if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
00124                         AddAutoEvent(device,id,(EndpointDescriptor*)d);
00125                     d += d[0];
00126                 }
00127             }
00128         }
00129         d += d[0];
00130     }
00131     return 0;
00132 }
00133 
00134 //  Implemented in main.cpp
00135 int OnDiskInsert(int device);
00136 
00137 //  Implemented in TestShell.cpp
00138 int OnBluetoothInsert(int device);
00139 
00140 //  Added by RadioJunkBox  ------------------------------------
00141 //  Implemented in MidiUSB.cpp 
00142 int OnMidiInsert(int device);
00143 //  -----------------------------------------------------------
00144 
00145 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
00146 {
00147     printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
00148     char s[128];
00149     for (int i = 1; i < 3; i++)
00150     {
00151         if (GetString(device,i,s,sizeof(s)) < 0)
00152             break;
00153         printf("%d: %s\n",i,s);
00154     }
00155     
00156     switch (interfaceDesc->bInterfaceClass)
00157     {
00158         case CLASS_MASS_STORAGE:
00159             if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50)
00160                 OnDiskInsert(device);    // it's SCSI!
00161             break;
00162         case CLASS_WIRELESS_CONTROLLER:
00163             if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
00164                 OnBluetoothInsert(device);    // it's bluetooth!
00165             break;
00166 //  Added by RadioJunkBox  ------------------------------------
00167         case CLASS_AUDIO:
00168             if (interfaceDesc->bInterfaceSubClass == 0x03 && interfaceDesc->bInterfaceProtocol == 0x00)
00169                 OnMidiInsert(device);    // it's MIDI!
00170             break;
00171         case CLASS_VENDOR_SPECIFIC:
00172             if (interfaceDesc->bInterfaceSubClass == 0x03 && interfaceDesc->bInterfaceProtocol == 0x00)
00173                 OnMidiInsert(device);    // it's MIDI! (EDIROL PC-50)
00174             break;
00175 //  ----------------------------------------------------------
00176         default:
00177             StartAutoEvent(device,1,0);
00178             break;
00179     }
00180 }