2012-07-17 18:41:05 +02:00
|
|
|
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2012-07-17 18:41:05 +02:00
|
|
|
This software may be distributed and modified under the terms of the GNU
|
|
|
|
General Public License version 2 (GPL2) as published by the Free Software
|
|
|
|
Foundation and appearing in the file GPL2.TXT included in the packaging of
|
|
|
|
this file. Please note that GPL2 Section 2[b] requires that all works based
|
|
|
|
on this software must also be made publicly available under the terms of
|
|
|
|
the GPL2 ("Copyleft").
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2012-07-17 18:41:05 +02:00
|
|
|
Contact information
|
|
|
|
-------------------
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2012-07-17 18:41:05 +02:00
|
|
|
Kristian Lauszus, TKJ Electronics
|
|
|
|
Web : http://www.tkjelectronics.com
|
|
|
|
e-mail : kristianl@tkjelectronics.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "XBOXUSB.h"
|
2013-11-11 11:48:09 +01:00
|
|
|
// To enable serial debugging see "settings.h"
|
2012-07-17 18:41:05 +02:00
|
|
|
//#define EXTRADEBUG // Uncomment to get even more debugging data
|
|
|
|
//#define PRINTREPORT // Uncomment to print the report send by the Xbox 360 Controller
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
XBOXUSB::XBOXUSB(USB *p) :
|
2012-07-17 18:41:05 +02:00
|
|
|
pUsb(p), // pointer to USB class instance - mandatory
|
|
|
|
bAddress(0), // device address - mandatory
|
|
|
|
bPollEnable(false) { // don't start polling before dongle is connected
|
2013-12-25 11:09:57 +01:00
|
|
|
for(uint8_t i = 0; i < XBOX_MAX_ENDPOINTS; i++) {
|
2013-03-28 09:46:43 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
if(pUsb) // register in USB subsystem
|
2013-03-28 09:46:43 +01:00
|
|
|
pUsb->RegisterDeviceClass(this); //set devConfig[] entry
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
|
2013-03-28 09:46:43 +01:00
|
|
|
uint8_t buf[sizeof (USB_DEVICE_DESCRIPTOR)];
|
2013-12-05 00:40:23 +01:00
|
|
|
USB_DEVICE_DESCRIPTOR * udd = reinterpret_cast<USB_DEVICE_DESCRIPTOR*>(buf);
|
2013-03-28 09:46:43 +01:00
|
|
|
uint8_t rcode;
|
|
|
|
UsbDevice *p = NULL;
|
|
|
|
EpInfo *oldep_ptr = NULL;
|
|
|
|
uint16_t PID;
|
|
|
|
uint16_t VID;
|
|
|
|
|
|
|
|
// get memory address of USB device address pool
|
|
|
|
AddressPool &addrPool = pUsb->GetAddressPool();
|
2012-07-17 18:41:05 +02:00
|
|
|
#ifdef EXTRADEBUG
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nXBOXUSB Init"), 0x80);
|
2012-07-17 18:41:05 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
// check if address has already been assigned to an instance
|
2013-12-25 11:09:57 +01:00
|
|
|
if(bAddress) {
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nAddress in use"), 0x80);
|
2012-07-17 18:41:05 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
return USB_ERROR_CLASS_INSTANCE_ALREADY_IN_USE;
|
|
|
|
}
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
// Get pointer to pseudo device with address 0 assigned
|
|
|
|
p = addrPool.GetUsbDevicePtr(0);
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
if(!p) {
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nAddress not found"), 0x80);
|
2012-07-17 18:41:05 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
}
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
if(!p->epinfo) {
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nepinfo is null"), 0x80);
|
2012-07-17 18:41:05 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
return USB_ERROR_EPINFO_IS_NULL;
|
|
|
|
}
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
// Save old pointer to EP_RECORD of address 0
|
|
|
|
oldep_ptr = p->epinfo;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
// Temporary assign new pointer to epInfo to p->epinfo in order to avoid toggle inconsistence
|
|
|
|
p->epinfo = epInfo;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
p->lowspeed = lowspeed;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
// Get device descriptor
|
|
|
|
rcode = pUsb->getDevDescr(0, 0, sizeof (USB_DEVICE_DESCRIPTOR), (uint8_t*)buf); // Get device descriptor - addr, ep, nbytes, data
|
|
|
|
// Restore p->epinfo
|
|
|
|
p->epinfo = oldep_ptr;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
if(rcode)
|
2013-03-28 09:46:43 +01:00
|
|
|
goto FailGetDevDescr;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-12-05 00:40:23 +01:00
|
|
|
VID = udd->idVendor;
|
|
|
|
PID = udd->idProduct;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
if(VID != XBOX_VID && VID != MADCATZ_VID && VID != JOYTECH_VID && VID != GAMESTOP_VID) // Check VID
|
2013-03-28 09:46:43 +01:00
|
|
|
goto FailUnknownDevice;
|
2013-12-25 11:09:57 +01:00
|
|
|
if(PID == XBOX_WIRELESS_PID) {
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nYou have plugged in a wireless Xbox 360 controller - it doesn't support USB communication"), 0x80);
|
2012-08-08 17:32:45 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
goto FailUnknownDevice;
|
2013-12-25 11:09:57 +01:00
|
|
|
} else if(PID == XBOX_WIRELESS_RECEIVER_PID || PID == XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID) {
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nThis library only supports Xbox 360 controllers via USB"), 0x80);
|
2012-08-08 17:32:45 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
goto FailUnknownDevice;
|
2014-02-16 18:13:19 +01:00
|
|
|
} else if(PID != XBOX_WIRED_PID && PID != MADCATZ_WIRED_PID && PID != GAMESTOP_WIRED_PID && PID != AFTERGLOW_WIRED_PID) // Check PID
|
2013-07-18 18:21:44 +02:00
|
|
|
goto FailUnknownDevice;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
// Allocate new address according to device class
|
|
|
|
bAddress = addrPool.AllocAddress(parent, false, port);
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
if(!bAddress)
|
2013-03-28 09:46:43 +01:00
|
|
|
return USB_ERROR_OUT_OF_ADDRESS_SPACE_IN_POOL;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
// Extract Max Packet Size from device descriptor
|
2013-12-05 00:40:23 +01:00
|
|
|
epInfo[0].maxPktSize = udd->bMaxPacketSize0;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
// Assign new address to the device
|
|
|
|
rcode = pUsb->setAddr(0, 0, bAddress);
|
2013-12-25 11:09:57 +01:00
|
|
|
if(rcode) {
|
2013-03-28 09:46:43 +01:00
|
|
|
p->lowspeed = false;
|
|
|
|
addrPool.FreeAddress(bAddress);
|
|
|
|
bAddress = 0;
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nsetAddr: "), 0x80);
|
2013-06-17 21:37:09 +02:00
|
|
|
D_PrintHex<uint8_t > (rcode, 0x80);
|
2013-07-20 12:57:23 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
return rcode;
|
|
|
|
}
|
2012-07-17 18:41:05 +02:00
|
|
|
#ifdef EXTRADEBUG
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nAddr: "), 0x80);
|
2013-06-17 21:37:09 +02:00
|
|
|
D_PrintHex<uint8_t > (bAddress, 0x80);
|
2012-07-17 18:41:05 +02:00
|
|
|
#endif
|
2013-12-19 03:58:24 +01:00
|
|
|
//delay(300); // Spec says you should wait at least 200ms
|
2013-12-05 00:40:23 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
p->lowspeed = false;
|
|
|
|
|
|
|
|
//get pointer to assigned address record
|
|
|
|
p = addrPool.GetUsbDevicePtr(bAddress);
|
2013-12-25 11:09:57 +01:00
|
|
|
if(!p)
|
2013-03-28 09:46:43 +01:00
|
|
|
return USB_ERROR_ADDRESS_NOT_FOUND_IN_POOL;
|
|
|
|
|
|
|
|
p->lowspeed = lowspeed;
|
|
|
|
|
|
|
|
// Assign epInfo to epinfo pointer - only EP0 is known
|
|
|
|
rcode = pUsb->setEpInfoEntry(bAddress, 1, epInfo);
|
2013-12-25 11:09:57 +01:00
|
|
|
if(rcode)
|
2013-03-28 09:46:43 +01:00
|
|
|
goto FailSetDevTblEntry;
|
|
|
|
|
|
|
|
/* The application will work in reduced host mode, so we can save program and data
|
|
|
|
memory space. After verifying the VID we will use known values for the
|
|
|
|
configuration values for device, interface, endpoints and HID for the XBOX360 Controllers */
|
|
|
|
|
|
|
|
/* Initialize data structures for endpoints of device */
|
|
|
|
epInfo[ XBOX_INPUT_PIPE ].epAddr = 0x01; // XBOX 360 report endpoint
|
2014-09-02 09:16:48 +02:00
|
|
|
epInfo[ XBOX_INPUT_PIPE ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
|
2013-03-28 09:46:43 +01:00
|
|
|
epInfo[ XBOX_INPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
|
|
|
|
epInfo[ XBOX_INPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
|
2013-12-05 00:40:23 +01:00
|
|
|
epInfo[ XBOX_INPUT_PIPE ].bmSndToggle = 0;
|
|
|
|
epInfo[ XBOX_INPUT_PIPE ].bmRcvToggle = 0;
|
2013-03-28 09:46:43 +01:00
|
|
|
epInfo[ XBOX_OUTPUT_PIPE ].epAddr = 0x02; // XBOX 360 output endpoint
|
2014-09-02 09:16:48 +02:00
|
|
|
epInfo[ XBOX_OUTPUT_PIPE ].epAttribs = USB_TRANSFER_TYPE_INTERRUPT;
|
2013-03-28 09:46:43 +01:00
|
|
|
epInfo[ XBOX_OUTPUT_PIPE ].bmNakPower = USB_NAK_NOWAIT; // Only poll once for interrupt endpoints
|
|
|
|
epInfo[ XBOX_OUTPUT_PIPE ].maxPktSize = EP_MAXPKTSIZE;
|
2013-12-05 00:40:23 +01:00
|
|
|
epInfo[ XBOX_OUTPUT_PIPE ].bmSndToggle = 0;
|
|
|
|
epInfo[ XBOX_OUTPUT_PIPE ].bmRcvToggle = 0;
|
2013-03-28 09:46:43 +01:00
|
|
|
|
|
|
|
rcode = pUsb->setEpInfoEntry(bAddress, 3, epInfo);
|
2013-12-25 11:09:57 +01:00
|
|
|
if(rcode)
|
2013-03-28 09:46:43 +01:00
|
|
|
goto FailSetDevTblEntry;
|
|
|
|
|
2013-07-14 00:43:35 +02:00
|
|
|
delay(200); // Give time for address change
|
2013-03-28 09:46:43 +01:00
|
|
|
|
|
|
|
rcode = pUsb->setConf(bAddress, epInfo[ XBOX_CONTROL_PIPE ].epAddr, 1);
|
2013-12-25 11:09:57 +01:00
|
|
|
if(rcode)
|
2013-03-30 15:29:16 +01:00
|
|
|
goto FailSetConfDescr;
|
2012-07-17 18:41:05 +02:00
|
|
|
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nXbox 360 Controller Connected\r\n"), 0x80);
|
2013-03-28 09:37:09 +01:00
|
|
|
#endif
|
2013-07-14 00:43:35 +02:00
|
|
|
onInit();
|
2013-03-28 09:46:43 +01:00
|
|
|
Xbox360Connected = true;
|
|
|
|
bPollEnable = true;
|
2013-07-14 00:43:35 +02:00
|
|
|
return 0; // Successful configuration
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-07-14 00:43:35 +02:00
|
|
|
/* Diagnostic messages */
|
2012-07-17 18:41:05 +02:00
|
|
|
FailGetDevDescr:
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-30 15:29:16 +01:00
|
|
|
NotifyFailGetDevDescr();
|
2013-03-28 09:46:43 +01:00
|
|
|
goto Fail;
|
2013-05-16 22:43:51 +02:00
|
|
|
#endif
|
2013-03-30 15:29:16 +01:00
|
|
|
|
2012-07-17 18:41:05 +02:00
|
|
|
FailSetDevTblEntry:
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-30 15:29:16 +01:00
|
|
|
NotifyFailSetDevTblEntry();
|
2013-03-28 09:46:43 +01:00
|
|
|
goto Fail;
|
2013-05-16 22:43:51 +02:00
|
|
|
#endif
|
2013-03-30 15:29:16 +01:00
|
|
|
|
|
|
|
FailSetConfDescr:
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-30 15:29:16 +01:00
|
|
|
NotifyFailSetConfDescr();
|
2013-05-16 22:43:51 +02:00
|
|
|
#endif
|
2013-12-06 22:58:16 +01:00
|
|
|
goto Fail;
|
|
|
|
|
2012-07-17 18:41:05 +02:00
|
|
|
FailUnknownDevice:
|
2013-06-12 05:11:43 +02:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-05-16 22:43:51 +02:00
|
|
|
NotifyFailUnknownDevice(VID, PID);
|
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
rcode = USB_DEV_CONFIG_ERROR_DEVICE_NOT_SUPPORTED;
|
2013-03-30 15:29:16 +01:00
|
|
|
|
2013-12-05 00:40:23 +01:00
|
|
|
Fail:
|
2013-12-06 22:58:16 +01:00
|
|
|
#ifdef DEBUG_USB_HOST
|
2013-03-28 09:46:43 +01:00
|
|
|
Notify(PSTR("\r\nXbox 360 Init Failed, error code: "), 0x80);
|
2013-03-30 15:29:16 +01:00
|
|
|
NotifyFail(rcode);
|
2013-05-16 22:43:51 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
Release();
|
|
|
|
return rcode;
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Performs a cleanup after failed Init() attempt */
|
|
|
|
uint8_t XBOXUSB::Release() {
|
2013-03-28 09:46:43 +01:00
|
|
|
Xbox360Connected = false;
|
|
|
|
pUsb->GetAddressPool().FreeAddress(bAddress);
|
|
|
|
bAddress = 0;
|
|
|
|
bPollEnable = false;
|
|
|
|
return 0;
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2013-03-28 09:37:09 +01:00
|
|
|
uint8_t XBOXUSB::Poll() {
|
2013-12-25 11:09:57 +01:00
|
|
|
if(!bPollEnable)
|
2013-03-28 09:46:43 +01:00
|
|
|
return 0;
|
|
|
|
uint16_t BUFFER_SIZE = EP_MAXPKTSIZE;
|
|
|
|
pUsb->inTransfer(bAddress, epInfo[ XBOX_INPUT_PIPE ].epAddr, &BUFFER_SIZE, readBuf); // input on endpoint 1
|
|
|
|
readReport();
|
2012-07-17 18:41:05 +02:00
|
|
|
#ifdef PRINTREPORT
|
2013-03-28 09:46:43 +01:00
|
|
|
printReport(); // Uncomment "#define PRINTREPORT" to print the report send by the Xbox 360 Controller
|
2012-07-17 18:41:05 +02:00
|
|
|
#endif
|
2013-03-28 09:46:43 +01:00
|
|
|
return 0;
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
|
|
|
|
2013-03-28 09:37:09 +01:00
|
|
|
void XBOXUSB::readReport() {
|
2013-12-25 11:09:57 +01:00
|
|
|
if(readBuf == NULL)
|
2013-03-28 09:46:43 +01:00
|
|
|
return;
|
2013-12-25 11:09:57 +01:00
|
|
|
if(readBuf[0] != 0x00 || readBuf[1] != 0x14) { // Check if it's the correct report - the controller also sends different status reports
|
2013-03-28 09:46:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ButtonState = (uint32_t)(readBuf[5] | ((uint16_t)readBuf[4] << 8) | ((uint32_t)readBuf[3] << 16) | ((uint32_t)readBuf[2] << 24));
|
|
|
|
|
|
|
|
hatValue[LeftHatX] = (int16_t)(((uint16_t)readBuf[7] << 8) | readBuf[6]);
|
|
|
|
hatValue[LeftHatY] = (int16_t)(((uint16_t)readBuf[9] << 8) | readBuf[8]);
|
|
|
|
hatValue[RightHatX] = (int16_t)(((uint16_t)readBuf[11] << 8) | readBuf[10]);
|
|
|
|
hatValue[RightHatY] = (int16_t)(((uint16_t)readBuf[13] << 8) | readBuf[12]);
|
|
|
|
|
|
|
|
//Notify(PSTR("\r\nButtonState"), 0x80);
|
|
|
|
//PrintHex<uint32_t>(ButtonState, 0x80);
|
|
|
|
|
2013-12-25 11:09:57 +01:00
|
|
|
if(ButtonState != OldButtonState) {
|
2013-03-28 09:46:43 +01:00
|
|
|
ButtonClickState = (ButtonState >> 16) & ((~OldButtonState) >> 16); // Update click state variable, but don't include the two trigger buttons L2 and R2
|
2013-12-25 11:09:57 +01:00
|
|
|
if(((uint8_t)OldButtonState) == 0 && ((uint8_t)ButtonState) != 0) // The L2 and R2 buttons are special as they are analog buttons
|
2013-03-28 09:46:43 +01:00
|
|
|
R2Clicked = true;
|
2013-12-25 11:09:57 +01:00
|
|
|
if((uint8_t)(OldButtonState >> 8) == 0 && (uint8_t)(ButtonState >> 8) != 0)
|
2013-03-28 09:46:43 +01:00
|
|
|
L2Clicked = true;
|
|
|
|
OldButtonState = ButtonState;
|
|
|
|
}
|
2013-03-28 09:37:09 +01:00
|
|
|
}
|
2012-07-17 18:41:05 +02:00
|
|
|
|
|
|
|
void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the report send by the Xbox 360 Controller
|
2013-01-19 15:43:28 +01:00
|
|
|
#ifdef PRINTREPORT
|
2013-12-25 11:09:57 +01:00
|
|
|
if(readBuf == NULL)
|
2013-03-28 09:46:43 +01:00
|
|
|
return;
|
2013-12-25 11:09:57 +01:00
|
|
|
for(uint8_t i = 0; i < XBOX_REPORT_BUFFER_SIZE; i++) {
|
2013-06-17 21:37:09 +02:00
|
|
|
D_PrintHex<uint8_t > (readBuf[i], 0x80);
|
2013-04-26 23:50:39 +02:00
|
|
|
Notify(PSTR(" "), 0x80);
|
2013-03-28 09:46:43 +01:00
|
|
|
}
|
2013-04-26 23:50:39 +02:00
|
|
|
Notify(PSTR("\r\n"), 0x80);
|
2013-03-28 09:37:09 +01:00
|
|
|
#endif
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
|
|
|
|
2014-01-04 13:43:49 +01:00
|
|
|
uint8_t XBOXUSB::getButtonPress(ButtonEnum b) {
|
2013-12-25 11:09:57 +01:00
|
|
|
if(b == L2) // These are analog buttons
|
2013-03-28 09:46:43 +01:00
|
|
|
return (uint8_t)(ButtonState >> 8);
|
2013-12-25 11:09:57 +01:00
|
|
|
else if(b == R2)
|
2013-03-28 09:46:43 +01:00
|
|
|
return (uint8_t)ButtonState;
|
2014-01-04 13:43:49 +01:00
|
|
|
return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]) << 16));
|
2013-02-17 01:03:32 +01:00
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2014-01-04 13:43:49 +01:00
|
|
|
bool XBOXUSB::getButtonClick(ButtonEnum b) {
|
2013-12-25 11:09:57 +01:00
|
|
|
if(b == L2) {
|
|
|
|
if(L2Clicked) {
|
2013-03-28 09:46:43 +01:00
|
|
|
L2Clicked = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-12-25 11:09:57 +01:00
|
|
|
} else if(b == R2) {
|
|
|
|
if(R2Clicked) {
|
2013-03-28 09:46:43 +01:00
|
|
|
R2Clicked = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-02-17 01:03:32 +01:00
|
|
|
}
|
2014-01-04 13:43:49 +01:00
|
|
|
uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]);
|
2013-03-28 09:46:43 +01:00
|
|
|
bool click = (ButtonClickState & button);
|
|
|
|
ButtonClickState &= ~button; // clear "click" event
|
|
|
|
return click;
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2014-01-04 13:43:49 +01:00
|
|
|
int16_t XBOXUSB::getAnalogHat(AnalogHatEnum a) {
|
2013-03-28 09:46:43 +01:00
|
|
|
return hatValue[a];
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
|
|
|
|
2013-02-17 01:03:32 +01:00
|
|
|
/* Xbox Controller commands */
|
2012-07-17 18:41:05 +02:00
|
|
|
void XBOXUSB::XboxCommand(uint8_t* data, uint16_t nbytes) {
|
2013-03-28 09:46:43 +01:00
|
|
|
//bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x00), Report Type (Output 0x02), interface (0x00), datalength, datalength, data)
|
|
|
|
pUsb->ctrlReq(bAddress, epInfo[XBOX_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x00, 0x02, 0x00, nbytes, nbytes, data, NULL);
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2013-02-17 01:03:32 +01:00
|
|
|
void XBOXUSB::setLedRaw(uint8_t value) {
|
2013-03-28 09:46:43 +01:00
|
|
|
writeBuf[0] = 0x01;
|
|
|
|
writeBuf[1] = 0x03;
|
|
|
|
writeBuf[2] = value;
|
2013-03-28 09:37:09 +01:00
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
XboxCommand(writeBuf, 3);
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2014-01-04 13:43:49 +01:00
|
|
|
void XBOXUSB::setLedOn(LEDEnum led) {
|
|
|
|
if(led == OFF)
|
|
|
|
setLedRaw(0);
|
|
|
|
else if(led != ALL) // All LEDs can't be on a the same time
|
|
|
|
setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]) + 4);
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2014-01-04 13:43:49 +01:00
|
|
|
void XBOXUSB::setLedBlink(LEDEnum led) {
|
|
|
|
setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]));
|
2013-02-17 01:03:32 +01:00
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2014-01-04 13:43:49 +01:00
|
|
|
void XBOXUSB::setLedMode(LEDModeEnum ledMode) { // This function is used to do some special LED stuff the controller supports
|
2013-03-28 09:46:43 +01:00
|
|
|
setLedRaw((uint8_t)ledMode);
|
2012-07-17 18:41:05 +02:00
|
|
|
}
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2012-07-17 18:41:05 +02:00
|
|
|
void XBOXUSB::setRumbleOn(uint8_t lValue, uint8_t rValue) {
|
2013-03-28 09:46:43 +01:00
|
|
|
writeBuf[0] = 0x00;
|
|
|
|
writeBuf[1] = 0x08;
|
|
|
|
writeBuf[2] = 0x00;
|
|
|
|
writeBuf[3] = lValue; // big weight
|
|
|
|
writeBuf[4] = rValue; // small weight
|
|
|
|
writeBuf[5] = 0x00;
|
|
|
|
writeBuf[6] = 0x00;
|
|
|
|
writeBuf[7] = 0x00;
|
|
|
|
|
|
|
|
XboxCommand(writeBuf, 8);
|
2013-03-28 09:37:09 +01:00
|
|
|
}
|
2013-07-14 00:43:35 +02:00
|
|
|
|
|
|
|
void XBOXUSB::onInit() {
|
2013-12-25 11:09:57 +01:00
|
|
|
if(pFuncOnInit)
|
2013-07-14 00:43:35 +02:00
|
|
|
pFuncOnInit(); // Call the user function
|
|
|
|
else
|
|
|
|
setLedOn(LED1);
|
|
|
|
}
|