test joystick demo

Dependencies:   mbed QEI USBDevice USBJoystick

Committer:
sean3381212
Date:
Tue Nov 12 07:17:38 2019 +0000
Revision:
2:712ceeae4f56
Parent:
0:e43878690c0e
Child:
3:5edd4d529cfd
add QEI

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
wim 0:e43878690c0e 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"
wim 0:e43878690c0e 27 #include "USBJoystick.h"
sean3381212 2:712ceeae4f56 28 #include "QEI.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
wim 0:e43878690c0e 36 DigitalOut myled1(LED1);
wim 0:e43878690c0e 37 DigitalOut myled2(LED2);
wim 0:e43878690c0e 38 DigitalOut myled3(LED3);
wim 0:e43878690c0e 39 DigitalOut heartbeatLED(LED4);
wim 0:e43878690c0e 40
wim 0:e43878690c0e 41 Ticker heartbeat;
wim 0:e43878690c0e 42 Serial pc(USBTX, USBRX); // tx, rx
wim 0:e43878690c0e 43
sean3381212 2:712ceeae4f56 44 //Use X4 encoding.
sean3381212 2:712ceeae4f56 45 QEI wheel(PA_6, PA_7, NC, 30, QEI::X4_ENCODING);
sean3381212 2:712ceeae4f56 46
wim 0:e43878690c0e 47 // Heartbeat monitor
wim 0:e43878690c0e 48 void pulse() {
wim 0:e43878690c0e 49 heartbeatLED = !heartbeatLED;
wim 0:e43878690c0e 50 }
wim 0:e43878690c0e 51
wim 0:e43878690c0e 52 void heartbeat_start() {
wim 0:e43878690c0e 53 heartbeatLED=1;
wim 0:e43878690c0e 54 heartbeat.attach(&pulse, 0.5);
wim 0:e43878690c0e 55 }
wim 0:e43878690c0e 56
wim 0:e43878690c0e 57 void heartbeat_stop() {
wim 0:e43878690c0e 58 heartbeat.detach();
wim 0:e43878690c0e 59 }
wim 0:e43878690c0e 60
wim 0:e43878690c0e 61
wim 0:e43878690c0e 62 int main() {
wim 0:e43878690c0e 63 uint16_t i = 0;
wim 0:e43878690c0e 64 int16_t throttle = 0;
wim 0:e43878690c0e 65 int16_t rudder = 0;
wim 0:e43878690c0e 66 int16_t x = 0;
wim 0:e43878690c0e 67 int16_t y = 0;
wim 0:e43878690c0e 68 int32_t radius = 120;
wim 0:e43878690c0e 69 int32_t angle = 0;
wim 0:e43878690c0e 70 uint8_t tmp = 0;
wim 0:e43878690c0e 71 uint32_t buttons = 0;
wim 0:e43878690c0e 72 uint8_t hat = 0;
wim 0:e43878690c0e 73
wim 0:e43878690c0e 74 pc.printf("Hello World from Joystick!\n\r");
wim 0:e43878690c0e 75
wim 0:e43878690c0e 76 heartbeat_start();
wim 0:e43878690c0e 77
wim 0:e43878690c0e 78 while (1) {
wim 0:e43878690c0e 79 // Basic Joystick
wim 0:e43878690c0e 80 throttle = (i >> 8) & 0xFF; // value -127 .. 128
wim 0:e43878690c0e 81 rudder = (i >> 8) & 0xFF; // value -127 .. 128
wim 0:e43878690c0e 82
wim 0:e43878690c0e 83 #if (BUTTONS4 == 1)
wim 0:e43878690c0e 84 buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button
wim 0:e43878690c0e 85 #endif
wim 0:e43878690c0e 86 #if (BUTTONS8 == 1)
wim 0:e43878690c0e 87 buttons = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button
wim 0:e43878690c0e 88 #endif
wim 0:e43878690c0e 89 #if (BUTTONS32 == 1)
wim 0:e43878690c0e 90 tmp = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button
wim 0:e43878690c0e 91 buttons = (( tmp << 0) & 0x000000FF);
wim 0:e43878690c0e 92 buttons = buttons | ((~tmp << 8) & 0x0000FF00);
wim 0:e43878690c0e 93 buttons = buttons | (( tmp << 16) & 0x00FF0000);
wim 0:e43878690c0e 94 buttons = buttons | ((~tmp << 24) & 0xFF000000);
wim 0:e43878690c0e 95 #endif
wim 0:e43878690c0e 96
wim 0:e43878690c0e 97 #if (HAT4 == 1)
wim 0:e43878690c0e 98 hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral
wim 0:e43878690c0e 99 #endif
wim 0:e43878690c0e 100 #if (HAT8 == 1)
wim 0:e43878690c0e 101 hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral
wim 0:e43878690c0e 102 #endif
wim 0:e43878690c0e 103 i++;
wim 0:e43878690c0e 104
sean3381212 2:712ceeae4f56 105 //x = cos((double)angle*3.14/180.0)*radius; // value -127 .. 128
sean3381212 2:712ceeae4f56 106 //y = sin((double)angle*3.14/180.0)*radius; // value -127 .. 128
sean3381212 2:712ceeae4f56 107 //angle += 3;
sean3381212 2:712ceeae4f56 108 x = wheel.getPulses();
wim 0:e43878690c0e 109
wim 0:e43878690c0e 110 joystick.update(throttle, rudder, x, y, buttons, hat);
wim 0:e43878690c0e 111 wait(0.001);
wim 0:e43878690c0e 112 }
wim 0:e43878690c0e 113
wim 0:e43878690c0e 114 pc.printf("Bye World!\n\r");
wim 0:e43878690c0e 115 }