21 #define DEBUG // Uncomment to print data for debugging
51 Notify(PSTR(
"\r\nXBOXRECV Init"), 0x80);
56 Notify(PSTR(
"\r\nAddress in use"), 0x80);
66 Notify(PSTR(
"\r\nAddress not found"), 0x80);
73 Notify(PSTR(
"\r\nepinfo is null"), 0x80);
98 goto FailUnknownDevice;
101 Notify(PSTR(
"\r\nYou'll need a wireless receiver for this libary to work"), 0x80);
103 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;
204 goto FailSetDevTblEntry;
210 goto FailSetConfDescr;
213 Notify(PSTR(
"\r\nXbox Wireless Receiver Connected\r\n"), 0x80);
238 Notify(PSTR(
"\r\nXbox 360 Init Failed, error code: "), 0x80);
248 for (uint8_t i = 0; i < 4; i++)
259 if (!timer || ((millis() - timer) > 3000)) {
265 for (uint8_t i = 0; i < 4; i++) {
278 if (bufferSize > 0) {
280 Notify(PSTR(
"Bytes Received: "), 0x80);
281 PrintHex<uint16_t > (bufferSize, 0x80);
282 Notify(PSTR(
"\r\n"), 0x80);
286 printReport(i, bufferSize);
293 void XBOXRECV::readReport(uint8_t controller) {
300 Notify(PSTR(
"Controller "), 0x80);
306 switch (readBuf[1]) {
307 case 0x80: str = PSTR(
" as controller\r\n");
309 case 0x40: str = PSTR(
" as headset\r\n");
311 case 0xC0: str = PSTR(
" as controller+headset\r\n");
314 Notify(PSTR(
": connected"), 0x80);
318 switch (controller) {
332 Notify(PSTR(
": disconnected\r\n"), 0x80);
337 if (readBuf[1] == 0x00 && readBuf[3] & 0x13 && readBuf[4] >= 0x22) {
338 controllerStatus[controller] = ((uint16_t)readBuf[3] << 8) | readBuf[4];
341 if (readBuf[1] != 0x01)
348 ButtonState[controller] = (uint32_t)(readBuf[9] | ((uint16_t)readBuf[8] << 8) | ((uint32_t)readBuf[7] << 16) | ((uint32_t)readBuf[6] << 24));
350 hatValue[controller][
LeftHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]);
351 hatValue[controller][
LeftHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]);
352 hatValue[controller][
RightHatX] = (int16_t)(((uint16_t)readBuf[15] << 8) | readBuf[14]);
353 hatValue[controller][
RightHatY] = (int16_t)(((uint16_t)readBuf[17] << 8) | readBuf[16]);
358 if (ButtonState[controller] != OldButtonState[controller]) {
359 buttonStateChanged[controller] =
true;
360 ButtonClickState[controller] = (ButtonState[controller] >> 16) & ((~OldButtonState[controller]) >> 16);
361 if (((uint8_t)OldButtonState[controller]) == 0 && ((uint8_t)ButtonState[controller]) != 0)
362 R2Clicked[controller] =
true;
363 if ((uint8_t)(OldButtonState[controller] >> 8) == 0 && (uint8_t)(ButtonState[controller] >> 8) != 0)
364 L2Clicked[controller] =
true;
365 OldButtonState[controller] = ButtonState[controller];
369 void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) {
373 Notify(PSTR(
"Controller "), 0x80);
376 for (uint8_t i = 0; i < nBytes; i++) {
377 PrintHex<uint8_t > (readBuf[i], 0x80);
380 Notify(PSTR(
"\r\n"), 0x80);
386 return (uint8_t)(ButtonState[controller] >> 8);
388 return (uint8_t)ButtonState[controller];
389 return (
bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&
XBOXBUTTONS[(uint8_t)b]) << 16));
394 if (L2Clicked[controller]) {
395 L2Clicked[controller] =
false;
399 }
else if (b ==
R2) {
400 if (R2Clicked[controller]) {
401 R2Clicked[controller] =
false;
406 uint16_t button = pgm_read_word(&
XBOXBUTTONS[(uint8_t)b]);
407 bool click = (ButtonClickState[controller] & button);
408 ButtonClickState[controller] &= ~button;
413 return hatValue[controller][a];
417 bool state = buttonStateChanged[controller];
418 buttonStateChanged[controller] =
false;
442 return ((controllerStatus[controller] & 0x00C0) >> 6);
445 void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) {
448 switch (controller) {
461 Notify(PSTR(
"Error sending Xbox message\r\n"), 0x80);
469 writeBuf[3] = value | 0x40;
471 XboxCommand(controller, writeBuf, 4);
491 void XBOXRECV::checkStatus() {
499 for (uint8_t i = 0; i < 4; i++) {
500 XboxCommand(i, writeBuf, 4);
507 for (uint8_t i = 0; i < 4; i++) {
509 XboxCommand(i, writeBuf, 4);
519 writeBuf[5] = lValue;
520 writeBuf[6] = rValue;
522 XboxCommand(controller, writeBuf, 7);