Olivier Chabloz / Xinput

Dependencies:   USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Xinput.h Source File

Xinput.h

00001 /*
00002  * Xinput.h
00003  *
00004  *  Created on: 8 sept. 2016
00005  *      Author: Olivier Chabloz
00006  */
00007 
00008 #ifndef XINPUT_H_
00009 #define XINPUT_H_
00010 
00011 #include "USBDevice.h"
00012 #include "USBHID.h"
00013 
00014 #define DEVICE_CLASS    0xFF
00015 #define DEVICE_SUBCLASS 0xFF
00016 #define DEVICE_PROTOCOL 0xFF
00017 #define DEVICE_VERSION 0x0114
00018 #define DEVICE_ATTRIBUTES 0xA0
00019 #define DEVICE_POWER    0xFA
00020 #define VENDOR_ID       0x045e
00021 #define PRODUCT_ID      0x028e
00022 #define MANUFACTURER_NAME   {'©','M','i','c','r','o','s','o','f','t'}
00023 #define MANUFACTURER_NAME_LEN   10
00024 #define PRODUCT_NAME        {'C','o','n','t','r','o','l','l','e','r'}
00025 #define PRODUCT_NAME_LEN    10
00026 #define EP0_SIZE    8
00027 #define NUM_ENDPOINTS   6
00028 #define NUM_USB_BUFFERS 24
00029 #define NUM_INTERFACE   4
00030 #define XINPUT_INTERFACE    0
00031 #define XINPUT_RX_ENDPOINT  EP2OUT
00032 #define XINPUT_RX_SIZE 3
00033 #define XINPUT_TX_ENDPOINT  EP1IN
00034 #define XINPUT_TX_SIZE 20
00035 #define CONFIG_DESC_SIZE 153
00036 #define ENDPOINT1_CONFIG ENDPOINT_TRANSIMIT_ONLY
00037 #define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_ONLY
00038 #define ENDPOINT3_CONFIG ENDPOINT_TRANSIMIT_ONLY
00039 #define ENDPOINT4_CONFIG ENDPOINT_RECEIVE_ONLY
00040 #define ENDPOINT5_CONFIG ENDPOINT_TRANSMIT_AND_RECEIVE
00041 #define ENDPOINT6_CONFIG ENDPOINT_TRANSIMIT_ONLY
00042 
00043 
00044 
00045 #define BUTTON_Y    15
00046 #define BUTTON_X    14
00047 #define BUTTON_B    13
00048 #define BUTTON_A    12
00049 
00050 #define BUTTON_LOGO 10
00051 #define BUTTON_RB    9
00052 #define BUTTON_LB    8
00053 #define BUTTON_R3    7
00054 #define BUTTON_L3    6
00055 #define BUTTON_Back  5
00056 #define BUTTON_Start 4
00057 #define BUTTON_Right 3
00058 #define BUTTON_Left  2
00059 #define BUTTON_Down  1
00060 #define BUTTON_Up    0
00061 
00062 
00063 #define STICK_LEFT_X  1
00064 #define STICK_LEFT_Y  2
00065 #define STICK_RIGHT_X 3
00066 #define STICK_RIGHT_Y 4
00067 #define TRIGGER_LEFT  5
00068 #define TRIGGER_RIGHT 6
00069 
00070 
00071 
00072 class Xinput: public USBDevice {
00073 public:
00074     Xinput():
00075         USBDevice( VENDOR_ID, PRODUCT_ID, 0x0001), buttons(0), left_stick_x(0), left_stick_y(0), right_stick_x(0), right_stick_y(0), left_trigger(0), right_trigger(0){
00076         connect();
00077     };
00078 
00079     virtual ~Xinput();
00080 
00081 
00082     /*
00083     * Get device descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00084     *
00085     * @returns pointer to the device descriptor
00086     */
00087     virtual uint8_t * deviceDesc();
00088 
00089     /*
00090     * Get string manufacturer descriptor
00091     *
00092     * @returns pointer to the string manufacturer descriptor
00093     */
00094     virtual uint8_t * stringImanufacturerDesc();
00095 
00096     /*
00097     * Get string product descriptor
00098     *
00099     * @returns pointer to the string product descriptor
00100     */
00101     virtual uint8_t * stringIproductDesc();
00102 
00103     /*
00104     * Get string configuration descriptor
00105     *
00106     * @returns pointer to the string configuration descriptor
00107     */
00108     virtual uint8_t * stringIConfigurationDesc();
00109 
00110     /*
00111     * Called by USBDevice layer. Set configuration of the device.
00112     * For instance, you can add all endpoints that you need on this function.
00113     *
00114     * @param configuration Number of the configuration
00115     * @returns true if class handles this request
00116     */
00117     virtual bool USBCallback_setConfiguration(uint8_t configuration);
00118 
00119 
00120 
00121     bool read_leds(uint8_t * data);
00122     void send_controls(void);
00123     void clear(void);
00124 
00125     /*
00126     * Update Xinput buttons
00127     *
00128     * @param button The button number to update
00129     * @param status 1 to activate, 0 to desactivate
00130     */
00131     void update_button(uint8_t button, uint8_t status);
00132 
00133     void update_analog(uint8_t stick, int16_t value);
00134     void update_analog(uint8_t stick, uint8_t value);
00135 protected:
00136     /*
00137     * Get configuration descriptor
00138     *
00139     * @returns pointer to the configuration descriptor
00140     */
00141     virtual uint8_t * configurationDesc();
00142 
00143 private:
00144     uint16_t buttons;
00145     int16_t left_stick_x;
00146     int16_t left_stick_y;
00147     int16_t right_stick_x;
00148     int16_t right_stick_y;
00149     uint8_t left_trigger;
00150     uint8_t right_trigger;
00151 };
00152 
00153 #endif /* XINPUT_H_ */