21 #define DEBUG // Uncomment to print data for debugging
51 Notify(PSTR(
"\r\nXBOXRECV Init"));
56 Notify(PSTR(
"\r\nAddress in use"));
66 Notify(PSTR(
"\r\nAddress not found"));
73 Notify(PSTR(
"\r\nepinfo is null"));
98 goto FailUnknownDevice;
101 Notify(PSTR(
"\r\nYou'll need a wireless receiver for this libary to work"));
103 goto FailUnknownDevice;
122 Notify(PSTR(
"\r\nsetAddr: "));
124 PrintHex<uint8_t>(rcode);
128 Notify(PSTR(
"\r\nAddr: "));
143 goto FailSetDevTblEntry;
204 goto FailSetDevTblEntry;
213 Notify(PSTR(
"\r\nXbox Wireless Receiver Connected\r\n"));
222 Notify(PSTR(
"\r\ngetDevDescr:"));
227 Notify(PSTR(
"\r\nsetDevTblEn:"));
232 Notify(PSTR(
"\r\nsetConf:"));
237 Notify(PSTR(
"\r\nUnknown Device Connected - VID: "));
238 PrintHex<uint16_t>(VID);
240 PrintHex<uint16_t>(PID);
246 Notify(PSTR(
"\r\nXbox 360 Init Failed, error code: "));
247 Serial.print(rcode,HEX);
256 for(uint8_t i=0;i<4;i++)
266 if(!timer || ((millis() - timer) > 3000)) {
272 for(uint8_t i=0;i<4;i++) {
283 Notify(PSTR(
"Bytes Received: "));
284 Serial.print(bufferSize);
289 printReport(i,bufferSize);
296 void XBOXRECV::readReport(uint8_t controller) {
303 Notify(PSTR(
"Controller "));
304 Serial.print(controller);
310 case 0x80: str = PSTR(
" as controller\r\n");
break;
311 case 0x40: str = PSTR(
" as headset\r\n");
break;
312 case 0xC0: str = PSTR(
" as controller+headset\r\n");
break;
314 Notify(PSTR(
": connected"));
318 switch (controller) {
319 case 0: led =
LED1;
break;
320 case 1: led =
LED2;
break;
321 case 2: led =
LED3;
break;
322 case 3: led =
LED4;
break;
328 Notify(PSTR(
": disconnected\r\n"));
333 if(readBuf[1] == 0x00 && readBuf[3] & 0x13 && readBuf[4] >= 0x22) {
334 controllerStatus[controller] = ((uint16_t)readBuf[3] << 8) | readBuf[4];
337 if(readBuf[1] != 0x01)
344 ButtonState[controller] = (uint32_t)(readBuf[9] | ((uint16_t)readBuf[8] << 8) | ((uint32_t)readBuf[7] << 16) | ((uint32_t)readBuf[6] << 24));
346 hatValue[controller][
LeftHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]);
347 hatValue[controller][
LeftHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]);
348 hatValue[controller][
RightHatX] = (int16_t)(((uint16_t)readBuf[15] << 8) | readBuf[14]);
349 hatValue[controller][
RightHatY] = (int16_t)(((uint16_t)readBuf[17] << 8) | readBuf[16]);
354 if(ButtonState[controller] != OldButtonState[controller]) {
355 buttonStateChanged[controller] =
true;
356 ButtonClickState[controller] = (ButtonState[controller] >> 16) & ((~OldButtonState[controller]) >> 16);
357 if(((uint8_t)OldButtonState[controller]) == 0 && ((uint8_t)ButtonState[controller]) != 0)
358 R2Clicked[controller] =
true;
359 if((uint8_t)(OldButtonState[controller] >> 8) == 0 && (uint8_t)(ButtonState[controller] >> 8) != 0)
360 L2Clicked[controller] =
true;
361 OldButtonState[controller] = ButtonState[controller];
365 void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) {
369 Notify(PSTR(
"Controller "));
370 Serial.print(controller);
372 for(uint8_t i = 0; i < nBytes;i++) {
373 PrintHex<uint8_t>(readBuf[i]);
381 return (uint8_t)(ButtonState[controller] >> 8);
383 return (uint8_t)ButtonState[controller];
384 return (ButtonState[controller] & ((uint32_t)pgm_read_word(&BUTTONS[(uint8_t)b]) << 16));
388 if(L2Clicked[controller]) {
389 L2Clicked[controller] =
false;
395 if(R2Clicked[controller]) {
396 R2Clicked[controller] =
false;
401 uint16_t button = pgm_read_word(&BUTTONS[(uint8_t)b]);
402 bool click = (ButtonClickState[controller] & button);
403 ButtonClickState[controller] &= ~button;
407 return hatValue[controller][a];
410 bool state = buttonStateChanged[controller];
411 buttonStateChanged[controller] =
false;
434 uint8_t batteryLevel = ((controllerStatus[controller] & 0x00C0) >> 6) * 33;
435 if(batteryLevel == 99)
440 void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) {
443 switch (controller) {
452 Notify(PSTR(
"Error sending Xbox message\r\n"));
459 writeBuf[3] = value | 0x40;
461 XboxCommand(controller, writeBuf, 4);
465 setLedRaw(controller,(pgm_read_byte(&LEDS[(uint8_t)led]))+4);
468 setLedRaw(controller,pgm_read_byte(&LEDS[(uint8_t)led]));
477 void XBOXRECV::checkStatus() {
485 for(uint8_t i=0; i<4; i++) {
486 XboxCommand(i, writeBuf, 4);
493 for(uint8_t i=0; i<4; i++) {
495 XboxCommand(i, writeBuf, 4);
505 writeBuf[5] = lValue;
506 writeBuf[6] = rValue;
508 XboxCommand(controller, writeBuf, 7);