2012-12-28 06:37:42 +01:00
|
|
|
/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
|
|
|
|
|
|
|
|
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").
|
|
|
|
|
|
|
|
Contact information
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
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-01-28 00:55:01 +01:00
|
|
|
#include "controllerEnums.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
|
|
|
|
|
|
|
|
#define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
|
|
|
|
|
|
|
|
#define XBOX_MAX_ENDPOINTS 9
|
|
|
|
|
|
|
|
enum LEDMode {
|
|
|
|
ROTATING = 0x0A,
|
|
|
|
FASTBLINK = 0x0B,
|
|
|
|
SLOWBLINK = 0x0C,
|
|
|
|
ALTERNATING = 0x0D,
|
|
|
|
};
|
|
|
|
|
|
|
|
class XBOXRECV : public USBDeviceConfig {
|
|
|
|
public:
|
|
|
|
XBOXRECV(USB *pUsb);
|
|
|
|
|
|
|
|
// USBDeviceConfig implementation
|
|
|
|
virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
|
|
|
|
virtual uint8_t Release();
|
|
|
|
virtual uint8_t Poll();
|
|
|
|
virtual uint8_t GetAddress() { return bAddress; };
|
|
|
|
virtual bool isReady() { return bPollEnable; };
|
|
|
|
|
2012-12-29 14:16:11 +01:00
|
|
|
/*
|
|
|
|
Xbox Controller Readings.
|
|
|
|
getButtonPress will return true as long as the button is held down
|
|
|
|
While getButtonClick will only return it once
|
|
|
|
So for instance if you need to increase a variable once you would use getButtonClick,
|
|
|
|
but if you need to drive a robot forward you would use getButtonPress
|
|
|
|
*/
|
2012-12-28 06:37:42 +01:00
|
|
|
uint8_t getButtonPress(uint8_t controller, Button b);
|
|
|
|
bool getButtonClick(uint8_t controller, Button b);
|
|
|
|
int16_t getAnalogHat(uint8_t controller, AnalogHat a);
|
|
|
|
|
2012-12-29 14:16:11 +01:00
|
|
|
/* Xbox Controller Command */
|
2012-12-28 06:37:42 +01:00
|
|
|
void setAllOff(uint8_t controller) { setRumbleOn(controller,0,0); setLedOff(controller); };
|
|
|
|
void setRumbleOff(uint8_t controller) { setRumbleOn(controller,0,0); };
|
|
|
|
void setRumbleOn(uint8_t controller, uint8_t lValue, uint8_t rValue);
|
|
|
|
void setLedRaw(uint8_t controller, uint8_t value);
|
|
|
|
void setLedOff(uint8_t controller) { setLedRaw(controller,0); };
|
|
|
|
void setLedOn(uint8_t controller, LED l);
|
|
|
|
void setLedBlink(uint8_t controller, LED l);
|
|
|
|
void setLedMode(uint8_t controller, LEDMode lm);
|
2013-01-08 08:16:01 +01:00
|
|
|
uint8_t getBatteryLevel(uint8_t controller); // Returns the battery level in percentage in 33% steps
|
|
|
|
bool buttonChanged(uint8_t controller);
|
2012-12-28 06:37:42 +01:00
|
|
|
|
2012-12-29 14:16:11 +01:00
|
|
|
bool XboxReceiverConnected; // True if a wireless receiver is connected
|
2013-01-06 03:43:03 +01:00
|
|
|
uint8_t Xbox360Connected[4]; // Variable used to indicate if the XBOX 360 controller is successfully connected
|
2012-12-28 06:37:42 +01:00
|
|
|
|
|
|
|
protected:
|
2012-12-29 14:16:11 +01:00
|
|
|
/* Mandatory members */
|
2012-12-28 06:37:42 +01:00
|
|
|
USB *pUsb;
|
|
|
|
uint8_t bAddress; // device address
|
|
|
|
EpInfo epInfo[XBOX_MAX_ENDPOINTS]; //endpoint info structure
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool bPollEnable;
|
|
|
|
|
2012-12-29 14:16:11 +01:00
|
|
|
/* Variables to store the buttons */
|
2012-12-28 06:37:42 +01:00
|
|
|
uint32_t ButtonState[4];
|
|
|
|
uint32_t OldButtonState[4];
|
|
|
|
uint16_t ButtonClickState[4];
|
|
|
|
int16_t hatValue[4][4];
|
2013-01-06 03:43:03 +01:00
|
|
|
uint16_t controllerStatus[4];
|
2013-01-08 08:16:01 +01:00
|
|
|
bool buttonStateChanged[4]; // True if a button has changed
|
2012-12-28 06:37:42 +01:00
|
|
|
|
2013-01-04 00:19:07 +01:00
|
|
|
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];
|
2013-01-06 03:43:03 +01:00
|
|
|
|
|
|
|
unsigned long timer; // Timing for checkStatus() signals
|
2012-12-28 06:37:42 +01:00
|
|
|
|
|
|
|
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);
|
2013-01-06 03:43:03 +01:00
|
|
|
void checkStatus();
|
2012-12-28 06:37:42 +01:00
|
|
|
};
|
2013-01-06 03:43:03 +01:00
|
|
|
#endif
|