USBJoystick updated for 32 buttons and added wait-for-connect.
Dependents: USBJoystick_HelloWorld2 GamePortAdapter USBJoyFromRC Retro_Controller_Adaptor
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 "USBHID.h" 00030 00031 #define REPORT_ID_JOYSTICK 4 00032 00033 //Configure Joystick 00034 #define HAT4 0 00035 #define HAT8 1 00036 00037 #define BUTTONS4 0 00038 #define BUTTONS8 0 00039 #define BUTTONS32 1 00040 00041 00042 /* Common usage */ 00043 enum JOY_BUTTON { 00044 JOY_B0 = 1, 00045 JOY_B1 = 2, 00046 JOY_B2 = 4, 00047 JOY_B3 = 8, 00048 }; 00049 00050 #if (HAT4 == 1) 00051 enum JOY_HAT { 00052 JOY_HAT_UP = 0, 00053 JOY_HAT_RIGHT = 1, 00054 JOY_HAT_DOWN = 2, 00055 JOY_HAT_LEFT = 3, 00056 JOY_HAT_NEUTRAL = 4, 00057 }; 00058 #endif 00059 #if (HAT8 == 1) 00060 enum JOY_HAT { 00061 JOY_HAT_UP = 0, 00062 JOY_HAT_UP_RIGHT = 1, 00063 JOY_HAT_RIGHT = 2, 00064 JOY_HAT_RIGHT_DOWN = 3, 00065 JOY_HAT_DOWN = 4, 00066 JOY_HAT_DOWN_LEFT = 5, 00067 JOY_HAT_LEFT = 6, 00068 JOY_HAT_LEFT_UP = 7, 00069 JOY_HAT_NEUTRAL = 8, 00070 }; 00071 #endif 00072 00073 /* X, Y and T limits */ 00074 /* These values do not directly map to screen pixels */ 00075 /* Zero may be interpreted as meaning 'no movement' */ 00076 #define JX_MIN_ABS (-127) /*!< The maximum value that we can move to the left on the x-axis */ 00077 #define JY_MIN_ABS (-127) /*!< The maximum value that we can move up on the y-axis */ 00078 #define JT_MIN_ABS (-127) /*!< The minimum value for the throttle */ 00079 #define JX_MAX_ABS (127) /*!< The maximum value that we can move to the right on the x-axis */ 00080 #define JY_MAX_ABS (127) /*!< The maximum value that we can move down on the y-axis */ 00081 #define JT_MAX_ABS (127) /*!< The maximum value for the throttle */ 00082 00083 /** 00084 * 00085 * USBJoystick example 00086 * @code 00087 * #include "mbed.h" 00088 * #include "USBJoystick.h" 00089 * 00090 * USBJoystick joystick; 00091 * 00092 * int main(void) 00093 * { 00094 * while (1) 00095 * { 00096 * joystick.move(20, 0); 00097 * wait(0.5); 00098 * } 00099 * } 00100 * 00101 * @endcode 00102 * 00103 * 00104 * @code 00105 * #include "mbed.h" 00106 * #include "USBJoystick.h" 00107 * 00108 * USBJoystick joystick; 00109 * 00110 * int main(void) { 00111 * uint16_t i = 0; 00112 * int16_t throttle = 0; 00113 * int16_t rudder = 0; 00114 * int16_t x = 0; 00115 * int16_t y = 0; 00116 * int32_t radius = 120; 00117 * int32_t angle = 0; 00118 * uint32_t buttons = 0; 00119 * uint8_t hat = 0; 00120 * 00121 * while (1) { 00122 * // Basic Joystick 00123 * throttle = (i >> 8) & 0xFF; // value -127 .. 128 00124 * rudder = (i >> 8) & 0xFF; // value -127 .. 128 00125 * buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button 00126 * hat = (i >> 8) & 0x07; // value 0 .. 7 or 8 for neutral 00127 * i++; 00128 * 00129 * x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 00130 * y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 00131 * angle += 3; 00132 * 00133 * joystick.update(throttle, rudder, x, y, buttons, hat); 00134 * 00135 * wait(0.001); 00136 * } 00137 * } 00138 * @endcode 00139 */ 00140 00141 00142 class USBJoystick: public USBHID { 00143 public: 00144 00145 /** 00146 * Constructor 00147 * 00148 * @param vendor_id Your vendor_id (default: 0x1234) 00149 * @param product_id Your product_id (default: 0x0002) 00150 * @param product_release Your product_release (default: 0x0001) 00151 */ 00152 // 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 00153 // 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 00154 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 00155 USBHID(0, 0, vendor_id, product_id, product_release, false) { 00156 _init(); 00157 connect(waitForConnect); 00158 }; 00159 00160 /** 00161 * Write state of the joystick 00162 * 00163 * @param t throttle position 00164 * @param r rudder position 00165 * @param x x-axis position 00166 * @param y y-axis position 00167 * @param buttons buttons state 00168 * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral) 00169 * @returns true if there is no error, false otherwise 00170 */ 00171 bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons, uint8_t hat); 00172 00173 /** 00174 * Write state of the joystick 00175 * 00176 * @returns true if there is no error, false otherwise 00177 */ 00178 bool update(); 00179 00180 /** 00181 * Move the throttle position 00182 * 00183 * @param t throttle position 00184 * @returns true if there is no error, false otherwise 00185 */ 00186 bool throttle(int16_t t); 00187 00188 /** 00189 * Move the rudder position 00190 * 00191 * @param r rudder position 00192 * @returns true if there is no error, false otherwise 00193 */ 00194 bool rudder(int16_t r); 00195 00196 /** 00197 * Move the cursor to (x, y) 00198 * 00199 * @param x-axis position 00200 * @param y-axis position 00201 * @returns true if there is no error, false otherwise 00202 */ 00203 bool move(int16_t x, int16_t y); 00204 00205 /** 00206 * Press one or several buttons 00207 * 00208 * @param buttons buttons state 00209 * @returns true if there is no error, false otherwise 00210 */ 00211 bool buttons(uint32_t buttons); 00212 00213 /** 00214 * Press hat 00215 * 00216 * @param hat hat state 00217 * @returns true if there is no error, false otherwise 00218 */ 00219 bool hat(uint8_t hat); 00220 00221 /** 00222 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength. 00223 * 00224 * @returns pointer to the report descriptor 00225 */ 00226 virtual uint8_t * reportDesc(); 00227 00228 private: 00229 int8_t _t; 00230 int8_t _r; 00231 int8_t _x; 00232 int8_t _y; 00233 uint32_t _buttons; 00234 uint8_t _hat; 00235 00236 void _init(); 00237 }; 00238 00239 #endif
Generated on Wed Jul 13 2022 03:22:01 by 1.7.2