A program to show the use of a wireless xbox 360 controller. Has details on what data is given out from the controller as well as normalized values for the triggers and sticks
Dependencies: USBHost USBHostXpad mbed
Fork of USBHostXpad_HelloWorld by
Diff: main.cpp
- Revision:
- 9:16ce7a241108
- Parent:
- 4:f8a5c8aa895a
- Child:
- 10:4b0c8727f0c3
--- a/main.cpp Thu Mar 14 14:23:42 2013 +0000 +++ b/main.cpp Tue Dec 10 06:53:49 2013 +0000 @@ -1,50 +1,39 @@ -#include "mbed.h" -#include "USBHostMSD.h" - -DigitalOut led(LED1); - -void msd_task(void const *) { - - USBHostMSD msd("usb"); - int i = 0; - - while(1) { - - // try to connect a MSD device - while(!msd.connect()) { - Thread::wait(500); - } - - // in a loop, append a file - // if the device is disconnected, we try to connect it again - while(1) { - - // append a file - FILE * fp = fopen("/usb/test1.txt", "a"); - - if (fp != NULL) { - fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++); - printf("Goodbye World!\r\n"); - fclose(fp); - } else { - printf("FILE == NULL\r\n"); - } - - Thread::wait(500); - - // if device disconnected, try to connect again - if (!msd.connected()) - break; - } - - } -} - - -int main() { - Thread msdTask(msd_task, NULL, osPriorityNormal, 1024 * 4); - while(1) { - led=!led; - Thread::wait(500); - } -} \ No newline at end of file +#include "mbed.h" +#include "USBHostXpad.h" + +Serial pc(USBTX, USBRX); +DigitalOut led(LED1); + +void onXpadEvent (int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r) { + 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); +} + +void xpad_task(void const *) { + + USBHostXpad xpad; + + while(1) { + // try to connect a Xbox 360 Wireless Controller + while(!xpad.connect()) + Thread::wait(500); + + // when connected, attach handler called on xpad event + xpad.attachEvent(onXpadEvent); + + // wait until the mouse is disconnected + while(xpad.connected()) + Thread::wait(500); + } +} + + +int main() { + pc.baud(115200); + pc.printf("----------\r\n"); + Thread xpadTask(xpad_task, NULL, osPriorityNormal, 1024 * 4); + while(1) { + led=!led; + Thread::wait(500); + } +} +