Wim Huiskamp
/
USBJoystick_HelloWorld
USB HID Device that emulates a Gamecontroller
The notebook page for this application is here
USBJoystick.h@0:5037d4be5b6d, 2012-01-07 (annotated)
- Committer:
- wim
- Date:
- Sat Jan 07 21:03:54 2012 +0000
- Revision:
- 0:5037d4be5b6d
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:5037d4be5b6d | 1 | /* USBJoystick.h */ |
wim | 0:5037d4be5b6d | 2 | /* USB device example: Joystick*/ |
wim | 0:5037d4be5b6d | 3 | /* Copyright (c) 2011 ARM Limited. All rights reserved. */ |
wim | 0:5037d4be5b6d | 4 | /* Modified Mouse code for Joystick - WH 2012 */ |
wim | 0:5037d4be5b6d | 5 | |
wim | 0:5037d4be5b6d | 6 | #ifndef USBJOYSTICK_H |
wim | 0:5037d4be5b6d | 7 | #define USBJOYSTICK_H |
wim | 0:5037d4be5b6d | 8 | |
wim | 0:5037d4be5b6d | 9 | #include "USBHID.h" |
wim | 0:5037d4be5b6d | 10 | |
wim | 0:5037d4be5b6d | 11 | #define REPORT_ID_JOYSTICK 4 |
wim | 0:5037d4be5b6d | 12 | |
wim | 0:5037d4be5b6d | 13 | /* Common usage */ |
wim | 0:5037d4be5b6d | 14 | enum JOY_BUTTON { |
wim | 0:5037d4be5b6d | 15 | JOY_B0 = 1, |
wim | 0:5037d4be5b6d | 16 | JOY_B1 = 2, |
wim | 0:5037d4be5b6d | 17 | JOY_B2 = 4, |
wim | 0:5037d4be5b6d | 18 | JOY_B3 = 8, |
wim | 0:5037d4be5b6d | 19 | }; |
wim | 0:5037d4be5b6d | 20 | |
wim | 0:5037d4be5b6d | 21 | #if(0) |
wim | 0:5037d4be5b6d | 22 | enum JOY_HAT { |
wim | 0:5037d4be5b6d | 23 | JOY_HAT_UP = 0, |
wim | 0:5037d4be5b6d | 24 | JOY_HAT_RIGHT = 1, |
wim | 0:5037d4be5b6d | 25 | JOY_HAT_DOWN = 2, |
wim | 0:5037d4be5b6d | 26 | JOY_HAT_LEFT = 3, |
wim | 0:5037d4be5b6d | 27 | JOY_HAT_NEUTRAL = 4, |
wim | 0:5037d4be5b6d | 28 | }; |
wim | 0:5037d4be5b6d | 29 | #else |
wim | 0:5037d4be5b6d | 30 | enum JOY_HAT { |
wim | 0:5037d4be5b6d | 31 | JOY_HAT_UP = 0, |
wim | 0:5037d4be5b6d | 32 | JOY_HAT_UP_RIGHT = 1, |
wim | 0:5037d4be5b6d | 33 | JOY_HAT_RIGHT = 2, |
wim | 0:5037d4be5b6d | 34 | JOY_HAT_RIGHT_DOWN = 3, |
wim | 0:5037d4be5b6d | 35 | JOY_HAT_DOWN = 4, |
wim | 0:5037d4be5b6d | 36 | JOY_HAT_DOWN_LEFT = 5, |
wim | 0:5037d4be5b6d | 37 | JOY_HAT_LEFT = 6, |
wim | 0:5037d4be5b6d | 38 | JOY_HAT_LEFT_UP = 7, |
wim | 0:5037d4be5b6d | 39 | JOY_HAT_NEUTRAL = 8, |
wim | 0:5037d4be5b6d | 40 | }; |
wim | 0:5037d4be5b6d | 41 | #endif |
wim | 0:5037d4be5b6d | 42 | |
wim | 0:5037d4be5b6d | 43 | /* X, Y and T limits */ |
wim | 0:5037d4be5b6d | 44 | /* These values do not directly map to screen pixels */ |
wim | 0:5037d4be5b6d | 45 | /* Zero may be interpreted as meaning 'no movement' */ |
wim | 0:5037d4be5b6d | 46 | #define JX_MIN_ABS (-127) /*!< The maximum value that we can move to the left on the x-axis */ |
wim | 0:5037d4be5b6d | 47 | #define JY_MIN_ABS (-127) /*!< The maximum value that we can move up on the y-axis */ |
wim | 0:5037d4be5b6d | 48 | #define JT_MIN_ABS (-127) /*!< The minimum value for the throttle */ |
wim | 0:5037d4be5b6d | 49 | #define JX_MAX_ABS (127) /*!< The maximum value that we can move to the right on the x-axis */ |
wim | 0:5037d4be5b6d | 50 | #define JY_MAX_ABS (127) /*!< The maximum value that we can move down on the y-axis */ |
wim | 0:5037d4be5b6d | 51 | #define JT_MAX_ABS (127) /*!< The maximum value for the throttle */ |
wim | 0:5037d4be5b6d | 52 | |
wim | 0:5037d4be5b6d | 53 | /** |
wim | 0:5037d4be5b6d | 54 | * |
wim | 0:5037d4be5b6d | 55 | * USBJoystick example |
wim | 0:5037d4be5b6d | 56 | * @code |
wim | 0:5037d4be5b6d | 57 | * #include "mbed.h" |
wim | 0:5037d4be5b6d | 58 | * #include "USBJoystick.h" |
wim | 0:5037d4be5b6d | 59 | * |
wim | 0:5037d4be5b6d | 60 | * USBJoystick joystick; |
wim | 0:5037d4be5b6d | 61 | * |
wim | 0:5037d4be5b6d | 62 | * int main(void) |
wim | 0:5037d4be5b6d | 63 | * { |
wim | 0:5037d4be5b6d | 64 | * while (1) |
wim | 0:5037d4be5b6d | 65 | * { |
wim | 0:5037d4be5b6d | 66 | * joystick.move(20, 0); |
wim | 0:5037d4be5b6d | 67 | * wait(0.5); |
wim | 0:5037d4be5b6d | 68 | * } |
wim | 0:5037d4be5b6d | 69 | * } |
wim | 0:5037d4be5b6d | 70 | * |
wim | 0:5037d4be5b6d | 71 | * @endcode |
wim | 0:5037d4be5b6d | 72 | * |
wim | 0:5037d4be5b6d | 73 | * |
wim | 0:5037d4be5b6d | 74 | * @code |
wim | 0:5037d4be5b6d | 75 | * #include "mbed.h" |
wim | 0:5037d4be5b6d | 76 | * #include "USBJoystick.h" |
wim | 0:5037d4be5b6d | 77 | * #include <math.h> |
wim | 0:5037d4be5b6d | 78 | * |
wim | 0:5037d4be5b6d | 79 | * USBJoystick joystick; |
wim | 0:5037d4be5b6d | 80 | * |
wim | 0:5037d4be5b6d | 81 | * int main(void) |
wim | 0:5037d4be5b6d | 82 | * { |
wim | 0:5037d4be5b6d | 83 | * int16_t i = 0; |
wim | 0:5037d4be5b6d | 84 | * int16_t throttle = 0; |
wim | 0:5037d4be5b6d | 85 | * int16_t rudder = 0; |
wim | 0:5037d4be5b6d | 86 | * int16_t x = 0; |
wim | 0:5037d4be5b6d | 87 | * int16_t y = 0; |
wim | 0:5037d4be5b6d | 88 | * int32_t radius = 120; |
wim | 0:5037d4be5b6d | 89 | * int32_t angle = 0; |
wim | 0:5037d4be5b6d | 90 | * int8_t button = 0; |
wim | 0:5037d4be5b6d | 91 | * int8_t hat = 0; |
wim | 0:5037d4be5b6d | 92 | * |
wim | 0:5037d4be5b6d | 93 | * while (1) { |
wim | 0:5037d4be5b6d | 94 | * // Basic Joystick |
wim | 0:5037d4be5b6d | 95 | * throttle = (i >> 8) & 0xFF; // value -127 .. 128 |
wim | 0:5037d4be5b6d | 96 | * rudder = (i >> 8) & 0xFF; // value -127 .. 128 |
wim | 0:5037d4be5b6d | 97 | * button = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button |
wim | 0:5037d4be5b6d | 98 | * hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral |
wim | 0:5037d4be5b6d | 99 | * i++; |
wim | 0:5037d4be5b6d | 100 | * |
wim | 0:5037d4be5b6d | 101 | * x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 |
wim | 0:5037d4be5b6d | 102 | * y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 |
wim | 0:5037d4be5b6d | 103 | * angle += 3; |
wim | 0:5037d4be5b6d | 104 | * |
wim | 0:5037d4be5b6d | 105 | * joystick.update(throttle, rudder, x, y, button, hat); |
wim | 0:5037d4be5b6d | 106 | * |
wim | 0:5037d4be5b6d | 107 | * wait(0.001); |
wim | 0:5037d4be5b6d | 108 | * } |
wim | 0:5037d4be5b6d | 109 | * } |
wim | 0:5037d4be5b6d | 110 | * @endcode |
wim | 0:5037d4be5b6d | 111 | */ |
wim | 0:5037d4be5b6d | 112 | |
wim | 0:5037d4be5b6d | 113 | |
wim | 0:5037d4be5b6d | 114 | class USBJoystick: public USBHID { |
wim | 0:5037d4be5b6d | 115 | public: |
wim | 0:5037d4be5b6d | 116 | |
wim | 0:5037d4be5b6d | 117 | /** |
wim | 0:5037d4be5b6d | 118 | * Constructor |
wim | 0:5037d4be5b6d | 119 | * |
wim | 0:5037d4be5b6d | 120 | * @param vendor_id Your vendor_id (default: 0x1234) |
wim | 0:5037d4be5b6d | 121 | * @param product_id Your product_id (default: 0x0002) |
wim | 0:5037d4be5b6d | 122 | * @param product_release Your product_release (default: 0x0001) |
wim | 0:5037d4be5b6d | 123 | */ |
wim | 0:5037d4be5b6d | 124 | USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001): |
wim | 0:5037d4be5b6d | 125 | USBHID(0, 0, vendor_id, product_id, product_release, false) |
wim | 0:5037d4be5b6d | 126 | { |
wim | 0:5037d4be5b6d | 127 | _init(); |
wim | 0:5037d4be5b6d | 128 | connect(); |
wim | 0:5037d4be5b6d | 129 | }; |
wim | 0:5037d4be5b6d | 130 | |
wim | 0:5037d4be5b6d | 131 | /** |
wim | 0:5037d4be5b6d | 132 | * Write a state of the mouse |
wim | 0:5037d4be5b6d | 133 | * |
wim | 0:5037d4be5b6d | 134 | * @param t throttle position |
wim | 0:5037d4be5b6d | 135 | * @param r rudder position |
wim | 0:5037d4be5b6d | 136 | * @param x x-axis position |
wim | 0:5037d4be5b6d | 137 | * @param y y-axis position |
wim | 0:5037d4be5b6d | 138 | * @param buttons buttons state |
wim | 0:5037d4be5b6d | 139 | * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral) |
wim | 0:5037d4be5b6d | 140 | * @returns true if there is no error, false otherwise |
wim | 0:5037d4be5b6d | 141 | */ |
wim | 0:5037d4be5b6d | 142 | bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint8_t buttons, uint8_t hat); |
wim | 0:5037d4be5b6d | 143 | |
wim | 0:5037d4be5b6d | 144 | /** |
wim | 0:5037d4be5b6d | 145 | * Write a state of the mouse |
wim | 0:5037d4be5b6d | 146 | * |
wim | 0:5037d4be5b6d | 147 | * @returns true if there is no error, false otherwise |
wim | 0:5037d4be5b6d | 148 | */ |
wim | 0:5037d4be5b6d | 149 | bool update(); |
wim | 0:5037d4be5b6d | 150 | |
wim | 0:5037d4be5b6d | 151 | /** |
wim | 0:5037d4be5b6d | 152 | * Move the throttle position |
wim | 0:5037d4be5b6d | 153 | * |
wim | 0:5037d4be5b6d | 154 | * @param t throttle position |
wim | 0:5037d4be5b6d | 155 | * @returns true if there is no error, false otherwise |
wim | 0:5037d4be5b6d | 156 | */ |
wim | 0:5037d4be5b6d | 157 | bool throttle(int16_t t); |
wim | 0:5037d4be5b6d | 158 | |
wim | 0:5037d4be5b6d | 159 | /** |
wim | 0:5037d4be5b6d | 160 | * Move the rudder position |
wim | 0:5037d4be5b6d | 161 | * |
wim | 0:5037d4be5b6d | 162 | * @param r rudder position |
wim | 0:5037d4be5b6d | 163 | * @returns true if there is no error, false otherwise |
wim | 0:5037d4be5b6d | 164 | */ |
wim | 0:5037d4be5b6d | 165 | bool rudder(int16_t r); |
wim | 0:5037d4be5b6d | 166 | |
wim | 0:5037d4be5b6d | 167 | /** |
wim | 0:5037d4be5b6d | 168 | * Move the cursor to (x, y) |
wim | 0:5037d4be5b6d | 169 | * |
wim | 0:5037d4be5b6d | 170 | * @param x-axis position |
wim | 0:5037d4be5b6d | 171 | * @param y-axis position |
wim | 0:5037d4be5b6d | 172 | * @returns true if there is no error, false otherwise |
wim | 0:5037d4be5b6d | 173 | */ |
wim | 0:5037d4be5b6d | 174 | bool move(int16_t x, int16_t y); |
wim | 0:5037d4be5b6d | 175 | |
wim | 0:5037d4be5b6d | 176 | /** |
wim | 0:5037d4be5b6d | 177 | * Press one or several buttons |
wim | 0:5037d4be5b6d | 178 | * |
wim | 0:5037d4be5b6d | 179 | * @param button button state |
wim | 0:5037d4be5b6d | 180 | * @returns true if there is no error, false otherwise |
wim | 0:5037d4be5b6d | 181 | */ |
wim | 0:5037d4be5b6d | 182 | bool button(uint8_t button); |
wim | 0:5037d4be5b6d | 183 | |
wim | 0:5037d4be5b6d | 184 | /** |
wim | 0:5037d4be5b6d | 185 | * Press hat |
wim | 0:5037d4be5b6d | 186 | * |
wim | 0:5037d4be5b6d | 187 | * @param hat hat state |
wim | 0:5037d4be5b6d | 188 | * @returns true if there is no error, false otherwise |
wim | 0:5037d4be5b6d | 189 | */ |
wim | 0:5037d4be5b6d | 190 | bool hat(uint8_t hat); |
wim | 0:5037d4be5b6d | 191 | |
wim | 0:5037d4be5b6d | 192 | /* |
wim | 0:5037d4be5b6d | 193 | * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
wim | 0:5037d4be5b6d | 194 | * |
wim | 0:5037d4be5b6d | 195 | * @returns pointer to the report descriptor |
wim | 0:5037d4be5b6d | 196 | */ |
wim | 0:5037d4be5b6d | 197 | virtual uint8_t * reportDesc(); |
wim | 0:5037d4be5b6d | 198 | |
wim | 0:5037d4be5b6d | 199 | private: |
wim | 0:5037d4be5b6d | 200 | int8_t _t; |
wim | 0:5037d4be5b6d | 201 | int8_t _r; |
wim | 0:5037d4be5b6d | 202 | int8_t _x; |
wim | 0:5037d4be5b6d | 203 | int8_t _y; |
wim | 0:5037d4be5b6d | 204 | uint8_t _button; |
wim | 0:5037d4be5b6d | 205 | uint8_t _hat; |
wim | 0:5037d4be5b6d | 206 | |
wim | 0:5037d4be5b6d | 207 | void _init(); |
wim | 0:5037d4be5b6d | 208 | }; |
wim | 0:5037d4be5b6d | 209 | |
wim | 0:5037d4be5b6d | 210 | #endif |