Testing 6DOF USB HID joystick device.

Dependencies:   USBDevice USBJoystick mbed

Committer:
smartsystemdesign
Date:
Fri Jan 13 08:22:21 2017 +0000
Revision:
4:dae441214ba4
Parent:
3:9b51b991e5d6
Child:
6:17b57fe0bc2e
Nevermind last commit message, I am standing at 11 bytes for the report descriptor (was thinking 64 bits).

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
smartsystemdesign 2:e9926793544d 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 2:e9926793544d 36 //PwmOut myled2(LED2);
smartsystemdesign 2:e9926793544d 37 //PwmOut myled3(LED3);
smartsystemdesign 2:e9926793544d 38 //DigitalOut 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 2:e9926793544d 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 2:e9926793544d 55 float mapf(float x, float in_min, float in_max, float out_min, float out_max)
smartsystemdesign 2:e9926793544d 56 {
smartsystemdesign 1:346022f10041 57 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
wim 0:e43878690c0e 58 }
wim 0:e43878690c0e 59
smartsystemdesign 1:346022f10041 60 // Heartbeat monitor
smartsystemdesign 2:e9926793544d 61 //void pulse()
smartsystemdesign 2:e9926793544d 62 //{
smartsystemdesign 2:e9926793544d 63 // heartbeatLED = !heartbeatLED;
smartsystemdesign 1:346022f10041 64 //}
smartsystemdesign 1:346022f10041 65 //
smartsystemdesign 2:e9926793544d 66 //void heartbeat_start()
smartsystemdesign 2:e9926793544d 67 //{
smartsystemdesign 2:e9926793544d 68 // heartbeatLED = 1;
smartsystemdesign 2:e9926793544d 69 // heartbeat.attach(&pulse, 0.5);
smartsystemdesign 1:346022f10041 70 //}
smartsystemdesign 1:346022f10041 71 //
smartsystemdesign 2:e9926793544d 72 //void heartbeat_stop()
smartsystemdesign 2:e9926793544d 73 //{
smartsystemdesign 2:e9926793544d 74 // heartbeat.detach();
smartsystemdesign 1:346022f10041 75 //}
wim 0:e43878690c0e 76
wim 0:e43878690c0e 77
smartsystemdesign 2:e9926793544d 78 int main()
smartsystemdesign 2:e9926793544d 79 {
smartsystemdesign 2:e9926793544d 80 uint16_t i = 0;
smartsystemdesign 2:e9926793544d 81 int16_t x = 0;
smartsystemdesign 2:e9926793544d 82 int16_t y = 0;
smartsystemdesign 2:e9926793544d 83 int16_t z = 0;
smartsystemdesign 2:e9926793544d 84 int16_t rx = 0;
smartsystemdesign 2:e9926793544d 85 int16_t ry = 0;
smartsystemdesign 2:e9926793544d 86 int16_t rz = 0;
smartsystemdesign 2:e9926793544d 87 uint8_t tmp = 0;
smartsystemdesign 2:e9926793544d 88 uint32_t buttons = 0;
smartsystemdesign 2:e9926793544d 89 uint8_t hat = 0;
wim 0:e43878690c0e 90
smartsystemdesign 2:e9926793544d 91 pc.printf("6 DOF Joystick\n\r");
smartsystemdesign 2:e9926793544d 92
smartsystemdesign 2:e9926793544d 93 // myled1 = 0;
smartsystemdesign 2:e9926793544d 94 // myled2 = 0;
smartsystemdesign 2:e9926793544d 95 // myled3 = 0;
smartsystemdesign 2:e9926793544d 96 // heartbeatLED = 0;
smartsystemdesign 1:346022f10041 97 // heartbeat_start();
wim 0:e43878690c0e 98
smartsystemdesign 2:e9926793544d 99 while (1) {
smartsystemdesign 2:e9926793544d 100 // 6DOF Joystick with buttons and a hat
wim 0:e43878690c0e 101
smartsystemdesign 1:346022f10041 102 #if (BUTTONS4 == 1)
smartsystemdesign 2:e9926793544d 103 buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button
smartsystemdesign 1:346022f10041 104 #endif
smartsystemdesign 1:346022f10041 105 #if (BUTTONS8 == 1)
smartsystemdesign 2:e9926793544d 106 buttons = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button
smartsystemdesign 1:346022f10041 107 #endif
smartsystemdesign 1:346022f10041 108 #if (BUTTONS32 == 1)
smartsystemdesign 2:e9926793544d 109 tmp = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button
smartsystemdesign 2:e9926793544d 110 buttons = (( tmp << 0) & 0x000000FF);
smartsystemdesign 2:e9926793544d 111 buttons = buttons | ((~tmp << 8) & 0x0000FF00);
smartsystemdesign 2:e9926793544d 112 buttons = buttons | (( tmp << 16) & 0x00FF0000);
smartsystemdesign 2:e9926793544d 113 buttons = buttons | ((~tmp << 24) & 0xFF000000);
smartsystemdesign 1:346022f10041 114 #endif
wim 0:e43878690c0e 115
smartsystemdesign 1:346022f10041 116 #if (HAT4 == 1)
smartsystemdesign 2:e9926793544d 117 hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral
wim 0:e43878690c0e 118 #endif
wim 0:e43878690c0e 119 #if (HAT8 == 1)
smartsystemdesign 2:e9926793544d 120 hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral
smartsystemdesign 1:346022f10041 121 #endif
smartsystemdesign 2:e9926793544d 122 i++;
smartsystemdesign 2:e9926793544d 123
smartsystemdesign 3:9b51b991e5d6 124 // joystick.move(joyXin.read_u16(), joyYin.read_u16(), joyZin.read_u16(), joyRxin.read_u16(), joyRyin.read_u16(), joyRzin.read_u16());
wim 0:e43878690c0e 125
smartsystemdesign 2:e9926793544d 126 x = map(joyXin.read_u16(), 0, 65535, -32768, 32768); // value -32768 .. 32767
smartsystemdesign 2:e9926793544d 127 y = map(joyYin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 128 z = map(joyZin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 129 rx = map(joyRxin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 130 ry = map(joyRyin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 131 rz = map(joyRzin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 132
smartsystemdesign 2:e9926793544d 133 myled1 = mapf(x, -32768, 32768, 0, 1.0);
smartsystemdesign 2:e9926793544d 134
smartsystemdesign 2:e9926793544d 135 pc.printf(" %d %d %d %d %d %d\r\n", x, y, z, rx, ry, rz);
smartsystemdesign 2:e9926793544d 136
smartsystemdesign 4:dae441214ba4 137 joystick.update(x, y, z, rx, ry, rz, buttons, hat);
smartsystemdesign 2:e9926793544d 138 wait(0.005);
smartsystemdesign 2:e9926793544d 139 }
wim 0:e43878690c0e 140 }