一関 Aチーム / ArduinoUsbHostShield
Committer:
kotakku
Date:
Sat Jan 18 15:06:35 2020 +0000
Revision:
0:b1ce54272580
1.0.0 first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kotakku 0:b1ce54272580 1 /* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved.
kotakku 0:b1ce54272580 2
kotakku 0:b1ce54272580 3 This software may be distributed and modified under the terms of the GNU
kotakku 0:b1ce54272580 4 General Public License version 2 (GPL2) as published by the Free Software
kotakku 0:b1ce54272580 5 Foundation and appearing in the file GPL2.TXT included in the packaging of
kotakku 0:b1ce54272580 6 this file. Please note that GPL2 Section 2[b] requires that all works based
kotakku 0:b1ce54272580 7 on this software must also be made publicly available under the terms of
kotakku 0:b1ce54272580 8 the GPL2 ("Copyleft").
kotakku 0:b1ce54272580 9
kotakku 0:b1ce54272580 10 Contact information
kotakku 0:b1ce54272580 11 -------------------
kotakku 0:b1ce54272580 12
kotakku 0:b1ce54272580 13 Kristian Lauszus, TKJ Electronics
kotakku 0:b1ce54272580 14 Web : http://www.tkjelectronics.com
kotakku 0:b1ce54272580 15 e-mail : kristianl@tkjelectronics.com
kotakku 0:b1ce54272580 16 */
kotakku 0:b1ce54272580 17
kotakku 0:b1ce54272580 18 #ifndef _xboxusb_h_
kotakku 0:b1ce54272580 19 #define _xboxusb_h_
kotakku 0:b1ce54272580 20
kotakku 0:b1ce54272580 21 #include "Usb.h"
kotakku 0:b1ce54272580 22 #include "usbhid.h"
kotakku 0:b1ce54272580 23 #include "xboxEnums.h"
kotakku 0:b1ce54272580 24
kotakku 0:b1ce54272580 25 /* Data Xbox 360 taken from descriptors */
kotakku 0:b1ce54272580 26 #define EP_MAXPKTSIZE 32 // max size for data via USB
kotakku 0:b1ce54272580 27
kotakku 0:b1ce54272580 28 /* Names we give to the 3 Xbox360 pipes */
kotakku 0:b1ce54272580 29 #define XBOX_CONTROL_PIPE 0
kotakku 0:b1ce54272580 30 #define XBOX_INPUT_PIPE 1
kotakku 0:b1ce54272580 31 #define XBOX_OUTPUT_PIPE 2
kotakku 0:b1ce54272580 32
kotakku 0:b1ce54272580 33 // PID and VID of the different devices
kotakku 0:b1ce54272580 34 #define XBOX_VID 0x045E // Microsoft Corporation
kotakku 0:b1ce54272580 35 #define MADCATZ_VID 0x1BAD // For unofficial Mad Catz controllers
kotakku 0:b1ce54272580 36 #define JOYTECH_VID 0x162E // For unofficial Joytech controllers
kotakku 0:b1ce54272580 37 #define GAMESTOP_VID 0x0E6F // Gamestop controller
kotakku 0:b1ce54272580 38
kotakku 0:b1ce54272580 39 #define XBOX_WIRED_PID 0x028E // Microsoft 360 Wired controller
kotakku 0:b1ce54272580 40 #define XBOX_WIRELESS_PID 0x028F // Wireless controller only support charging
kotakku 0:b1ce54272580 41 #define XBOX_WIRELESS_RECEIVER_PID 0x0719 // Microsoft Wireless Gaming Receiver
kotakku 0:b1ce54272580 42 #define XBOX_WIRELESS_RECEIVER_THIRD_PARTY_PID 0x0291 // Third party Wireless Gaming Receiver
kotakku 0:b1ce54272580 43 #define MADCATZ_WIRED_PID 0xF016 // Mad Catz wired controller
kotakku 0:b1ce54272580 44 #define JOYTECH_WIRED_PID 0xBEEF // For Joytech wired controller
kotakku 0:b1ce54272580 45 #define GAMESTOP_WIRED_PID 0x0401 // Gamestop wired controller
kotakku 0:b1ce54272580 46 #define AFTERGLOW_WIRED_PID 0x0213 // Afterglow wired controller - it uses the same VID as a Gamestop controller
kotakku 0:b1ce54272580 47
kotakku 0:b1ce54272580 48 #define XBOX_REPORT_BUFFER_SIZE 14 // Size of the input report buffer
kotakku 0:b1ce54272580 49
kotakku 0:b1ce54272580 50 #define XBOX_MAX_ENDPOINTS 3
kotakku 0:b1ce54272580 51
kotakku 0:b1ce54272580 52 /** This class implements support for a Xbox wired controller via USB. */
kotakku 0:b1ce54272580 53 class XBOXUSB : public USBDeviceConfig {
kotakku 0:b1ce54272580 54 public:
kotakku 0:b1ce54272580 55 /**
kotakku 0:b1ce54272580 56 * Constructor for the XBOXUSB class.
kotakku 0:b1ce54272580 57 * @param pUsb Pointer to USB class instance.
kotakku 0:b1ce54272580 58 */
kotakku 0:b1ce54272580 59 XBOXUSB(USB *pUsb);
kotakku 0:b1ce54272580 60
kotakku 0:b1ce54272580 61 /** @name USBDeviceConfig implementation */
kotakku 0:b1ce54272580 62 /**
kotakku 0:b1ce54272580 63 * Initialize the Xbox Controller.
kotakku 0:b1ce54272580 64 * @param parent Hub number.
kotakku 0:b1ce54272580 65 * @param port Port number on the hub.
kotakku 0:b1ce54272580 66 * @param lowspeed Speed of the device.
kotakku 0:b1ce54272580 67 * @return 0 on success.
kotakku 0:b1ce54272580 68 */
kotakku 0:b1ce54272580 69 uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
kotakku 0:b1ce54272580 70 /**
kotakku 0:b1ce54272580 71 * Release the USB device.
kotakku 0:b1ce54272580 72 * @return 0 on success.
kotakku 0:b1ce54272580 73 */
kotakku 0:b1ce54272580 74 uint8_t Release();
kotakku 0:b1ce54272580 75 /**
kotakku 0:b1ce54272580 76 * Poll the USB Input endpoins and run the state machines.
kotakku 0:b1ce54272580 77 * @return 0 on success.
kotakku 0:b1ce54272580 78 */
kotakku 0:b1ce54272580 79 uint8_t Poll();
kotakku 0:b1ce54272580 80
kotakku 0:b1ce54272580 81 /**
kotakku 0:b1ce54272580 82 * Get the device address.
kotakku 0:b1ce54272580 83 * @return The device address.
kotakku 0:b1ce54272580 84 */
kotakku 0:b1ce54272580 85 virtual uint8_t GetAddress() {
kotakku 0:b1ce54272580 86 return bAddress;
kotakku 0:b1ce54272580 87 };
kotakku 0:b1ce54272580 88
kotakku 0:b1ce54272580 89 /**
kotakku 0:b1ce54272580 90 * Used to check if the controller has been initialized.
kotakku 0:b1ce54272580 91 * @return True if it's ready.
kotakku 0:b1ce54272580 92 */
kotakku 0:b1ce54272580 93 virtual bool isReady() {
kotakku 0:b1ce54272580 94 return bPollEnable;
kotakku 0:b1ce54272580 95 };
kotakku 0:b1ce54272580 96
kotakku 0:b1ce54272580 97 /**
kotakku 0:b1ce54272580 98 * Used by the USB core to check what this driver support.
kotakku 0:b1ce54272580 99 * @param vid The device's VID.
kotakku 0:b1ce54272580 100 * @param pid The device's PID.
kotakku 0:b1ce54272580 101 * @return Returns true if the device's VID and PID matches this driver.
kotakku 0:b1ce54272580 102 */
kotakku 0:b1ce54272580 103 virtual bool VIDPIDOK(uint16_t vid, uint16_t pid) {
kotakku 0:b1ce54272580 104 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 || pid == JOYTECH_WIRED_PID));
kotakku 0:b1ce54272580 105 };
kotakku 0:b1ce54272580 106 /**@}*/
kotakku 0:b1ce54272580 107
kotakku 0:b1ce54272580 108 /** @name Xbox Controller functions */
kotakku 0:b1ce54272580 109 /**
kotakku 0:b1ce54272580 110 * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
kotakku 0:b1ce54272580 111 *
kotakku 0:b1ce54272580 112 * While getButtonClick(ButtonEnum b) will only return it once.
kotakku 0:b1ce54272580 113 *
kotakku 0:b1ce54272580 114 * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
kotakku 0:b1ce54272580 115 * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
kotakku 0:b1ce54272580 116 * @param b ::ButtonEnum to read.
kotakku 0:b1ce54272580 117 * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
kotakku 0:b1ce54272580 118 */
kotakku 0:b1ce54272580 119 uint8_t getButtonPress(ButtonEnum b);
kotakku 0:b1ce54272580 120 bool getButtonClick(ButtonEnum b);
kotakku 0:b1ce54272580 121 /**@}*/
kotakku 0:b1ce54272580 122
kotakku 0:b1ce54272580 123 /** @name Xbox Controller functions */
kotakku 0:b1ce54272580 124 /**
kotakku 0:b1ce54272580 125 * Return the analog value from the joysticks on the controller.
kotakku 0:b1ce54272580 126 * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
kotakku 0:b1ce54272580 127 * @return Returns a signed 16-bit integer.
kotakku 0:b1ce54272580 128 */
kotakku 0:b1ce54272580 129 int16_t getAnalogHat(AnalogHatEnum a);
kotakku 0:b1ce54272580 130
kotakku 0:b1ce54272580 131 /** Turn rumble off and all the LEDs on the controller. */
kotakku 0:b1ce54272580 132 void setAllOff() {
kotakku 0:b1ce54272580 133 setRumbleOn(0, 0);
kotakku 0:b1ce54272580 134 setLedRaw(0);
kotakku 0:b1ce54272580 135 };
kotakku 0:b1ce54272580 136
kotakku 0:b1ce54272580 137 /** Turn rumble off the controller. */
kotakku 0:b1ce54272580 138 void setRumbleOff() {
kotakku 0:b1ce54272580 139 setRumbleOn(0, 0);
kotakku 0:b1ce54272580 140 };
kotakku 0:b1ce54272580 141 /**
kotakku 0:b1ce54272580 142 * Turn rumble on.
kotakku 0:b1ce54272580 143 * @param lValue Left motor (big weight) inside the controller.
kotakku 0:b1ce54272580 144 * @param rValue Right motor (small weight) inside the controller.
kotakku 0:b1ce54272580 145 */
kotakku 0:b1ce54272580 146 void setRumbleOn(uint8_t lValue, uint8_t rValue);
kotakku 0:b1ce54272580 147 /**
kotakku 0:b1ce54272580 148 * Set LED value. Without using the ::LEDEnum or ::LEDModeEnum.
kotakku 0:b1ce54272580 149 * @param value See:
kotakku 0:b1ce54272580 150 * setLedOff(), setLedOn(LEDEnum l),
kotakku 0:b1ce54272580 151 * setLedBlink(LEDEnum l), and setLedMode(LEDModeEnum lm).
kotakku 0:b1ce54272580 152 */
kotakku 0:b1ce54272580 153 void setLedRaw(uint8_t value);
kotakku 0:b1ce54272580 154
kotakku 0:b1ce54272580 155 /** Turn all LEDs off the controller. */
kotakku 0:b1ce54272580 156 void setLedOff() {
kotakku 0:b1ce54272580 157 setLedRaw(0);
kotakku 0:b1ce54272580 158 };
kotakku 0:b1ce54272580 159 /**
kotakku 0:b1ce54272580 160 * Turn on a LED by using ::LEDEnum.
kotakku 0:b1ce54272580 161 * @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
kotakku 0:b1ce54272580 162 */
kotakku 0:b1ce54272580 163 void setLedOn(LEDEnum l);
kotakku 0:b1ce54272580 164 /**
kotakku 0:b1ce54272580 165 * Turn on a LED by using ::LEDEnum.
kotakku 0:b1ce54272580 166 * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
kotakku 0:b1ce54272580 167 */
kotakku 0:b1ce54272580 168 void setLedBlink(LEDEnum l);
kotakku 0:b1ce54272580 169 /**
kotakku 0:b1ce54272580 170 * Used to set special LED modes supported by the Xbox controller.
kotakku 0:b1ce54272580 171 * @param lm See ::LEDModeEnum.
kotakku 0:b1ce54272580 172 */
kotakku 0:b1ce54272580 173 void setLedMode(LEDModeEnum lm);
kotakku 0:b1ce54272580 174
kotakku 0:b1ce54272580 175 /**
kotakku 0:b1ce54272580 176 * Used to call your own function when the controller is successfully initialized.
kotakku 0:b1ce54272580 177 * @param funcOnInit Function to call.
kotakku 0:b1ce54272580 178 */
kotakku 0:b1ce54272580 179 void attachOnInit(void (*funcOnInit)(void)) {
kotakku 0:b1ce54272580 180 pFuncOnInit = funcOnInit;
kotakku 0:b1ce54272580 181 };
kotakku 0:b1ce54272580 182 /**@}*/
kotakku 0:b1ce54272580 183
kotakku 0:b1ce54272580 184 /** True if a Xbox 360 controller is connected. */
kotakku 0:b1ce54272580 185 bool Xbox360Connected;
kotakku 0:b1ce54272580 186
kotakku 0:b1ce54272580 187 protected:
kotakku 0:b1ce54272580 188 /** Pointer to USB class instance. */
kotakku 0:b1ce54272580 189 USB *pUsb;
kotakku 0:b1ce54272580 190 /** Device address. */
kotakku 0:b1ce54272580 191 uint8_t bAddress;
kotakku 0:b1ce54272580 192 /** Endpoint info structure. */
kotakku 0:b1ce54272580 193 EpInfo epInfo[XBOX_MAX_ENDPOINTS];
kotakku 0:b1ce54272580 194
kotakku 0:b1ce54272580 195 private:
kotakku 0:b1ce54272580 196 /**
kotakku 0:b1ce54272580 197 * Called when the controller is successfully initialized.
kotakku 0:b1ce54272580 198 * Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
kotakku 0:b1ce54272580 199 * This is useful for instance if you want to set the LEDs in a specific way.
kotakku 0:b1ce54272580 200 */
kotakku 0:b1ce54272580 201 void onInit();
kotakku 0:b1ce54272580 202 void (*pFuncOnInit)(void); // Pointer to function called in onInit()
kotakku 0:b1ce54272580 203
kotakku 0:b1ce54272580 204 bool bPollEnable;
kotakku 0:b1ce54272580 205
kotakku 0:b1ce54272580 206 /* Variables to store the buttons */
kotakku 0:b1ce54272580 207 uint32_t ButtonState;
kotakku 0:b1ce54272580 208 uint32_t OldButtonState;
kotakku 0:b1ce54272580 209 uint16_t ButtonClickState;
kotakku 0:b1ce54272580 210 int16_t hatValue[4];
kotakku 0:b1ce54272580 211 uint16_t controllerStatus;
kotakku 0:b1ce54272580 212
kotakku 0:b1ce54272580 213 bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
kotakku 0:b1ce54272580 214 bool R2Clicked;
kotakku 0:b1ce54272580 215
kotakku 0:b1ce54272580 216 uint8_t readBuf[EP_MAXPKTSIZE]; // General purpose buffer for input data
kotakku 0:b1ce54272580 217 uint8_t writeBuf[8]; // General purpose buffer for output data
kotakku 0:b1ce54272580 218
kotakku 0:b1ce54272580 219 void readReport(); // read incoming data
kotakku 0:b1ce54272580 220 void printReport(); // print incoming date - Uncomment for debugging
kotakku 0:b1ce54272580 221
kotakku 0:b1ce54272580 222 /* Private commands */
kotakku 0:b1ce54272580 223 void XboxCommand(uint8_t* data, uint16_t nbytes);
kotakku 0:b1ce54272580 224 };
kotakku 0:b1ce54272580 225 #endif
kotakku 0:b1ce54272580 226