BlueUSBをつかってみた
Dependencies: FatFileSystem mbed
Fork of PS3_BlueUSB by
Ps3USB.cpp
00001 /* 00002 Copyright (c) 2011 Bart Janssens 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 */ 00022 00023 #include <stdio.h> 00024 #include <stdlib.h> 00025 #include <string.h> 00026 00027 #include "USBHost.h" 00028 #include "hci.h" 00029 #include "Utils.h" 00030 #include "ps3.h" 00031 00032 #include "mbed.h" 00033 00034 00035 00036 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol) 00037 #define PS3EVT AUTOEVT(CLASS_HID,0,0) 00038 #define byteswap(x) ((x >> 8) | (x << 8)) 00039 00040 u8 ps3_data[48]; 00041 00042 00043 Ps3USB::Ps3USB(int device, int configuration, int interfaceNumber) 00044 { 00045 printf("Creating new sixaxis \r\n"); 00046 _device = device; 00047 _configuration = configuration; 00048 _interfaceNumber = interfaceNumber; 00049 printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); 00050 int result; 00051 int err; 00052 00053 _count = 1; 00054 00055 u8 abuffer[48] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00056 0x00, 0x02, 0xff, 0x27, 0x10, 0x00, 0x32, 0xff, 00057 0x27, 0x10, 0x00, 0x32, 0xff, 0x27, 0x10, 0x00, 00058 0x32, 0xff, 0x27, 0x10, 0x00, 0x32, 0x00, 0x00, 00059 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 00060 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 00061 memcpy(ledrumble,abuffer,48); 00062 00063 EndpointDescriptor* ep; 00064 00065 u8 buffer[255]; 00066 err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); 00067 if (err < 0) 00068 printf("Failed to get descriptor\r\n"); 00069 00070 00071 int len = buffer[2] | (buffer[3] << 8); 00072 u8* d = buffer; 00073 u8* end = d + len; 00074 while (d < end) 00075 { 00076 if (d[1] == DESCRIPTOR_TYPE_INTERFACE) 00077 { 00078 InterfaceDescriptor* id = (InterfaceDescriptor*)d; 00079 if (id->bInterfaceNumber == _interfaceNumber) 00080 { 00081 d += d[0]; 00082 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) 00083 { 00084 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) 00085 ep = (EndpointDescriptor*)d; 00086 00087 if (ep->bEndpointAddress == 0x02) { 00088 printf("PS3 input endpoint (0x02) found\r\n"); 00089 input_ep = 0x02; 00090 00091 } 00092 if (ep->bEndpointAddress == 0x81) { 00093 printf("PS3 output endpoint (0x81) found\r\n"); 00094 output_ep = 0x81; 00095 //AddAutoEvent(device,id,(EndpointDescriptor*)d); 00096 } 00097 d += d[0]; 00098 } 00099 } 00100 } 00101 d += d[0]; 00102 } 00103 00104 } 00105 00106 int Ps3USB::Enable() 00107 { 00108 int err; 00109 00110 u8 enable[4] = {0x42,0x0c,0x00,0x00}; 00111 00112 00113 err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f4,0, enable, sizeof(enable), 0, 0 ); 00114 //printf("set report result = %d\r\n", err); 00115 _count ++; 00116 if (_count == 25) _count = 1; 00117 00118 err = USBInterruptTransfer(_device,output_ep,ps3_data,sizeof(ps3_data),PS3EventCallback,this); 00119 wait_ms(0.1); 00120 00121 return 0; 00122 00123 } 00124 00125 00126 00127 00128 int Ps3USB::SetPair(u8* bdAddr) 00129 { 00130 int err; 00131 00132 u8 buf[8]; 00133 u8 buf2[6]; 00134 00135 memcpy(buf2,bdAddr,6); 00136 00137 buf[0] = 0x01; 00138 buf[1] = 0x00; 00139 buf[2] = buf2[0]; 00140 buf[3] = buf2[1]; 00141 buf[4] = buf2[2]; 00142 buf[5] = buf2[3]; 00143 buf[6] = buf2[4]; 00144 buf[7] = buf2[5]; 00145 00146 //set Mac address 00147 err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f5, 0, buf, sizeof(buf), 0, 0 ); 00148 wait_ms(0.1); 00149 printf("set Mac address to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n", buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], err); 00150 00151 return 0; 00152 } 00153 00154 int Ps3USB::ShowPair() 00155 { 00156 int err; 00157 00158 u8 buf[8]; 00159 //get Mac address 00160 err = USBControlTransfer(_device, DEVICE_TO_HOST|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf, sizeof(buf), 0, 0 ); 00161 wait_ms(0.1); 00162 printf("Mac address is set to %02X:%02X:%02X:%02X:%02X:%02X , result = %d\r\n",buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], err); 00163 00164 00165 return 0; 00166 } 00167 00168 int Ps3USB::Led(int i) 00169 { 00170 int err; 00171 u8 ledpattern[7] = {0x02, 0x04, 0x08, 0x10, 0x12, 0x14, 0x18 }; 00172 u8 buf[48]; 00173 00174 if (i < 7) ledrumble[9] = ledpattern[i]; 00175 memcpy(buf, ledrumble, 48); 00176 00177 err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x0201,0, buf, sizeof(buf), 0, 0 ); 00178 wait_ms(0.1); 00179 00180 return 0; 00181 } 00182 00183 // left and right: duration and power, both from 0 to 255 00184 int Ps3USB::Rumble(u8 duration_right, u8 power_right, u8 duration_left, u8 power_left) 00185 { 00186 int err; 00187 u8 buf[48]; 00188 00189 memcpy(buf, ledrumble, 48); 00190 buf[1] = duration_right; 00191 buf[2] = power_right; 00192 buf[3] = duration_left; 00193 buf[4] = power_left; 00194 00195 err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x0201,0, buf, sizeof(buf), 0, 0 ); 00196 wait_ms(0.1); 00197 00198 return 0; 00199 } 00200 00201 00202 void PS3EventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) 00203 { 00204 Ps3USB* t = (Ps3USB*)userData; 00205 00206 t->_count ++; 00207 if (t->_count == 25) t->_count = 1; 00208 00209 ParsePs3Result(data, sizeof(ps3report),t->_count); 00210 USBInterruptTransfer(device, endpoint , data, len, PS3EventCallback, userData); 00211 wait_ms(0.1); 00212 00213 } 00214 00215 int ParsePs3Result(const u8* data,int len,int count) 00216 { 00217 ps3report* _ps3report = (ps3report*)data; 00218 if (count == 24) printf("LSX LSY RSX RSY UPA RPA DPA RPA L2 R2 L1 R1 TRI CIR CRO SQU ACX ACY ACZ GYZ \r\n"); 00219 printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %4d %4d %4d %4d \r\n", 00220 _ps3report->LeftStickX, 00221 _ps3report->LeftStickY, 00222 _ps3report->RightStickX, 00223 _ps3report->RightStickY, 00224 _ps3report->PressureUp, 00225 _ps3report->PressureRight, 00226 _ps3report->PressureDown, 00227 _ps3report->PressureLeft, 00228 _ps3report->PressureL2, 00229 _ps3report->PressureR2, 00230 _ps3report->PressureL1, 00231 _ps3report->PressureR1, 00232 _ps3report->PressureTriangle, 00233 _ps3report->PressureCircle, 00234 _ps3report->PressureCross, 00235 _ps3report->PressureSquare, 00236 (_ps3report->AccelX), 00237 (_ps3report->AccelY), 00238 (_ps3report->AccelZ), 00239 (_ps3report->GyroZ)); 00240 //printfBytes("data",data,len); 00241 } 00242 00243 00244 00245
Generated on Wed Jul 13 2022 12:19:22 by 1.7.2