19 #define DEBUG // Uncomment to print data for debugging
23 PS3USB::PS3USB(
USB *p, uint8_t btadr5, uint8_t btadr4, uint8_t btadr3, uint8_t btadr2, uint8_t btadr1, uint8_t btadr0) :
38 my_bdaddr[5] = btadr5;
39 my_bdaddr[4] = btadr4;
40 my_bdaddr[3] = btadr3;
41 my_bdaddr[2] = btadr2;
42 my_bdaddr[1] = btadr1;
43 my_bdaddr[0] = btadr0;
46 uint8_t
PS3USB::Init(uint8_t parent, uint8_t port,
bool lowspeed) {
57 Notify(PSTR(
"\r\nPS3USB Init"), 0x80);
62 Notify(PSTR(
"\r\nAddress in use"), 0x80);
72 Notify(PSTR(
"\r\nAddress not found"), 0x80);
79 Notify(PSTR(
"\r\nepinfo is null"), 0x80);
104 goto FailUnknownDevice;
122 Notify(PSTR(
"\r\nsetAddr: "), 0x80);
124 PrintHex<uint8_t > (rcode, 0x80);
128 Notify(PSTR(
"\r\nAddr: "), 0x80);
129 PrintHex<uint8_t > (
bAddress, 0x80);
143 goto FailSetDevTblEntry;
166 goto FailSetDevTblEntry;
172 goto FailSetConfDescr;
177 Notify(PSTR(
"\r\nDualshock 3 Controller Connected"), 0x80);
182 Notify(PSTR(
"\r\nNavigation Controller Connected"), 0x80);
195 for (uint8_t i = 6; i < 10; i++)
199 Notify(PSTR(
"\r\nMotion Controller Connected"), 0x80);
209 Notify(PSTR(
"\r\nBluetooth Address was set to: "), 0x80);
210 for (int8_t i = 5; i > 0; i--) {
211 PrintHex<uint8_t > (my_bdaddr[i], 0x80);
214 PrintHex<uint8_t > (my_bdaddr[0], 0x80);
218 Notify(PSTR(
"\r\n"), 0x80);
240 Notify(PSTR(
"\r\nPS3 Init Failed, error code: "), 0x80);
265 if (millis() - timer > 100) {
272 if (millis() - timer > 4000)
281 void PS3USB::readReport() {
285 ButtonState = (uint32_t)(readBuf[2] | ((uint16_t)readBuf[3] << 8) | ((uint32_t)readBuf[4] << 16));
290 if (ButtonState != OldButtonState) {
291 ButtonClickState = ButtonState & ~OldButtonState;
292 OldButtonState = ButtonState;
296 void PS3USB::printReport() {
301 PrintHex<uint8_t > (readBuf[i], 0x80);
304 Notify(PSTR(
"\r\n"), 0x80);
309 return (ButtonState & pgm_read_dword(&
BUTTONS[(uint8_t)b]));
313 uint32_t button = pgm_read_dword(&
BUTTONS[(uint8_t)b]);
314 bool click = (ButtonClickState & button);
315 ButtonClickState &= ~button;
322 return (uint8_t)(readBuf[(pgm_read_byte(&
ANALOGBUTTONS[(uint8_t)a])) - 9]);
328 return (uint8_t)(readBuf[((uint8_t)a + 6)]);
334 return ((readBuf[((uint16_t)a) - 9] << 8) | readBuf[((uint16_t)a + 1) - 9]);
344 const double zeroG = 511.5;
353 double angle = (atan2(accYval, accZval) + PI) * RAD_TO_DEG;
356 double angle = (atan2(accXval, accZval) + PI) * RAD_TO_DEG;
366 if (readBuf[((uint16_t)c >> 8) - 9] == ((uint8_t)c & 0xff))
373 char statusOutput[100];
375 strcpy(statusOutput,
"ConnectionStatus: ");
379 else strcat(statusOutput,
"Error");
382 strcat(statusOutput,
" - PowerRating: ");
391 else strcat(statusOutput,
"Error");
393 strcat(statusOutput,
" - WirelessStatus: ");
396 else if (
getStatus(
Cable)) strcat(statusOutput,
"Cable - Rumble is off");
399 else strcat(statusOutput,
"Error");
407 void PS3USB::PS3_Command(uint8_t* data, uint16_t nbytes) {
409 pUsb->
ctrlReq(
bAddress,
epInfo[
PS3_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0x01, 0x02, 0x00, nbytes, nbytes, data, NULL);
416 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
425 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
429 if ((mode & 0x30) > 0x00) {
430 uint8_t power[2] = { 0xff, 0x00 };
439 void PS3USB::setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower) {
440 writeBuf[1] = rightDuration;
441 writeBuf[2] = rightPower;
442 writeBuf[3] = leftDuration;
443 writeBuf[4] = leftPower;
444 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
449 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
452 writeBuf[9] &= ~((uint8_t)((pgm_read_byte(&
LEDS[(uint8_t)a]) & 0x0f) << 1));
453 PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
456 writeBuf[9] |= (uint8_t)((pgm_read_byte(&
LEDS[(uint8_t)a]) & 0x0f) << 1);
457 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);
469 for (uint8_t i = 0; i < 6; i++)
470 buf[i + 2] = BDADDR[5 - i];
473 pUsb->
ctrlReq(
bAddress,
epInfo[
PS3_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0xF5, 0x03, 0x00, 8, 8, buf, NULL);
476 void PS3USB::enable_sixaxis() {
484 pUsb->
ctrlReq(
bAddress,
epInfo[
PS3_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0xF4, 0x03, 0x00, 4, 4, cmd_buf, NULL);
488 void PS3USB::Move_Command(uint8_t* data, uint16_t nbytes) {
502 moveSetBulb((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color));
507 if (rumble < 64 && rumble != 0)
508 Notify(PSTR(
"\r\nThe rumble value has to at least 64, or approximately 25%"), 0x80);
511 writeBuf[6] = rumble;
525 for (uint8_t i = 0; i < 6; i++)
526 buf[i + 1] = BDADDR[i];
529 pUsb->
ctrlReq(
bAddress,
epInfo[
PS3_CONTROL_PIPE].epAddr,
bmREQ_HID_OUT,
HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);