Demo for USBJoystick updated for 32 buttons.

Dependencies:   USBDevice USBJoystick mbed

Committer:
wim
Date:
Thu Jan 05 14:23:14 2017 +0000
Revision:
0:e43878690c0e
Demo for USBJoystick, updated for 32 buttons.

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