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

Revision:
0:bd0f6bf72a8b
Child:
1:5bb153989f33
--- /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
+