19 #define DEBUG // Uncomment to print data for debugging
23 const uint8_t PS3_REPORT_BUFFER[]
PROGMEM = {
24 0x00, 0x00, 0x00, 0x00, 0x00,
25 0x00, 0x00, 0x00, 0x00, 0x00,
26 0xff, 0x27, 0x10, 0x00, 0x32,
27 0xff, 0x27, 0x10, 0x00, 0x32,
28 0xff, 0x27, 0x10, 0x00, 0x32,
29 0xff, 0x27, 0x10, 0x00, 0x32,
30 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
34 const uint8_t MOVE_REPORT_BUFFER[]
PROGMEM = {
41 PS3USB::PS3USB(
USB *p, uint8_t btadr5, uint8_t btadr4, uint8_t btadr3, uint8_t btadr2, uint8_t btadr1, uint8_t btadr0):
56 my_bdaddr[5] = btadr5;
57 my_bdaddr[4] = btadr4;
58 my_bdaddr[3] = btadr3;
59 my_bdaddr[2] = btadr2;
60 my_bdaddr[1] = btadr1;
61 my_bdaddr[0] = btadr0;
64 uint8_t
PS3USB::Init(uint8_t parent, uint8_t port,
bool lowspeed) {
75 Notify(PSTR(
"\r\nPS3USB Init"));
80 Notify(PSTR(
"\r\nAddress in use"));
90 Notify(PSTR(
"\r\nAddress not found"));
97 Notify(PSTR(
"\r\nepinfo is null"));
116 goto FailGetDevDescr;
122 goto FailUnknownDevice;
140 Notify(PSTR(
"\r\nsetAddr: "));
142 PrintHex<uint8_t>(rcode);
146 Notify(PSTR(
"\r\nAddr: "));
161 goto FailSetDevTblEntry;
184 goto FailSetDevTblEntry;
195 Notify(PSTR(
"\r\nDualshock 3 Controller Connected"));
200 Notify(PSTR(
"\r\nNavigation Controller Connected"));
211 writeBuf[i] = pgm_read_byte(&PS3_REPORT_BUFFER[i]);
213 for (uint8_t i = 6; i < 10; i++)
218 Notify(PSTR(
"\r\nMotion Controller Connected"));
226 writeBuf[i] = pgm_read_byte(&MOVE_REPORT_BUFFER[i]);
237 Notify(PSTR(
"\r\ngetDevDescr:"));
242 Notify(PSTR(
"\r\nsetDevTblEn:"));
247 Notify(PSTR(
"\r\nsetConf:"));
252 Notify(PSTR(
"\r\nUnknown Device Connected - VID: "));
253 PrintHex<uint16_t>(VID);
255 PrintHex<uint16_t>(PID);
261 Notify(PSTR(
"\r\nPS3 Init Failed, error code: "));
262 Serial.print(rcode,HEX);
285 if(millis() - timer > 100) {
293 if (millis() - timer > 4000)
302 void PS3USB::readReport() {
306 ButtonState = (uint32_t)(readBuf[2] | ((uint16_t)readBuf[3] << 8) | ((uint32_t)readBuf[4] << 16));
311 if(ButtonState != OldButtonState) {
312 ButtonClickState = ButtonState & ~OldButtonState;
313 OldButtonState = ButtonState;
317 void PS3USB::printReport() {
322 PrintHex<uint8_t>(readBuf[i]);
330 return (ButtonState & pgm_read_dword(&BUTTONS[(uint8_t)b]));
333 uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]);
334 bool click = (ButtonClickState & button);
335 ButtonClickState &= ~button;
341 return (uint8_t)(readBuf[(pgm_read_byte(&ANALOGBUTTONS[(uint8_t)a]))-9]);
346 return (uint8_t)(readBuf[((uint8_t)a+6)]);
351 return ((readBuf[((uint16_t)a)-9] << 8) | readBuf[((uint16_t)a + 1)-9]);
360 const double zeroG = 511.5;
369 double angle = (atan2(accYval,accZval)+PI)*RAD_TO_DEG;
372 double angle = (atan2(accXval,accZval)+PI)*RAD_TO_DEG;
381 if (readBuf[((uint16_t)c >> 8)-9] == ((uint8_t)c & 0xff))
387 char statusOutput[100];
389 strcpy(statusOutput,
"ConnectionStatus: ");
393 else strcat(statusOutput,
"Error");
396 strcat(statusOutput,
" - PowerRating: ");
405 else strcat(statusOutput,
"Error");
407 strcat(statusOutput,
" - WirelessStatus: ");
410 else if (
getStatus(
Cable)) strcat(statusOutput,
"Cable - Rumble is off");
413 else strcat(statusOutput,
"Error");
420 void PS3USB::PS3_Command(uint8_t* data, uint16_t nbytes) {
422 pUsb->
ctrlReq(
bAddress,
epInfo[
PS3_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0x01, 0x02, 0x00, nbytes, nbytes, data, NULL);
426 writeBuf[i] = pgm_read_byte(&PS3_REPORT_BUFFER[i]);
428 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
436 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
445 if ((mode & 0x30) > 0) {
456 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
460 writeBuf[9] &= ~((uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1));
461 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
464 writeBuf[9] |= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1);
465 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
468 writeBuf[9] ^= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1);
469 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
476 for (uint8_t i = 0; i < 6; i++)
477 buf[i+2] = BDADDR[5 - i];
480 pUsb->
ctrlReq(
bAddress,
epInfo[
PS3_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
482 Notify(PSTR(
"\r\nBluetooth Address was set to: "));
483 for(int8_t i = 5; i > 0; i--) {
484 PrintHex<uint8_t>(my_bdaddr[i]);
487 PrintHex<uint8_t>(my_bdaddr[0]);
491 void PS3USB::enable_sixaxis() {
499 pUsb->
ctrlReq(
bAddress,
epInfo[
PS3_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0xF4, 0x03, 0x00, 4, 4, cmd_buf, NULL);
503 void PS3USB::Move_Command(uint8_t* data, uint16_t nbytes) {
516 moveSetBulb((uint8_t)(color >> 16),(uint8_t)(color >> 8),(uint8_t)(color));
520 if(rumble < 64 && rumble != 0)
521 Notify(PSTR(
"\r\nThe rumble value has to at least 64, or approximately 25%"));
524 writeBuf[6] = rumble;
537 for (uint8_t i = 0; i < 6; i++)
538 buf[i + 1] = BDADDR[i];
541 pUsb->
ctrlReq(
bAddress,
epInfo[
PS3_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00,11,11, buf, NULL);
543 Notify(PSTR(
"\r\nBluetooth Address was set to: "));
544 for(int8_t i = 5; i > 0; i--) {
545 PrintHex<uint8_t>(my_bdaddr[i]);
548 PrintHex<uint8_t>(my_bdaddr[0]);