A compilation of code from different sources to provide support for a Playstation 3 controller via bluetooth on the m3pi.

Dependencies:   TextLCD mbed

Fork of mbed_TANK_PS3 by Yasuhiko YAMAMOTO

Committer:
srsmitherman
Date:
Tue Jan 01 02:10:08 2013 +0000
Revision:
2:895f70862eb9
Parent:
1:ae49669c5e92
M3pi support

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenbumono 0:44619612f575 1 /*
kenbumono 0:44619612f575 2 Copyright (c) 2010 Peter Barrett
kenbumono 0:44619612f575 3
kenbumono 0:44619612f575 4 Permission is hereby granted, free of charge, to any person obtaining a copy
kenbumono 0:44619612f575 5 of this software and associated documentation files (the "Software"), to deal
kenbumono 0:44619612f575 6 in the Software without restriction, including without limitation the rights
kenbumono 0:44619612f575 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
kenbumono 0:44619612f575 8 copies of the Software, and to permit persons to whom the Software is
kenbumono 0:44619612f575 9 furnished to do so, subject to the following conditions:
kenbumono 0:44619612f575 10
kenbumono 0:44619612f575 11 The above copyright notice and this permission notice shall be included in
kenbumono 0:44619612f575 12 all copies or substantial portions of the Software.
kenbumono 0:44619612f575 13
kenbumono 0:44619612f575 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
kenbumono 0:44619612f575 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
kenbumono 0:44619612f575 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
kenbumono 0:44619612f575 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
kenbumono 0:44619612f575 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenbumono 0:44619612f575 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
kenbumono 0:44619612f575 20 THE SOFTWARE.
kenbumono 0:44619612f575 21 */
kenbumono 0:44619612f575 22
kenbumono 0:44619612f575 23 /*
kenbumono 0:44619612f575 24 Tue Apr 26 2011 Bart Janssens: added PS3 USB support
kenbumono 0:44619612f575 25 */
kenbumono 0:44619612f575 26
kenbumono 0:44619612f575 27 #include <stdio.h>
kenbumono 0:44619612f575 28 #include <stdlib.h>
kenbumono 0:44619612f575 29 #include <stdio.h>
kenbumono 0:44619612f575 30 #include <string.h>
kenbumono 0:44619612f575 31
kenbumono 0:44619612f575 32 #include "USBHost.h"
kenbumono 0:44619612f575 33 #include "Utils.h"
srsmitherman 1:ae49669c5e92 34 #include "ps3BT.h"
kenbumono 0:44619612f575 35
kenbumono 0:44619612f575 36 #define AUTOEVT(_class,_subclass,_protocol) (((_class) << 16) | ((_subclass) << 8) | _protocol)
kenbumono 0:44619612f575 37
kenbumono 0:44619612f575 38 void AutoEventCallback(int device, int endpoint, int status, u8* data, int len, void* userData)
kenbumono 0:44619612f575 39 {
kenbumono 0:44619612f575 40 USBInterruptTransfer(device,endpoint,data,len,AutoEventCallback,userData);
kenbumono 0:44619612f575 41 }
kenbumono 0:44619612f575 42
kenbumono 0:44619612f575 43 // Establish transfers for interrupt events
kenbumono 0:44619612f575 44 void AddAutoEvent(int device, InterfaceDescriptor* id, EndpointDescriptor* ed)
kenbumono 0:44619612f575 45 {
srsmitherman 1:ae49669c5e92 46 //printf("message from endpoint %02X\r\n",ed->bEndpointAddress);
srsmitherman 1:ae49669c5e92 47 //printf("Class Sub Proto: %02X %02X %02X\r\n",id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
kenbumono 0:44619612f575 48 //if ((ed->bmAttributes & 3) != ENDPOINT_INTERRUPT || !(ed->bEndpointAddress & 0x80))
kenbumono 0:44619612f575 49 // return;
kenbumono 0:44619612f575 50
kenbumono 0:44619612f575 51 // Make automatic interrupt enpoints for known devices
kenbumono 0:44619612f575 52 u32 evt = AUTOEVT(id->bInterfaceClass,id->bInterfaceSubClass,id->bInterfaceProtocol);
srsmitherman 1:ae49669c5e92 53 //printf("Evt: %08X \r\n",evt);
kenbumono 0:44619612f575 54 u8* dst = 0;
kenbumono 0:44619612f575 55 int len;
kenbumono 0:44619612f575 56 switch (evt)
kenbumono 0:44619612f575 57 {
kenbumono 0:44619612f575 58 default:
kenbumono 0:44619612f575 59 printf("Interrupt endpoint %02X %08X\r\n",ed->bEndpointAddress,evt);
kenbumono 0:44619612f575 60 break;
kenbumono 0:44619612f575 61 }
kenbumono 0:44619612f575 62 if (dst)
kenbumono 0:44619612f575 63 {
kenbumono 0:44619612f575 64 printf("Auto Event for %02X %08X\r\n",ed->bEndpointAddress,evt);
kenbumono 0:44619612f575 65 USBInterruptTransfer(device,ed->bEndpointAddress,dst,len,AutoEventCallback,(void*)evt);
kenbumono 0:44619612f575 66 }
kenbumono 0:44619612f575 67 }
kenbumono 0:44619612f575 68
kenbumono 0:44619612f575 69 void PrintString(int device, int i)
kenbumono 0:44619612f575 70 {
kenbumono 0:44619612f575 71 u8 buffer[256];
kenbumono 0:44619612f575 72 int le = GetDescriptor(device,DESCRIPTOR_TYPE_STRING,i,buffer,255);
kenbumono 0:44619612f575 73 if (le < 0)
kenbumono 0:44619612f575 74 return;
kenbumono 0:44619612f575 75 char* dst = (char*)buffer;
kenbumono 0:44619612f575 76 for (int j = 2; j < le; j += 2)
kenbumono 0:44619612f575 77 *dst++ = buffer[j];
kenbumono 0:44619612f575 78 *dst = 0;
srsmitherman 1:ae49669c5e92 79 printf("%d:%s\n",i,(const char*)buffer);
kenbumono 0:44619612f575 80 }
kenbumono 0:44619612f575 81
kenbumono 0:44619612f575 82 // Walk descriptors and create endpoints for a given device
kenbumono 0:44619612f575 83 int StartAutoEvent(int device, int configuration, int interfaceNumber)
kenbumono 0:44619612f575 84 {
kenbumono 0:44619612f575 85
kenbumono 0:44619612f575 86 printf("StartAutoEvent \r\n");
kenbumono 0:44619612f575 87
kenbumono 0:44619612f575 88 u8 buffer[255];
kenbumono 0:44619612f575 89 int err = GetDescriptor(device,DESCRIPTOR_TYPE_CONFIGURATION,0,buffer,255);
kenbumono 0:44619612f575 90 if (err < 0)
kenbumono 0:44619612f575 91 return err;
kenbumono 0:44619612f575 92
kenbumono 0:44619612f575 93 int len = buffer[2] | (buffer[3] << 8);
kenbumono 0:44619612f575 94 u8* d = buffer;
kenbumono 0:44619612f575 95 u8* end = d + len;
kenbumono 0:44619612f575 96 while (d < end)
kenbumono 0:44619612f575 97 {
kenbumono 0:44619612f575 98 if (d[1] == DESCRIPTOR_TYPE_INTERFACE)
kenbumono 0:44619612f575 99 {
kenbumono 0:44619612f575 100 InterfaceDescriptor* id = (InterfaceDescriptor*)d;
kenbumono 0:44619612f575 101 if (id->bInterfaceNumber == interfaceNumber)
kenbumono 0:44619612f575 102 {
kenbumono 0:44619612f575 103 d += d[0];
kenbumono 0:44619612f575 104 while (d < end && d[1] != DESCRIPTOR_TYPE_INTERFACE)
kenbumono 0:44619612f575 105 {
kenbumono 0:44619612f575 106 if (d[1] == DESCRIPTOR_TYPE_ENDPOINT)
kenbumono 0:44619612f575 107 AddAutoEvent(device,id,(EndpointDescriptor*)d);
kenbumono 0:44619612f575 108 d += d[0];
kenbumono 0:44619612f575 109 }
kenbumono 0:44619612f575 110 }
kenbumono 0:44619612f575 111 }
kenbumono 0:44619612f575 112 d += d[0];
kenbumono 0:44619612f575 113 }
kenbumono 0:44619612f575 114 return 0;
kenbumono 0:44619612f575 115 }
kenbumono 0:44619612f575 116
srsmitherman 1:ae49669c5e92 117 // Implemented in BTRuntime.cpp
kenbumono 0:44619612f575 118 int OnBluetoothInsert(int device);
kenbumono 0:44619612f575 119
kenbumono 0:44619612f575 120 void OnLoadDevice(int device, DeviceDescriptor* deviceDesc, InterfaceDescriptor* interfaceDesc)
kenbumono 0:44619612f575 121 {
kenbumono 0:44619612f575 122 printf("LoadDevice %d %02X:%02X:%02X\r\n",device,interfaceDesc->bInterfaceClass,interfaceDesc->bInterfaceSubClass,interfaceDesc->bInterfaceProtocol);
kenbumono 0:44619612f575 123 char s[128];
kenbumono 0:44619612f575 124
kenbumono 0:44619612f575 125 for (int i = 1; i < 3; i++)
kenbumono 0:44619612f575 126 {
kenbumono 0:44619612f575 127 if (GetString(device,i,s,sizeof(s)) < 0)
kenbumono 0:44619612f575 128 break;
kenbumono 0:44619612f575 129 printf("%d: %s\r\n",i,s);
kenbumono 0:44619612f575 130 }
kenbumono 0:44619612f575 131
kenbumono 0:44619612f575 132 switch (interfaceDesc->bInterfaceClass)
kenbumono 0:44619612f575 133 {
kenbumono 0:44619612f575 134 case CLASS_WIRELESS_CONTROLLER:
kenbumono 0:44619612f575 135 if (interfaceDesc->bInterfaceSubClass == 0x01 && interfaceDesc->bInterfaceProtocol == 0x01)
kenbumono 0:44619612f575 136 OnBluetoothInsert(device); // it's bluetooth!
kenbumono 0:44619612f575 137 break;
kenbumono 0:44619612f575 138 default:
srsmitherman 1:ae49669c5e92 139 StartAutoEvent(device,1,0);
kenbumono 0:44619612f575 140 break;
kenbumono 0:44619612f575 141 }
kenbumono 0:44619612f575 142 }