Simon Ford / Mbed 2 deprecated WiiRacing

Dependencies:   mbed m3pi ID12RFIDIRQ

Committer:
simon
Date:
Mon Apr 11 23:03:22 2011 +0000
Revision:
0:e2ef12467862

        

Who changed what in which revision?

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