BlackOneとAndroidの連携デモプログラム AndroidAccessoryを改造してBlackOneとAndroidが連携できるようにしました。 サポートしているのは、デモアプリの ”Buttons” B1-SW1, B2-SW2, B3-SW3 ”LED2” RGB-LED のみです。 LCDに表示するイメージをマイクロSDカードに入れてLCDのソケットに挿入しておく必要があります。 イメージは、320X240ドットで”\Image”という名前のフォルダの直下に”10.jpg”という名前で入れてください。

Dependencies:   TextLCD mbed

Committer:
techand
Date:
Fri Dec 23 04:33:33 2011 +0000
Revision:
0:7b556109fd46

        

Who changed what in which revision?

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