Zoltan Hudak / UsbHostMAX3421E

Dependents:   UsbHostMAX3421E_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers XBOXOLD.h Source File

XBOXOLD.h

00001 /* Copyright (C) 2013 Kristian Lauszus, TKJ Electronics. All rights reserved.
00002 
00003  This software may be distributed and modified under the terms of the GNU
00004  General Public License version 2 (GPL2) as published by the Free Software
00005  Foundation and appearing in the file GPL2.TXT included in the packaging of
00006  this file. Please note that GPL2 Section 2[b] requires that all works based
00007  on this software must also be made publicly available under the terms of
00008  the GPL2 ("Copyleft").
00009 
00010  Contact information
00011  -------------------
00012 
00013  Kristian Lauszus, TKJ Electronics
00014  Web      :  http://www.tkjelectronics.com
00015  e-mail   :  kristianl@tkjelectronics.com
00016  */
00017 
00018 #ifndef _xboxold_h_
00019 #define _xboxold_h_
00020 
00021 #include "Usb.h"
00022 #include "usbhid.h"
00023 #include "controllerEnums.h"
00024 
00025 /* Data Xbox taken from descriptors */
00026 #define EP_MAXPKTSIZE       32 // Max size for data via USB
00027 
00028 /* Names we give to the 3 Xbox pipes */
00029 #define XBOX_CONTROL_PIPE    0
00030 #define XBOX_INPUT_PIPE      1
00031 #define XBOX_OUTPUT_PIPE     2
00032 
00033 // PID and VID of the different devices
00034 #define XBOX_VID                                0x045E // Microsoft Corporation
00035 #define MADCATZ_VID                             0x1BAD // For unofficial Mad Catz controllers
00036 #define JOYTECH_VID                             0x162E // For unofficial Joytech controllers
00037 
00038 #define XBOX_OLD_PID1                           0x0202 // Original Microsoft Xbox controller (US)
00039 #define XBOX_OLD_PID2                           0x0285 // Original Microsoft Xbox controller (Japan)
00040 #define XBOX_OLD_PID3                           0x0287 // Microsoft Microsoft Xbox Controller S
00041 #define XBOX_OLD_PID4                           0x0289 // Smaller Microsoft Xbox controller (US)
00042 
00043 #define XBOX_MAX_ENDPOINTS   3
00044 
00045 /** This class implements support for a the original Xbox controller via USB. */
00046 class XBOXOLD : public USBDeviceConfig {
00047 public:
00048         /**
00049          * Constructor for the XBOXOLD class.
00050          * @param  pUsb   Pointer to USB class instance.
00051          */
00052         XBOXOLD(Usb *pUsb);
00053 
00054         /** @name USBDeviceConfig implementation */
00055         /**
00056          * Initialize the Xbox Controller.
00057          * @param  parent   Hub number.
00058          * @param  port     Port number on the hub.
00059          * @param  lowspeed Speed of the device.
00060          * @return          0 on success.
00061          */
00062         uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
00063         /**
00064          * Release the USB device.
00065          * @return 0 on success.
00066          */
00067         uint8_t Release();
00068         /**
00069          * Poll the USB Input endpoins and run the state machines.
00070          * @return 0 on success.
00071          */
00072         uint8_t Poll();
00073 
00074         /**
00075          * Get the device address.
00076          * @return The device address.
00077          */
00078         virtual uint8_t GetAddress() {
00079                 return bAddress;
00080         };
00081 
00082         /**
00083          * Used to check if the controller has been initialized.
00084          * @return True if it's ready.
00085          */
00086         virtual bool isReady() {
00087                 return bPollEnable;
00088         };
00089 
00090         /**
00091          * Used by the USB core to check what this driver support.
00092          * @param  vid The device's VID.
00093          * @param  pid The device's PID.
00094          * @return     Returns true if the device's VID and PID matches this driver.
00095          */
00096         virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
00097                 return ((vid == XBOX_VID || vid == MADCATZ_VID || vid == JOYTECH_VID) && (pid == XBOX_OLD_PID1 || pid == XBOX_OLD_PID2 || pid == XBOX_OLD_PID3 || pid == XBOX_OLD_PID4));
00098         };
00099         /**@}*/
00100 
00101         /** @name Xbox Controller functions */
00102         /**
00103          * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
00104          *
00105          * While getButtonClick(ButtonEnum b) will only return it once.
00106          *
00107          * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
00108          * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
00109          * @param  b          ::ButtonEnum to read.
00110          * @return            getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
00111          */
00112         uint8_t getButtonPress(ButtonEnum b);
00113         bool getButtonClick(ButtonEnum b);
00114         /**@}*/
00115 
00116         /** @name Xbox Controller functions */
00117         /**
00118          * Return the analog value from the joysticks on the controller.
00119          * @param  a          Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
00120          * @return            Returns a signed 16-bit integer.
00121          */
00122         int16_t getAnalogHat(AnalogHatEnum a);
00123 
00124         /** Turn rumble off the controller. */
00125         void setRumbleOff() {
00126                 setRumbleOn(0, 0);
00127         };
00128         /**
00129          * Turn rumble on.
00130          * @param lValue     Left motor (big weight) inside the controller.
00131          * @param rValue     Right motor (small weight) inside the controller.
00132          */
00133         void setRumbleOn(uint8_t lValue, uint8_t rValue);
00134 
00135         /**
00136          * Used to call your own function when the controller is successfully initialized.
00137          * @param funcOnInit Function to call.
00138          */
00139         void attachOnInit(void (*funcOnInit)(void)) {
00140                 pFuncOnInit = funcOnInit;
00141         };
00142         /**@}*/
00143 
00144         /** True if a Xbox controller is connected. */
00145         bool XboxConnected;
00146 
00147 protected:
00148         /** Pointer to USB class instance. */
00149         Usb *pUsb;
00150         /** Device address. */
00151         uint8_t bAddress;
00152         /** Endpoint info structure. */
00153         EpInfo epInfo[XBOX_MAX_ENDPOINTS];
00154 
00155 private:
00156         /**
00157          * Called when the controller is successfully initialized.
00158          * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
00159          * This is useful for instance if you want to set the LEDs in a specific way.
00160          */
00161         void (*pFuncOnInit)(void); // Pointer to function called in onInit()
00162 
00163         bool bPollEnable;
00164 
00165         /* Variables to store the digital buttons */
00166         uint8_t ButtonState;
00167         uint8_t OldButtonState;
00168         uint8_t ButtonClickState;
00169 
00170         /* Variables to store the analog buttons */
00171         uint8_t buttonValues[8]; // A, B, X, Y, BLACK, WHITE, L1, and R1
00172         uint8_t oldButtonValues[8];
00173         bool buttonClicked[8];
00174 
00175         int16_t hatValue[4]; // Joystick values
00176 
00177         uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
00178 
00179         void readReport(); // Read incoming data
00180         void printReport(uint16_t length); // Print incoming date
00181 
00182         /* Private commands */
00183         void XboxCommand(uint8_t* data, uint16_t nbytes);
00184 };
00185 #endif