#include "XBOXRECV.h"
XBOXRECV::XBOXRECV(USB *p) :
pUsb(p), bAddress(0), bPollEnable(false) { for(uint8_t i = 0; i < XBOX_MAX_ENDPOINTS; i++) {
epInfo[i].epAddr = 0;
epInfo[i].maxPktSize = (i) ? 0 : 8;
epInfo[i].epAttribs = 0;
epInfo[i].bmNakPower = (i) ? USB_NAK_NOWAIT : USB_NAK_MAX_POWER;
}
if(pUsb) pUsb->RegisterDeviceClass(this); }
uint8_t XBOXRECV::ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed) {
const uint8_t constBufSize = sizeof (USB_DEVICE_DESCRIPTOR);
uint8_t buf[constBufSize];
USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
uint8_t rcode;
UsbDevice *p = NULL;
EpInfo *oldep_ptr = NULL;
uint16_t PID, VID;
AddressPool &addrPool = pUsb->GetAddressPool(); #ifdef EXTRADEBUG
Notify(PSTR("\r\nXBOXRECV Init"), 0x80);
#endif
if(bAddress) { #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nAddress in use"), 0x80);
#endif
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
}
p = addrPool.GetUsbDevicePtr(0);
if(!p) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nAddress not found"), 0x80);
#endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
}
if(!p->epinfo) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nepinfo is null"), 0x80);
#endif
return USB_ERROR_EPINFO_IS_NULL;
}
oldep_ptr = p->epinfo; p->epinfo = epInfo; p->lowspeed = lowspeed;
rcode = pUsb->getDevDescr(0, 0, constBufSize, (uint8_t*)buf);
p->epinfo = oldep_ptr;
if(rcode)
goto FailGetDevDescr;
VID = udd->idVendor;
PID = udd->idProduct;
if((VID != XBOX_VID && VID != MADCATZ_VID && VID != JOYTECH_VID) || (PID != XBOX_WIRELESS_RECEIVER_PID && PID != XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID)) { #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nYou'll need a wireless receiver for this libary to work"), 0x80);
#endif
goto FailUnknownDevice;
}
bAddress = addrPool.AllocAddress(parent, false, port);
if(!bAddress) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nOut of address space"), 0x80);
#endif
return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
}
epInfo[0].maxPktSize = udd->bMaxPacketSize0;
delay(20);
return USB_ERROR_CONFIG_REQUIRES_ADDITIONAL_RESET;
FailGetDevDescr:
#ifdef DEBUG_USB_HOST
NotifyFailGetDevDescr(rcode);
#endif
if(rcode != hrJERR)
rcode = USB_ERROR_FailGetDevDescr;
goto Fail;
FailUnknownDevice:
#ifdef DEBUG_USB_HOST
NotifyFailUnknownDevice(VID, PID);
#endif
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
Fail:
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80);
NotifyFail(rcode);
#endif
Release();
return rcode;
};
uint8_t XBOXRECV::Init(uint8_t parent, uint8_t port, bool lowspeed) {
uint8_t rcode;
AddressPool &addrPool = pUsb->GetAddressPool();
#ifdef EXTRADEBUG
Notify(PSTR("\r\nBTD Init"), 0x80);
#endif
UsbDevice *p = addrPool.GetUsbDevicePtr(bAddress);
if(!p) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nAddress not found"), 0x80);
#endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
}
delay(300);
rcode = pUsb->setAddr(0, 0, bAddress); if(rcode) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nsetAddr: "), 0x80);
D_PrintHex<uint8_t > (rcode, 0x80);
#endif
p->lowspeed = false;
goto Fail;
}
#ifdef EXTRADEBUG
Notify(PSTR("\r\nAddr: "), 0x80);
D_PrintHex<uint8_t > (bAddress, 0x80);
#endif
p->lowspeed = false;
p = addrPool.GetUsbDevicePtr(bAddress); if(!p) {
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nAddress not found"), 0x80);
#endif
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
}
p->lowspeed = lowspeed;
rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo); if(rcode)
goto FailSetDevTblEntry;
epInfo[ XBOX_INPUT_PIPE_1 ].epAddr = 0x01; epInfo[ XBOX_INPUT_PIPE_1 ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
epInfo[ XBOX_INPUT_PIPE_1 ].bmNakPower = USB_NAK_NOWAIT; epInfo[ XBOX_INPUT_PIPE_1 ].maxPktSize = EP_MAXPKTSIZE;
epInfo[ XBOX_INPUT_PIPE_1 ].bmSndToggle = 0;
epInfo[ XBOX_INPUT_PIPE_1 ].bmRcvToggle = 0;
epInfo[ XBOX_OUTPUT_PIPE_1 ].epAddr = 0x01; epInfo[ XBOX_OUTPUT_PIPE_1 ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
epInfo[ XBOX_OUTPUT_PIPE_1 ].bmNakPower = USB_NAK_NOWAIT; epInfo[ XBOX_OUTPUT_PIPE_1 ].maxPktSize = EP_MAXPKTSIZE;
epInfo[ XBOX_OUTPUT_PIPE_1 ].bmSndToggle = 0;
epInfo[ XBOX_OUTPUT_PIPE_1 ].bmRcvToggle = 0;
epInfo[ XBOX_INPUT_PIPE_2 ].epAddr = 0x03; epInfo[ XBOX_INPUT_PIPE_2 ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
epInfo[ XBOX_INPUT_PIPE_2 ].bmNakPower = USB_NAK_NOWAIT; epInfo[ XBOX_INPUT_PIPE_2 ].maxPktSize = EP_MAXPKTSIZE;
epInfo[ XBOX_INPUT_PIPE_2 ].bmSndToggle = 0;
epInfo[ XBOX_INPUT_PIPE_2 ].bmRcvToggle = 0;
epInfo[ XBOX_OUTPUT_PIPE_2 ].epAddr = 0x03; epInfo[ XBOX_OUTPUT_PIPE_2 ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
epInfo[ XBOX_OUTPUT_PIPE_2 ].bmNakPower = USB_NAK_NOWAIT; epInfo[ XBOX_OUTPUT_PIPE_2 ].maxPktSize = EP_MAXPKTSIZE;
epInfo[ XBOX_OUTPUT_PIPE_2 ].bmSndToggle = 0;
epInfo[ XBOX_OUTPUT_PIPE_2 ].bmRcvToggle = 0;
epInfo[ XBOX_INPUT_PIPE_3 ].epAddr = 0x05; epInfo[ XBOX_INPUT_PIPE_3 ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
epInfo[ XBOX_INPUT_PIPE_3 ].bmNakPower = USB_NAK_NOWAIT; epInfo[ XBOX_INPUT_PIPE_3 ].maxPktSize = EP_MAXPKTSIZE;
epInfo[ XBOX_INPUT_PIPE_3 ].bmSndToggle = 0;
epInfo[ XBOX_INPUT_PIPE_3 ].bmRcvToggle = 0;
epInfo[ XBOX_OUTPUT_PIPE_3 ].epAddr = 0x05; epInfo[ XBOX_OUTPUT_PIPE_3 ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
epInfo[ XBOX_OUTPUT_PIPE_3 ].bmNakPower = USB_NAK_NOWAIT; epInfo[ XBOX_OUTPUT_PIPE_3 ].maxPktSize = EP_MAXPKTSIZE;
epInfo[ XBOX_OUTPUT_PIPE_3 ].bmSndToggle = 0;
epInfo[ XBOX_OUTPUT_PIPE_3 ].bmRcvToggle = 0;
epInfo[ XBOX_INPUT_PIPE_4 ].epAddr = 0x07; epInfo[ XBOX_INPUT_PIPE_4 ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
epInfo[ XBOX_INPUT_PIPE_4 ].bmNakPower = USB_NAK_NOWAIT; epInfo[ XBOX_INPUT_PIPE_4 ].maxPktSize = EP_MAXPKTSIZE;
epInfo[ XBOX_INPUT_PIPE_4 ].bmSndToggle = 0;
epInfo[ XBOX_INPUT_PIPE_4 ].bmRcvToggle = 0;
epInfo[ XBOX_OUTPUT_PIPE_4 ].epAddr = 0x07; epInfo[ XBOX_OUTPUT_PIPE_4 ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
epInfo[ XBOX_OUTPUT_PIPE_4 ].bmNakPower = USB_NAK_NOWAIT; epInfo[ XBOX_OUTPUT_PIPE_4 ].maxPktSize = EP_MAXPKTSIZE;
epInfo[ XBOX_OUTPUT_PIPE_4 ].bmSndToggle = 0;
epInfo[ XBOX_OUTPUT_PIPE_4 ].bmRcvToggle = 0;
rcode = pUsb->setEpInfoEntry(bAddress, 9, epInfo);
if(rcode)
goto FailSetDevTblEntry;
delay(200);
rcode = pUsb->setConf(bAddress, epInfo[ XBOX_CONTROL_PIPE ].epAddr, 1);
if(rcode)
goto FailSetConfDescr;
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nXbox Wireless Receiver Connected\r\n"), 0x80);
#endif
XboxReceiverConnected = true;
bPollEnable = true;
checkStatusTimer = 0; return 0;
FailSetDevTblEntry:
#ifdef DEBUG_USB_HOST
NotifyFailSetDevTblEntry();
goto Fail;
#endif
FailSetConfDescr:
#ifdef DEBUG_USB_HOST
NotifyFailSetConfDescr();
#endif
Fail:
#ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80);
NotifyFail(rcode);
#endif
Release();
return rcode;
}
uint8_t XBOXRECV::Release() {
XboxReceiverConnected = false;
for(uint8_t i = 0; i < 4; i++)
Xbox360Connected[i] = 0x00;
pUsb->GetAddressPool().FreeAddress(bAddress);
bAddress = 0;
bPollEnable = false;
return 0;
}
uint8_t XBOXRECV::Poll() {
if(!bPollEnable)
return 0;
if(!checkStatusTimer || ((millis() - checkStatusTimer) > 3000)) { checkStatusTimer = millis();
checkStatus();
}
uint8_t inputPipe;
uint16_t bufferSize;
for(uint8_t i = 0; i < 4; i++) {
if(i == 0)
inputPipe = XBOX_INPUT_PIPE_1;
else if(i == 1)
inputPipe = XBOX_INPUT_PIPE_2;
else if(i == 2)
inputPipe = XBOX_INPUT_PIPE_3;
else
inputPipe = XBOX_INPUT_PIPE_4;
bufferSize = EP_MAXPKTSIZE; pUsb->inTransfer(bAddress, epInfo[ inputPipe ].epAddr, &bufferSize, readBuf);
if(bufferSize > 0) { #ifdef EXTRADEBUG
Notify(PSTR("Bytes Received: "), 0x80);
D_PrintHex<uint16_t > (bufferSize, 0x80);
Notify(PSTR("\r\n"), 0x80);
#endif
readReport(i);
#ifdef PRINTREPORT
printReport(i, bufferSize); #endif
}
}
return 0;
}
void XBOXRECV::readReport(uint8_t controller) {
if(readBuf == NULL)
return;
if(readBuf[0] == 0x08 && readBuf[1] != Xbox360Connected[controller]) {
Xbox360Connected[controller] = readBuf[1];
#ifdef DEBUG_USB_HOST
Notify(PSTR("Controller "), 0x80);
Notify(controller, 0x80);
#endif
if(Xbox360Connected[controller]) {
#ifdef DEBUG_USB_HOST
const char* str = 0;
switch(readBuf[1]) {
case 0x80: str = PSTR(" as controller\r\n");
break;
case 0x40: str = PSTR(" as headset\r\n");
break;
case 0xC0: str = PSTR(" as controller+headset\r\n");
break;
}
Notify(PSTR(": connected"), 0x80);
Notify(str, 0x80);
#endif
onInit(controller);
}
#ifdef DEBUG_USB_HOST
else
Notify(PSTR(": disconnected\r\n"), 0x80);
#endif
return;
}
if(readBuf[1] == 0x00 && readBuf[3] & 0x13 && readBuf[4] >= 0x22) {
controllerStatus[controller] = ((uint16_t)readBuf[3] << 8) | readBuf[4];
return;
}
if(readBuf[1] != 0x01) return;
if(!Xbox360Connected[controller])
Xbox360Connected[controller] |= 0x80;
ButtonState[controller] = (uint32_t)(readBuf[9] | ((uint16_t)readBuf[8] << 8) | ((uint32_t)readBuf[7] << 16) | ((uint32_t)readBuf[6] << 24));
hatValue[controller][LeftHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]);
hatValue[controller][LeftHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]);
hatValue[controller][RightHatX] = (int16_t)(((uint16_t)readBuf[15] << 8) | readBuf[14]);
hatValue[controller][RightHatY] = (int16_t)(((uint16_t)readBuf[17] << 8) | readBuf[16]);
if(ButtonState[controller] != OldButtonState[controller]) {
buttonStateChanged[controller] = true;
ButtonClickState[controller] = (ButtonState[controller] >> 16) & ((~OldButtonState[controller]) >> 16); if(((uint8_t)OldButtonState[controller]) == 0 && ((uint8_t)ButtonState[controller]) != 0) R2Clicked[controller] = true;
if((uint8_t)(OldButtonState[controller] >> 8) == 0 && (uint8_t)(ButtonState[controller] >> 8) != 0)
L2Clicked[controller] = true;
OldButtonState[controller] = ButtonState[controller];
}
}
void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) { #ifdef PRINTREPORT
if(readBuf == NULL)
return;
Notify(PSTR("Controller "), 0x80);
Notify(controller, 0x80);
Notify(PSTR(": "), 0x80);
for(uint8_t i = 0; i < nBytes; i++) {
D_PrintHex<uint8_t > (readBuf[i], 0x80);
Notify(PSTR(" "), 0x80);
}
Notify(PSTR("\r\n"), 0x80);
#endif
}
uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) {
if(b == L2) return (uint8_t)(ButtonState[controller] >> 8);
else if(b == R2)
return (uint8_t)ButtonState[controller];
return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]) << 16));
}
bool XBOXRECV::getButtonClick(ButtonEnum b, uint8_t controller) {
if(b == L2) {
if(L2Clicked[controller]) {
L2Clicked[controller] = false;
return true;
}
return false;
} else if(b == R2) {
if(R2Clicked[controller]) {
R2Clicked[controller] = false;
return true;
}
return false;
}
uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]);
bool click = (ButtonClickState[controller] & button);
ButtonClickState[controller] &= ~button; return click;
}
int16_t XBOXRECV::getAnalogHat(AnalogHatEnum a, uint8_t controller) {
return hatValue[controller][a];
}
bool XBOXRECV::buttonChanged(uint8_t controller) {
bool state = buttonStateChanged[controller];
buttonStateChanged[controller] = false;
return state;
}
uint8_t XBOXRECV::getBatteryLevel(uint8_t controller) {
return ((controllerStatus[controller] & 0x00C0) >> 6);
}
void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) {
#ifdef EXTRADEBUG
uint8_t rcode;
#endif
uint8_t outputPipe;
switch(controller) {
case 0: outputPipe = XBOX_OUTPUT_PIPE_1;
break;
case 1: outputPipe = XBOX_OUTPUT_PIPE_2;
break;
case 2: outputPipe = XBOX_OUTPUT_PIPE_3;
break;
case 3: outputPipe = XBOX_OUTPUT_PIPE_4;
break;
default:
return;
}
#ifdef EXTRADEBUG
rcode =
#endif
pUsb->outTransfer(bAddress, epInfo[ outputPipe ].epAddr, nbytes, data);
#ifdef EXTRADEBUG
if(rcode)
Notify(PSTR("Error sending Xbox message\r\n"), 0x80);
#endif
}
void XBOXRECV::disconnect(uint8_t controller) {
writeBuf[0] = 0x00;
writeBuf[1] = 0x00;
writeBuf[2] = 0x08;
writeBuf[3] = 0xC0;
XboxCommand(controller, writeBuf, 4);
}
void XBOXRECV::setLedRaw(uint8_t value, uint8_t controller) {
writeBuf[0] = 0x00;
writeBuf[1] = 0x00;
writeBuf[2] = 0x08;
writeBuf[3] = value | 0x40;
XboxCommand(controller, writeBuf, 4);
}
void XBOXRECV::setLedOn(LEDEnum led, uint8_t controller) {
if(led == OFF)
setLedRaw(0, controller);
else if(led != ALL) setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]) + 4, controller);
}
void XBOXRECV::setLedBlink(LEDEnum led, uint8_t controller) {
setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]), controller);
}
void XBOXRECV::setLedMode(LEDModeEnum ledMode, uint8_t controller) { setLedRaw((uint8_t)ledMode, controller);
}
void XBOXRECV::checkStatus() {
if(!bPollEnable)
return;
writeBuf[0] = 0x08;
writeBuf[1] = 0x00;
writeBuf[2] = 0x0f;
writeBuf[3] = 0xc0;
for(uint8_t i = 0; i < 4; i++) {
XboxCommand(i, writeBuf, 4);
}
writeBuf[0] = 0x00;
writeBuf[1] = 0x00;
writeBuf[2] = 0x00;
writeBuf[3] = 0x40;
for(uint8_t i = 0; i < 4; i++) {
if(Xbox360Connected[i])
XboxCommand(i, writeBuf, 4);
}
}
void XBOXRECV::setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller) {
writeBuf[0] = 0x00;
writeBuf[1] = 0x01;
writeBuf[2] = 0x0f;
writeBuf[3] = 0xc0;
writeBuf[4] = 0x00;
writeBuf[5] = lValue; writeBuf[6] = rValue;
XboxCommand(controller, writeBuf, 7);
}
void XBOXRECV::onInit(uint8_t controller) {
if(pFuncOnInit)
pFuncOnInit(); else {
LEDEnum led;
if(controller == 0)
led = LED1;
else if(controller == 1)
led = LED2;
else if(controller == 2)
led = LED3;
else
led = LED4;
setLedOn(led, controller);
}
}