Demo for USBJoystick updated for 32 buttons.

Dependencies:   USBDevice USBJoystick_SIM mbed USBJoystick_2

Dependents:   USBJoystick_2

Fork of USBJoystick_HelloWorld2 by Wim Huiskamp

Committer:
Cirrus01
Date:
Sat Sep 29 09:28:24 2018 +0000
Revision:
4:dc3556a31262
Parent:
3:3ddaf1227e1b
Bug fixes for Descriptor and Hat Button reading

Who changed what in which revision?

UserRevisionLine numberNew 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 USBJoystick joystick;
wim 0:e43878690c0e 31
wim 0:e43878690c0e 32 // Variables for Heartbeat and Status monitoring
Cirrus01 1:b106cf2e99ba 33 DigitalOut myled1(LED4);
wim 0:e43878690c0e 34 DigitalOut myled2(LED2);
wim 0:e43878690c0e 35 DigitalOut myled3(LED3);
Cirrus01 1:b106cf2e99ba 36 DigitalOut heartbeatLED(LED1);
Cirrus01 1:b106cf2e99ba 37
Cirrus01 4:dc3556a31262 38 AnalogIn inX(A0); // X
Cirrus01 4:dc3556a31262 39 AnalogIn inY(A1); // Y
Cirrus01 4:dc3556a31262 40 AnalogIn inRudder(A2); // Rz (Rudder)
Cirrus01 4:dc3556a31262 41 AnalogIn inThrottle(A3); // Slider (Throttle)
Cirrus01 4:dc3556a31262 42 AnalogIn inBreaks(A4); // Z (Breaks)
Cirrus01 4:dc3556a31262 43 AnalogIn inFlaps(A5); // Rx (Flaps)
wim 0:e43878690c0e 44
wim 0:e43878690c0e 45 Ticker heartbeat;
wim 0:e43878690c0e 46 Serial pc(USBTX, USBRX); // tx, rx
wim 0:e43878690c0e 47
Cirrus01 1:b106cf2e99ba 48 // number of elements in an array
Cirrus01 1:b106cf2e99ba 49 #define countof(x) (sizeof(x)/sizeof((x)[0]))
Cirrus01 1:b106cf2e99ba 50
Cirrus01 1:b106cf2e99ba 51 // button input map array
Cirrus01 1:b106cf2e99ba 52 DigitalIn *buttonDigIn[NUM_OF_BUTTONS]; // config.h
Cirrus01 1:b106cf2e99ba 53
Cirrus01 1:b106cf2e99ba 54 // hat button input map array
Cirrus01 1:b106cf2e99ba 55 DigitalIn *hatDigIn[NUM_OF_HAT_BUTTONS]; // config.h
Cirrus01 1:b106cf2e99ba 56
wim 0:e43878690c0e 57 // Heartbeat monitor
Cirrus01 1:b106cf2e99ba 58 void pulse()
Cirrus01 1:b106cf2e99ba 59 {
Cirrus01 1:b106cf2e99ba 60 heartbeatLED = !heartbeatLED;
Cirrus01 1:b106cf2e99ba 61 }
Cirrus01 1:b106cf2e99ba 62
Cirrus01 1:b106cf2e99ba 63 void heartbeat_start()
Cirrus01 1:b106cf2e99ba 64 {
Cirrus01 1:b106cf2e99ba 65 heartbeatLED=1;
Cirrus01 1:b106cf2e99ba 66 heartbeat.attach(&pulse, 0.5);
Cirrus01 1:b106cf2e99ba 67 }
Cirrus01 1:b106cf2e99ba 68
Cirrus01 1:b106cf2e99ba 69 void heartbeat_stop()
Cirrus01 1:b106cf2e99ba 70 {
Cirrus01 1:b106cf2e99ba 71 heartbeat.detach();
wim 0:e43878690c0e 72 }
wim 0:e43878690c0e 73
Cirrus01 1:b106cf2e99ba 74 // button state
Cirrus01 1:b106cf2e99ba 75 struct ButtonState
Cirrus01 1:b106cf2e99ba 76 {
Cirrus01 1:b106cf2e99ba 77 // current on/off state
Cirrus01 1:b106cf2e99ba 78 int pressed;
Cirrus01 1:b106cf2e99ba 79
Cirrus01 1:b106cf2e99ba 80 // Sticky time remaining for current state. When a
Cirrus01 1:b106cf2e99ba 81 // state transition occurs, we set this to a debounce
Cirrus01 1:b106cf2e99ba 82 // period. Future state transitions will be ignored
Cirrus01 1:b106cf2e99ba 83 // until the debounce time elapses.
Cirrus01 1:b106cf2e99ba 84 int bt;
Cirrus01 1:b106cf2e99ba 85 } buttonState[NUM_OF_BUTTONS];
wim 0:e43878690c0e 86
Cirrus01 1:b106cf2e99ba 87 // timer for button reports
Cirrus01 1:b106cf2e99ba 88 static Timer buttonTimer;
Cirrus01 1:b106cf2e99ba 89
Cirrus01 1:b106cf2e99ba 90 // initialize the button inputs
Cirrus01 1:b106cf2e99ba 91 void initButtons()
Cirrus01 1:b106cf2e99ba 92 {
Cirrus01 1:b106cf2e99ba 93 // create the digital inputs
Cirrus01 4:dc3556a31262 94 for (int i = 0 ; i < countof(buttonDigIn) ; i++)
Cirrus01 1:b106cf2e99ba 95 {
Cirrus01 1:b106cf2e99ba 96 if (i < countof(buttonMap) && buttonMap[i] != NC)
Cirrus01 1:b106cf2e99ba 97 buttonDigIn[i] = new DigitalIn(buttonMap[i]);
Cirrus01 1:b106cf2e99ba 98 else
Cirrus01 1:b106cf2e99ba 99 buttonDigIn[i] = 0;
Cirrus01 1:b106cf2e99ba 100 }
Cirrus01 1:b106cf2e99ba 101
Cirrus01 1:b106cf2e99ba 102 // start the button timer
Cirrus01 1:b106cf2e99ba 103 buttonTimer.start();
wim 0:e43878690c0e 104 }
wim 0:e43878690c0e 105
wim 0:e43878690c0e 106
Cirrus01 1:b106cf2e99ba 107 // read the button input state
Cirrus01 1:b106cf2e99ba 108 uint32_t readButtons()
Cirrus01 1:b106cf2e99ba 109 {
Cirrus01 1:b106cf2e99ba 110 // start with all buttons off
Cirrus01 1:b106cf2e99ba 111 uint32_t buttons = 0;
Cirrus01 1:b106cf2e99ba 112
Cirrus01 1:b106cf2e99ba 113 // figure the time elapsed since the last scan
Cirrus01 1:b106cf2e99ba 114 int dt = buttonTimer.read_ms();
Cirrus01 1:b106cf2e99ba 115
Cirrus01 1:b106cf2e99ba 116 // reset the timef for the next scan
Cirrus01 1:b106cf2e99ba 117 buttonTimer.reset();
wim 0:e43878690c0e 118
Cirrus01 1:b106cf2e99ba 119 // scan the button list
Cirrus01 1:b106cf2e99ba 120 uint32_t bit = 1;
Cirrus01 1:b106cf2e99ba 121 DigitalIn **di = buttonDigIn;
Cirrus01 1:b106cf2e99ba 122 ButtonState *bs = buttonState;
Cirrus01 4:dc3556a31262 123 for (int i = 0 ; i < countof(buttonDigIn) ; i++, di++, bs++, bit <<= 1)
Cirrus01 1:b106cf2e99ba 124 {
Cirrus01 1:b106cf2e99ba 125 // read this button
Cirrus01 1:b106cf2e99ba 126 if (*di != 0)
Cirrus01 1:b106cf2e99ba 127 {
Cirrus01 1:b106cf2e99ba 128 // deduct the elapsed time since the last update
Cirrus01 1:b106cf2e99ba 129 // from the button's remaining sticky time
Cirrus01 1:b106cf2e99ba 130 bs->bt -= dt;
Cirrus01 1:b106cf2e99ba 131 if (bs->bt < 0)
Cirrus01 1:b106cf2e99ba 132 bs->bt = 0;
Cirrus01 1:b106cf2e99ba 133
Cirrus01 1:b106cf2e99ba 134 // If the sticky time has elapsed, note the new physical
Cirrus01 1:b106cf2e99ba 135 // state of the button. If we still have sticky time
Cirrus01 1:b106cf2e99ba 136 // remaining, ignore the physical state; the last state
Cirrus01 1:b106cf2e99ba 137 // change persists until the sticky time elapses so that
Cirrus01 1:b106cf2e99ba 138 // we smooth out any "bounce" (electrical transients that
Cirrus01 1:b106cf2e99ba 139 // occur when the switch contact is opened or closed).
Cirrus01 1:b106cf2e99ba 140 if (bs->bt == 0)
Cirrus01 1:b106cf2e99ba 141 {
Cirrus01 1:b106cf2e99ba 142 // get the new physical state
Cirrus01 1:b106cf2e99ba 143 int pressed = !(*di)->read();
Cirrus01 1:b106cf2e99ba 144
Cirrus01 1:b106cf2e99ba 145 // update the button's logical state if this is a change
Cirrus01 1:b106cf2e99ba 146 if (pressed != bs->pressed)
Cirrus01 1:b106cf2e99ba 147 {
Cirrus01 1:b106cf2e99ba 148 // store the new state
Cirrus01 1:b106cf2e99ba 149 bs->pressed = pressed;
Cirrus01 1:b106cf2e99ba 150
Cirrus01 1:b106cf2e99ba 151 // start a new sticky period for debouncing this
Cirrus01 1:b106cf2e99ba 152 // state change
Cirrus01 1:b106cf2e99ba 153 bs->bt = 10;
Cirrus01 1:b106cf2e99ba 154 }
Cirrus01 1:b106cf2e99ba 155 }
Cirrus01 1:b106cf2e99ba 156
Cirrus01 1:b106cf2e99ba 157 // if it's pressed, OR its bit into the state
Cirrus01 1:b106cf2e99ba 158 if (bs->pressed)
Cirrus01 1:b106cf2e99ba 159 buttons |= bit;
Cirrus01 4:dc3556a31262 160 pc.printf("Buttons: %d\n", buttons);
Cirrus01 1:b106cf2e99ba 161 }
Cirrus01 1:b106cf2e99ba 162 }
Cirrus01 1:b106cf2e99ba 163
Cirrus01 1:b106cf2e99ba 164 // return the new button list
Cirrus01 1:b106cf2e99ba 165 return buttons;
Cirrus01 1:b106cf2e99ba 166 }
wim 0:e43878690c0e 167
Cirrus01 1:b106cf2e99ba 168 // hat state
Cirrus01 1:b106cf2e99ba 169 struct HatState
Cirrus01 1:b106cf2e99ba 170 {
Cirrus01 1:b106cf2e99ba 171 // current on/off state
Cirrus01 1:b106cf2e99ba 172 int pressed;
Cirrus01 1:b106cf2e99ba 173
Cirrus01 1:b106cf2e99ba 174 // Sticky time remaining for current state. When a
Cirrus01 1:b106cf2e99ba 175 // state transition occurs, we set this to a debounce
Cirrus01 1:b106cf2e99ba 176 // period. Future state transitions will be ignored
Cirrus01 1:b106cf2e99ba 177 // until the debounce time elapses.
Cirrus01 1:b106cf2e99ba 178 int ht;
Cirrus01 1:b106cf2e99ba 179 } hatState[NUM_OF_HAT_BUTTONS];
Cirrus01 1:b106cf2e99ba 180
Cirrus01 1:b106cf2e99ba 181 // timer for hat reports
Cirrus01 1:b106cf2e99ba 182 static Timer hatTimer;
Cirrus01 1:b106cf2e99ba 183
Cirrus01 1:b106cf2e99ba 184 // initialize the hat inputs
Cirrus01 1:b106cf2e99ba 185 void initHat()
Cirrus01 1:b106cf2e99ba 186 {
Cirrus01 1:b106cf2e99ba 187 // create the digital inputs
Cirrus01 4:dc3556a31262 188 for (int i = 0 ; i <= countof(hatDigIn) ; i++)
Cirrus01 1:b106cf2e99ba 189 {
Cirrus01 1:b106cf2e99ba 190 if (i < countof(hatMap) && hatMap[i] != NC)
Cirrus01 1:b106cf2e99ba 191 hatDigIn[i] = new DigitalIn(hatMap[i]);
Cirrus01 1:b106cf2e99ba 192 else
Cirrus01 1:b106cf2e99ba 193 hatDigIn[i] = 0;
Cirrus01 1:b106cf2e99ba 194 }
Cirrus01 4:dc3556a31262 195
Cirrus01 4:dc3556a31262 196 // start the hat timer
Cirrus01 4:dc3556a31262 197 hatTimer.start();
Cirrus01 1:b106cf2e99ba 198 }
wim 0:e43878690c0e 199
Cirrus01 1:b106cf2e99ba 200 // read the hat button input state
Cirrus01 1:b106cf2e99ba 201 uint8_t readHat()
Cirrus01 1:b106cf2e99ba 202 {
Cirrus01 1:b106cf2e99ba 203 // start with all buttons off
Cirrus01 1:b106cf2e99ba 204 uint8_t hat = 0;
Cirrus01 1:b106cf2e99ba 205
Cirrus01 1:b106cf2e99ba 206 // figure the time elapsed since the last scan
Cirrus01 1:b106cf2e99ba 207 int dt = hatTimer.read_ms();
Cirrus01 1:b106cf2e99ba 208
Cirrus01 2:967da2faedcd 209 // reset the time for the next scan
Cirrus01 1:b106cf2e99ba 210 hatTimer.reset();
Cirrus01 1:b106cf2e99ba 211
Cirrus01 1:b106cf2e99ba 212 // scan the button list
Cirrus01 1:b106cf2e99ba 213 uint8_t bit = 1;
Cirrus01 1:b106cf2e99ba 214 DigitalIn **di = hatDigIn;
Cirrus01 1:b106cf2e99ba 215 HatState *hs = hatState;
Cirrus01 4:dc3556a31262 216 for (int i = 0 ; i < countof(hatDigIn) ; i++, di++, hs++)
Cirrus01 1:b106cf2e99ba 217 {
Cirrus01 1:b106cf2e99ba 218 // read this button
Cirrus01 1:b106cf2e99ba 219 if (*di != 0)
Cirrus01 1:b106cf2e99ba 220 {
Cirrus01 1:b106cf2e99ba 221 // deduct the elapsed time since the last update
Cirrus01 1:b106cf2e99ba 222 // from the button's remaining sticky time
Cirrus01 1:b106cf2e99ba 223 hs->ht -= dt;
Cirrus01 1:b106cf2e99ba 224 if (hs->ht < 0)
Cirrus01 1:b106cf2e99ba 225 hs->ht = 0;
Cirrus01 1:b106cf2e99ba 226
Cirrus01 1:b106cf2e99ba 227 // If the sticky time has elapsed, note the new physical
Cirrus01 1:b106cf2e99ba 228 // state of the button. If we still have sticky time
Cirrus01 1:b106cf2e99ba 229 // remaining, ignore the physical state; the last state
Cirrus01 1:b106cf2e99ba 230 // change persists until the sticky time elapses so that
Cirrus01 1:b106cf2e99ba 231 // we smooth out any "bounce" (electrical transients that
Cirrus01 1:b106cf2e99ba 232 // occur when the switch contact is opened or closed).
Cirrus01 1:b106cf2e99ba 233 if (hs->ht == 0)
Cirrus01 1:b106cf2e99ba 234 {
Cirrus01 1:b106cf2e99ba 235 // get the new physical state
Cirrus01 1:b106cf2e99ba 236 int pressed = !(*di)->read();
Cirrus01 1:b106cf2e99ba 237
Cirrus01 1:b106cf2e99ba 238 // update the button's logical state if this is a change
Cirrus01 1:b106cf2e99ba 239 if (pressed != hs->pressed)
Cirrus01 1:b106cf2e99ba 240 {
Cirrus01 1:b106cf2e99ba 241 // store the new state
Cirrus01 1:b106cf2e99ba 242 hs->pressed = pressed;
Cirrus01 1:b106cf2e99ba 243
Cirrus01 1:b106cf2e99ba 244 // start a new sticky period for debouncing this
Cirrus01 1:b106cf2e99ba 245 // state change
Cirrus01 1:b106cf2e99ba 246 hs->ht = 10;
Cirrus01 1:b106cf2e99ba 247 }
Cirrus01 1:b106cf2e99ba 248 }
Cirrus01 1:b106cf2e99ba 249
Cirrus01 1:b106cf2e99ba 250 // if it's pressed, OR its bit into the state
Cirrus01 1:b106cf2e99ba 251 if (hs->pressed)
Cirrus01 1:b106cf2e99ba 252 hat |= bit;
Cirrus01 4:dc3556a31262 253 pc.printf("Hat: %d\n", hat);
Cirrus01 1:b106cf2e99ba 254 }
Cirrus01 4:dc3556a31262 255 bit <<= 1;
Cirrus01 1:b106cf2e99ba 256 }
Cirrus01 1:b106cf2e99ba 257
Cirrus01 2:967da2faedcd 258 // translate values read to descriptor values
Cirrus01 2:967da2faedcd 259 #if HAT4 && !HAT4_8
Cirrus01 3:3ddaf1227e1b 260 if (hat == 0x01) // 00000001
Cirrus01 3:3ddaf1227e1b 261 hat = JOY_HAT_UP; // 0
Cirrus01 3:3ddaf1227e1b 262 else if (hat == 0x02) // 00000010
Cirrus01 3:3ddaf1227e1b 263 hat = JOY_HAT_RIGHT; // 1
Cirrus01 3:3ddaf1227e1b 264 else if (hat == 0x04) // 00000100
Cirrus01 3:3ddaf1227e1b 265 hat = JOY_HAT_DOWN; // 2
Cirrus01 3:3ddaf1227e1b 266 else if (hat == 0x08) // 00001000
Cirrus01 3:3ddaf1227e1b 267 hat = JOY_HAT_LEFT; // 3
Cirrus01 3:3ddaf1227e1b 268 else
Cirrus01 3:3ddaf1227e1b 269 hat = JOY_HAT_NEUTRAL; // 4
Cirrus01 2:967da2faedcd 270 #endif
Cirrus01 2:967da2faedcd 271 #if HAT4 && HAT4_8
Cirrus01 3:3ddaf1227e1b 272 if (hat == 0x01) // 00000001
Cirrus01 3:3ddaf1227e1b 273 hat = JOY_HAT_UP; // 0
Cirrus01 3:3ddaf1227e1b 274 else if (hat == 0x03) // 00000011
Cirrus01 3:3ddaf1227e1b 275 hat = JOY_HAT_UP_RIGHT; // 1
Cirrus01 3:3ddaf1227e1b 276 else if (hat == 0x02) // 00000010
Cirrus01 3:3ddaf1227e1b 277 hat = JOY_HAT_RIGHT; // 2
Cirrus01 3:3ddaf1227e1b 278 else if (hat == 0x06) // 00000110
Cirrus01 3:3ddaf1227e1b 279 hat = JOY_HAT_DOWN_RIGHT; // 3
Cirrus01 3:3ddaf1227e1b 280 else if (hat == 0x04) // 00000100
Cirrus01 3:3ddaf1227e1b 281 hat = JOY_HAT_DOWN; // 4
Cirrus01 3:3ddaf1227e1b 282 else if (hat == 0x0C) // 00001100
Cirrus01 3:3ddaf1227e1b 283 hat = JOY_HAT_DOWN_LEFT; // 5
Cirrus01 3:3ddaf1227e1b 284 else if (hat == 0x08) // 00001000
Cirrus01 3:3ddaf1227e1b 285 hat = JOY_HAT_LEFT; // 6
Cirrus01 3:3ddaf1227e1b 286 else if (hat == 0x09) // 00001001
Cirrus01 3:3ddaf1227e1b 287 hat = JOY_HAT_UP_LEFT; // 7
Cirrus01 2:967da2faedcd 288 else
Cirrus01 3:3ddaf1227e1b 289 hat = JOY_HAT_NEUTRAL; // 8
Cirrus01 2:967da2faedcd 290 #endif
Cirrus01 2:967da2faedcd 291 #if HAT8
Cirrus01 3:3ddaf1227e1b 292 if (hat == 0x01) // 00000001
Cirrus01 3:3ddaf1227e1b 293 hat = JOY_HAT_UP; // 0
Cirrus01 3:3ddaf1227e1b 294 else if (hat == 0x02) // 00000010
Cirrus01 3:3ddaf1227e1b 295 hat = JOY_HAT_UP_RIGHT; // 1
Cirrus01 3:3ddaf1227e1b 296 else if (hat == 0x04) // 00000100
Cirrus01 3:3ddaf1227e1b 297 hat = JOY_HAT_RIGHT; // 2
Cirrus01 3:3ddaf1227e1b 298 else if (hat == 0x08) // 00001000
Cirrus01 3:3ddaf1227e1b 299 hat = JOY_HAT_DOWN_RIGHT; // 3
Cirrus01 3:3ddaf1227e1b 300 else if (hat == 0x10) // 00010000
Cirrus01 3:3ddaf1227e1b 301 hat = JOY_HAT_DOWN; // 4
Cirrus01 3:3ddaf1227e1b 302 else if (hat == 0x20) // 00100000
Cirrus01 3:3ddaf1227e1b 303 hat = JOY_HAT_DOWN_LEFT; // 5
Cirrus01 3:3ddaf1227e1b 304 else if (hat == 0x40) // 01000000
Cirrus01 3:3ddaf1227e1b 305 hat = JOY_HAT_LEFT; // 6
Cirrus01 3:3ddaf1227e1b 306 else if (hat == 0x80) // 10000000
Cirrus01 3:3ddaf1227e1b 307 hat = JOY_HAT_UP_LEFT; // 7
Cirrus01 2:967da2faedcd 308 else
Cirrus01 3:3ddaf1227e1b 309 hat = JOY_HAT_NEUTRAL; // 8
Cirrus01 2:967da2faedcd 310 #endif
Cirrus01 2:967da2faedcd 311
Cirrus01 1:b106cf2e99ba 312 // return the new button list
Cirrus01 4:dc3556a31262 313 pc.printf("Return Hat: %d", hat);
Cirrus01 1:b106cf2e99ba 314 return hat;
Cirrus01 1:b106cf2e99ba 315 }
wim 0:e43878690c0e 316
Cirrus01 1:b106cf2e99ba 317 int main()
Cirrus01 1:b106cf2e99ba 318 {
Cirrus01 4:dc3556a31262 319 uint16_t x = 0;
Cirrus01 4:dc3556a31262 320 uint16_t y = 0;
Cirrus01 4:dc3556a31262 321 uint16_t breaks = 0;
Cirrus01 4:dc3556a31262 322 uint16_t flaps = 0;
Cirrus01 4:dc3556a31262 323 uint16_t rudder = 0;
Cirrus01 4:dc3556a31262 324 uint16_t throttle = 0;
Cirrus01 4:dc3556a31262 325 uint8_t hat = 0;
Cirrus01 1:b106cf2e99ba 326 uint32_t buttons = 0;
Cirrus01 2:967da2faedcd 327
Cirrus01 4:dc3556a31262 328
Cirrus01 4:dc3556a31262 329 // const int16_t l = 32767;
Cirrus01 4:dc3556a31262 330 const uint16_t m = 65535;
Cirrus01 1:b106cf2e99ba 331
Cirrus01 1:b106cf2e99ba 332 pc.printf("Hello World from Joystick!\n\r");
Cirrus01 1:b106cf2e99ba 333
Cirrus01 1:b106cf2e99ba 334 initButtons();
Cirrus01 1:b106cf2e99ba 335 initHat();
Cirrus01 1:b106cf2e99ba 336
Cirrus01 1:b106cf2e99ba 337 heartbeat_start();
wim 0:e43878690c0e 338
Cirrus01 4:dc3556a31262 339 pc.printf("x, y, breaks, flaps, rudder, throttle, hat, buttons\n\n\r");
Cirrus01 4:dc3556a31262 340
Cirrus01 1:b106cf2e99ba 341 while (1) {
Cirrus01 1:b106cf2e99ba 342
Cirrus01 4:dc3556a31262 343 x = inX.read() * m;
Cirrus01 4:dc3556a31262 344 y = inY.read() * m;
Cirrus01 4:dc3556a31262 345 breaks = inBreaks.read() * m;
Cirrus01 4:dc3556a31262 346 flaps = inFlaps.read() * m;
Cirrus01 4:dc3556a31262 347 rudder = inRudder.read() * m;
Cirrus01 4:dc3556a31262 348 throttle = inThrottle.read() * m;
Cirrus01 4:dc3556a31262 349 hat = readHat();
Cirrus01 1:b106cf2e99ba 350 buttons = readButtons();
wim 0:e43878690c0e 351
Cirrus01 4:dc3556a31262 352 pc.printf("%d, %d, %d, %d, %d, %d, %d, %d\n\r", x, y, breaks, flaps, rudder, throttle, hat, buttons);
wim 0:e43878690c0e 353
Cirrus01 4:dc3556a31262 354 joystick.update(x, y, breaks, flaps, rudder, throttle, hat, buttons);
Cirrus01 1:b106cf2e99ba 355 wait(0.01);
Cirrus01 1:b106cf2e99ba 356 }
Cirrus01 1:b106cf2e99ba 357
Cirrus01 1:b106cf2e99ba 358 //pc.printf("Bye World!\n\r");
wim 0:e43878690c0e 359 }