Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: USBJoystick_HelloWorld2_wip
Fork of USBJoystick by
USBJoystick.h
00001 /* mbed USBJoystick Library 00002 * Copyright (c) 2012, v01: Initial version, WH, 00003 * Modified USBMouse code ARM Limited. 00004 * (c) 2010-2011 mbed.org, MIT License 00005 * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button 00006 * 00007 * Permission is hereby granted, free of charge, to any person obtaining a copy 00008 * of this software and associated documentation files (the "Software"), to deal 00009 * in the Software without restriction, inclumosig without limitation the rights 00010 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00011 * copies of the Software, and to permit persons to whom the Software is 00012 * furnished to do so, subject to the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included in 00015 * all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00023 * THE SOFTWARE. 00024 */ 00025 00026 #ifndef USBJOYSTICK_H 00027 #define USBJOYSTICK_H 00028 00029 #include "USBPID.h" 00030 00031 #define REPORT_ID_JOYSTICK 4 00032 00033 //Configure Joystick 00034 00035 //Number of Hat Buttons 00036 #define HAT4 1 00037 #define HAT8 0 00038 00039 //Number of Buttons 00040 #define BUTTONS4 0 00041 #define BUTTONS8 0 00042 #define BUTTONS32 1 00043 00044 //Force Feedback 00045 #define FFB 1 00046 00047 00048 /* Common usage */ 00049 enum JOY_BUTTON { 00050 JOY_B0 = 1, 00051 JOY_B1 = 2, 00052 JOY_B2 = 4, 00053 JOY_B3 = 8, 00054 }; 00055 00056 #if (HAT4 == 1) 00057 enum JOY_HAT { 00058 JOY_HAT_UP = 0, 00059 JOY_HAT_RIGHT = 1, 00060 JOY_HAT_DOWN = 2, 00061 JOY_HAT_LEFT = 3, 00062 JOY_HAT_NEUTRAL = 4, 00063 }; 00064 #endif 00065 #if (HAT8 == 1) 00066 enum JOY_HAT { 00067 JOY_HAT_UP = 0, 00068 JOY_HAT_UP_RIGHT = 1, 00069 JOY_HAT_RIGHT = 2, 00070 JOY_HAT_RIGHT_DOWN = 3, 00071 JOY_HAT_DOWN = 4, 00072 JOY_HAT_DOWN_LEFT = 5, 00073 JOY_HAT_LEFT = 6, 00074 JOY_HAT_LEFT_UP = 7, 00075 JOY_HAT_NEUTRAL = 8, 00076 }; 00077 #endif 00078 00079 /* X, Y and T limits */ 00080 /* These values do not directly map to screen pixels */ 00081 /* Zero may be interpreted as meaning 'no movement' */ 00082 #define JX_MIN_ABS (-127) /*!< The maximum value that we can move to the left on the x-axis */ 00083 #define JY_MIN_ABS (-127) /*!< The maximum value that we can move up on the y-axis */ 00084 #define JT_MIN_ABS (-127) /*!< The minimum value for the throttle */ 00085 #define JR_MIN_ABS (-127) /*!< The minimum value for the rudder */ 00086 #define JF_MIN_ABS (-127) /*!< The minimum value for the flaps */ 00087 #define JB_MIN_ABS (-127) /*!< The minimum value for the breaks */ 00088 #define JB_MIN_ABS (-127) /*!< The minimum value for the gear */ 00089 #define JX_MAX_ABS (127) /*!< The maximum value that we can move to the right on the x-axis */ 00090 #define JY_MAX_ABS (127) /*!< The maximum value that we can move down on the y-axis */ 00091 #define JT_MAX_ABS (127) /*!< The maximum value for the throttle */ 00092 #define JR_MAX_ABS (127) /*!< The maximum value for the rudder */ 00093 #define JF_MAX_ABS (127) /*!< The maximum value for the flaps */ 00094 #define JB_MAX_ABS (127) /*!< The maximum value for the breaks */ 00095 #define JB_MAX_ABS (127) /*!< The maximum value for the gear */ 00096 00097 /** 00098 * 00099 * USBJoystick example 00100 * @code 00101 * #include "mbed.h" 00102 * #include "USBJoystick.h" 00103 * 00104 * USBJoystick joystick; 00105 * 00106 * int main(void) 00107 * { 00108 * while (1) 00109 * { 00110 * joystick.move(20, 0); 00111 * wait(0.5); 00112 * } 00113 * } 00114 * 00115 * @endcode 00116 * 00117 * 00118 * @code 00119 * #include "mbed.h" 00120 * #include "USBJoystick.h" 00121 * 00122 * USBJoystick joystick; 00123 * 00124 * int main(void) { 00125 * uint16_t i = 0; 00126 * int16_t throttle = 0; 00127 * int16_t rudder = 0; 00128 * int16_t x = 0; 00129 * int16_t y = 0; 00130 * int32_t radius = 120; 00131 * int32_t angle = 0; 00132 * uint32_t buttons = 0; 00133 * uint8_t hat = 0; 00134 * 00135 * while (1) { 00136 * // Basic Joystick 00137 * throttle = (i >> 8) & 0xFF; // value -127 .. 128 00138 * rudder = (i >> 8) & 0xFF; // value -127 .. 128 00139 * buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button 00140 * hat = (i >> 8) & 0x07; // value 0 .. 7 or 8 for neutral 00141 * i++; 00142 * 00143 * x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 00144 * y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 00145 * angle += 3; 00146 * 00147 * joystick.update(throttle, rudder, x, y, buttons, hat); 00148 * 00149 * wait(0.001); 00150 * } 00151 * } 00152 * @endcode 00153 */ 00154 00155 00156 class USBJoystick: public USBPID 00157 { 00158 public: 00159 00160 /** 00161 * Constructor 00162 * 00163 * @param vendor_id Your vendor_id (default: 0x1234) 00164 * @param product_id Your product_id (default: 0x0002) 00165 * @param product_release Your product_release (default: 0x0001) 00166 */ 00167 // 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 00168 // 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 00169 // 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 00170 USBJoystick(uint16_t vendor_id = 0x1209, uint16_t product_id = 0x0601, uint16_t product_release = 0x0101, int waitForConnect = true): // 32 buttons, no padding on buttons, plus Gear, Flaps and Breaks 00171 USBPID(0, 0, vendor_id, product_id, product_release, false) { 00172 _init(); 00173 connect(waitForConnect); 00174 }; 00175 00176 /** 00177 * Write state of the joystick 00178 * 00179 * @param t throttle position 00180 * @param r rudder position 00181 * @param f flaps position 00182 * @param b breaks position 00183 * @param x x-axis position 00184 * @param y y-axis position 00185 * @param buttons buttons state 00186 * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral) 00187 * @param 00188 * @returns true if there is no error, false otherwise 00189 */ 00190 bool update(int16_t t, int16_t r, int16_t f, int16_t b, int16_t x, int16_t y, uint32_t buttons, uint8_t hat); 00191 00192 /** 00193 * Write state of the joystick 00194 * 00195 * @returns true if there is no error, false otherwise 00196 */ 00197 bool update(); 00198 00199 /** 00200 * Move the throttle position 00201 * 00202 * @param t throttle position 00203 * @returns true if there is no error, false otherwise 00204 */ 00205 bool throttle(int16_t t); 00206 00207 /** 00208 * Move the rudder position 00209 * 00210 * @param r rudder position 00211 * @returns true if there is no error, false otherwise 00212 */ 00213 bool rudder(int16_t r); 00214 00215 /** 00216 * Move the flaps position 00217 * 00218 * @param f flaps position 00219 * @returns true if there is no error, false otherwise 00220 */ 00221 bool flaps(int16_t r); 00222 00223 /** 00224 * Move the breaks position 00225 * 00226 * @param b breaks position 00227 * @returns true if there is no error, false otherwise 00228 */ 00229 bool breaks(int16_t b); 00230 00231 /** 00232 * Move the cursor to (x, y) 00233 * 00234 * @param x-axis position 00235 * @param y-axis position 00236 * @returns true if there is no error, false otherwise 00237 */ 00238 bool move(int16_t x, int16_t y); 00239 00240 /** 00241 * Press one or several buttons 00242 * 00243 * @param buttons buttons state 00244 * @returns true if there is no error, false otherwise 00245 */ 00246 bool buttons(uint32_t buttons); 00247 00248 /** 00249 * Press hat 00250 * 00251 * @param hat hat state 00252 * @returns true if there is no error, false otherwise 00253 */ 00254 bool hat(uint8_t hat); 00255 00256 00257 /** 00258 * To define the interface descriptor string. 00259 * 00260 * @returns pointer to the interface descriptor string 00261 */ 00262 // virtual uint8_t * stringIinterfaceDesc(); 00263 00264 /** 00265 * To define the product descriptor string. 00266 * 00267 * @returns pointer to the product descriptor string 00268 */ 00269 // virtual uint8_t * stringIproductDesc(); 00270 00271 /** 00272 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. 00273 * 00274 * @returns pointer to the report descriptor 00275 */ 00276 virtual uint8_t * reportDesc(); 00277 00278 /* 00279 * Called when a data is received on the OUT endpoint. Useful to switch on LED of LOCK keys 00280 * 00281 * @returns if handle by subclass, return true 00282 */ 00283 virtual bool EPINT_OUT_callback(); 00284 00285 private: 00286 int8_t _t; 00287 int8_t _r; 00288 int8_t _f; 00289 int8_t _b; 00290 int8_t _g; 00291 int8_t _x; 00292 int8_t _y; 00293 uint32_t _buttons; 00294 uint8_t _hat; 00295 00296 void _init(); 00297 00298 }; 00299 00300 #endif
Generated on Tue Jul 19 2022 20:53:24 by
1.7.2
