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

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Tue Dec 10 06:50:37 2013 +0000
Child:
1:5bb153989f33
Commit message:
1st build;

Changed in this revision

USBHostXpad.cpp Show annotated file Show diff for this revision Revisions of this file
USBHostXpad.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHostXpad.cpp	Tue Dec 10 06:50:37 2013 +0000
@@ -0,0 +1,217 @@
+/*
+ * Xbox 360 Wireless Controller for Windows library
+ * for mbed USBHost library
+ * Copyright (c) 2013 Hiroshi Suga
+ *
+ * VID=0x045e PID=0x0719
+ *
+ * Note: 2012-12-10
+ *   The library has some problems.
+ *   - X-logo lamp remains flickering.
+ *   - probe() function is irresponsible.
+ */
+
+#include "USBHostXpad.h"
+
+#if 1 or USBHOST_XPAD
+
+#include "dbg.h"
+
+#define DEVICE_TO_HOST  0x80
+#define HOST_TO_DEVICE  0x00
+
+USBHostXpad::USBHostXpad()
+{
+    host = USBHost::getHostInst();
+    init();
+}
+
+void USBHostXpad::init() {
+    dev_connected = false;
+    dev = NULL;
+    int_in = NULL;
+    int_out = NULL;
+    xpad_intf = -1;
+    xpad_device_found = false;
+    nb_ep = 0;
+}
+
+bool USBHostXpad::connected()
+{
+    return dev_connected;
+}
+
+bool USBHostXpad::connect()
+{
+
+    if (dev_connected) {
+        return true;
+    }
+
+    for (uint8_t i = 0; i < MAX_DEVICE_CONNECTED; i++) {
+        if ((dev = host->getDevice(i)) != NULL) {
+            
+            USB_DBG("Trying to connect MSD device\r\n");
+            
+            if(host->enumerate(dev, this))
+                break;
+
+            if (xpad_device_found) {
+                xpad_device_found = false;
+                int_in = dev->getEndpoint(xpad_intf, INTERRUPT_ENDPOINT, IN);
+                int_out = dev->getEndpoint(xpad_intf, INTERRUPT_ENDPOINT, OUT);
+                
+                if (!int_in || !int_out)
+                    continue;
+
+                USB_INFO("New MSD device: VID:%04x PID:%04x [dev: %p - intf: %d]", dev->getVid(), dev->getPid(), dev, xpad_intf);
+                USB_INFO("endpoint in:%08x out:%08x", int_in, int_out);
+                dev->setName("XPAD", xpad_intf);
+                host->registerDriver(dev, xpad_intf, this, &USBHostXpad::init);
+
+                int_in->attach(this, &USBHostXpad::rxHandler);
+                probe(0x00, 0x00, 0x08, 0x40);
+                probe(0x01, 0x03, 0x09, 0x01);
+                probe(0x01, 0x03, 0x08, 0x01);
+                probe(0x01, 0x03, 0x09, 0x00);
+                probe(0x01, 0x03, 0x09, 0x01);
+                probe(0x01, 0x03, 0x08, 0x01);
+                probe(0x01, 0x03, 0x09, 0x00);
+                probe(0x01, 0x03, 0x09, 0x01);
+                probe(0x01, 0x03, 0x08, 0x01);
+                probe(0x01, 0x03, 0x09, 0x00);
+                probe(0x01, 0x03, 0x09, 0x01);
+                probe(0x01, 0x03, 0x08, 0x01);
+                probe(0x01, 0x03, 0x09, 0x00);
+                host->interruptRead(dev, int_in, report, int_in->getSize(), false);
+                dev_connected = true;
+//                USB_INFO("RETURN TRUE");
+                return true;
+            }
+
+        } //if()
+    } //for()
+//    USB_INFO("RETURN FALSE");
+    init();
+    return false;
+}
+
+void USBHostXpad::rxHandler() {
+    int len_listen = int_in->getSize();
+    int len = int_in->getLengthTransferred();
+
+    USB_INFO("rxHandler len:%d data:%02x %02x", len, report[0], report[1]);
+    for (int i = 0; i < len_listen; i ++) {
+        std::printf(" %02x", report[i]);
+    }
+    std::printf("\r\n");
+
+    if (report[0] == 0x08 && report[1] == 0x80) {
+        probe(0x00, 0x00, 0x00, 0x40);
+        goto exit;
+    } else
+    if (report[0] == 0x00 && report[1] == 0x0f) {
+        probe(0x00, 0x00, 0x08, 0x46);
+        probe(0x00, 0x00, 0x00, 0x40);
+        goto exit;
+    } else
+    if (report[0] == 0x00 && report[1] == 0x00 && report[2] == 0x00 && report[3] == 0x0f) {
+        //
+        goto exit;
+    } else
+    if (report[0] != 0x00 || !(report[1] & 1)) { // Check if it's the correct report - the controller also sends different status reports
+        goto exit;
+    }
+
+    buttons = ((uint32_t)report[7] << 8) | report[6];
+    trigger_l = report[8];
+    trigger_r = report[9];
+
+    stick_lx = ((int16_t)report[11] << 8) | report[10];
+    stick_ly = ((int16_t)report[13] << 8) | report[12];
+    stick_rx = ((int16_t)report[15] << 8) | report[14];
+    stick_ry = ((int16_t)report[17] << 8) | report[16];
+
+    if (onUpdate) {
+        (*onUpdate)(buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l, trigger_r);
+    }
+exit:
+    if (dev)
+        host->interruptRead(dev, int_in, report, len_listen, false);
+}
+
+/*virtual*/ void USBHostXpad::setVidPid(uint16_t vid, uint16_t pid)
+{
+    // we don't check VID/PID for MSD driver
+}
+
+/*virtual*/ bool USBHostXpad::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
+{
+    USB_INFO("parseInterface class:%02x subclass:%02x protocol:%02x [nb: %d - intf: %d]", intf_class, intf_subclass, intf_protocol, intf_nb, xpad_intf);
+    if ((xpad_intf == -1) &&
+        (intf_class == 0xff) &&
+        (intf_subclass == 0x5d) &&
+        (intf_protocol == 0x81)) {
+        xpad_intf = intf_nb;
+        return true;
+    }
+    return false;
+}
+
+/*virtual*/ bool USBHostXpad::useEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir) //Must return true if the endpoint will be used
+{
+    USB_INFO("useEndpoint nb:%d type:%d dir:%d", intf_nb, type, dir);
+    if (intf_nb == xpad_intf) {
+        if (type == INTERRUPT_ENDPOINT) {
+            nb_ep ++;
+            if (nb_ep == 2)
+                xpad_device_found = true;
+            return true;
+        }
+    }
+    return false;
+}
+
+void USBHostXpad::poll ()
+{
+    probe(0x08, 0x00, 0x0f, 0xc0);
+    probe(0x08, 0x00, 0x0f, 0xc0);
+    probe(0x08, 0x00, 0x0f, 0xc0);
+    probe(0x08, 0x00, 0x0f, 0xc0);
+}
+
+bool USBHostXpad::probe(int val1, int val2, int val3, int val4)
+{
+    unsigned char bdata[32];
+    memset(bdata, 0, sizeof(bdata));
+    bdata[0] = val1;
+    bdata[1] = val2;
+    bdata[2] = val3;
+    bdata[3] = val4;
+    if (dev)
+        host->interruptWrite(dev, int_out, bdata, 12, false);
+    return true;
+}
+
+bool USBHostXpad::effect(uint8_t strong, uint8_t weak)
+{
+    unsigned char odata[32];
+
+    odata[0] = 0x00;
+    odata[1] = 0x01;
+    odata[2] = 0x0F;
+    odata[3] = 0xC0;
+    odata[4] = 0x00;
+    odata[5] = strong;
+    odata[6] = weak;
+    odata[7] = 0x00;
+    odata[8] = 0x00;
+    odata[9] = 0x00;
+    odata[10] = 0x00;
+    odata[11] = 0x00;
+    host->interruptWrite(dev, int_in, odata, 12, false);
+    return true;
+}
+
+#endif
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBHostXpad.h	Tue Dec 10 06:50:37 2013 +0000
@@ -0,0 +1,106 @@
+/*
+ * 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_TOP    0x0001
+#define XPAD_HAT_BOTTOM 0x0002
+#define XPAD_HAT_LEFT   0x0004
+#define XPAD_HAT_RIGHT  0x0008
+#define XPAD_START      0x0010
+#define XPAD_BACK       0x0020
+#define XPAD_ANALOG_L   0x0040
+#define XPAD_ANALOG_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
+
+/** 
+ * 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 effect(uint8_t strong, uint8_t weak);
+
+    void poll ();
+
+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 probe(int val1, int val2, int val3, int val4);
+
+};
+
+#endif
+
+#endif