2012-07-17 18:41:05 +02:00
|
|
|
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
2013-03-28 09:46:43 +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:46:43 +01:00
|
|
|
|
2012-07-17 18:41:05 +02:00
|
|
|
Contact information
|
|
|
|
-------------------
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2012-07-17 18:41:05 +02:00
|
|
|
Kristian Lauszus, TKJ Electronics
|
|
|
|
Web : http://www.tkjelectronics.com
|
|
|
|
e-mail : kristianl@tkjelectronics.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _xboxusb_h_
|
|
|
|
#define _xboxusb_h_
|
|
|
|
|
|
|
|
#include "Usb.h"
|
2013-03-28 09:46:43 +01:00
|
|
|
#include "xboxEnums.h"
|
2012-07-17 18:41:05 +02:00
|
|
|
|
|
|
|
/* Data Xbox 360 taken from descriptors */
|
|
|
|
#define EP_MAXPKTSIZE 32 // max size for data via USB
|
|
|
|
|
|
|
|
/* Names we give to the 3 Xbox360 pipes */
|
|
|
|
#define XBOX_CONTROL_PIPE 0
|
|
|
|
#define XBOX_INPUT_PIPE 1
|
|
|
|
#define XBOX_OUTPUT_PIPE 2
|
|
|
|
|
2012-10-03 21:19:52 +02:00
|
|
|
// PID and VID of the different devices
|
2014-02-16 18:13:19 +01:00
|
|
|
#define XBOX_VID 0x045E // Microsoft Corporation
|
|
|
|
#define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
|
|
|
|
#define JOYTECH_VID 0x162E // For unofficial Joytech controllers
|
|
|
|
#define GAMESTOP_VID 0x0E6F // Gamestop controller
|
|
|
|
|
|
|
|
#define XBOX_WIRED_PID 0x028E // Microsoft 360 Wired controller
|
|
|
|
#define XBOX_WIRELESS_PID 0x028F // Wireless controller only support charging
|
|
|
|
#define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
|
|
|
|
#define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
|
|
|
|
#define MADCATZ_WIRED_PID 0xF016 // Mad Catz wired controller
|
|
|
|
#define GAMESTOP_WIRED_PID 0x0401 // Gamestop wired controller
|
|
|
|
#define AFTERGLOW_WIRED_PID 0x0213 // Afterglow wired controller - it uses the same VID as a Gamestop controller
|
2012-07-17 18:41:05 +02:00
|
|
|
|
|
|
|
#define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
|
|
|
|
|
2012-10-03 21:19:52 +02:00
|
|
|
// Used in control endpoint header for HID Commands
|
2012-07-17 18:41:05 +02:00
|
|
|
#define bmREQ_HID_OUT USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_CLASS|USB_SETUP_RECIPIENT_INTERFACE
|
|
|
|
#define HID_REQUEST_SET_REPORT 0x09
|
|
|
|
|
|
|
|
#define XBOX_MAX_ENDPOINTS 3
|
|
|
|
|
2013-02-17 01:03:32 +01:00
|
|
|
/** This class implements support for a Xbox wired controller via USB. */
|
2012-08-04 19:08:43 +02:00
|
|
|
class XBOXUSB : public USBDeviceConfig {
|
2012-07-17 18:41:05 +02:00
|
|
|
public:
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* Constructor for the XBOXUSB class.
|
|
|
|
* @param pUsb Pointer to USB class instance.
|
|
|
|
*/
|
|
|
|
XBOXUSB(USB *pUsb);
|
|
|
|
|
|
|
|
/** @name USBDeviceConfig implementation */
|
|
|
|
/**
|
|
|
|
* Initialize the Xbox Controller.
|
|
|
|
* @param parent Hub number.
|
|
|
|
* @param port Port number on the hub.
|
|
|
|
* @param lowspeed Speed of the device.
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
|
|
|
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
|
|
|
|
/**
|
|
|
|
* Release the USB device.
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
|
|
|
virtual uint8_t Release();
|
|
|
|
/**
|
|
|
|
* Poll the USB Input endpoins and run the state machines.
|
|
|
|
* @return 0 on success.
|
|
|
|
*/
|
|
|
|
virtual uint8_t Poll();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the device address.
|
|
|
|
* @return The device address.
|
|
|
|
*/
|
|
|
|
virtual uint8_t GetAddress() {
|
|
|
|
return bAddress;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to check if the controller has been initialized.
|
|
|
|
* @return True if it's ready.
|
|
|
|
*/
|
|
|
|
virtual bool isReady() {
|
|
|
|
return bPollEnable;
|
|
|
|
};
|
2013-07-15 18:20:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used by the USB core to check what this driver support.
|
|
|
|
* @param vid The device's VID.
|
|
|
|
* @param pid The device's PID.
|
|
|
|
* @return Returns true if the device's VID and PID matches this driver.
|
|
|
|
*/
|
|
|
|
virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
|
2014-02-16 18:13:19 +01:00
|
|
|
return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID || vid == GAMESTOP_VID) && (pid == XBOX_WIRED_PID || pid == MADCATZ_WIRED_PID || pid == GAMESTOP_WIRED_PID || pid == AFTERGLOW_WIRED_PID));
|
2013-07-15 18:20:58 +02:00
|
|
|
};
|
2013-03-28 09:46:43 +01:00
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/** @name Xbox Controller functions */
|
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* getButtonPress(ButtonEnum b) will return true as long as the button is held down.
|
2013-03-28 09:46:43 +01:00
|
|
|
*
|
2014-01-04 13:43:49 +01:00
|
|
|
* While getButtonClick(ButtonEnum b) will only return it once.
|
2013-03-28 09:46:43 +01:00
|
|
|
*
|
2014-01-04 13:43:49 +01:00
|
|
|
* So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
|
|
|
|
* but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
|
|
|
|
* @param b ::ButtonEnum to read.
|
|
|
|
* @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
uint8_t getButtonPress(ButtonEnum b);
|
|
|
|
bool getButtonClick(ButtonEnum b);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/** @name Xbox Controller functions */
|
|
|
|
/**
|
|
|
|
* Return the analog value from the joysticks on the controller.
|
|
|
|
* @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
|
|
|
|
* @return Returns a signed 16-bit integer.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
int16_t getAnalogHat(AnalogHatEnum a);
|
2013-03-28 09:46:43 +01:00
|
|
|
|
|
|
|
/** Turn rumble off and all the LEDs on the controller. */
|
|
|
|
void setAllOff() {
|
|
|
|
setRumbleOn(0, 0);
|
|
|
|
setLedRaw(0);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Turn rumble off the controller. */
|
|
|
|
void setRumbleOff() {
|
|
|
|
setRumbleOn(0, 0);
|
|
|
|
};
|
|
|
|
/**
|
|
|
|
* Turn rumble on.
|
|
|
|
* @param lValue Left motor (big weight) inside the controller.
|
|
|
|
* @param rValue Right motor (small weight) inside the controller.
|
|
|
|
*/
|
|
|
|
void setRumbleOn(uint8_t lValue, uint8_t rValue);
|
|
|
|
/**
|
2014-01-10 18:15:12 +01:00
|
|
|
* Set LED value. Without using the ::LEDEnum or ::LEDModeEnum.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @param value See:
|
2014-01-04 13:43:49 +01:00
|
|
|
* setLedOff(), setLedOn(LEDEnum l),
|
|
|
|
* setLedBlink(LEDEnum l), and setLedMode(LEDModeEnum lm).
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
|
|
|
void setLedRaw(uint8_t value);
|
|
|
|
|
|
|
|
/** Turn all LEDs off the controller. */
|
|
|
|
void setLedOff() {
|
|
|
|
setLedRaw(0);
|
|
|
|
};
|
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Turn on a LED by using ::LEDEnum.
|
|
|
|
* @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
void setLedOn(LEDEnum l);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
2014-01-04 13:43:49 +01:00
|
|
|
* Turn on a LED by using ::LEDEnum.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
|
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
void setLedBlink(LEDEnum l);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* Used to set special LED modes supported by the Xbox controller.
|
2014-01-10 18:15:12 +01:00
|
|
|
* @param lm See ::LEDModeEnum.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2014-01-04 13:43:49 +01:00
|
|
|
void setLedMode(LEDModeEnum lm);
|
2013-07-14 00:43:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to call your own function when the controller is successfully initialized.
|
|
|
|
* @param funcOnInit Function to call.
|
|
|
|
*/
|
|
|
|
void attachOnInit(void (*funcOnInit)(void)) {
|
|
|
|
pFuncOnInit = funcOnInit;
|
|
|
|
};
|
2013-03-28 09:46:43 +01:00
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/** True if a Xbox 360 controller is connected. */
|
|
|
|
bool Xbox360Connected;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/** Pointer to USB class instance. */
|
|
|
|
USB *pUsb;
|
|
|
|
/** Device address. */
|
|
|
|
uint8_t bAddress;
|
|
|
|
/** Endpoint info structure. */
|
|
|
|
EpInfo epInfo[XBOX_MAX_ENDPOINTS];
|
|
|
|
|
|
|
|
private:
|
2013-07-14 00:43:35 +02:00
|
|
|
/**
|
|
|
|
* Called when the controller is successfully initialized.
|
|
|
|
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
|
|
|
|
* This is useful for instance if you want to set the LEDs in a specific way.
|
|
|
|
*/
|
|
|
|
void onInit();
|
|
|
|
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
|
|
|
|
|
2013-03-28 09:46:43 +01:00
|
|
|
bool bPollEnable;
|
|
|
|
|
|
|
|
/* Variables to store the buttons */
|
|
|
|
uint32_t ButtonState;
|
|
|
|
uint32_t OldButtonState;
|
|
|
|
uint16_t ButtonClickState;
|
|
|
|
int16_t hatValue[4];
|
|
|
|
uint16_t controllerStatus;
|
|
|
|
|
|
|
|
bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
|
|
|
|
bool R2Clicked;
|
|
|
|
|
|
|
|
uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
|
2013-07-19 12:49:13 +02:00
|
|
|
uint8_t writeBuf[8]; // General purpose buffer for output data
|
2013-03-28 09:46:43 +01:00
|
|
|
|
|
|
|
void readReport(); // read incoming data
|
|
|
|
void printReport(); // print incoming date - Uncomment for debugging
|
|
|
|
|
|
|
|
/* Private commands */
|
|
|
|
void XboxCommand(uint8_t* data, uint16_t nbytes);
|
2012-07-17 18:41:05 +02:00
|
|
|
};
|
|
|
|
#endif
|