3/5
Dependencies: mbed
Ps3USB.cpp@1:530908de68c6, 2019-03-05 (annotated)
- Committer:
- yuki0701
- Date:
- Tue Mar 05 04:30:35 2019 +0000
- Revision:
- 1:530908de68c6
- Parent:
- 0:a56be39653d0
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
la00noix | 0:a56be39653d0 | 1 | /* |
la00noix | 0:a56be39653d0 | 2 | Copyright (c) 2011 Bart Janssens |
la00noix | 0:a56be39653d0 | 3 | |
la00noix | 0:a56be39653d0 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
la00noix | 0:a56be39653d0 | 5 | of this software and associated documentation files (the "Software"), to deal |
la00noix | 0:a56be39653d0 | 6 | in the Software without restriction, including without limitation the rights |
la00noix | 0:a56be39653d0 | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
la00noix | 0:a56be39653d0 | 8 | copies of the Software, and to permit persons to whom the Software is |
la00noix | 0:a56be39653d0 | 9 | furnished to do so, subject to the following conditions: |
la00noix | 0:a56be39653d0 | 10 | |
la00noix | 0:a56be39653d0 | 11 | The above copyright notice and this permission notice shall be included in |
la00noix | 0:a56be39653d0 | 12 | all copies or substantial portions of the Software. |
la00noix | 0:a56be39653d0 | 13 | |
la00noix | 0:a56be39653d0 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
la00noix | 0:a56be39653d0 | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
la00noix | 0:a56be39653d0 | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
la00noix | 0:a56be39653d0 | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
la00noix | 0:a56be39653d0 | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
la00noix | 0:a56be39653d0 | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
la00noix | 0:a56be39653d0 | 20 | THE SOFTWARE. |
la00noix | 0:a56be39653d0 | 21 | */ |
la00noix | 0:a56be39653d0 | 22 | |
la00noix | 0:a56be39653d0 | 23 | #include <stdio.h> |
la00noix | 0:a56be39653d0 | 24 | #include <stdlib.h> |
la00noix | 0:a56be39653d0 | 25 | #include <string.h> |
la00noix | 0:a56be39653d0 | 26 | |
la00noix | 0:a56be39653d0 | 27 | #include "USBHost.h" |
la00noix | 0:a56be39653d0 | 28 | #include "hci.h" |
la00noix | 0:a56be39653d0 | 29 | #include "Utils.h" |
la00noix | 0:a56be39653d0 | 30 | #include "ps3.h" |
la00noix | 0:a56be39653d0 | 31 | #include "User.h" |
la00noix | 0:a56be39653d0 | 32 | |
la00noix | 0:a56be39653d0 | 33 | #include "mbed.h" |
la00noix | 0:a56be39653d0 | 34 | |
la00noix | 0:a56be39653d0 | 35 | #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol) |
la00noix | 0:a56be39653d0 | 36 | #define PS3EVT AUTOEVT(CLASS_HID,0,0) |
la00noix | 0:a56be39653d0 | 37 | #define byteswap(x) ((x >> 8) | (x << 8)) |
la00noix | 0:a56be39653d0 | 38 | |
la00noix | 0:a56be39653d0 | 39 | u8 ps3_data[48]; |
la00noix | 0:a56be39653d0 | 40 | |
la00noix | 0:a56be39653d0 | 41 | Ps3USB::Ps3USB(int device, int configuration, int interfaceNumber) |
la00noix | 0:a56be39653d0 | 42 | { |
la00noix | 0:a56be39653d0 | 43 | printf("Creating new sixaxis \r\n"); |
la00noix | 0:a56be39653d0 | 44 | _device = device; |
la00noix | 0:a56be39653d0 | 45 | _configuration = configuration; |
la00noix | 0:a56be39653d0 | 46 | _interfaceNumber = interfaceNumber; |
la00noix | 0:a56be39653d0 | 47 | printf("device = %d configuration = %d interfaceNumber = %d\r\n", device, configuration, interfaceNumber); |
la00noix | 0:a56be39653d0 | 48 | int result; |
la00noix | 0:a56be39653d0 | 49 | int err; |
la00noix | 0:a56be39653d0 | 50 | |
la00noix | 0:a56be39653d0 | 51 | _count = 1; |
la00noix | 0:a56be39653d0 | 52 | |
la00noix | 0:a56be39653d0 | 53 | u8 abuffer[48] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
la00noix | 0:a56be39653d0 | 54 | 0x00, 0x02, 0xff, 0x27, 0x10, 0x00, 0x32, 0xff, |
la00noix | 0:a56be39653d0 | 55 | 0x27, 0x10, 0x00, 0x32, 0xff, 0x27, 0x10, 0x00, |
la00noix | 0:a56be39653d0 | 56 | 0x32, 0xff, 0x27, 0x10, 0x00, 0x32, 0x00, 0x00, |
la00noix | 0:a56be39653d0 | 57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
la00noix | 0:a56be39653d0 | 58 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
la00noix | 0:a56be39653d0 | 59 | memcpy(ledrumble,abuffer,48); |
la00noix | 0:a56be39653d0 | 60 | |
la00noix | 0:a56be39653d0 | 61 | EndpointDescriptor* ep; |
la00noix | 0:a56be39653d0 | 62 | |
la00noix | 0:a56be39653d0 | 63 | u8 buffer[255]; |
la00noix | 0:a56be39653d0 | 64 | err = GetDescriptor(_device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255); |
la00noix | 0:a56be39653d0 | 65 | if (err < 0) |
la00noix | 0:a56be39653d0 | 66 | printf("Failed to get descriptor\r\n"); |
la00noix | 0:a56be39653d0 | 67 | |
la00noix | 0:a56be39653d0 | 68 | |
la00noix | 0:a56be39653d0 | 69 | int len = buffer[2] | (buffer[3] << 8); |
la00noix | 0:a56be39653d0 | 70 | u8* d = buffer; |
la00noix | 0:a56be39653d0 | 71 | u8* end = d + len; |
la00noix | 0:a56be39653d0 | 72 | while (d < end) |
la00noix | 0:a56be39653d0 | 73 | { |
la00noix | 0:a56be39653d0 | 74 | if (d[1] == DESCRIPTOR_TYPE_INTERFACE) |
la00noix | 0:a56be39653d0 | 75 | { |
la00noix | 0:a56be39653d0 | 76 | InterfaceDescriptor* id = (InterfaceDescriptor*)d; |
la00noix | 0:a56be39653d0 | 77 | if (id->bInterfaceNumber == _interfaceNumber) |
la00noix | 0:a56be39653d0 | 78 | { |
la00noix | 0:a56be39653d0 | 79 | d += d[0]; |
la00noix | 0:a56be39653d0 | 80 | while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE) |
la00noix | 0:a56be39653d0 | 81 | { |
la00noix | 0:a56be39653d0 | 82 | if (d[1] == DESCRIPTOR_TYPE_ENDPOINT) |
la00noix | 0:a56be39653d0 | 83 | ep = (EndpointDescriptor*)d; |
la00noix | 0:a56be39653d0 | 84 | |
la00noix | 0:a56be39653d0 | 85 | if (ep->bEndpointAddress == 0x02) { |
la00noix | 0:a56be39653d0 | 86 | printf("PS3 input endpoint (0x02) found\r\n"); |
la00noix | 0:a56be39653d0 | 87 | input_ep = 0x02; |
la00noix | 0:a56be39653d0 | 88 | |
la00noix | 0:a56be39653d0 | 89 | } |
la00noix | 0:a56be39653d0 | 90 | if (ep->bEndpointAddress == 0x81) { |
la00noix | 0:a56be39653d0 | 91 | printf("PS3 output endpoint (0x81) found\r\n"); |
la00noix | 0:a56be39653d0 | 92 | output_ep = 0x81; |
la00noix | 0:a56be39653d0 | 93 | //AddAutoEvent(device,id,(EndpointDescriptor*)d); |
la00noix | 0:a56be39653d0 | 94 | } |
la00noix | 0:a56be39653d0 | 95 | d += d[0]; |
la00noix | 0:a56be39653d0 | 96 | } |
la00noix | 0:a56be39653d0 | 97 | } |
la00noix | 0:a56be39653d0 | 98 | } |
la00noix | 0:a56be39653d0 | 99 | d += d[0]; |
la00noix | 0:a56be39653d0 | 100 | } |
la00noix | 0:a56be39653d0 | 101 | |
la00noix | 0:a56be39653d0 | 102 | } |
la00noix | 0:a56be39653d0 | 103 | |
la00noix | 0:a56be39653d0 | 104 | int Ps3USB::Enable() |
la00noix | 0:a56be39653d0 | 105 | { |
la00noix | 0:a56be39653d0 | 106 | int err; |
la00noix | 0:a56be39653d0 | 107 | |
la00noix | 0:a56be39653d0 | 108 | u8 enable[4] = {0x42,0x0c,0x00,0x00}; |
la00noix | 0:a56be39653d0 | 109 | |
la00noix | 0:a56be39653d0 | 110 | |
la00noix | 0:a56be39653d0 | 111 | err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f4,0, enable, sizeof(enable), 0, 0 ); |
la00noix | 0:a56be39653d0 | 112 | //printf("set report result = %d\r\n", err); |
la00noix | 0:a56be39653d0 | 113 | _count ++; |
la00noix | 0:a56be39653d0 | 114 | if (_count == 25) _count = 1; |
la00noix | 0:a56be39653d0 | 115 | |
la00noix | 0:a56be39653d0 | 116 | err = USBInterruptTransfer(_device,output_ep,ps3_data,sizeof(ps3_data),PS3EventCallback,this); |
la00noix | 0:a56be39653d0 | 117 | wait_ms(4); |
la00noix | 0:a56be39653d0 | 118 | |
la00noix | 0:a56be39653d0 | 119 | return 0; |
la00noix | 0:a56be39653d0 | 120 | |
la00noix | 0:a56be39653d0 | 121 | } |
la00noix | 0:a56be39653d0 | 122 | |
la00noix | 0:a56be39653d0 | 123 | |
la00noix | 0:a56be39653d0 | 124 | |
la00noix | 0:a56be39653d0 | 125 | |
la00noix | 0:a56be39653d0 | 126 | int Ps3USB::SetPair(u8* bdAddr) |
la00noix | 0:a56be39653d0 | 127 | { |
la00noix | 0:a56be39653d0 | 128 | int err; |
la00noix | 0:a56be39653d0 | 129 | |
la00noix | 0:a56be39653d0 | 130 | u8 buf[8]; |
la00noix | 0:a56be39653d0 | 131 | u8 buf2[6]; |
la00noix | 0:a56be39653d0 | 132 | |
la00noix | 0:a56be39653d0 | 133 | memcpy(buf2,bdAddr,6); |
la00noix | 0:a56be39653d0 | 134 | |
la00noix | 0:a56be39653d0 | 135 | buf[0] = 0x01; |
la00noix | 0:a56be39653d0 | 136 | buf[1] = 0x00; |
la00noix | 0:a56be39653d0 | 137 | buf[2] = buf2[0]; |
la00noix | 0:a56be39653d0 | 138 | buf[3] = buf2[1]; |
la00noix | 0:a56be39653d0 | 139 | buf[4] = buf2[2]; |
la00noix | 0:a56be39653d0 | 140 | buf[5] = buf2[3]; |
la00noix | 0:a56be39653d0 | 141 | buf[6] = buf2[4]; |
la00noix | 0:a56be39653d0 | 142 | buf[7] = buf2[5]; |
la00noix | 0:a56be39653d0 | 143 | |
la00noix | 0:a56be39653d0 | 144 | //set Mac address |
la00noix | 0:a56be39653d0 | 145 | err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x03f5, 0, buf, sizeof(buf), 0, 0 ); |
la00noix | 0:a56be39653d0 | 146 | wait_ms(4); |
la00noix | 0:a56be39653d0 | 147 | 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); |
la00noix | 0:a56be39653d0 | 148 | |
la00noix | 0:a56be39653d0 | 149 | return 0; |
la00noix | 0:a56be39653d0 | 150 | } |
la00noix | 0:a56be39653d0 | 151 | |
la00noix | 0:a56be39653d0 | 152 | int Ps3USB::ShowPair() |
la00noix | 0:a56be39653d0 | 153 | { |
la00noix | 0:a56be39653d0 | 154 | int err; |
la00noix | 0:a56be39653d0 | 155 | |
la00noix | 0:a56be39653d0 | 156 | u8 buf[8]; |
la00noix | 0:a56be39653d0 | 157 | //get Mac address |
la00noix | 0:a56be39653d0 | 158 | err = USBControlTransfer(_device, DEVICE_TO_HOST|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_GET_REPORT, 0x03f5, 0, buf, sizeof(buf), 0, 0 ); |
la00noix | 0:a56be39653d0 | 159 | wait_ms(4); |
la00noix | 0:a56be39653d0 | 160 | 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); |
la00noix | 0:a56be39653d0 | 161 | |
la00noix | 0:a56be39653d0 | 162 | |
la00noix | 0:a56be39653d0 | 163 | return 0; |
la00noix | 0:a56be39653d0 | 164 | } |
la00noix | 0:a56be39653d0 | 165 | |
la00noix | 0:a56be39653d0 | 166 | int Ps3USB::Led(int i) |
la00noix | 0:a56be39653d0 | 167 | { |
la00noix | 0:a56be39653d0 | 168 | int err; |
la00noix | 0:a56be39653d0 | 169 | u8 ledpattern[7] = {0x02, 0x04, 0x08, 0x10, 0x12, 0x14, 0x18 }; |
la00noix | 0:a56be39653d0 | 170 | u8 buf[48]; |
la00noix | 0:a56be39653d0 | 171 | |
la00noix | 0:a56be39653d0 | 172 | if (i < 7) ledrumble[9] = ledpattern[i]; |
la00noix | 0:a56be39653d0 | 173 | memcpy(buf, ledrumble, 48); |
la00noix | 0:a56be39653d0 | 174 | |
la00noix | 0:a56be39653d0 | 175 | err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x0201,0, buf, sizeof(buf), 0, 0 ); |
la00noix | 0:a56be39653d0 | 176 | wait_ms(4); |
la00noix | 0:a56be39653d0 | 177 | |
la00noix | 0:a56be39653d0 | 178 | return 0; |
la00noix | 0:a56be39653d0 | 179 | } |
la00noix | 0:a56be39653d0 | 180 | |
la00noix | 0:a56be39653d0 | 181 | // left and right: duration and power, both from 0 to 255 |
la00noix | 0:a56be39653d0 | 182 | int Ps3USB::Rumble(u8 duration_right, u8 power_right, u8 duration_left, u8 power_left) |
la00noix | 0:a56be39653d0 | 183 | { |
la00noix | 0:a56be39653d0 | 184 | int err; |
la00noix | 0:a56be39653d0 | 185 | u8 buf[48]; |
la00noix | 0:a56be39653d0 | 186 | |
la00noix | 0:a56be39653d0 | 187 | memcpy(buf, ledrumble, 48); |
la00noix | 0:a56be39653d0 | 188 | buf[1] = duration_right; |
la00noix | 0:a56be39653d0 | 189 | buf[2] = power_right; |
la00noix | 0:a56be39653d0 | 190 | buf[3] = duration_left; |
la00noix | 0:a56be39653d0 | 191 | buf[4] = power_left; |
la00noix | 0:a56be39653d0 | 192 | |
la00noix | 0:a56be39653d0 | 193 | err = USBControlTransfer(_device, HOST_TO_DEVICE|REQUEST_TYPE_CLASS|RECIPIENT_INTERFACE, HID_REQUEST_SET_REPORT, 0x0201,0, buf, sizeof(buf), 0, 0 ); |
la00noix | 0:a56be39653d0 | 194 | wait_ms(4); |
la00noix | 0:a56be39653d0 | 195 | |
la00noix | 0:a56be39653d0 | 196 | return 0; |
la00noix | 0:a56be39653d0 | 197 | } |
la00noix | 0:a56be39653d0 | 198 | |
la00noix | 0:a56be39653d0 | 199 | |
la00noix | 0:a56be39653d0 | 200 | void PS3EventCallback(int device, int endpoint, int status, u8* data, int len, void* userData) |
la00noix | 0:a56be39653d0 | 201 | { |
la00noix | 0:a56be39653d0 | 202 | Ps3USB* t = (Ps3USB*)userData; |
la00noix | 0:a56be39653d0 | 203 | |
la00noix | 0:a56be39653d0 | 204 | t->_count ++; |
la00noix | 0:a56be39653d0 | 205 | if (t->_count == 25) t->_count = 1; |
la00noix | 0:a56be39653d0 | 206 | |
la00noix | 0:a56be39653d0 | 207 | ParsePs3Result(data, sizeof(ps3report),t->_count); |
la00noix | 0:a56be39653d0 | 208 | USBInterruptTransfer(device, endpoint , data, len, PS3EventCallback, userData); |
la00noix | 0:a56be39653d0 | 209 | wait_ms(4); |
la00noix | 0:a56be39653d0 | 210 | |
la00noix | 0:a56be39653d0 | 211 | } |
la00noix | 0:a56be39653d0 | 212 | |
la00noix | 0:a56be39653d0 | 213 | int ParsePs3Result(const u8* data,int len,int count) |
la00noix | 0:a56be39653d0 | 214 | { |
la00noix | 0:a56be39653d0 | 215 | ps3report* _ps3report = (ps3report*)data; |
la00noix | 0:a56be39653d0 | 216 | /* 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"); |
la00noix | 0:a56be39653d0 | 217 | printf("%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %4d %4d %4d %4d \r\n", |
la00noix | 0:a56be39653d0 | 218 | _ps3report->LeftStickX, |
la00noix | 0:a56be39653d0 | 219 | _ps3report->LeftStickY, |
la00noix | 0:a56be39653d0 | 220 | _ps3report->RightStickX, |
la00noix | 0:a56be39653d0 | 221 | _ps3report->RightStickY, |
la00noix | 0:a56be39653d0 | 222 | _ps3report->PressureUp, |
la00noix | 0:a56be39653d0 | 223 | _ps3report->PressureRight, |
la00noix | 0:a56be39653d0 | 224 | _ps3report->PressureDown, |
la00noix | 0:a56be39653d0 | 225 | _ps3report->PressureLeft, |
la00noix | 0:a56be39653d0 | 226 | _ps3report->PressureL2, |
la00noix | 0:a56be39653d0 | 227 | _ps3report->PressureR2, |
la00noix | 0:a56be39653d0 | 228 | _ps3report->PressureL1, |
la00noix | 0:a56be39653d0 | 229 | _ps3report->PressureR1, |
la00noix | 0:a56be39653d0 | 230 | _ps3report->PressureTriangle, |
la00noix | 0:a56be39653d0 | 231 | _ps3report->PressureCircle, |
la00noix | 0:a56be39653d0 | 232 | _ps3report->PressureCross, |
la00noix | 0:a56be39653d0 | 233 | _ps3report->PressureSquare, |
la00noix | 0:a56be39653d0 | 234 | (_ps3report->AccelX), |
la00noix | 0:a56be39653d0 | 235 | (_ps3report->AccelY), |
la00noix | 0:a56be39653d0 | 236 | (_ps3report->AccelZ), |
la00noix | 0:a56be39653d0 | 237 | (_ps3report->GyroZ)); |
la00noix | 0:a56be39653d0 | 238 | */ |
la00noix | 0:a56be39653d0 | 239 | UserLoop(0,data); |
la00noix | 0:a56be39653d0 | 240 | //printfBytes("data",data,len); |
la00noix | 0:a56be39653d0 | 241 | } |
la00noix | 0:a56be39653d0 | 242 | |
la00noix | 0:a56be39653d0 | 243 |