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.
Dependencies: USBDevice USBJoystick_SIM mbed USBJoystick_2
Fork of USBJoystick_HelloWorld2 by
main.cpp
00001 /* mbed USBJoystick Library Demo 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 #include "mbed.h" 00027 #include "config.h" 00028 #include "USBJoystick.h" 00029 00030 USBJoystick joystick; 00031 00032 // Variables for Heartbeat and Status monitoring 00033 DigitalOut myled1(LED4); 00034 DigitalOut myled2(LED2); 00035 DigitalOut myled3(LED3); 00036 DigitalOut heartbeatLED(LED1); 00037 00038 AnalogIn inX(A0); // X 00039 AnalogIn inY(A1); // Y 00040 AnalogIn inRudder(A2); // Rz (Rudder) 00041 AnalogIn inThrottle(A3); // Slider (Throttle) 00042 AnalogIn inBreaks(A4); // Z (Breaks) 00043 AnalogIn inFlaps(A5); // Rx (Flaps) 00044 00045 Ticker heartbeat; 00046 Serial pc(USBTX, USBRX); // tx, rx 00047 00048 // number of elements in an array 00049 #define countof(x) (sizeof(x)/sizeof((x)[0])) 00050 00051 // button input map array 00052 DigitalIn *buttonDigIn[NUM_OF_BUTTONS]; // config.h 00053 00054 // hat button input map array 00055 DigitalIn *hatDigIn[NUM_OF_HAT_BUTTONS]; // config.h 00056 00057 // Heartbeat monitor 00058 void pulse() 00059 { 00060 heartbeatLED = !heartbeatLED; 00061 } 00062 00063 void heartbeat_start() 00064 { 00065 heartbeatLED=1; 00066 heartbeat.attach(&pulse, 0.5); 00067 } 00068 00069 void heartbeat_stop() 00070 { 00071 heartbeat.detach(); 00072 } 00073 00074 // button state 00075 struct ButtonState 00076 { 00077 // current on/off state 00078 int pressed; 00079 00080 // Sticky time remaining for current state. When a 00081 // state transition occurs, we set this to a debounce 00082 // period. Future state transitions will be ignored 00083 // until the debounce time elapses. 00084 int bt; 00085 } buttonState[NUM_OF_BUTTONS]; 00086 00087 // timer for button reports 00088 static Timer buttonTimer; 00089 00090 // initialize the button inputs 00091 void initButtons() 00092 { 00093 // create the digital inputs 00094 for (int i = 0 ; i < countof(buttonDigIn) ; i++) 00095 { 00096 if (i < countof(buttonMap) && buttonMap[i] != NC) 00097 buttonDigIn[i] = new DigitalIn(buttonMap[i]); 00098 else 00099 buttonDigIn[i] = 0; 00100 } 00101 00102 // start the button timer 00103 buttonTimer.start(); 00104 } 00105 00106 00107 // read the button input state 00108 uint32_t readButtons() 00109 { 00110 // start with all buttons off 00111 uint32_t buttons = 0; 00112 00113 // figure the time elapsed since the last scan 00114 int dt = buttonTimer.read_ms(); 00115 00116 // reset the timef for the next scan 00117 buttonTimer.reset(); 00118 00119 // scan the button list 00120 uint32_t bit = 1; 00121 DigitalIn **di = buttonDigIn; 00122 ButtonState *bs = buttonState; 00123 for (int i = 0 ; i < countof(buttonDigIn) ; i++, di++, bs++, bit <<= 1) 00124 { 00125 // read this button 00126 if (*di != 0) 00127 { 00128 // deduct the elapsed time since the last update 00129 // from the button's remaining sticky time 00130 bs->bt -= dt; 00131 if (bs->bt < 0) 00132 bs->bt = 0; 00133 00134 // If the sticky time has elapsed, note the new physical 00135 // state of the button. If we still have sticky time 00136 // remaining, ignore the physical state; the last state 00137 // change persists until the sticky time elapses so that 00138 // we smooth out any "bounce" (electrical transients that 00139 // occur when the switch contact is opened or closed). 00140 if (bs->bt == 0) 00141 { 00142 // get the new physical state 00143 int pressed = !(*di)->read(); 00144 00145 // update the button's logical state if this is a change 00146 if (pressed != bs->pressed) 00147 { 00148 // store the new state 00149 bs->pressed = pressed; 00150 00151 // start a new sticky period for debouncing this 00152 // state change 00153 bs->bt = 10; 00154 } 00155 } 00156 00157 // if it's pressed, OR its bit into the state 00158 if (bs->pressed) 00159 buttons |= bit; 00160 pc.printf("Buttons: %d\n", buttons); 00161 } 00162 } 00163 00164 // return the new button list 00165 return buttons; 00166 } 00167 00168 // hat state 00169 struct HatState 00170 { 00171 // current on/off state 00172 int pressed; 00173 00174 // Sticky time remaining for current state. When a 00175 // state transition occurs, we set this to a debounce 00176 // period. Future state transitions will be ignored 00177 // until the debounce time elapses. 00178 int ht; 00179 } hatState[NUM_OF_HAT_BUTTONS]; 00180 00181 // timer for hat reports 00182 static Timer hatTimer; 00183 00184 // initialize the hat inputs 00185 void initHat() 00186 { 00187 // create the digital inputs 00188 for (int i = 0 ; i <= countof(hatDigIn) ; i++) 00189 { 00190 if (i < countof(hatMap) && hatMap[i] != NC) 00191 hatDigIn[i] = new DigitalIn(hatMap[i]); 00192 else 00193 hatDigIn[i] = 0; 00194 } 00195 00196 // start the hat timer 00197 hatTimer.start(); 00198 } 00199 00200 // read the hat button input state 00201 uint8_t readHat() 00202 { 00203 // start with all buttons off 00204 uint8_t hat = 0; 00205 00206 // figure the time elapsed since the last scan 00207 int dt = hatTimer.read_ms(); 00208 00209 // reset the time for the next scan 00210 hatTimer.reset(); 00211 00212 // scan the button list 00213 uint8_t bit = 1; 00214 DigitalIn **di = hatDigIn; 00215 HatState *hs = hatState; 00216 for (int i = 0 ; i < countof(hatDigIn) ; i++, di++, hs++) 00217 { 00218 // read this button 00219 if (*di != 0) 00220 { 00221 // deduct the elapsed time since the last update 00222 // from the button's remaining sticky time 00223 hs->ht -= dt; 00224 if (hs->ht < 0) 00225 hs->ht = 0; 00226 00227 // If the sticky time has elapsed, note the new physical 00228 // state of the button. If we still have sticky time 00229 // remaining, ignore the physical state; the last state 00230 // change persists until the sticky time elapses so that 00231 // we smooth out any "bounce" (electrical transients that 00232 // occur when the switch contact is opened or closed). 00233 if (hs->ht == 0) 00234 { 00235 // get the new physical state 00236 int pressed = !(*di)->read(); 00237 00238 // update the button's logical state if this is a change 00239 if (pressed != hs->pressed) 00240 { 00241 // store the new state 00242 hs->pressed = pressed; 00243 00244 // start a new sticky period for debouncing this 00245 // state change 00246 hs->ht = 10; 00247 } 00248 } 00249 00250 // if it's pressed, OR its bit into the state 00251 if (hs->pressed) 00252 hat |= bit; 00253 pc.printf("Hat: %d\n", hat); 00254 } 00255 bit <<= 1; 00256 } 00257 00258 // translate values read to descriptor values 00259 #if HAT4 && !HAT4_8 00260 if (hat == 0x01) // 00000001 00261 hat = JOY_HAT_UP; // 0 00262 else if (hat == 0x02) // 00000010 00263 hat = JOY_HAT_RIGHT; // 1 00264 else if (hat == 0x04) // 00000100 00265 hat = JOY_HAT_DOWN; // 2 00266 else if (hat == 0x08) // 00001000 00267 hat = JOY_HAT_LEFT; // 3 00268 else 00269 hat = JOY_HAT_NEUTRAL; // 4 00270 #endif 00271 #if HAT4 && HAT4_8 00272 if (hat == 0x01) // 00000001 00273 hat = JOY_HAT_UP; // 0 00274 else if (hat == 0x03) // 00000011 00275 hat = JOY_HAT_UP_RIGHT; // 1 00276 else if (hat == 0x02) // 00000010 00277 hat = JOY_HAT_RIGHT; // 2 00278 else if (hat == 0x06) // 00000110 00279 hat = JOY_HAT_DOWN_RIGHT; // 3 00280 else if (hat == 0x04) // 00000100 00281 hat = JOY_HAT_DOWN; // 4 00282 else if (hat == 0x0C) // 00001100 00283 hat = JOY_HAT_DOWN_LEFT; // 5 00284 else if (hat == 0x08) // 00001000 00285 hat = JOY_HAT_LEFT; // 6 00286 else if (hat == 0x09) // 00001001 00287 hat = JOY_HAT_UP_LEFT; // 7 00288 else 00289 hat = JOY_HAT_NEUTRAL; // 8 00290 #endif 00291 #if HAT8 00292 if (hat == 0x01) // 00000001 00293 hat = JOY_HAT_UP; // 0 00294 else if (hat == 0x02) // 00000010 00295 hat = JOY_HAT_UP_RIGHT; // 1 00296 else if (hat == 0x04) // 00000100 00297 hat = JOY_HAT_RIGHT; // 2 00298 else if (hat == 0x08) // 00001000 00299 hat = JOY_HAT_DOWN_RIGHT; // 3 00300 else if (hat == 0x10) // 00010000 00301 hat = JOY_HAT_DOWN; // 4 00302 else if (hat == 0x20) // 00100000 00303 hat = JOY_HAT_DOWN_LEFT; // 5 00304 else if (hat == 0x40) // 01000000 00305 hat = JOY_HAT_LEFT; // 6 00306 else if (hat == 0x80) // 10000000 00307 hat = JOY_HAT_UP_LEFT; // 7 00308 else 00309 hat = JOY_HAT_NEUTRAL; // 8 00310 #endif 00311 00312 // return the new button list 00313 pc.printf("Return Hat: %d", hat); 00314 return hat; 00315 } 00316 00317 int main() 00318 { 00319 uint16_t x = 0; 00320 uint16_t y = 0; 00321 uint16_t breaks = 0; 00322 uint16_t flaps = 0; 00323 uint16_t rudder = 0; 00324 uint16_t throttle = 0; 00325 uint8_t hat = 0; 00326 uint32_t buttons = 0; 00327 00328 00329 // const int16_t l = 32767; 00330 const uint16_t m = 65535; 00331 00332 pc.printf("Hello World from Joystick!\n\r"); 00333 00334 initButtons(); 00335 initHat(); 00336 00337 heartbeat_start(); 00338 00339 pc.printf("x, y, breaks, flaps, rudder, throttle, hat, buttons\n\n\r"); 00340 00341 while (1) { 00342 00343 x = inX.read() * m; 00344 y = inY.read() * m; 00345 breaks = inBreaks.read() * m; 00346 flaps = inFlaps.read() * m; 00347 rudder = inRudder.read() * m; 00348 throttle = inThrottle.read() * m; 00349 hat = readHat(); 00350 buttons = readButtons(); 00351 00352 pc.printf("%d, %d, %d, %d, %d, %d, %d, %d\n\r", x, y, breaks, flaps, rudder, throttle, hat, buttons); 00353 00354 joystick.update(x, y, breaks, flaps, rudder, throttle, hat, buttons); 00355 wait(0.01); 00356 } 00357 00358 //pc.printf("Bye World!\n\r"); 00359 }
Generated on Tue Jul 12 2022 11:12:17 by
1.7.2
