test
USBJoystick.h@0:e086541742c3, 2017-01-05 (annotated)
- Committer:
- wim
- Date:
- Thu Jan 05 14:22:02 2017 +0000
- Revision:
- 0:e086541742c3
- Child:
- 1:bd9a9b533043
USBJoystick updated for 32 buttons and added wait-for-connect.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:e086541742c3 | 1 | /* mbed USBJoystick Library |
wim | 0:e086541742c3 | 2 | * Copyright (c) 2012, v01: Initial version, WH, |
wim | 0:e086541742c3 | 3 | * Modified USBMouse code ARM Limited. |
wim | 0:e086541742c3 | 4 | * (c) 2010-2011 mbed.org, MIT License |
wim | 0:e086541742c3 | 5 | * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button |
wim | 0:e086541742c3 | 6 | * |
wim | 0:e086541742c3 | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:e086541742c3 | 8 | * of this software and associated documentation files (the "Software"), to deal |
wim | 0:e086541742c3 | 9 | * in the Software without restriction, inclumosig without limitation the rights |
wim | 0:e086541742c3 | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:e086541742c3 | 11 | * copies of the Software, and to permit persons to whom the Software is |
wim | 0:e086541742c3 | 12 | * furnished to do so, subject to the following conditions: |
wim | 0:e086541742c3 | 13 | * |
wim | 0:e086541742c3 | 14 | * The above copyright notice and this permission notice shall be included in |
wim | 0:e086541742c3 | 15 | * all copies or substantial portions of the Software. |
wim | 0:e086541742c3 | 16 | * |
wim | 0:e086541742c3 | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:e086541742c3 | 18 | * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:e086541742c3 | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:e086541742c3 | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:e086541742c3 | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:e086541742c3 | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:e086541742c3 | 23 | * THE SOFTWARE. |
wim | 0:e086541742c3 | 24 | */ |
wim | 0:e086541742c3 | 25 | |
wim | 0:e086541742c3 | 26 | #ifndef USBJOYSTICK_H |
wim | 0:e086541742c3 | 27 | #define USBJOYSTICK_H |
wim | 0:e086541742c3 | 28 | |
wim | 0:e086541742c3 | 29 | #include "USBHID.h" |
wim | 0:e086541742c3 | 30 | |
wim | 0:e086541742c3 | 31 | #define REPORT_ID_JOYSTICK 4 |
wim | 0:e086541742c3 | 32 | |
wim | 0:e086541742c3 | 33 | //Configure Joystick |
wim | 0:e086541742c3 | 34 | #define HAT4 0 |
wim | 0:e086541742c3 | 35 | #define HAT8 1 |
wim | 0:e086541742c3 | 36 | |
wim | 0:e086541742c3 | 37 | #define BUTTONS4 0 |
wim | 0:e086541742c3 | 38 | #define BUTTONS8 0 |
wim | 0:e086541742c3 | 39 | #define BUTTONS32 1 |
wim | 0:e086541742c3 | 40 | |
wim | 0:e086541742c3 | 41 | |
wim | 0:e086541742c3 | 42 | /* Common usage */ |
wim | 0:e086541742c3 | 43 | enum JOY_BUTTON { |
wim | 0:e086541742c3 | 44 | JOY_B0 = 1, |
wim | 0:e086541742c3 | 45 | JOY_B1 = 2, |
wim | 0:e086541742c3 | 46 | JOY_B2 = 4, |
wim | 0:e086541742c3 | 47 | JOY_B3 = 8, |
wim | 0:e086541742c3 | 48 | }; |
wim | 0:e086541742c3 | 49 | |
wim | 0:e086541742c3 | 50 | #if (HAT4 == 1) |
wim | 0:e086541742c3 | 51 | enum JOY_HAT { |
wim | 0:e086541742c3 | 52 | JOY_HAT_UP = 0, |
wim | 0:e086541742c3 | 53 | JOY_HAT_RIGHT = 1, |
wim | 0:e086541742c3 | 54 | JOY_HAT_DOWN = 2, |
wim | 0:e086541742c3 | 55 | JOY_HAT_LEFT = 3, |
wim | 0:e086541742c3 | 56 | JOY_HAT_NEUTRAL = 4, |
wim | 0:e086541742c3 | 57 | }; |
wim | 0:e086541742c3 | 58 | #endif |
wim | 0:e086541742c3 | 59 | #if (HAT8 == 1) |
wim | 0:e086541742c3 | 60 | enum JOY_HAT { |
wim | 0:e086541742c3 | 61 | JOY_HAT_UP = 0, |
wim | 0:e086541742c3 | 62 | JOY_HAT_UP_RIGHT = 1, |
wim | 0:e086541742c3 | 63 | JOY_HAT_RIGHT = 2, |
wim | 0:e086541742c3 | 64 | JOY_HAT_RIGHT_DOWN = 3, |
wim | 0:e086541742c3 | 65 | JOY_HAT_DOWN = 4, |
wim | 0:e086541742c3 | 66 | JOY_HAT_DOWN_LEFT = 5, |
wim | 0:e086541742c3 | 67 | JOY_HAT_LEFT = 6, |
wim | 0:e086541742c3 | 68 | JOY_HAT_LEFT_UP = 7, |
wim | 0:e086541742c3 | 69 | JOY_HAT_NEUTRAL = 8, |
wim | 0:e086541742c3 | 70 | }; |
wim | 0:e086541742c3 | 71 | #endif |
wim | 0:e086541742c3 | 72 | |
wim | 0:e086541742c3 | 73 | /* X, Y and T limits */ |
wim | 0:e086541742c3 | 74 | /* These values do not directly map to screen pixels */ |
wim | 0:e086541742c3 | 75 | /* Zero may be interpreted as meaning 'no movement' */ |
wim | 0:e086541742c3 | 76 | #define JX_MIN_ABS (-127) /*!< The maximum value that we can move to the left on the x-axis */ |
wim | 0:e086541742c3 | 77 | #define JY_MIN_ABS (-127) /*!< The maximum value that we can move up on the y-axis */ |
wim | 0:e086541742c3 | 78 | #define JT_MIN_ABS (-127) /*!< The minimum value for the throttle */ |
wim | 0:e086541742c3 | 79 | #define JX_MAX_ABS (127) /*!< The maximum value that we can move to the right on the x-axis */ |
wim | 0:e086541742c3 | 80 | #define JY_MAX_ABS (127) /*!< The maximum value that we can move down on the y-axis */ |
wim | 0:e086541742c3 | 81 | #define JT_MAX_ABS (127) /*!< The maximum value for the throttle */ |
wim | 0:e086541742c3 | 82 | |
wim | 0:e086541742c3 | 83 | /** |
wim | 0:e086541742c3 | 84 | * |
wim | 0:e086541742c3 | 85 | * USBJoystick example |
wim | 0:e086541742c3 | 86 | * @code |
wim | 0:e086541742c3 | 87 | * #include "mbed.h" |
wim | 0:e086541742c3 | 88 | * #include "USBJoystick.h" |
wim | 0:e086541742c3 | 89 | * |
wim | 0:e086541742c3 | 90 | * USBJoystick joystick; |
wim | 0:e086541742c3 | 91 | * |
wim | 0:e086541742c3 | 92 | * int main(void) |
wim | 0:e086541742c3 | 93 | * { |
wim | 0:e086541742c3 | 94 | * while (1) |
wim | 0:e086541742c3 | 95 | * { |
wim | 0:e086541742c3 | 96 | * joystick.move(20, 0); |
wim | 0:e086541742c3 | 97 | * wait(0.5); |
wim | 0:e086541742c3 | 98 | * } |
wim | 0:e086541742c3 | 99 | * } |
wim | 0:e086541742c3 | 100 | * |
wim | 0:e086541742c3 | 101 | * @endcode |
wim | 0:e086541742c3 | 102 | * |
wim | 0:e086541742c3 | 103 | * |
wim | 0:e086541742c3 | 104 | * @code |
wim | 0:e086541742c3 | 105 | * #include "mbed.h" |
wim | 0:e086541742c3 | 106 | * #include "USBJoystick.h" |
wim | 0:e086541742c3 | 107 | * |
wim | 0:e086541742c3 | 108 | * USBJoystick joystick; |
wim | 0:e086541742c3 | 109 | * |
wim | 0:e086541742c3 | 110 | * int main(void) { |
wim | 0:e086541742c3 | 111 | * uint16_t i = 0; |
wim | 0:e086541742c3 | 112 | * int16_t throttle = 0; |
wim | 0:e086541742c3 | 113 | * int16_t rudder = 0; |
wim | 0:e086541742c3 | 114 | * int16_t x = 0; |
wim | 0:e086541742c3 | 115 | * int16_t y = 0; |
wim | 0:e086541742c3 | 116 | * int32_t radius = 120; |
wim | 0:e086541742c3 | 117 | * int32_t angle = 0; |
wim | 0:e086541742c3 | 118 | * uint32_t buttons = 0; |
wim | 0:e086541742c3 | 119 | * uint8_t hat = 0; |
wim | 0:e086541742c3 | 120 | * |
wim | 0:e086541742c3 | 121 | * while (1) { |
wim | 0:e086541742c3 | 122 | * // Basic Joystick |
wim | 0:e086541742c3 | 123 | * throttle = (i >> 8) & 0xFF; // value -127 .. 128 |
wim | 0:e086541742c3 | 124 | * rudder = (i >> 8) & 0xFF; // value -127 .. 128 |
wim | 0:e086541742c3 | 125 | * buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button |
wim | 0:e086541742c3 | 126 | * hat = (i >> 8) & 0x07; // value 0 .. 7 or 8 for neutral |
wim | 0:e086541742c3 | 127 | * i++; |
wim | 0:e086541742c3 | 128 | * |
wim | 0:e086541742c3 | 129 | * x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 |
wim | 0:e086541742c3 | 130 | * y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 |
wim | 0:e086541742c3 | 131 | * angle += 3; |
wim | 0:e086541742c3 | 132 | * |
wim | 0:e086541742c3 | 133 | * joystick.update(throttle, rudder, x, y, buttons, hat); |
wim | 0:e086541742c3 | 134 | * |
wim | 0:e086541742c3 | 135 | * wait(0.001); |
wim | 0:e086541742c3 | 136 | * } |
wim | 0:e086541742c3 | 137 | * } |
wim | 0:e086541742c3 | 138 | * @endcode |
wim | 0:e086541742c3 | 139 | */ |
wim | 0:e086541742c3 | 140 | |
wim | 0:e086541742c3 | 141 | |
wim | 0:e086541742c3 | 142 | class USBJoystick: public USBHID { |
wim | 0:e086541742c3 | 143 | public: |
wim | 0:e086541742c3 | 144 | |
wim | 0:e086541742c3 | 145 | /** |
wim | 0:e086541742c3 | 146 | * Constructor |
wim | 0:e086541742c3 | 147 | * |
wim | 0:e086541742c3 | 148 | * @param vendor_id Your vendor_id (default: 0x1234) |
wim | 0:e086541742c3 | 149 | * @param product_id Your product_id (default: 0x0002) |
wim | 0:e086541742c3 | 150 | * @param product_release Your product_release (default: 0x0001) |
wim | 0:e086541742c3 | 151 | */ |
wim | 0:e086541742c3 | 152 | // USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001, int waitForConnect = true): // 4 buttons, no padding on buttons |
wim | 0:e086541742c3 | 153 | // USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0500, uint16_t product_release = 0x0001, int waitForConnect = true): // 8 buttons, no padding on buttons |
wim | 0:e086541742c3 | 154 | USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0600, uint16_t product_release = 0x0001, int waitForConnect = true): // 32 buttons, no padding on buttons |
wim | 0:e086541742c3 | 155 | USBHID(0, 0, vendor_id, product_id, product_release, false) { |
wim | 0:e086541742c3 | 156 | _init(); |
wim | 0:e086541742c3 | 157 | connect(waitForConnect); |
wim | 0:e086541742c3 | 158 | }; |
wim | 0:e086541742c3 | 159 | |
wim | 0:e086541742c3 | 160 | /** |
wim | 0:e086541742c3 | 161 | * Write state of the joystick |
wim | 0:e086541742c3 | 162 | * |
wim | 0:e086541742c3 | 163 | * @param t throttle position |
wim | 0:e086541742c3 | 164 | * @param r rudder position |
wim | 0:e086541742c3 | 165 | * @param x x-axis position |
wim | 0:e086541742c3 | 166 | * @param y y-axis position |
wim | 0:e086541742c3 | 167 | * @param buttons buttons state |
wim | 0:e086541742c3 | 168 | * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral) |
wim | 0:e086541742c3 | 169 | * @returns true if there is no error, false otherwise |
wim | 0:e086541742c3 | 170 | */ |
wim | 0:e086541742c3 | 171 | bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons, uint8_t hat); |
wim | 0:e086541742c3 | 172 | |
wim | 0:e086541742c3 | 173 | /** |
wim | 0:e086541742c3 | 174 | * Write state of the joystick |
wim | 0:e086541742c3 | 175 | * |
wim | 0:e086541742c3 | 176 | * @returns true if there is no error, false otherwise |
wim | 0:e086541742c3 | 177 | */ |
wim | 0:e086541742c3 | 178 | bool update(); |
wim | 0:e086541742c3 | 179 | |
wim | 0:e086541742c3 | 180 | /** |
wim | 0:e086541742c3 | 181 | * Move the throttle position |
wim | 0:e086541742c3 | 182 | * |
wim | 0:e086541742c3 | 183 | * @param t throttle position |
wim | 0:e086541742c3 | 184 | * @returns true if there is no error, false otherwise |
wim | 0:e086541742c3 | 185 | */ |
wim | 0:e086541742c3 | 186 | bool throttle(int16_t t); |
wim | 0:e086541742c3 | 187 | |
wim | 0:e086541742c3 | 188 | /** |
wim | 0:e086541742c3 | 189 | * Move the rudder position |
wim | 0:e086541742c3 | 190 | * |
wim | 0:e086541742c3 | 191 | * @param r rudder position |
wim | 0:e086541742c3 | 192 | * @returns true if there is no error, false otherwise |
wim | 0:e086541742c3 | 193 | */ |
wim | 0:e086541742c3 | 194 | bool rudder(int16_t r); |
wim | 0:e086541742c3 | 195 | |
wim | 0:e086541742c3 | 196 | /** |
wim | 0:e086541742c3 | 197 | * Move the cursor to (x, y) |
wim | 0:e086541742c3 | 198 | * |
wim | 0:e086541742c3 | 199 | * @param x-axis position |
wim | 0:e086541742c3 | 200 | * @param y-axis position |
wim | 0:e086541742c3 | 201 | * @returns true if there is no error, false otherwise |
wim | 0:e086541742c3 | 202 | */ |
wim | 0:e086541742c3 | 203 | bool move(int16_t x, int16_t y); |
wim | 0:e086541742c3 | 204 | |
wim | 0:e086541742c3 | 205 | /** |
wim | 0:e086541742c3 | 206 | * Press one or several buttons |
wim | 0:e086541742c3 | 207 | * |
wim | 0:e086541742c3 | 208 | * @param buttons buttons state |
wim | 0:e086541742c3 | 209 | * @returns true if there is no error, false otherwise |
wim | 0:e086541742c3 | 210 | */ |
wim | 0:e086541742c3 | 211 | bool buttons(uint32_t buttons); |
wim | 0:e086541742c3 | 212 | |
wim | 0:e086541742c3 | 213 | /** |
wim | 0:e086541742c3 | 214 | * Press hat |
wim | 0:e086541742c3 | 215 | * |
wim | 0:e086541742c3 | 216 | * @param hat hat state |
wim | 0:e086541742c3 | 217 | * @returns true if there is no error, false otherwise |
wim | 0:e086541742c3 | 218 | */ |
wim | 0:e086541742c3 | 219 | bool hat(uint8_t hat); |
wim | 0:e086541742c3 | 220 | |
wim | 0:e086541742c3 | 221 | /** |
wim | 0:e086541742c3 | 222 | * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. |
wim | 0:e086541742c3 | 223 | * |
wim | 0:e086541742c3 | 224 | * @returns pointer to the report descriptor |
wim | 0:e086541742c3 | 225 | */ |
wim | 0:e086541742c3 | 226 | virtual uint8_t * reportDesc(); |
wim | 0:e086541742c3 | 227 | |
wim | 0:e086541742c3 | 228 | private: |
wim | 0:e086541742c3 | 229 | int8_t _t; |
wim | 0:e086541742c3 | 230 | int8_t _r; |
wim | 0:e086541742c3 | 231 | int8_t _x; |
wim | 0:e086541742c3 | 232 | int8_t _y; |
wim | 0:e086541742c3 | 233 | uint32_t _buttons; |
wim | 0:e086541742c3 | 234 | uint8_t _hat; |
wim | 0:e086541742c3 | 235 | |
wim | 0:e086541742c3 | 236 | void _init(); |
wim | 0:e086541742c3 | 237 | }; |
wim | 0:e086541742c3 | 238 | |
wim | 0:e086541742c3 | 239 | #endif |