主導機 mbed用のプログラムです

Dependencies:   mbed

Fork of F3RC_syudou_master by F3RC1班

Committer:
yuto17320508
Date:
Wed Aug 23 10:10:26 2017 +0000
Revision:
19:a3f57c9833b6
Parent:
0:736c76a75def
a

Who changed what in which revision?

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