Demo for USBJoystick updated for 32 buttons.
Dependencies: USBDevice USBJoystick_SIM mbed USBJoystick_2
Fork of USBJoystick_HelloWorld2 by
main.cpp@1:b106cf2e99ba, 2018-07-22 (annotated)
- Committer:
- Cirrus01
- Date:
- Sun Jul 22 10:36:35 2018 +0000
- Revision:
- 1:b106cf2e99ba
- Parent:
- 0:e43878690c0e
- Child:
- 2:967da2faedcd
Inital
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wim | 0:e43878690c0e | 1 | /* mbed USBJoystick Library Demo |
wim | 0:e43878690c0e | 2 | * Copyright (c) 2012, v01: Initial version, WH, |
wim | 0:e43878690c0e | 3 | * Modified USBMouse code ARM Limited. |
wim | 0:e43878690c0e | 4 | * (c) 2010-2011 mbed.org, MIT License |
Cirrus01 | 1:b106cf2e99ba | 5 | * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button |
wim | 0:e43878690c0e | 6 | * |
wim | 0:e43878690c0e | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wim | 0:e43878690c0e | 8 | * of this software and associated documentation files (the "Software"), to deal |
wim | 0:e43878690c0e | 9 | * in the Software without restriction, inclumosig without limitation the rights |
wim | 0:e43878690c0e | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wim | 0:e43878690c0e | 11 | * copies of the Software, and to permit persons to whom the Software is |
wim | 0:e43878690c0e | 12 | * furnished to do so, subject to the following conditions: |
wim | 0:e43878690c0e | 13 | * |
wim | 0:e43878690c0e | 14 | * The above copyright notice and this permission notice shall be included in |
wim | 0:e43878690c0e | 15 | * all copies or substantial portions of the Software. |
wim | 0:e43878690c0e | 16 | * |
wim | 0:e43878690c0e | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wim | 0:e43878690c0e | 18 | * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wim | 0:e43878690c0e | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wim | 0:e43878690c0e | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wim | 0:e43878690c0e | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wim | 0:e43878690c0e | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wim | 0:e43878690c0e | 23 | * THE SOFTWARE. |
wim | 0:e43878690c0e | 24 | */ |
wim | 0:e43878690c0e | 25 | |
wim | 0:e43878690c0e | 26 | #include "mbed.h" |
Cirrus01 | 1:b106cf2e99ba | 27 | #include "config.h" |
wim | 0:e43878690c0e | 28 | #include "USBJoystick.h" |
wim | 0:e43878690c0e | 29 | |
wim | 0:e43878690c0e | 30 | //#define LANDTIGER 1 |
wim | 0:e43878690c0e | 31 | |
wim | 0:e43878690c0e | 32 | //USBMouse mouse; |
wim | 0:e43878690c0e | 33 | USBJoystick joystick; |
wim | 0:e43878690c0e | 34 | |
wim | 0:e43878690c0e | 35 | // Variables for Heartbeat and Status monitoring |
Cirrus01 | 1:b106cf2e99ba | 36 | DigitalOut myled1(LED4); |
wim | 0:e43878690c0e | 37 | DigitalOut myled2(LED2); |
wim | 0:e43878690c0e | 38 | DigitalOut myled3(LED3); |
Cirrus01 | 1:b106cf2e99ba | 39 | DigitalOut heartbeatLED(LED1); |
Cirrus01 | 1:b106cf2e99ba | 40 | |
Cirrus01 | 1:b106cf2e99ba | 41 | AnalogIn inX(A0); |
Cirrus01 | 1:b106cf2e99ba | 42 | AnalogIn inY(A1); |
Cirrus01 | 1:b106cf2e99ba | 43 | AnalogIn inRudder(A2); |
Cirrus01 | 1:b106cf2e99ba | 44 | AnalogIn inThrottle(A3); |
Cirrus01 | 1:b106cf2e99ba | 45 | AnalogIn inBreaks(A4); |
Cirrus01 | 1:b106cf2e99ba | 46 | AnalogIn inFlaps(A5); |
wim | 0:e43878690c0e | 47 | |
wim | 0:e43878690c0e | 48 | Ticker heartbeat; |
wim | 0:e43878690c0e | 49 | Serial pc(USBTX, USBRX); // tx, rx |
wim | 0:e43878690c0e | 50 | |
Cirrus01 | 1:b106cf2e99ba | 51 | // number of elements in an array |
Cirrus01 | 1:b106cf2e99ba | 52 | #define countof(x) (sizeof(x)/sizeof((x)[0])) |
Cirrus01 | 1:b106cf2e99ba | 53 | |
Cirrus01 | 1:b106cf2e99ba | 54 | // button input map array |
Cirrus01 | 1:b106cf2e99ba | 55 | DigitalIn *buttonDigIn[NUM_OF_BUTTONS]; // config.h |
Cirrus01 | 1:b106cf2e99ba | 56 | |
Cirrus01 | 1:b106cf2e99ba | 57 | // hat button input map array |
Cirrus01 | 1:b106cf2e99ba | 58 | DigitalIn *hatDigIn[NUM_OF_HAT_BUTTONS]; // config.h |
Cirrus01 | 1:b106cf2e99ba | 59 | |
wim | 0:e43878690c0e | 60 | // Heartbeat monitor |
Cirrus01 | 1:b106cf2e99ba | 61 | void pulse() |
Cirrus01 | 1:b106cf2e99ba | 62 | { |
Cirrus01 | 1:b106cf2e99ba | 63 | heartbeatLED = !heartbeatLED; |
Cirrus01 | 1:b106cf2e99ba | 64 | } |
Cirrus01 | 1:b106cf2e99ba | 65 | |
Cirrus01 | 1:b106cf2e99ba | 66 | void heartbeat_start() |
Cirrus01 | 1:b106cf2e99ba | 67 | { |
Cirrus01 | 1:b106cf2e99ba | 68 | heartbeatLED=1; |
Cirrus01 | 1:b106cf2e99ba | 69 | heartbeat.attach(&pulse, 0.5); |
Cirrus01 | 1:b106cf2e99ba | 70 | } |
Cirrus01 | 1:b106cf2e99ba | 71 | |
Cirrus01 | 1:b106cf2e99ba | 72 | void heartbeat_stop() |
Cirrus01 | 1:b106cf2e99ba | 73 | { |
Cirrus01 | 1:b106cf2e99ba | 74 | heartbeat.detach(); |
wim | 0:e43878690c0e | 75 | } |
wim | 0:e43878690c0e | 76 | |
Cirrus01 | 1:b106cf2e99ba | 77 | // button state |
Cirrus01 | 1:b106cf2e99ba | 78 | struct ButtonState |
Cirrus01 | 1:b106cf2e99ba | 79 | { |
Cirrus01 | 1:b106cf2e99ba | 80 | // current on/off state |
Cirrus01 | 1:b106cf2e99ba | 81 | int pressed; |
Cirrus01 | 1:b106cf2e99ba | 82 | |
Cirrus01 | 1:b106cf2e99ba | 83 | // Sticky time remaining for current state. When a |
Cirrus01 | 1:b106cf2e99ba | 84 | // state transition occurs, we set this to a debounce |
Cirrus01 | 1:b106cf2e99ba | 85 | // period. Future state transitions will be ignored |
Cirrus01 | 1:b106cf2e99ba | 86 | // until the debounce time elapses. |
Cirrus01 | 1:b106cf2e99ba | 87 | int bt; |
Cirrus01 | 1:b106cf2e99ba | 88 | } buttonState[NUM_OF_BUTTONS]; |
wim | 0:e43878690c0e | 89 | |
Cirrus01 | 1:b106cf2e99ba | 90 | // timer for button reports |
Cirrus01 | 1:b106cf2e99ba | 91 | static Timer buttonTimer; |
Cirrus01 | 1:b106cf2e99ba | 92 | |
Cirrus01 | 1:b106cf2e99ba | 93 | // initialize the button inputs |
Cirrus01 | 1:b106cf2e99ba | 94 | void initButtons() |
Cirrus01 | 1:b106cf2e99ba | 95 | { |
Cirrus01 | 1:b106cf2e99ba | 96 | // create the digital inputs |
Cirrus01 | 1:b106cf2e99ba | 97 | for (int i = 0 ; i < countof(buttonDigIn) ; ++i) |
Cirrus01 | 1:b106cf2e99ba | 98 | { |
Cirrus01 | 1:b106cf2e99ba | 99 | if (i < countof(buttonMap) && buttonMap[i] != NC) |
Cirrus01 | 1:b106cf2e99ba | 100 | buttonDigIn[i] = new DigitalIn(buttonMap[i]); |
Cirrus01 | 1:b106cf2e99ba | 101 | else |
Cirrus01 | 1:b106cf2e99ba | 102 | buttonDigIn[i] = 0; |
Cirrus01 | 1:b106cf2e99ba | 103 | } |
Cirrus01 | 1:b106cf2e99ba | 104 | |
Cirrus01 | 1:b106cf2e99ba | 105 | // start the button timer |
Cirrus01 | 1:b106cf2e99ba | 106 | buttonTimer.start(); |
wim | 0:e43878690c0e | 107 | } |
wim | 0:e43878690c0e | 108 | |
wim | 0:e43878690c0e | 109 | |
Cirrus01 | 1:b106cf2e99ba | 110 | // read the button input state |
Cirrus01 | 1:b106cf2e99ba | 111 | uint32_t readButtons() |
Cirrus01 | 1:b106cf2e99ba | 112 | { |
Cirrus01 | 1:b106cf2e99ba | 113 | // start with all buttons off |
Cirrus01 | 1:b106cf2e99ba | 114 | uint32_t buttons = 0; |
Cirrus01 | 1:b106cf2e99ba | 115 | |
Cirrus01 | 1:b106cf2e99ba | 116 | // figure the time elapsed since the last scan |
Cirrus01 | 1:b106cf2e99ba | 117 | int dt = buttonTimer.read_ms(); |
Cirrus01 | 1:b106cf2e99ba | 118 | |
Cirrus01 | 1:b106cf2e99ba | 119 | // reset the timef for the next scan |
Cirrus01 | 1:b106cf2e99ba | 120 | buttonTimer.reset(); |
wim | 0:e43878690c0e | 121 | |
Cirrus01 | 1:b106cf2e99ba | 122 | // scan the button list |
Cirrus01 | 1:b106cf2e99ba | 123 | uint32_t bit = 1; |
Cirrus01 | 1:b106cf2e99ba | 124 | DigitalIn **di = buttonDigIn; |
Cirrus01 | 1:b106cf2e99ba | 125 | ButtonState *bs = buttonState; |
Cirrus01 | 1:b106cf2e99ba | 126 | for (int i = 0 ; i < countof(buttonDigIn) ; ++i, ++di, ++bs, bit <<= 1) |
Cirrus01 | 1:b106cf2e99ba | 127 | { |
Cirrus01 | 1:b106cf2e99ba | 128 | // read this button |
Cirrus01 | 1:b106cf2e99ba | 129 | if (*di != 0) |
Cirrus01 | 1:b106cf2e99ba | 130 | { |
Cirrus01 | 1:b106cf2e99ba | 131 | // deduct the elapsed time since the last update |
Cirrus01 | 1:b106cf2e99ba | 132 | // from the button's remaining sticky time |
Cirrus01 | 1:b106cf2e99ba | 133 | bs->bt -= dt; |
Cirrus01 | 1:b106cf2e99ba | 134 | if (bs->bt < 0) |
Cirrus01 | 1:b106cf2e99ba | 135 | bs->bt = 0; |
Cirrus01 | 1:b106cf2e99ba | 136 | |
Cirrus01 | 1:b106cf2e99ba | 137 | // If the sticky time has elapsed, note the new physical |
Cirrus01 | 1:b106cf2e99ba | 138 | // state of the button. If we still have sticky time |
Cirrus01 | 1:b106cf2e99ba | 139 | // remaining, ignore the physical state; the last state |
Cirrus01 | 1:b106cf2e99ba | 140 | // change persists until the sticky time elapses so that |
Cirrus01 | 1:b106cf2e99ba | 141 | // we smooth out any "bounce" (electrical transients that |
Cirrus01 | 1:b106cf2e99ba | 142 | // occur when the switch contact is opened or closed). |
Cirrus01 | 1:b106cf2e99ba | 143 | if (bs->bt == 0) |
Cirrus01 | 1:b106cf2e99ba | 144 | { |
Cirrus01 | 1:b106cf2e99ba | 145 | // get the new physical state |
Cirrus01 | 1:b106cf2e99ba | 146 | int pressed = !(*di)->read(); |
Cirrus01 | 1:b106cf2e99ba | 147 | |
Cirrus01 | 1:b106cf2e99ba | 148 | // update the button's logical state if this is a change |
Cirrus01 | 1:b106cf2e99ba | 149 | if (pressed != bs->pressed) |
Cirrus01 | 1:b106cf2e99ba | 150 | { |
Cirrus01 | 1:b106cf2e99ba | 151 | // store the new state |
Cirrus01 | 1:b106cf2e99ba | 152 | bs->pressed = pressed; |
Cirrus01 | 1:b106cf2e99ba | 153 | |
Cirrus01 | 1:b106cf2e99ba | 154 | // start a new sticky period for debouncing this |
Cirrus01 | 1:b106cf2e99ba | 155 | // state change |
Cirrus01 | 1:b106cf2e99ba | 156 | bs->bt = 10; |
Cirrus01 | 1:b106cf2e99ba | 157 | } |
Cirrus01 | 1:b106cf2e99ba | 158 | } |
Cirrus01 | 1:b106cf2e99ba | 159 | |
Cirrus01 | 1:b106cf2e99ba | 160 | // if it's pressed, OR its bit into the state |
Cirrus01 | 1:b106cf2e99ba | 161 | if (bs->pressed) |
Cirrus01 | 1:b106cf2e99ba | 162 | buttons |= bit; |
Cirrus01 | 1:b106cf2e99ba | 163 | } |
Cirrus01 | 1:b106cf2e99ba | 164 | } |
Cirrus01 | 1:b106cf2e99ba | 165 | |
Cirrus01 | 1:b106cf2e99ba | 166 | // return the new button list |
Cirrus01 | 1:b106cf2e99ba | 167 | return buttons; |
Cirrus01 | 1:b106cf2e99ba | 168 | } |
wim | 0:e43878690c0e | 169 | |
Cirrus01 | 1:b106cf2e99ba | 170 | // hat state |
Cirrus01 | 1:b106cf2e99ba | 171 | struct HatState |
Cirrus01 | 1:b106cf2e99ba | 172 | { |
Cirrus01 | 1:b106cf2e99ba | 173 | // current on/off state |
Cirrus01 | 1:b106cf2e99ba | 174 | int pressed; |
Cirrus01 | 1:b106cf2e99ba | 175 | |
Cirrus01 | 1:b106cf2e99ba | 176 | // Sticky time remaining for current state. When a |
Cirrus01 | 1:b106cf2e99ba | 177 | // state transition occurs, we set this to a debounce |
Cirrus01 | 1:b106cf2e99ba | 178 | // period. Future state transitions will be ignored |
Cirrus01 | 1:b106cf2e99ba | 179 | // until the debounce time elapses. |
Cirrus01 | 1:b106cf2e99ba | 180 | int ht; |
Cirrus01 | 1:b106cf2e99ba | 181 | } hatState[NUM_OF_HAT_BUTTONS]; |
Cirrus01 | 1:b106cf2e99ba | 182 | |
Cirrus01 | 1:b106cf2e99ba | 183 | // timer for hat reports |
Cirrus01 | 1:b106cf2e99ba | 184 | static Timer hatTimer; |
Cirrus01 | 1:b106cf2e99ba | 185 | |
Cirrus01 | 1:b106cf2e99ba | 186 | // initialize the hat inputs |
Cirrus01 | 1:b106cf2e99ba | 187 | void initHat() |
Cirrus01 | 1:b106cf2e99ba | 188 | { |
Cirrus01 | 1:b106cf2e99ba | 189 | // create the digital inputs |
Cirrus01 | 1:b106cf2e99ba | 190 | for (int i = 0 ; i < countof(hatDigIn) ; ++i) |
Cirrus01 | 1:b106cf2e99ba | 191 | { |
Cirrus01 | 1:b106cf2e99ba | 192 | if (i < countof(hatMap) && hatMap[i] != NC) |
Cirrus01 | 1:b106cf2e99ba | 193 | hatDigIn[i] = new DigitalIn(hatMap[i]); |
Cirrus01 | 1:b106cf2e99ba | 194 | else |
Cirrus01 | 1:b106cf2e99ba | 195 | hatDigIn[i] = 0; |
Cirrus01 | 1:b106cf2e99ba | 196 | } |
Cirrus01 | 1:b106cf2e99ba | 197 | } |
wim | 0:e43878690c0e | 198 | |
Cirrus01 | 1:b106cf2e99ba | 199 | // read the hat button input state |
Cirrus01 | 1:b106cf2e99ba | 200 | uint8_t readHat() |
Cirrus01 | 1:b106cf2e99ba | 201 | { |
Cirrus01 | 1:b106cf2e99ba | 202 | // start with all buttons off |
Cirrus01 | 1:b106cf2e99ba | 203 | uint8_t hat = 0; |
Cirrus01 | 1:b106cf2e99ba | 204 | |
Cirrus01 | 1:b106cf2e99ba | 205 | // figure the time elapsed since the last scan |
Cirrus01 | 1:b106cf2e99ba | 206 | int dt = hatTimer.read_ms(); |
Cirrus01 | 1:b106cf2e99ba | 207 | |
Cirrus01 | 1:b106cf2e99ba | 208 | // reset the timef for the next scan |
Cirrus01 | 1:b106cf2e99ba | 209 | hatTimer.reset(); |
Cirrus01 | 1:b106cf2e99ba | 210 | |
Cirrus01 | 1:b106cf2e99ba | 211 | // scan the button list |
Cirrus01 | 1:b106cf2e99ba | 212 | uint8_t bit = 1; |
Cirrus01 | 1:b106cf2e99ba | 213 | DigitalIn **di = hatDigIn; |
Cirrus01 | 1:b106cf2e99ba | 214 | HatState *hs = hatState; |
Cirrus01 | 1:b106cf2e99ba | 215 | for (int i = 0 ; i < countof(hatDigIn) ; ++i, ++di, ++hs, bit <<= 1) |
Cirrus01 | 1:b106cf2e99ba | 216 | { |
Cirrus01 | 1:b106cf2e99ba | 217 | // read this button |
Cirrus01 | 1:b106cf2e99ba | 218 | if (*di != 0) |
Cirrus01 | 1:b106cf2e99ba | 219 | { |
Cirrus01 | 1:b106cf2e99ba | 220 | // deduct the elapsed time since the last update |
Cirrus01 | 1:b106cf2e99ba | 221 | // from the button's remaining sticky time |
Cirrus01 | 1:b106cf2e99ba | 222 | hs->ht -= dt; |
Cirrus01 | 1:b106cf2e99ba | 223 | if (hs->ht < 0) |
Cirrus01 | 1:b106cf2e99ba | 224 | hs->ht = 0; |
Cirrus01 | 1:b106cf2e99ba | 225 | |
Cirrus01 | 1:b106cf2e99ba | 226 | // If the sticky time has elapsed, note the new physical |
Cirrus01 | 1:b106cf2e99ba | 227 | // state of the button. If we still have sticky time |
Cirrus01 | 1:b106cf2e99ba | 228 | // remaining, ignore the physical state; the last state |
Cirrus01 | 1:b106cf2e99ba | 229 | // change persists until the sticky time elapses so that |
Cirrus01 | 1:b106cf2e99ba | 230 | // we smooth out any "bounce" (electrical transients that |
Cirrus01 | 1:b106cf2e99ba | 231 | // occur when the switch contact is opened or closed). |
Cirrus01 | 1:b106cf2e99ba | 232 | if (hs->ht == 0) |
Cirrus01 | 1:b106cf2e99ba | 233 | { |
Cirrus01 | 1:b106cf2e99ba | 234 | // get the new physical state |
Cirrus01 | 1:b106cf2e99ba | 235 | int pressed = !(*di)->read(); |
Cirrus01 | 1:b106cf2e99ba | 236 | |
Cirrus01 | 1:b106cf2e99ba | 237 | // update the button's logical state if this is a change |
Cirrus01 | 1:b106cf2e99ba | 238 | if (pressed != hs->pressed) |
Cirrus01 | 1:b106cf2e99ba | 239 | { |
Cirrus01 | 1:b106cf2e99ba | 240 | // store the new state |
Cirrus01 | 1:b106cf2e99ba | 241 | hs->pressed = pressed; |
Cirrus01 | 1:b106cf2e99ba | 242 | |
Cirrus01 | 1:b106cf2e99ba | 243 | // start a new sticky period for debouncing this |
Cirrus01 | 1:b106cf2e99ba | 244 | // state change |
Cirrus01 | 1:b106cf2e99ba | 245 | hs->ht = 10; |
Cirrus01 | 1:b106cf2e99ba | 246 | } |
Cirrus01 | 1:b106cf2e99ba | 247 | } |
Cirrus01 | 1:b106cf2e99ba | 248 | |
Cirrus01 | 1:b106cf2e99ba | 249 | // if it's pressed, OR its bit into the state |
Cirrus01 | 1:b106cf2e99ba | 250 | if (hs->pressed) |
Cirrus01 | 1:b106cf2e99ba | 251 | hat |= bit; |
Cirrus01 | 1:b106cf2e99ba | 252 | pc.printf("Hat: %d\n", hat); |
Cirrus01 | 1:b106cf2e99ba | 253 | } |
Cirrus01 | 1:b106cf2e99ba | 254 | } |
Cirrus01 | 1:b106cf2e99ba | 255 | |
Cirrus01 | 1:b106cf2e99ba | 256 | // return the new button list |
Cirrus01 | 1:b106cf2e99ba | 257 | //pc.printf("Hat: %d", hat); |
Cirrus01 | 1:b106cf2e99ba | 258 | return hat; |
Cirrus01 | 1:b106cf2e99ba | 259 | } |
wim | 0:e43878690c0e | 260 | |
Cirrus01 | 1:b106cf2e99ba | 261 | int main() |
Cirrus01 | 1:b106cf2e99ba | 262 | { |
Cirrus01 | 1:b106cf2e99ba | 263 | //uint16_t i = 0; |
Cirrus01 | 1:b106cf2e99ba | 264 | int16_t throttle = 0; |
Cirrus01 | 1:b106cf2e99ba | 265 | int16_t rudder = 0; |
Cirrus01 | 1:b106cf2e99ba | 266 | int16_t breaks = 0; |
Cirrus01 | 1:b106cf2e99ba | 267 | int16_t flaps = 0; |
Cirrus01 | 1:b106cf2e99ba | 268 | int16_t x = 0; |
Cirrus01 | 1:b106cf2e99ba | 269 | int16_t y = 0; |
Cirrus01 | 1:b106cf2e99ba | 270 | //int32_t radius = 120; |
Cirrus01 | 1:b106cf2e99ba | 271 | //int32_t angle = 0; |
Cirrus01 | 1:b106cf2e99ba | 272 | //uint8_t tmp = 0; |
Cirrus01 | 1:b106cf2e99ba | 273 | //uint32_t tmp = 1; |
Cirrus01 | 1:b106cf2e99ba | 274 | uint32_t buttons = 0; |
Cirrus01 | 1:b106cf2e99ba | 275 | uint8_t hat = 0; |
Cirrus01 | 1:b106cf2e99ba | 276 | |
Cirrus01 | 1:b106cf2e99ba | 277 | pc.printf("Hello World from Joystick!\n\r"); |
Cirrus01 | 1:b106cf2e99ba | 278 | |
Cirrus01 | 1:b106cf2e99ba | 279 | initButtons(); |
Cirrus01 | 1:b106cf2e99ba | 280 | initHat(); |
Cirrus01 | 1:b106cf2e99ba | 281 | |
Cirrus01 | 1:b106cf2e99ba | 282 | heartbeat_start(); |
wim | 0:e43878690c0e | 283 | |
Cirrus01 | 1:b106cf2e99ba | 284 | while (1) { |
Cirrus01 | 1:b106cf2e99ba | 285 | // Basic Joystick |
Cirrus01 | 1:b106cf2e99ba | 286 | // throttle = (i >> 8) & 0xFF; // value -127 .. 128 |
Cirrus01 | 1:b106cf2e99ba | 287 | // rudder = (i >> 8) & 0xFF; // value -127 .. 128 |
Cirrus01 | 1:b106cf2e99ba | 288 | // breaks = (i >> 8) & 0xFF; // value 0 .. 255 |
Cirrus01 | 1:b106cf2e99ba | 289 | // flaps = (i >> 8) & 0xFF; // value 0 .. 255 |
Cirrus01 | 1:b106cf2e99ba | 290 | |
Cirrus01 | 1:b106cf2e99ba | 291 | /* |
Cirrus01 | 1:b106cf2e99ba | 292 | #if (BUTTONS4 == 1) |
Cirrus01 | 1:b106cf2e99ba | 293 | buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button |
Cirrus01 | 1:b106cf2e99ba | 294 | #endif |
Cirrus01 | 1:b106cf2e99ba | 295 | #if (BUTTONS8 == 1) |
Cirrus01 | 1:b106cf2e99ba | 296 | buttons = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button |
Cirrus01 | 1:b106cf2e99ba | 297 | #endif |
Cirrus01 | 1:b106cf2e99ba | 298 | #if (BUTTONS32 == 1) |
Cirrus01 | 1:b106cf2e99ba | 299 | //tmp = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button |
Cirrus01 | 1:b106cf2e99ba | 300 | //buttons = (( tmp << 0) & 0x000000FF); |
Cirrus01 | 1:b106cf2e99ba | 301 | //buttons = buttons | ((~tmp << 8) & 0x0000FF00); |
Cirrus01 | 1:b106cf2e99ba | 302 | //buttons = buttons | (( tmp << 16) & 0x00FF0000); |
Cirrus01 | 1:b106cf2e99ba | 303 | //buttons = buttons | ((~tmp << 24) & 0xFF000000);// |
Cirrus01 | 1:b106cf2e99ba | 304 | buttons = tmp; |
Cirrus01 | 1:b106cf2e99ba | 305 | tmp += 1; |
Cirrus01 | 1:b106cf2e99ba | 306 | pc.printf("Tmp: %u\n", tmp); |
Cirrus01 | 1:b106cf2e99ba | 307 | #endif |
Cirrus01 | 1:b106cf2e99ba | 308 | |
Cirrus01 | 1:b106cf2e99ba | 309 | #if (HAT4 == 1) |
Cirrus01 | 1:b106cf2e99ba | 310 | hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral |
wim | 0:e43878690c0e | 311 | #endif |
wim | 0:e43878690c0e | 312 | #if (HAT8 == 1) |
Cirrus01 | 1:b106cf2e99ba | 313 | hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral |
Cirrus01 | 1:b106cf2e99ba | 314 | #endif |
Cirrus01 | 1:b106cf2e99ba | 315 | i++; |
Cirrus01 | 1:b106cf2e99ba | 316 | |
Cirrus01 | 1:b106cf2e99ba | 317 | //x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128 |
Cirrus01 | 1:b106cf2e99ba | 318 | //y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128 |
Cirrus01 | 1:b106cf2e99ba | 319 | //angle += 3; |
Cirrus01 | 1:b106cf2e99ba | 320 | */ |
Cirrus01 | 1:b106cf2e99ba | 321 | |
Cirrus01 | 1:b106cf2e99ba | 322 | buttons = readButtons(); |
Cirrus01 | 1:b106cf2e99ba | 323 | hat = readHat(); |
Cirrus01 | 1:b106cf2e99ba | 324 | //pc.printf("Hat: %d\n", hat); |
wim | 0:e43878690c0e | 325 | |
Cirrus01 | 1:b106cf2e99ba | 326 | throttle = inThrottle.read() * 256 - 128; |
Cirrus01 | 1:b106cf2e99ba | 327 | if(throttle < -127) |
Cirrus01 | 1:b106cf2e99ba | 328 | throttle = -127; |
Cirrus01 | 1:b106cf2e99ba | 329 | if(throttle > 127) |
Cirrus01 | 1:b106cf2e99ba | 330 | throttle = 127; |
Cirrus01 | 1:b106cf2e99ba | 331 | rudder = inRudder.read() * 256 - 128; |
Cirrus01 | 1:b106cf2e99ba | 332 | if(rudder < -127) |
Cirrus01 | 1:b106cf2e99ba | 333 | rudder = -127; |
Cirrus01 | 1:b106cf2e99ba | 334 | if(rudder > 127) |
Cirrus01 | 1:b106cf2e99ba | 335 | rudder = 127; |
Cirrus01 | 1:b106cf2e99ba | 336 | breaks = inBreaks.read() * 256 - 127; |
Cirrus01 | 1:b106cf2e99ba | 337 | if(breaks < -127) |
Cirrus01 | 1:b106cf2e99ba | 338 | breaks = -127; |
Cirrus01 | 1:b106cf2e99ba | 339 | if(breaks > 127) |
Cirrus01 | 1:b106cf2e99ba | 340 | breaks = 127; |
Cirrus01 | 1:b106cf2e99ba | 341 | flaps = inFlaps.read() * 256 - 127; |
Cirrus01 | 1:b106cf2e99ba | 342 | if(flaps < -127) |
Cirrus01 | 1:b106cf2e99ba | 343 | flaps = -127; |
Cirrus01 | 1:b106cf2e99ba | 344 | if(flaps > 127) |
Cirrus01 | 1:b106cf2e99ba | 345 | flaps = 127; |
wim | 0:e43878690c0e | 346 | |
Cirrus01 | 1:b106cf2e99ba | 347 | x = inX.read() * 256 - 127; |
Cirrus01 | 1:b106cf2e99ba | 348 | if(x < -127) |
Cirrus01 | 1:b106cf2e99ba | 349 | x = -127; |
Cirrus01 | 1:b106cf2e99ba | 350 | if(x > 127) |
Cirrus01 | 1:b106cf2e99ba | 351 | x = 127; |
Cirrus01 | 1:b106cf2e99ba | 352 | y = inY.read() * 254 - 127; |
Cirrus01 | 1:b106cf2e99ba | 353 | if(y < -127) |
Cirrus01 | 1:b106cf2e99ba | 354 | y = -127; |
Cirrus01 | 1:b106cf2e99ba | 355 | if(y < 127) |
Cirrus01 | 1:b106cf2e99ba | 356 | y = 127; |
Cirrus01 | 1:b106cf2e99ba | 357 | |
Cirrus01 | 1:b106cf2e99ba | 358 | joystick.update(throttle, rudder, breaks, flaps, x, y, buttons, hat); |
Cirrus01 | 1:b106cf2e99ba | 359 | wait(0.01); |
Cirrus01 | 1:b106cf2e99ba | 360 | } |
Cirrus01 | 1:b106cf2e99ba | 361 | |
Cirrus01 | 1:b106cf2e99ba | 362 | //pc.printf("Bye World!\n\r"); |
wim | 0:e43878690c0e | 363 | } |