Xbox 360 Wireless Controller for Windows library. sample: http://mbed.org/users/okini3939/code/USBHostXpad_HelloWorld/

Dependents:   USBHostXpad_HelloWorld USBHostXpad_HelloWorld

Xbox 360 Wireless Controller for Windows

Microsoftの XBOX 360 ワイヤレスコントローラーを、パソコン用のUSB接続型レシーバーで mbed に接続して使えるライブラリです。

USB Host 機能を使いますので mbed LPC1768 専用です。

たまに処理が停止する不具合があります。

/media/uploads/samux/usb_host_schema.jpg

USBHostXpad.h

Committer:
okini3939
Date:
2013-12-11
Revision:
1:5bb153989f33
Parent:
0:bd0f6bf72a8b
Child:
2:2749f4e649db

File content as of revision 1:5bb153989f33:

/*
 * Xbox 360 Wireless Controller for Windows library
 * for mbed USBHost library
 * Copyright (c) 2013 Hiroshi Suga
 *
 * VID=0x045e PID=0x0719
 */

#ifndef USBHostXpad_H
#define USBHostXpad_H

#include "USBHostConf.h"

#if 1 or USBHOST_XPAD

#include "USBHost.h"

#define XPAD_HAT_UP     0x0001
#define XPAD_HAT_DOWN   0x0002
#define XPAD_HAT_LEFT   0x0004
#define XPAD_HAT_RIGHT  0x0008
#define XPAD_START      0x0010
#define XPAD_BACK       0x0020
#define XPAD_STICK_L    0x0040
#define XPAD_STICK_R    0x0080
#define XPAD_PAD_LB     0x0100
#define XPAD_PAD_RB     0x0200
#define XPAD_XLOGO      0x0400
#define XPAD_PAD_A      0x1000
#define XPAD_PAD_B      0x2000
#define XPAD_PAD_X      0x4000
#define XPAD_PAD_Y      0x8000

#define XPAD_LED_OFF    0x00
#define XPAD_LED_BLINK  0x01
#define XPAD_LED1_FLASH 0x02
#define XPAD_LED2_FLASH 0x03
#define XPAD_LED3_FLASH 0x04
#define XPAD_LED4_FLASH 0x05
#define XPAD_LED1_ON    0x06
#define XPAD_LED2_ON    0x07
#define XPAD_LED3_ON    0x08
#define XPAD_LED4_ON    0x09
#define XPAD_LED_ROTATE 0x0a
#define XPAD_LED_ALTERNATE 0x0d


/** 
 * A class to communicate a USB flash disk
 */
class USBHostXpad : public IUSBEnumerator {
public:
    /**
    * Constructor
    *
    * @param rootdir mount name
    */
    USBHostXpad();

    /**
    * Check if a MSD device is connected
    *
    * @return true if a MSD device is connected
    */
    bool connected();

    /**
     * Try to connect to a MSD device
     *
     * @return true if connection was successful
     */
    bool connect();

    /**
     * Attach a callback called when a mouse event is received
     *
     * @param ptr function pointer
     */
    inline void attachEvent(void (*ptr)(int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r)) {
        if (ptr != NULL) {
            onUpdate = ptr;
        }
    }

    bool led(int cmd);
    bool rumble(uint8_t large, uint8_t small);

    void restart ();

protected:
    //From IUSBEnumerator
    virtual void setVidPid(uint16_t vid, uint16_t pid);
    virtual bool parseInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); //Must return true if the interface should be parsed
    virtual bool useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir); //Must return true if the endpoint will be used

private:
    USBHost * host;
    USBDeviceConnected * dev;
    bool dev_connected;

    USBEndpoint * int_in;
    USBEndpoint * int_out;
    uint8_t nb_ep;
    int xpad_intf;
    bool xpad_device_found;
    uint8_t report[32];

    uint32_t buttons;
    int16_t stick_lx, stick_ly, stick_rx, stick_ry;
    uint8_t trigger_l, trigger_r;

    void rxHandler();
    void (*onUpdate)(int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r);
    void init();
    bool send(int val1, int val2, int val3, int val4);

};

#endif

#endif