Initial

Dependencies:   mbed USBDevice USBJoystick_SIM

Committer:
Cirrus01
Date:
Wed Mar 25 14:20:37 2020 +0000
Revision:
6:d3042649530d
Parent:
5:64b1b58873f6
Child:
7:6e1b826a62b6
Removed two buttons and added an encoder instead

Who changed what in which revision?

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