2012-12-28 06:37:42 +01:00
|
|
|
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2012-12-28 06:37:42 +01: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-12-28 06:37:42 +01:00
|
|
|
Contact information
|
|
|
|
-------------------
|
2013-03-28 09:46:43 +01:00
|
|
|
|
2012-12-28 06:37:42 +01:00
|
|
|
Kristian Lauszus, TKJ Electronics
|
|
|
|
Web : http://www.tkjelectronics.com
|
|
|
|
e-mail : kristianl@tkjelectronics.com
|
2013-01-06 03:43:03 +01:00
|
|
|
|
|
|
|
getBatteryLevel and checkStatus functions made by timstamp.co.uk found using BusHound from Perisoft.net
|
2012-12-28 06:37:42 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _xboxrecv_h_
|
|
|
|
#define _xboxrecv_h_
|
|
|
|
|
|
|
|
#if defined(ARDUINO) && ARDUINO >= 100
|
|
|
|
#include "Arduino.h"
|
|
|
|
#else
|
|
|
|
#include "WProgram.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "Usb.h"
|
2013-03-28 09:46:43 +01:00
|
|
|
#include "xboxEnums.h"
|
2012-12-28 06:37:42 +01:00
|
|
|
|
|
|
|
/* Data Xbox 360 taken from descriptors */
|
|
|
|
#define EP_MAXPKTSIZE 32 // max size for data via USB
|
|
|
|
|
|
|
|
/* Endpoint types */
|
|
|
|
#define EP_INTERRUPT 0x03
|
|
|
|
|
2012-12-29 14:16:11 +01:00
|
|
|
/* Names we give to the 9 Xbox360 pipes */
|
2012-12-28 06:37:42 +01:00
|
|
|
#define XBOX_CONTROL_PIPE 0
|
|
|
|
#define XBOX_INPUT_PIPE_1 1
|
|
|
|
#define XBOX_OUTPUT_PIPE_1 2
|
|
|
|
#define XBOX_INPUT_PIPE_2 3
|
|
|
|
#define XBOX_OUTPUT_PIPE_2 4
|
|
|
|
#define XBOX_INPUT_PIPE_3 5
|
|
|
|
#define XBOX_OUTPUT_PIPE_3 6
|
|
|
|
#define XBOX_INPUT_PIPE_4 7
|
|
|
|
#define XBOX_OUTPUT_PIPE_4 8
|
|
|
|
|
|
|
|
// PID and VID of the different devices
|
|
|
|
#define XBOX_VID 0x045E // Microsoft Corporation
|
|
|
|
#define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
|
|
|
|
#define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
|
|
|
|
|
2013-02-17 01:03:32 +01:00
|
|
|
#define MADCATZ_VID 0x1BAD // For unofficial Mad Catz receivers
|
2012-12-28 06:37:42 +01:00
|
|
|
|
|
|
|
#define XBOX_MAX_ENDPOINTS 9
|
|
|
|
|
2013-02-05 19:51:45 +01:00
|
|
|
/**
|
2013-02-17 01:25:49 +01:00
|
|
|
* This class implements support for a Xbox Wireless receiver.
|
2013-02-05 19:51:45 +01:00
|
|
|
*
|
|
|
|
* Up to four controllers can connect to one receiver, if more is needed one can use a second receiver via the USBHub class.
|
|
|
|
*/
|
2012-12-28 06:37:42 +01:00
|
|
|
class XBOXRECV : public USBDeviceConfig {
|
|
|
|
public:
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* Constructor for the XBOXRECV class.
|
|
|
|
* @param pUsb Pointer to USB class instance.
|
|
|
|
*/
|
|
|
|
XBOXRECV(USB *pUsb);
|
|
|
|
|
|
|
|
/** @name USBDeviceConfig implementation */
|
|
|
|
/**
|
|
|
|
* Initialize the Xbox wireless receiver.
|
|
|
|
* @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;
|
|
|
|
};
|
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/** @name Xbox Controller functions */
|
|
|
|
/**
|
|
|
|
* getButtonPress(uint8_t controller, Button b) will return true as long as the button is held down.
|
|
|
|
*
|
|
|
|
* While getButtonClick(uint8_t controller, Button b) will only return it once.
|
|
|
|
*
|
|
|
|
* So you instance if you need to increase a variable once you would use getButtonClick(uint8_t controller, Button b),
|
|
|
|
* but if you need to drive a robot forward you would use getButtonPress(uint8_t controller, Button b).
|
|
|
|
* @param b ::Button to read.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to read from. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @return getButtonClick(uint8_t controller, Button b) will return a bool, but getButtonPress(uint8_t controller, Button b)
|
|
|
|
* will return a byte if reading ::L2 or ::R2.
|
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
uint8_t getButtonPress(Button b, uint8_t controller = 0);
|
|
|
|
bool getButtonClick(Button b, uint8_t controller = 0);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/** @name Xbox Controller functions */
|
|
|
|
/**
|
2013-05-16 22:41:52 +02:00
|
|
|
* Return the analog value from the joysticks on the controller.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to read from. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @return Returns a signed 16-bit integer.
|
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
int16_t getAnalogHat(AnalogHat a, uint8_t controller = 0);
|
2013-03-28 09:46:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn rumble off and all the LEDs on the specific controller.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to write to. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
void setAllOff(uint8_t controller = 0) {
|
|
|
|
setRumbleOn(0, 0, controller);
|
2013-03-28 09:46:43 +01:00
|
|
|
setLedOff(controller);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn rumble off the specific controller.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to write to. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
void setRumbleOff(uint8_t controller = 0) {
|
|
|
|
setRumbleOn(0, 0, controller);
|
2013-03-28 09:46:43 +01:00
|
|
|
};
|
|
|
|
/**
|
2013-05-16 22:41:52 +02:00
|
|
|
* Turn rumble on.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @param lValue Left motor (big weight) inside the controller.
|
|
|
|
* @param rValue Right motor (small weight) inside the controller.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to write to. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
void setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller = 0);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* Set LED value. Without using the ::LED or ::LEDMode enum.
|
|
|
|
* @param value See:
|
|
|
|
* setLedOff(uint8_t controller), setLedOn(uint8_t controller, LED l),
|
|
|
|
* setLedBlink(uint8_t controller, LED l), and setLedMode(uint8_t controller, LEDMode lm).
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to write to. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
void setLedRaw(uint8_t value, uint8_t controller = 0);
|
2013-03-28 09:46:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn all LEDs off the specific controller.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to write to. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
void setLedOff(uint8_t controller = 0) {
|
|
|
|
setLedRaw(0, controller);
|
2013-03-28 09:46:43 +01:00
|
|
|
};
|
|
|
|
/**
|
2013-05-16 22:41:52 +02:00
|
|
|
* Turn on a LED by using the ::LED enum.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @param l ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to write to. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
void setLedOn(LED l, uint8_t controller = 0);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
2013-05-16 22:41:52 +02:00
|
|
|
* Turn on a LED by using the ::LED enum.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to write to. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
void setLedBlink(LED l, uint8_t controller = 0);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
2013-05-16 22:41:52 +02:00
|
|
|
* Used to set special LED modes supported by the Xbox controller.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @param lm See ::LEDMode.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to write to. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
void setLedMode(LEDMode lm, uint8_t controller = 0);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* Used to get the battery level from the controller.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to read from. Default to 0.
|
2013-03-30 16:25:28 +01:00
|
|
|
* @return Returns the battery level as an integer in the range of 0-3.
|
2013-03-28 09:46:43 +01:00
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
uint8_t getBatteryLevel(uint8_t controller = 0);
|
2013-03-28 09:46:43 +01:00
|
|
|
/**
|
|
|
|
* Used to check if a button has changed.
|
2013-05-16 22:41:52 +02:00
|
|
|
* @param controller The controller to read from. Default to 0.
|
2013-03-28 09:46:43 +01:00
|
|
|
* @return True if a button has changed.
|
|
|
|
*/
|
2013-05-16 22:41:52 +02:00
|
|
|
bool buttonChanged(uint8_t controller = 0);
|
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 wireless receiver is connected. */
|
|
|
|
bool XboxReceiverConnected;
|
|
|
|
/** Variable used to indicate if the XBOX 360 controller is successfully connected. */
|
|
|
|
uint8_t Xbox360Connected[4];
|
|
|
|
|
|
|
|
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.
|
|
|
|
* @param controller The initialized controller.
|
|
|
|
*/
|
|
|
|
void onInit(uint8_t controller);
|
|
|
|
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[4];
|
|
|
|
uint32_t OldButtonState[4];
|
|
|
|
uint16_t ButtonClickState[4];
|
|
|
|
int16_t hatValue[4][4];
|
|
|
|
uint16_t controllerStatus[4];
|
|
|
|
bool buttonStateChanged[4]; // True if a button has changed
|
|
|
|
|
|
|
|
bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not
|
|
|
|
bool R2Clicked[4];
|
|
|
|
|
|
|
|
unsigned long timer; // Timing for checkStatus() signals
|
|
|
|
|
|
|
|
uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
|
|
|
|
uint8_t writeBuf[EP_MAXPKTSIZE]; // General purpose buffer for output data
|
|
|
|
|
|
|
|
void readReport(uint8_t controller); // read incoming data
|
|
|
|
void printReport(uint8_t controller, uint8_t nBytes); // print incoming date - Uncomment for debugging
|
|
|
|
|
|
|
|
/* Private commands */
|
|
|
|
void XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes);
|
|
|
|
void checkStatus();
|
2012-12-28 06:37:42 +01:00
|
|
|
};
|
2013-01-06 03:43:03 +01:00
|
|
|
#endif
|