Testing 6DOF USB HID joystick device.
Dependencies: USBDevice USBJoystick mbed
main.cpp@1:346022f10041, 2017-01-13 (annotated)
- Committer:
- smartsystemdesign
- Date:
- Fri Jan 13 07:11:30 2017 +0000
- Revision:
- 1:346022f10041
- Parent:
- 0:e43878690c0e
- Child:
- 2:e9926793544d
Updated for 6DOF joystick. The USB game controller is showing up correctly in Windows 10 with the appropriate controls, however I cannot get anything to move in joystick properties.
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 |
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 |
smartsystemdesign | 1:346022f10041 | 35 | PwmOut myled1(LED1); |
smartsystemdesign | 1:346022f10041 | 36 | PwmOut myled2(LED2); |
smartsystemdesign | 1:346022f10041 | 37 | PwmOut myled3(LED3); |
smartsystemdesign | 1:346022f10041 | 38 | PwmOut heartbeatLED(LED4); |
smartsystemdesign | 1:346022f10041 | 39 | |
smartsystemdesign | 1:346022f10041 | 40 | AnalogIn joyXin(A0); |
smartsystemdesign | 1:346022f10041 | 41 | AnalogIn joyYin(A1); |
smartsystemdesign | 1:346022f10041 | 42 | AnalogIn joyZin(A2); |
smartsystemdesign | 1:346022f10041 | 43 | AnalogIn joyRxin(A3); |
smartsystemdesign | 1:346022f10041 | 44 | AnalogIn joyRyin(A4); |
smartsystemdesign | 1:346022f10041 | 45 | AnalogIn joyRzin(A5); |
wim | 0:e43878690c0e | 46 | |
wim | 0:e43878690c0e | 47 | Ticker heartbeat; |
wim | 0:e43878690c0e | 48 | Serial pc(USBTX, USBRX); // tx, rx |
wim | 0:e43878690c0e | 49 | |
smartsystemdesign | 1:346022f10041 | 50 | int16_t map(int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max) // found here: C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\WMath.cpp |
smartsystemdesign | 1:346022f10041 | 51 | { |
smartsystemdesign | 1:346022f10041 | 52 | return (int16_t)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); |
smartsystemdesign | 1:346022f10041 | 53 | } |
smartsystemdesign | 1:346022f10041 | 54 | |
smartsystemdesign | 1:346022f10041 | 55 | float mapf(float x, float in_min, float in_max, float out_min, float out_max) { |
smartsystemdesign | 1:346022f10041 | 56 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
wim | 0:e43878690c0e | 57 | } |
wim | 0:e43878690c0e | 58 | |
smartsystemdesign | 1:346022f10041 | 59 | // Heartbeat monitor |
smartsystemdesign | 1:346022f10041 | 60 | //void pulse() { |
smartsystemdesign | 1:346022f10041 | 61 | // heartbeatLED = !heartbeatLED; |
smartsystemdesign | 1:346022f10041 | 62 | //} |
smartsystemdesign | 1:346022f10041 | 63 | // |
smartsystemdesign | 1:346022f10041 | 64 | //void heartbeat_start() { |
smartsystemdesign | 1:346022f10041 | 65 | // heartbeatLED=1; |
smartsystemdesign | 1:346022f10041 | 66 | // heartbeat.attach(&pulse, 0.5); |
smartsystemdesign | 1:346022f10041 | 67 | //} |
smartsystemdesign | 1:346022f10041 | 68 | // |
smartsystemdesign | 1:346022f10041 | 69 | //void heartbeat_stop() { |
smartsystemdesign | 1:346022f10041 | 70 | // heartbeat.detach(); |
smartsystemdesign | 1:346022f10041 | 71 | //} |
wim | 0:e43878690c0e | 72 | |
wim | 0:e43878690c0e | 73 | |
wim | 0:e43878690c0e | 74 | int main() { |
wim | 0:e43878690c0e | 75 | uint16_t i = 0; |
wim | 0:e43878690c0e | 76 | int16_t x = 0; |
wim | 0:e43878690c0e | 77 | int16_t y = 0; |
smartsystemdesign | 1:346022f10041 | 78 | int16_t z = 0; |
smartsystemdesign | 1:346022f10041 | 79 | int16_t rx = 0; |
smartsystemdesign | 1:346022f10041 | 80 | int16_t ry = 0; |
smartsystemdesign | 1:346022f10041 | 81 | int16_t rz = 0; |
wim | 0:e43878690c0e | 82 | uint8_t tmp = 0; |
smartsystemdesign | 1:346022f10041 | 83 | uint32_t buttons = 0; |
smartsystemdesign | 1:346022f10041 | 84 | uint8_t hat = 0; |
wim | 0:e43878690c0e | 85 | |
wim | 0:e43878690c0e | 86 | pc.printf("Hello World from Joystick!\n\r"); |
wim | 0:e43878690c0e | 87 | |
smartsystemdesign | 1:346022f10041 | 88 | // heartbeat_start(); |
wim | 0:e43878690c0e | 89 | |
wim | 0:e43878690c0e | 90 | while (1) { |
smartsystemdesign | 1:346022f10041 | 91 | // 6DOF Joystick with buttons and a hat |
wim | 0:e43878690c0e | 92 | |
smartsystemdesign | 1:346022f10041 | 93 | #if (BUTTONS4 == 1) |
smartsystemdesign | 1:346022f10041 | 94 | buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button |
smartsystemdesign | 1:346022f10041 | 95 | #endif |
smartsystemdesign | 1:346022f10041 | 96 | #if (BUTTONS8 == 1) |
smartsystemdesign | 1:346022f10041 | 97 | buttons = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button |
smartsystemdesign | 1:346022f10041 | 98 | #endif |
smartsystemdesign | 1:346022f10041 | 99 | #if (BUTTONS32 == 1) |
smartsystemdesign | 1:346022f10041 | 100 | tmp = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button |
wim | 0:e43878690c0e | 101 | buttons = (( tmp << 0) & 0x000000FF); |
wim | 0:e43878690c0e | 102 | buttons = buttons | ((~tmp << 8) & 0x0000FF00); |
wim | 0:e43878690c0e | 103 | buttons = buttons | (( tmp << 16) & 0x00FF0000); |
wim | 0:e43878690c0e | 104 | buttons = buttons | ((~tmp << 24) & 0xFF000000); |
smartsystemdesign | 1:346022f10041 | 105 | #endif |
wim | 0:e43878690c0e | 106 | |
smartsystemdesign | 1:346022f10041 | 107 | #if (HAT4 == 1) |
smartsystemdesign | 1:346022f10041 | 108 | hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral |
wim | 0:e43878690c0e | 109 | #endif |
wim | 0:e43878690c0e | 110 | #if (HAT8 == 1) |
wim | 0:e43878690c0e | 111 | hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral |
smartsystemdesign | 1:346022f10041 | 112 | #endif |
smartsystemdesign | 1:346022f10041 | 113 | i++; |
smartsystemdesign | 1:346022f10041 | 114 | |
smartsystemdesign | 1:346022f10041 | 115 | // joystick.move(joyXin, joyYin, joyZin, joyRxin, joyRyin, joyRzin); |
wim | 0:e43878690c0e | 116 | |
smartsystemdesign | 1:346022f10041 | 117 | x = map(joyXin.read_u16(), 0, 65535, -32768, 32768); // value -32768 .. 32767 |
smartsystemdesign | 1:346022f10041 | 118 | y = map(joyYin.read_u16(), 0, 65535, -32768, 32768); |
smartsystemdesign | 1:346022f10041 | 119 | z = map(joyZin.read_u16(), 0, 65535, -32768, 32768); |
smartsystemdesign | 1:346022f10041 | 120 | rx = map(joyRxin.read_u16(), 0, 65535, -32768, 32768); |
smartsystemdesign | 1:346022f10041 | 121 | ry = map(joyRyin.read_u16(), 0, 65535, -32768, 32768); |
smartsystemdesign | 1:346022f10041 | 122 | rz = map(joyRzin.read_u16(), 0, 65535, -32768, 32768); |
smartsystemdesign | 1:346022f10041 | 123 | |
smartsystemdesign | 1:346022f10041 | 124 | myled1 = mapf(x, -32768, 32768, 0, 1.0); |
smartsystemdesign | 1:346022f10041 | 125 | |
smartsystemdesign | 1:346022f10041 | 126 | joystick.update(x, y, z, rx, ry, rz, buttons, hat); |
smartsystemdesign | 1:346022f10041 | 127 | wait(0.005); |
wim | 0:e43878690c0e | 128 | } |
wim | 0:e43878690c0e | 129 | } |