21 #define DEBUG // Uncomment to print data for debugging
32 const uint16_t BUTTONS[]
PROGMEM = {
82 Notify(PSTR(
"\r\nXBOXRECV Init"));
87 Notify(PSTR(
"\r\nAddress in use"));
97 Notify(PSTR(
"\r\nAddress not found"));
104 Notify(PSTR(
"\r\nepinfo is null"));
123 goto FailGetDevDescr;
129 goto FailUnknownDevice;
132 Notify(PSTR(
"\r\nYou'll need a wireless receiver for this libary to work"));
134 goto FailUnknownDevice;
153 Notify(PSTR(
"\r\nsetAddr: "));
155 PrintHex<uint8_t>(rcode);
159 Notify(PSTR(
"\r\nAddr: "));
174 goto FailSetDevTblEntry;
235 goto FailSetDevTblEntry;
244 Notify(PSTR(
"\r\nXbox Wireless Receiver Connected\r\n"));
253 Notify(PSTR(
"\r\ngetDevDescr:"));
258 Notify(PSTR(
"\r\nsetDevTblEn:"));
263 Notify(PSTR(
"\r\nsetConf:"));
268 Notify(PSTR(
"\r\nUnknown Device Connected - VID: "));
269 PrintHex<uint16_t>(VID);
271 PrintHex<uint16_t>(PID);
277 Notify(PSTR(
"\r\nXbox 360 Init Failed, error code: "));
278 Serial.print(rcode,HEX);
287 for(uint8_t i=0;i<4;i++)
297 if(!timer || ((millis() - timer) > 3000)) {
303 for(uint8_t i=0;i<4;i++) {
314 Notify(PSTR(
"Bytes Received: "));
315 Serial.print(bufferSize);
320 printReport(i,bufferSize);
327 void XBOXRECV::readReport(uint8_t controller) {
334 Notify(PSTR(
"Controller "));
335 Serial.print(controller);
341 case 0x80: str = PSTR(
" as controller\r\n");
break;
342 case 0x40: str = PSTR(
" as headset\r\n");
break;
343 case 0xC0: str = PSTR(
" as controller+headset\r\n");
break;
345 Notify(PSTR(
": connected"));
349 switch (controller) {
350 case 0: led =
LED1;
break;
351 case 1: led =
LED2;
break;
352 case 2: led =
LED3;
break;
353 case 3: led =
LED4;
break;
359 Notify(PSTR(
": disconnected\r\n"));
364 if(readBuf[1] == 0x00 && readBuf[3] & 0x13 && readBuf[4] >= 0x22) {
365 controllerStatus[controller] = ((uint16_t)readBuf[3] << 8) | readBuf[4];
368 if(readBuf[1] != 0x01)
375 ButtonState[controller] = (uint32_t)(readBuf[9] | ((uint16_t)readBuf[8] << 8) | ((uint32_t)readBuf[7] << 16) | ((uint32_t)readBuf[6] << 24));
377 hatValue[controller][
LeftHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]);
378 hatValue[controller][
LeftHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]);
379 hatValue[controller][
RightHatX] = (int16_t)(((uint16_t)readBuf[15] << 8) | readBuf[14]);
380 hatValue[controller][
RightHatY] = (int16_t)(((uint16_t)readBuf[17] << 8) | readBuf[16]);
385 if(ButtonState[controller] != OldButtonState[controller]) {
386 buttonStateChanged[controller] =
true;
387 ButtonClickState[controller] = (ButtonState[controller] >> 16) & ((~OldButtonState[controller]) >> 16);
388 if(((uint8_t)OldButtonState[controller]) == 0 && ((uint8_t)ButtonState[controller]) != 0)
389 R2Clicked[controller] =
true;
390 if((uint8_t)(OldButtonState[controller] >> 8) == 0 && (uint8_t)(ButtonState[controller] >> 8) != 0)
391 L2Clicked[controller] =
true;
392 OldButtonState[controller] = ButtonState[controller];
396 void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) {
400 Notify(PSTR(
"Controller "));
401 Serial.print(controller);
403 for(uint8_t i = 0; i < nBytes;i++) {
404 PrintHex<uint8_t>(readBuf[i]);
412 return (uint8_t)(ButtonState[controller] >> 8);
414 return (uint8_t)ButtonState[controller];
415 return (ButtonState[controller] & ((uint32_t)pgm_read_word(&BUTTONS[(uint8_t)b]) << 16));
419 if(L2Clicked[controller]) {
420 L2Clicked[controller] =
false;
426 if(R2Clicked[controller]) {
427 R2Clicked[controller] =
false;
432 uint16_t button = pgm_read_word(&BUTTONS[(uint8_t)b]);
433 bool click = (ButtonClickState[controller] & button);
434 ButtonClickState[controller] &= ~button;
438 return hatValue[controller][a];
441 bool state = buttonStateChanged[controller];
442 buttonStateChanged[controller] =
false;
465 uint8_t batteryLevel = ((controllerStatus[controller] & 0x00C0) >> 6) * 33;
466 if(batteryLevel == 99)
471 void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) {
474 switch (controller) {
483 Notify(PSTR(
"Error sending Xbox message\r\n"));
490 writeBuf[3] = value | 0x40;
492 XboxCommand(controller, writeBuf, 4);
496 setLedRaw(controller,(pgm_read_byte(&LEDS[(uint8_t)led]))+4);
499 setLedRaw(controller,pgm_read_byte(&LEDS[(uint8_t)led]));
508 void XBOXRECV::checkStatus() {
516 for(uint8_t i=0; i<4; i++) {
517 XboxCommand(i, writeBuf, 4);
524 for(uint8_t i=0; i<4; i++) {
526 XboxCommand(i, writeBuf, 4);
536 writeBuf[5] = lValue;
537 writeBuf[6] = rValue;
539 XboxCommand(controller, writeBuf, 7);