Xbox 360 Wireless Controller for Windows library

Dependencies:   USBHost USBHostXpad mbed

Fork of USBHostMSD_HelloWorld by Samuel Mokrani

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBHostXpad.h"
00003 
00004 Serial pc(USBTX, USBRX);
00005 DigitalOut led(LED1);
00006 volatile int poll = 0;
00007 
00008 void onXpadEvent (int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r) {
00009     std::printf("Xpad: %04x %-5d %-5d %-5d %-5d %02x %02x\r\n", buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l, trigger_r);
00010     poll = 0;
00011 }
00012 
00013 void xpad_task(void const *) {
00014     USBHostXpad xpad;
00015 
00016     while(1) {
00017         // try to connect a Xbox 360 Wireless Controller
00018         while(!xpad.connect())
00019             Thread::wait(500);
00020     
00021         // when connected, attach handler called on xpad event
00022         xpad.attachEvent(onXpadEvent);
00023 
00024         xpad.led(USBHostXpad::LED_ROTATE);
00025         Thread::wait(500);
00026         xpad.rumble(0xff, 0);
00027         Thread::wait(500);
00028         xpad.rumble(0, 0xff);
00029         Thread::wait(500);
00030         xpad.rumble(0, 0);
00031         Thread::wait(500);
00032         xpad.led(USBHostXpad::LED1_ON);
00033 
00034         // wait until the mouse is disconnected
00035         while(xpad.connected()) {
00036             Thread::wait(500);
00037             poll ++;
00038             if (poll > 10) {
00039                 xpad.restart();
00040                 poll = 0;
00041             }
00042         }
00043     }
00044 }
00045 
00046 
00047 int main() {
00048     pc.baud(115200);
00049     pc.printf("----------\r\n");
00050     Thread xpadTask(xpad_task, NULL, osPriorityNormal, 1024 * 4);
00051     while(1) {
00052         led=!led;
00053         Thread::wait(500);
00054     }
00055 }
00056