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
main.cpp@13:015ec003c063, 2016-03-09 (annotated)
- Committer:
- JakBlackburn
- Date:
- Wed Mar 09 11:03:36 2016 +0000
- Revision:
- 13:015ec003c063
- Parent:
- 11:f76e120a8520
- Child:
- 14:9a82555f7a3b
added values for the buttons and normalized the values of the triggers and analogue sticks.; Also documented how the Xpad gives out data
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 9:16ce7a241108 | 1 | #include "mbed.h" |
okini3939 | 9:16ce7a241108 | 2 | #include "USBHostXpad.h" |
okini3939 | 9:16ce7a241108 | 3 | |
okini3939 | 9:16ce7a241108 | 4 | Serial pc(USBTX, USBRX); |
okini3939 | 9:16ce7a241108 | 5 | DigitalOut led(LED1); |
okini3939 | 10:4b0c8727f0c3 | 6 | volatile int poll = 0; |
JakBlackburn | 13:015ec003c063 | 7 | /** |
JakBlackburn | 13:015ec003c063 | 8 | @namespace AXYB |
JakBlackburn | 13:015ec003c063 | 9 | @brief Integer for storing the hex of the A X Y B buttons |
JakBlackburn | 13:015ec003c063 | 10 | @brief XPad returns a 4 digit hex for all buttons- AXYB buttons are stored in first value |
JakBlackburn | 13:015ec003c063 | 11 | @param A - given as a 1 |
JakBlackburn | 13:015ec003c063 | 12 | @param B - given as a 2 |
JakBlackburn | 13:015ec003c063 | 13 | @param X - given as a 4 |
JakBlackburn | 13:015ec003c063 | 14 | @param Y - given as a 8 |
JakBlackburn | 13:015ec003c063 | 15 | */ |
JakBlackburn | 13:015ec003c063 | 16 | uint8_t AXYB=0x0; |
JakBlackburn | 13:015ec003c063 | 17 | /** |
JakBlackburn | 13:015ec003c063 | 18 | @namespace XLBRB |
JakBlackburn | 13:015ec003c063 | 19 | @brief Integer for storing the hex of the LB,RB and center X buttons |
JakBlackburn | 13:015ec003c063 | 20 | @brief XPad returns a 4 digit hex for all buttons- AXYB buttons are stored in second value |
JakBlackburn | 13:015ec003c063 | 21 | @param LB - given as a 1 |
JakBlackburn | 13:015ec003c063 | 22 | @param R - given as a 2 |
JakBlackburn | 13:015ec003c063 | 23 | @param X - given as a 4 |
okini3939 | 9:16ce7a241108 | 24 | |
JakBlackburn | 13:015ec003c063 | 25 | */ |
JakBlackburn | 13:015ec003c063 | 26 | uint8_t XLBRB=0x0; |
JakBlackburn | 13:015ec003c063 | 27 | |
JakBlackburn | 13:015ec003c063 | 28 | /** |
JakBlackburn | 13:015ec003c063 | 29 | @namespace bkStrtLCRC |
JakBlackburn | 13:015ec003c063 | 30 | @brief Integer for storing the hex of the Left analog button,Right analog button,back and start buttons |
JakBlackburn | 13:015ec003c063 | 31 | @brief XPad returns a 4 digit hex for all buttons- AXYB buttons are stored in third value |
JakBlackburn | 13:015ec003c063 | 32 | @param start - given as a 1 |
JakBlackburn | 13:015ec003c063 | 33 | @param back - given as a 2 |
JakBlackburn | 13:015ec003c063 | 34 | @param LC - given as a 4 |
JakBlackburn | 13:015ec003c063 | 35 | @param RC - given as a 8 |
JakBlackburn | 13:015ec003c063 | 36 | */ |
JakBlackburn | 13:015ec003c063 | 37 | uint8_t bkStrtLCRC=0x0; |
JakBlackburn | 13:015ec003c063 | 38 | /** |
JakBlackburn | 13:015ec003c063 | 39 | @namespace DPad |
JakBlackburn | 13:015ec003c063 | 40 | @brief Integer for storing the hex of the Directional buttons |
JakBlackburn | 13:015ec003c063 | 41 | @brief XPad returns a 4 digit hex for all buttons- AXYB buttons are stored in fourth value |
JakBlackburn | 13:015ec003c063 | 42 | @param Up - given as a 1 |
JakBlackburn | 13:015ec003c063 | 43 | @param Down - given as a 2 |
JakBlackburn | 13:015ec003c063 | 44 | @param Left - given as a 4 |
JakBlackburn | 13:015ec003c063 | 45 | @param Right - given as a 8 |
JakBlackburn | 13:015ec003c063 | 46 | */ |
JakBlackburn | 13:015ec003c063 | 47 | uint8_t DPad=0x0; |
JakBlackburn | 13:015ec003c063 | 48 | /** |
JakBlackburn | 13:015ec003c063 | 49 | @namespace LSY |
JakBlackburn | 13:015ec003c063 | 50 | @brief float for storing the value of the Left Analogue Stick's Y axis |
JakBlackburn | 13:015ec003c063 | 51 | @brief XPad returns a value between -32768(down) and 32767(up) |
JakBlackburn | 13:015ec003c063 | 52 | @there is a deadzone between around -4000 and 4000 where the value returned is not consistent when in the fixed position(assummed 0,0 point) |
JakBlackburn | 13:015ec003c063 | 53 | */ |
JakBlackburn | 13:015ec003c063 | 54 | float LSY=0x0; |
JakBlackburn | 13:015ec003c063 | 55 | /** |
JakBlackburn | 13:015ec003c063 | 56 | @namespace LSX |
JakBlackburn | 13:015ec003c063 | 57 | @brief float for storing the value of the Left Analogue Stick's X axis |
JakBlackburn | 13:015ec003c063 | 58 | @brief XPad returns a value between -32768(down) and 32767(up) |
JakBlackburn | 13:015ec003c063 | 59 | @there is a deadzone between around -4000 and 4000 where the value returned is not consistent when in the fixed position(assummed 0,0 point) |
JakBlackburn | 13:015ec003c063 | 60 | */ |
JakBlackburn | 13:015ec003c063 | 61 | float LSX=0x0; |
JakBlackburn | 13:015ec003c063 | 62 | /** |
JakBlackburn | 13:015ec003c063 | 63 | @namespace RSY |
JakBlackburn | 13:015ec003c063 | 64 | @brief float for storing the value of the Right Analogue Stick's Y axis |
JakBlackburn | 13:015ec003c063 | 65 | @brief XPad returns a value between -32768(down) and 32767(up) |
JakBlackburn | 13:015ec003c063 | 66 | @there is a deadzone between around -4000 and 4000 where the value returned is not consistent when in the fixed position(assummed 0,0 point) |
JakBlackburn | 13:015ec003c063 | 67 | */ |
JakBlackburn | 13:015ec003c063 | 68 | float RSY=0x0; |
JakBlackburn | 13:015ec003c063 | 69 | /** |
JakBlackburn | 13:015ec003c063 | 70 | @namespace RSX |
JakBlackburn | 13:015ec003c063 | 71 | @brief float for storing the value of the Right Analogue Stick's X axis |
JakBlackburn | 13:015ec003c063 | 72 | @brief XPad returns a value between -32768(down) and 32767(up) |
JakBlackburn | 13:015ec003c063 | 73 | @there is a deadzone between around -4000 and 4000 where the value returned is not consistent when in the fixed position(assummed 0,0 point) |
JakBlackburn | 13:015ec003c063 | 74 | */ |
JakBlackburn | 13:015ec003c063 | 75 | float RSX=0x0; |
JakBlackburn | 13:015ec003c063 | 76 | /** |
JakBlackburn | 13:015ec003c063 | 77 | @namespace sN |
JakBlackburn | 13:015ec003c063 | 78 | @brief float for storing the stick Normalising value |
JakBlackburn | 13:015ec003c063 | 79 | @brief makes the range of the sticks -10 to 10 |
JakBlackburn | 13:015ec003c063 | 80 | */ |
JakBlackburn | 13:015ec003c063 | 81 | const float sN=0.000305175781;//(10/32768) |
JakBlackburn | 13:015ec003c063 | 82 | /** |
JakBlackburn | 13:015ec003c063 | 83 | @namespace Lt |
JakBlackburn | 13:015ec003c063 | 84 | @brief float for storing the value of the Left trigger |
JakBlackburn | 13:015ec003c063 | 85 | @brief XPad returns a value between 0(not pressed) and 255(fully pressed) |
JakBlackburn | 13:015ec003c063 | 86 | @ |
JakBlackburn | 13:015ec003c063 | 87 | */ |
JakBlackburn | 13:015ec003c063 | 88 | float Lt=0x0; |
JakBlackburn | 13:015ec003c063 | 89 | /** |
JakBlackburn | 13:015ec003c063 | 90 | @namespace Rt |
JakBlackburn | 13:015ec003c063 | 91 | @brief float for storing the value of the Left trigger |
JakBlackburn | 13:015ec003c063 | 92 | @brief XPad returns a value between 0(not pressed) and 255(fully pressed) |
JakBlackburn | 13:015ec003c063 | 93 | @ |
JakBlackburn | 13:015ec003c063 | 94 | */ |
JakBlackburn | 13:015ec003c063 | 95 | float Rt=0x0; |
JakBlackburn | 13:015ec003c063 | 96 | /** |
JakBlackburn | 13:015ec003c063 | 97 | @namespace tN |
JakBlackburn | 13:015ec003c063 | 98 | @brief float for storing the trigger Normalising value |
JakBlackburn | 13:015ec003c063 | 99 | @brief makes the range of the triggers 0 to 10 |
JakBlackburn | 13:015ec003c063 | 100 | */ |
JakBlackburn | 13:015ec003c063 | 101 | const float tN=0.03921568627;//(10/255) |
JakBlackburn | 13:015ec003c063 | 102 | |
JakBlackburn | 13:015ec003c063 | 103 | void XpadValues(int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r){ |
JakBlackburn | 13:015ec003c063 | 104 | std::printf("Xpad: %04x %-5d %-5d %-5d %-5d %d %d\r\n", buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l, trigger_r);//%04x makes it display (hex) 0 as 0000 %02x |
JakBlackburn | 13:015ec003c063 | 105 | std::printf("buttons: %04x\r\n",buttons); |
JakBlackburn | 13:015ec003c063 | 106 | std::printf("AXYB: %x\r\n",AXYB); |
JakBlackburn | 13:015ec003c063 | 107 | std::printf("XLBRB: %x\r\n",XLBRB); |
JakBlackburn | 13:015ec003c063 | 108 | std::printf("bkStrtLCRC: %x\r\n",bkStrtLCRC); |
JakBlackburn | 13:015ec003c063 | 109 | std::printf("Dpad: %x\r\n",DPad); |
JakBlackburn | 13:015ec003c063 | 110 | std::printf("LSY: %.2f\r\n",LSY); |
JakBlackburn | 13:015ec003c063 | 111 | std::printf("LSX: %.2f\r\n",LSX); |
JakBlackburn | 13:015ec003c063 | 112 | std::printf("RSY: %.2f\r\n",RSY); |
JakBlackburn | 13:015ec003c063 | 113 | std::printf("RSX: %.2f\r\n",RSX); |
JakBlackburn | 13:015ec003c063 | 114 | std::printf("Lt: %.2f\r\n",Lt); |
JakBlackburn | 13:015ec003c063 | 115 | std::printf("Rt: %.2f\r\n",Rt); |
JakBlackburn | 13:015ec003c063 | 116 | } |
JakBlackburn | 13:015ec003c063 | 117 | |
JakBlackburn | 13:015ec003c063 | 118 | |
JakBlackburn | 13:015ec003c063 | 119 | void onXpadEvent (int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r) { |
JakBlackburn | 13:015ec003c063 | 120 | AXYB=buttons>>12; |
JakBlackburn | 13:015ec003c063 | 121 | XLBRB=(buttons&0x0f00)>>8; |
JakBlackburn | 13:015ec003c063 | 122 | bkStrtLCRC=(buttons&0x00f0)>>4; |
JakBlackburn | 13:015ec003c063 | 123 | DPad=buttons&0x000f; |
JakBlackburn | 13:015ec003c063 | 124 | LSY=stick_ly*sN; |
JakBlackburn | 13:015ec003c063 | 125 | LSX=stick_lx*sN; |
JakBlackburn | 13:015ec003c063 | 126 | RSY=stick_ry*sN; |
JakBlackburn | 13:015ec003c063 | 127 | RSX=stick_rx*sN; |
JakBlackburn | 13:015ec003c063 | 128 | Lt=trigger_l*tN; |
JakBlackburn | 13:015ec003c063 | 129 | Rt=trigger_r*tN; |
JakBlackburn | 13:015ec003c063 | 130 | //XpadValues(buttons, stick_lx, stick_ly, stick_rx, stick_ry, trigger_l, trigger_r); |
okini3939 | 10:4b0c8727f0c3 | 131 | poll = 0; |
okini3939 | 9:16ce7a241108 | 132 | } |
okini3939 | 9:16ce7a241108 | 133 | |
okini3939 | 9:16ce7a241108 | 134 | void xpad_task(void const *) { |
okini3939 | 9:16ce7a241108 | 135 | USBHostXpad xpad; |
okini3939 | 9:16ce7a241108 | 136 | |
okini3939 | 9:16ce7a241108 | 137 | while(1) { |
okini3939 | 9:16ce7a241108 | 138 | // try to connect a Xbox 360 Wireless Controller |
okini3939 | 9:16ce7a241108 | 139 | while(!xpad.connect()) |
okini3939 | 9:16ce7a241108 | 140 | Thread::wait(500); |
okini3939 | 9:16ce7a241108 | 141 | |
okini3939 | 9:16ce7a241108 | 142 | // when connected, attach handler called on xpad event |
okini3939 | 9:16ce7a241108 | 143 | xpad.attachEvent(onXpadEvent); |
okini3939 | 10:4b0c8727f0c3 | 144 | |
okini3939 | 11:f76e120a8520 | 145 | xpad.led(USBHostXpad::LED_ROTATE); |
okini3939 | 10:4b0c8727f0c3 | 146 | Thread::wait(500); |
okini3939 | 10:4b0c8727f0c3 | 147 | xpad.rumble(0xff, 0); |
okini3939 | 10:4b0c8727f0c3 | 148 | Thread::wait(500); |
okini3939 | 10:4b0c8727f0c3 | 149 | xpad.rumble(0, 0xff); |
okini3939 | 10:4b0c8727f0c3 | 150 | Thread::wait(500); |
okini3939 | 10:4b0c8727f0c3 | 151 | xpad.rumble(0, 0); |
okini3939 | 10:4b0c8727f0c3 | 152 | Thread::wait(500); |
okini3939 | 11:f76e120a8520 | 153 | xpad.led(USBHostXpad::LED1_ON); |
okini3939 | 10:4b0c8727f0c3 | 154 | |
okini3939 | 9:16ce7a241108 | 155 | // wait until the mouse is disconnected |
okini3939 | 10:4b0c8727f0c3 | 156 | while(xpad.connected()) { |
okini3939 | 9:16ce7a241108 | 157 | Thread::wait(500); |
okini3939 | 10:4b0c8727f0c3 | 158 | poll ++; |
okini3939 | 10:4b0c8727f0c3 | 159 | if (poll > 10) { |
okini3939 | 10:4b0c8727f0c3 | 160 | xpad.restart(); |
okini3939 | 10:4b0c8727f0c3 | 161 | poll = 0; |
okini3939 | 10:4b0c8727f0c3 | 162 | } |
okini3939 | 10:4b0c8727f0c3 | 163 | } |
okini3939 | 9:16ce7a241108 | 164 | } |
okini3939 | 9:16ce7a241108 | 165 | } |
okini3939 | 9:16ce7a241108 | 166 | |
okini3939 | 9:16ce7a241108 | 167 | |
okini3939 | 9:16ce7a241108 | 168 | int main() { |
JakBlackburn | 13:015ec003c063 | 169 | pc.baud(9600); |
okini3939 | 9:16ce7a241108 | 170 | pc.printf("----------\r\n"); |
okini3939 | 9:16ce7a241108 | 171 | Thread xpadTask(xpad_task, NULL, osPriorityNormal, 1024 * 4); |
okini3939 | 9:16ce7a241108 | 172 | while(1) { |
JakBlackburn | 13:015ec003c063 | 173 | // led=!led; |
JakBlackburn | 13:015ec003c063 | 174 | // Thread::wait(500); |
okini3939 | 9:16ce7a241108 | 175 | } |
okini3939 | 9:16ce7a241108 | 176 | } |
okini3939 | 9:16ce7a241108 | 177 |