主導機 mbed用のプログラムです 改良しました

Dependencies:   mbed

Fork of F3RC_syudou_master by 日記

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Ps3USB.cpp Source File

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 #include "User.h"
00032 
00033 #include "mbed.h"
00034 
00035 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
00036 #define PS3EVT AUTOEVT(CLASS_HID,0,0)
00037 #define byteswap(x) ((x >> 8) | (x << 8))
00038 
00039 u8 ps3_data[48];
00040 
00041 Ps3USB::Ps3USB(int device, int configuration, int interfaceNumber)
00042 {
00043         printf("Creating new sixaxis \r\n");
00044         _device = device;
00045         _configuration = configuration;
00046         _interfaceNumber = interfaceNumber;
00047         printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber);
00048         int result;
00049         int err;
00050         
00051         _count = 1;
00052         
00053         u8 abuffer[48] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00054                           0x00, 0x02, 0xff, 0x27, 0x10, 0x00, 0x32, 0xff,
00055                           0x27, 0x10, 0x00, 0x32, 0xff, 0x27, 0x10, 0x00,
00056                           0x32, 0xff, 0x27, 0x10, 0x00, 0x32, 0x00, 0x00,
00057                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
00058                           0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
00059         memcpy(ledrumble,abuffer,48);
00060         
00061         EndpointDescriptor* ep;
00062                 
00063         u8 buffer[255];
00064         err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
00065         if (err < 0)
00066             printf("Failed to get descriptor\r\n");
00067             
00068 
00069     int len = buffer[2] | (buffer[3] << 8);
00070     u8* d = buffer;
00071     u8* end = d + len;
00072     while (d < end)
00073     {
00074         if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
00075         {
00076             InterfaceDescriptor* id = (InterfaceDescriptor*)d;
00077             if (id->bInterfaceNumber == _interfaceNumber)
00078             {
00079                  d += d[0];
00080                 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
00081                 {
00082                     if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
00083                         ep = (EndpointDescriptor*)d;
00084                         
00085                         if (ep->bEndpointAddress == 0x02)  {
00086                             printf("PS3 input endpoint (0x02) found\r\n");
00087                             input_ep = 0x02;
00088                             
00089                         }
00090                         if (ep->bEndpointAddress == 0x81)  {
00091                             printf("PS3 output endpoint (0x81) found\r\n");
00092                             output_ep = 0x81;
00093                             //AddAutoEvent(device,id,(EndpointDescriptor*)d);
00094                         }
00095                     d += d[0];
00096                 }
00097             }
00098         }
00099         d += d[0];
00100     }
00101     
00102     }
00103     
00104     int Ps3USB::Enable()
00105     {
00106         int err;
00107         
00108         u8 enable[4] = {0x42,0x0c,0x00,0x00};
00109 
00110         
00111         err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f4,0, enable, sizeof(enable), 0, 0 );
00112         //printf("set report result = %d\r\n", err);
00113         _count ++;
00114         if (_count == 25) _count = 1;
00115         
00116         err = USBInterruptTransfer(_device,output_ep,ps3_data,sizeof(ps3_data),PS3EventCallback,this);
00117         wait_ms(4);
00118         
00119         return 0;
00120     
00121     }
00122     
00123     
00124    
00125     
00126     int Ps3USB::SetPair(u8* bdAddr)
00127     {
00128         int err;
00129         
00130         u8 buf[8];
00131         u8 buf2[6];
00132       
00133         memcpy(buf2,bdAddr,6);
00134       
00135         buf[0] = 0x01;
00136         buf[1] = 0x00;
00137         buf[2] = buf2[0];
00138         buf[3] = buf2[1];
00139         buf[4] = buf2[2];
00140         buf[5] = buf2[3];
00141         buf[6] = buf2[4];
00142         buf[7] = buf2[5];
00143       
00144         //set Mac address    
00145         err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f5, 0, buf, sizeof(buf), 0, 0 );
00146         wait_ms(4);
00147         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);
00148 
00149         return 0;
00150     }
00151     
00152     int Ps3USB::ShowPair()
00153     {
00154         int err;
00155         
00156         u8 buf[8];
00157         //get Mac address    
00158         err = USBControlTransfer(_device, DEVICE_TO_HOST|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf, sizeof(buf), 0, 0 );
00159         wait_ms(4);
00160         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);
00161 
00162         
00163         return 0;
00164     }
00165     
00166     int Ps3USB::Led(int i)
00167     {
00168         int err;
00169         u8 ledpattern[7] = {0x02, 0x04, 0x08, 0x10, 0x12, 0x14, 0x18 };
00170         u8 buf[48];
00171 
00172         if (i < 7) ledrumble[9] = ledpattern[i];
00173         memcpy(buf, ledrumble, 48);
00174         
00175         err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x0201,0, buf, sizeof(buf), 0, 0 );
00176         wait_ms(4);
00177       
00178         return 0;
00179     }
00180     
00181     // left and right: duration and power, both from 0 to 255
00182     int Ps3USB::Rumble(u8 duration_right, u8 power_right, u8 duration_left, u8 power_left)
00183     {
00184         int err;
00185         u8 buf[48];
00186         
00187         memcpy(buf, ledrumble, 48);
00188         buf[1] = duration_right;
00189         buf[2] = power_right;
00190         buf[3] = duration_left;
00191         buf[4] = power_left;
00192          
00193         err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x0201,0, buf, sizeof(buf), 0, 0 );
00194         wait_ms(4);
00195         
00196         return 0;
00197     }
00198 
00199 
00200     void PS3EventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
00201     {
00202         Ps3USB* t = (Ps3USB*)userData;
00203 
00204         t->_count ++;
00205         if (t->_count == 25) t->_count = 1;
00206         
00207         ParsePs3Result(data, sizeof(ps3report),t->_count);
00208         USBInterruptTransfer(device, endpoint , data, len, PS3EventCallback, userData);
00209         wait_ms(4);
00210 
00211     }
00212     
00213     int ParsePs3Result(const u8* data,int len,int count)
00214     {
00215         ps3report* _ps3report = (ps3report*)data;    
00216 /*      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");
00217         printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %4d %4d %4d %4d \r\n",
00218                             _ps3report->LeftStickX,
00219                             _ps3report->LeftStickY,
00220                             _ps3report->RightStickX,
00221                             _ps3report->RightStickY,
00222                             _ps3report->PressureUp,
00223                             _ps3report->PressureRight,
00224                             _ps3report->PressureDown,
00225                             _ps3report->PressureLeft,
00226                             _ps3report->PressureL2,
00227                             _ps3report->PressureR2,
00228                             _ps3report->PressureL1,
00229                             _ps3report->PressureR1,
00230                             _ps3report->PressureTriangle,
00231                             _ps3report->PressureCircle,
00232                             _ps3report->PressureCross,
00233                             _ps3report->PressureSquare,
00234                             (_ps3report->AccelX),
00235                             (_ps3report->AccelY),
00236                             (_ps3report->AccelZ),
00237                             (_ps3report->GyroZ));
00238  */             
00239         UserLoop(0,data);
00240         //printfBytes("data",data,len);
00241     }
00242 
00243