うおーるぼっと用プログラム Wiiリモコンからのダイレクト操作モードのみ BlueUSBをベースに使用しています。

Dependencies:   BD6211F mbed SimpleFilter

Committer:
jksoft
Date:
Fri Apr 29 15:50:23 2011 +0000
Revision:
0:4f749f62c6d7

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jksoft 0:4f749f62c6d7 1
jksoft 0:4f749f62c6d7 2 /*
jksoft 0:4f749f62c6d7 3 Copyright (c) 2010 Peter Barrett
jksoft 0:4f749f62c6d7 4
jksoft 0:4f749f62c6d7 5 Permission is hereby granted, free of charge, to any person obtaining a copy
jksoft 0:4f749f62c6d7 6 of this software and associated documentation files (the "Software"), to deal
jksoft 0:4f749f62c6d7 7 in the Software without restriction, including without limitation the rights
jksoft 0:4f749f62c6d7 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jksoft 0:4f749f62c6d7 9 copies of the Software, and to permit persons to whom the Software is
jksoft 0:4f749f62c6d7 10 furnished to do so, subject to the following conditions:
jksoft 0:4f749f62c6d7 11
jksoft 0:4f749f62c6d7 12 The above copyright notice and this permission notice shall be included in
jksoft 0:4f749f62c6d7 13 all copies or substantial portions of the Software.
jksoft 0:4f749f62c6d7 14
jksoft 0:4f749f62c6d7 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jksoft 0:4f749f62c6d7 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jksoft 0:4f749f62c6d7 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jksoft 0:4f749f62c6d7 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jksoft 0:4f749f62c6d7 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jksoft 0:4f749f62c6d7 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jksoft 0:4f749f62c6d7 21 THE SOFTWARE.
jksoft 0:4f749f62c6d7 22 */
jksoft 0:4f749f62c6d7 23
jksoft 0:4f749f62c6d7 24 #include "mbed.h"
jksoft 0:4f749f62c6d7 25 #include "USBHost.h"
jksoft 0:4f749f62c6d7 26 #include "Utils.h"
jksoft 0:4f749f62c6d7 27
jksoft 0:4f749f62c6d7 28 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
jksoft 0:4f749f62c6d7 29 #define AUTO_KEYBOARD AUTOEVT(CLASS_HID,1,1)
jksoft 0:4f749f62c6d7 30 #define AUTO_MOUSE AUTOEVT(CLASS_HID,1,2)
jksoft 0:4f749f62c6d7 31
jksoft 0:4f749f62c6d7 32 u8 auto_mouse[4]; // buttons,dx,dy,scroll
jksoft 0:4f749f62c6d7 33 u8 auto_keyboard[8]; // modifiers,reserved,keycode1..keycode6
jksoft 0:4f749f62c6d7 34 u8 auto_joystick[4]; // x,y,buttons,throttle
jksoft 0:4f749f62c6d7 35
jksoft 0:4f749f62c6d7 36 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
jksoft 0:4f749f62c6d7 37 {
jksoft 0:4f749f62c6d7 38 int evt = (int)userData;
jksoft 0:4f749f62c6d7 39 switch (evt)
jksoft 0:4f749f62c6d7 40 {
jksoft 0:4f749f62c6d7 41 case AUTO_KEYBOARD:
jksoft 0:4f749f62c6d7 42 printf("AUTO_KEYBOARD ");
jksoft 0:4f749f62c6d7 43 break;
jksoft 0:4f749f62c6d7 44 case AUTO_MOUSE:
jksoft 0:4f749f62c6d7 45 printf("AUTO_MOUSE ");
jksoft 0:4f749f62c6d7 46 break;
jksoft 0:4f749f62c6d7 47 default:
jksoft 0:4f749f62c6d7 48 printf("HUH ");
jksoft 0:4f749f62c6d7 49 }
jksoft 0:4f749f62c6d7 50 printfBytes("data",data,len);
jksoft 0:4f749f62c6d7 51 USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData);
jksoft 0:4f749f62c6d7 52 }
jksoft 0:4f749f62c6d7 53
jksoft 0:4f749f62c6d7 54 // Establish transfers for interrupt events
jksoft 0:4f749f62c6d7 55 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed)
jksoft 0:4f749f62c6d7 56 {
jksoft 0:4f749f62c6d7 57 if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80))
jksoft 0:4f749f62c6d7 58 return;
jksoft 0:4f749f62c6d7 59
jksoft 0:4f749f62c6d7 60 // Make automatic interrupt enpoints for known devices
jksoft 0:4f749f62c6d7 61 u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
jksoft 0:4f749f62c6d7 62 u8* dst = 0;
jksoft 0:4f749f62c6d7 63 int len;
jksoft 0:4f749f62c6d7 64 switch (evt)
jksoft 0:4f749f62c6d7 65 {
jksoft 0:4f749f62c6d7 66 case AUTO_MOUSE:
jksoft 0:4f749f62c6d7 67 dst = auto_mouse;
jksoft 0:4f749f62c6d7 68 len = sizeof(auto_mouse);
jksoft 0:4f749f62c6d7 69 break;
jksoft 0:4f749f62c6d7 70 case AUTO_KEYBOARD:
jksoft 0:4f749f62c6d7 71 dst = auto_keyboard;
jksoft 0:4f749f62c6d7 72 len = sizeof(auto_keyboard);
jksoft 0:4f749f62c6d7 73 break;
jksoft 0:4f749f62c6d7 74 default:
jksoft 0:4f749f62c6d7 75 printf("Interrupt endpoint %02X %08X\n",ed->bEndpointAddress,evt);
jksoft 0:4f749f62c6d7 76 break;
jksoft 0:4f749f62c6d7 77 }
jksoft 0:4f749f62c6d7 78 if (dst)
jksoft 0:4f749f62c6d7 79 {
jksoft 0:4f749f62c6d7 80 printf("Auto Event for %02X %08X\n",ed->bEndpointAddress,evt);
jksoft 0:4f749f62c6d7 81 USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt);
jksoft 0:4f749f62c6d7 82 }
jksoft 0:4f749f62c6d7 83 }
jksoft 0:4f749f62c6d7 84
jksoft 0:4f749f62c6d7 85 void PrintString(int device, int i)
jksoft 0:4f749f62c6d7 86 {
jksoft 0:4f749f62c6d7 87 u8 buffer[256];
jksoft 0:4f749f62c6d7 88 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255);
jksoft 0:4f749f62c6d7 89 if (le < 0)
jksoft 0:4f749f62c6d7 90 return;
jksoft 0:4f749f62c6d7 91 char* dst = (char*)buffer;
jksoft 0:4f749f62c6d7 92 for (int j = 2; j < le; j += 2)
jksoft 0:4f749f62c6d7 93 *dst++ = buffer[j];
jksoft 0:4f749f62c6d7 94 *dst = 0;
jksoft 0:4f749f62c6d7 95 printf("%d:%s\n",i,(const char*)buffer);
jksoft 0:4f749f62c6d7 96 }
jksoft 0:4f749f62c6d7 97
jksoft 0:4f749f62c6d7 98 // Walk descriptors and create endpoints for a given device
jksoft 0:4f749f62c6d7 99 int StartAutoEvent(int device, int configuration, int interfaceNumber)
jksoft 0:4f749f62c6d7 100 {
jksoft 0:4f749f62c6d7 101 u8 buffer[255];
jksoft 0:4f749f62c6d7 102 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
jksoft 0:4f749f62c6d7 103 if (err < 0)
jksoft 0:4f749f62c6d7 104 return err;
jksoft 0:4f749f62c6d7 105
jksoft 0:4f749f62c6d7 106 int len = buffer[2] | (buffer[3] << 8);
jksoft 0:4f749f62c6d7 107 u8* d = buffer;
jksoft 0:4f749f62c6d7 108 u8* end = d + len;
jksoft 0:4f749f62c6d7 109 while (d < end)
jksoft 0:4f749f62c6d7 110 {
jksoft 0:4f749f62c6d7 111 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
jksoft 0:4f749f62c6d7 112 {
jksoft 0:4f749f62c6d7 113 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
jksoft 0:4f749f62c6d7 114 if (id->bInterfaceNumber == interfaceNumber)
jksoft 0:4f749f62c6d7 115 {
jksoft 0:4f749f62c6d7 116 d += d[0];
jksoft 0:4f749f62c6d7 117 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
jksoft 0:4f749f62c6d7 118 {
jksoft 0:4f749f62c6d7 119 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
jksoft 0:4f749f62c6d7 120 AddAutoEvent(device,id,(EndpointDescriptor*)d);
jksoft 0:4f749f62c6d7 121 d += d[0];
jksoft 0:4f749f62c6d7 122 }
jksoft 0:4f749f62c6d7 123 }
jksoft 0:4f749f62c6d7 124 }
jksoft 0:4f749f62c6d7 125 d += d[0];
jksoft 0:4f749f62c6d7 126 }
jksoft 0:4f749f62c6d7 127 return 0;
jksoft 0:4f749f62c6d7 128 }
jksoft 0:4f749f62c6d7 129
jksoft 0:4f749f62c6d7 130 // Implemented in main.cpp
jksoft 0:4f749f62c6d7 131 int OnDiskInsert(int device);
jksoft 0:4f749f62c6d7 132
jksoft 0:4f749f62c6d7 133 // Implemented in TestShell.cpp
jksoft 0:4f749f62c6d7 134 int OnBluetoothInsert(int device);
jksoft 0:4f749f62c6d7 135
jksoft 0:4f749f62c6d7 136 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
jksoft 0:4f749f62c6d7 137 {
jksoft 0:4f749f62c6d7 138 printf("LoadDevice %d %02X:%02X:%02X\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
jksoft 0:4f749f62c6d7 139 char s[128];
jksoft 0:4f749f62c6d7 140 for (int i = 1; i < 3; i++)
jksoft 0:4f749f62c6d7 141 {
jksoft 0:4f749f62c6d7 142 if (GetString(device,i,s,sizeof(s)) < 0)
jksoft 0:4f749f62c6d7 143 break;
jksoft 0:4f749f62c6d7 144 printf("%d: %s\n",i,s);
jksoft 0:4f749f62c6d7 145 }
jksoft 0:4f749f62c6d7 146
jksoft 0:4f749f62c6d7 147 switch (interfaceDesc->bInterfaceClass)
jksoft 0:4f749f62c6d7 148 {
jksoft 0:4f749f62c6d7 149 case CLASS_MASS_STORAGE:
jksoft 0:4f749f62c6d7 150 if (interfaceDesc->bInterfaceSubClass == 0x06 && interfaceDesc->bInterfaceProtocol == 0x50)
jksoft 0:4f749f62c6d7 151 OnDiskInsert(device); // it's SCSI!
jksoft 0:4f749f62c6d7 152 break;
jksoft 0:4f749f62c6d7 153 case CLASS_WIRELESS_CONTROLLER:
jksoft 0:4f749f62c6d7 154 if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
jksoft 0:4f749f62c6d7 155 OnBluetoothInsert(device); // it's bluetooth!
jksoft 0:4f749f62c6d7 156 break;
jksoft 0:4f749f62c6d7 157 default:
jksoft 0:4f749f62c6d7 158 StartAutoEvent(device,1,0);
jksoft 0:4f749f62c6d7 159 break;
jksoft 0:4f749f62c6d7 160 }
jksoft 0:4f749f62c6d7 161 }