Testing 6DOF USB HID joystick device.

Dependencies:   USBDevice USBJoystick mbed

Committer:
smartsystemdesign
Date:
Sun Jan 22 07:30:05 2017 +0000
Revision:
8:35f1e9541554
Parent:
7:2a5519440195
Update.

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 */
smartsystemdesign 6:17b57fe0bc2e 25
smartsystemdesign 6:17b57fe0bc2e 26 // Note: you must connect the usb cable to your computer before the program will proceed
smartsystemdesign 7:2a5519440195 27
smartsystemdesign 7:2a5519440195 28 // USB parts from: https://developer.mbed.org/forum/helloworld/topic/3496/
wim 0:e43878690c0e 29
wim 0:e43878690c0e 30 #include "mbed.h"
wim 0:e43878690c0e 31 #include "USBJoystick.h"
wim 0:e43878690c0e 32
wim 0:e43878690c0e 33 //USBMouse mouse;
wim 0:e43878690c0e 34 USBJoystick joystick;
wim 0:e43878690c0e 35
wim 0:e43878690c0e 36 // Variables for Heartbeat and Status monitoring
smartsystemdesign 1:346022f10041 37 PwmOut myled1(LED1);
smartsystemdesign 2:e9926793544d 38 //PwmOut myled2(LED2);
smartsystemdesign 2:e9926793544d 39 //PwmOut myled3(LED3);
smartsystemdesign 2:e9926793544d 40 //DigitalOut heartbeatLED(LED4);
smartsystemdesign 1:346022f10041 41
smartsystemdesign 1:346022f10041 42 AnalogIn joyXin(A0);
smartsystemdesign 1:346022f10041 43 AnalogIn joyYin(A1);
smartsystemdesign 1:346022f10041 44 AnalogIn joyZin(A2);
smartsystemdesign 1:346022f10041 45 AnalogIn joyRxin(A3);
smartsystemdesign 1:346022f10041 46 AnalogIn joyRyin(A4);
smartsystemdesign 1:346022f10041 47 AnalogIn joyRzin(A5);
wim 0:e43878690c0e 48
wim 0:e43878690c0e 49 Ticker heartbeat;
wim 0:e43878690c0e 50 Serial pc(USBTX, USBRX); // tx, rx
wim 0:e43878690c0e 51
smartsystemdesign 1:346022f10041 52 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 53 {
smartsystemdesign 2:e9926793544d 54 return (int16_t)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min);
smartsystemdesign 1:346022f10041 55 }
smartsystemdesign 1:346022f10041 56
smartsystemdesign 2:e9926793544d 57 float mapf(float x, float in_min, float in_max, float out_min, float out_max)
smartsystemdesign 2:e9926793544d 58 {
smartsystemdesign 1:346022f10041 59 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
wim 0:e43878690c0e 60 }
wim 0:e43878690c0e 61
smartsystemdesign 1:346022f10041 62 // Heartbeat monitor
smartsystemdesign 2:e9926793544d 63 //void pulse()
smartsystemdesign 2:e9926793544d 64 //{
smartsystemdesign 2:e9926793544d 65 // heartbeatLED = !heartbeatLED;
smartsystemdesign 1:346022f10041 66 //}
smartsystemdesign 1:346022f10041 67 //
smartsystemdesign 2:e9926793544d 68 //void heartbeat_start()
smartsystemdesign 2:e9926793544d 69 //{
smartsystemdesign 2:e9926793544d 70 // heartbeatLED = 1;
smartsystemdesign 2:e9926793544d 71 // heartbeat.attach(&pulse, 0.5);
smartsystemdesign 1:346022f10041 72 //}
smartsystemdesign 1:346022f10041 73 //
smartsystemdesign 2:e9926793544d 74 //void heartbeat_stop()
smartsystemdesign 2:e9926793544d 75 //{
smartsystemdesign 2:e9926793544d 76 // heartbeat.detach();
smartsystemdesign 1:346022f10041 77 //}
wim 0:e43878690c0e 78
wim 0:e43878690c0e 79
smartsystemdesign 2:e9926793544d 80 int main()
smartsystemdesign 2:e9926793544d 81 {
smartsystemdesign 2:e9926793544d 82 uint16_t i = 0;
smartsystemdesign 2:e9926793544d 83 int16_t x = 0;
smartsystemdesign 2:e9926793544d 84 int16_t y = 0;
smartsystemdesign 2:e9926793544d 85 int16_t z = 0;
smartsystemdesign 2:e9926793544d 86 int16_t rx = 0;
smartsystemdesign 2:e9926793544d 87 int16_t ry = 0;
smartsystemdesign 2:e9926793544d 88 int16_t rz = 0;
smartsystemdesign 2:e9926793544d 89 uint8_t tmp = 0;
smartsystemdesign 2:e9926793544d 90 uint32_t buttons = 0;
smartsystemdesign 2:e9926793544d 91 uint8_t hat = 0;
wim 0:e43878690c0e 92
smartsystemdesign 2:e9926793544d 93 pc.printf("6 DOF Joystick\n\r");
smartsystemdesign 2:e9926793544d 94
smartsystemdesign 2:e9926793544d 95 // myled1 = 0;
smartsystemdesign 2:e9926793544d 96 // myled2 = 0;
smartsystemdesign 2:e9926793544d 97 // myled3 = 0;
smartsystemdesign 2:e9926793544d 98 // heartbeatLED = 0;
smartsystemdesign 1:346022f10041 99 // heartbeat_start();
wim 0:e43878690c0e 100
smartsystemdesign 2:e9926793544d 101 while (1) {
smartsystemdesign 2:e9926793544d 102 // 6DOF Joystick with buttons and a hat
wim 0:e43878690c0e 103
smartsystemdesign 1:346022f10041 104 #if (BUTTONS4 == 1)
smartsystemdesign 2:e9926793544d 105 buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button
smartsystemdesign 1:346022f10041 106 #endif
smartsystemdesign 1:346022f10041 107 #if (BUTTONS8 == 1)
smartsystemdesign 2:e9926793544d 108 buttons = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button
smartsystemdesign 1:346022f10041 109 #endif
smartsystemdesign 1:346022f10041 110 #if (BUTTONS32 == 1)
smartsystemdesign 2:e9926793544d 111 tmp = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button
smartsystemdesign 2:e9926793544d 112 buttons = (( tmp << 0) & 0x000000FF);
smartsystemdesign 2:e9926793544d 113 buttons = buttons | ((~tmp << 8) & 0x0000FF00);
smartsystemdesign 2:e9926793544d 114 buttons = buttons | (( tmp << 16) & 0x00FF0000);
smartsystemdesign 2:e9926793544d 115 buttons = buttons | ((~tmp << 24) & 0xFF000000);
smartsystemdesign 1:346022f10041 116 #endif
wim 0:e43878690c0e 117
smartsystemdesign 1:346022f10041 118 #if (HAT4 == 1)
smartsystemdesign 2:e9926793544d 119 hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral
wim 0:e43878690c0e 120 #endif
wim 0:e43878690c0e 121 #if (HAT8 == 1)
smartsystemdesign 2:e9926793544d 122 hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral
smartsystemdesign 1:346022f10041 123 #endif
smartsystemdesign 2:e9926793544d 124 i++;
smartsystemdesign 2:e9926793544d 125
smartsystemdesign 3:9b51b991e5d6 126 // joystick.move(joyXin.read_u16(), joyYin.read_u16(), joyZin.read_u16(), joyRxin.read_u16(), joyRyin.read_u16(), joyRzin.read_u16());
wim 0:e43878690c0e 127
smartsystemdesign 2:e9926793544d 128 x = map(joyXin.read_u16(), 0, 65535, -32768, 32768); // value -32768 .. 32767
smartsystemdesign 2:e9926793544d 129 y = map(joyYin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 130 z = map(joyZin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 131 rx = map(joyRxin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 132 ry = map(joyRyin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 133 rz = map(joyRzin.read_u16(), 0, 65535, -32768, 32768);
smartsystemdesign 2:e9926793544d 134
smartsystemdesign 2:e9926793544d 135 myled1 = mapf(x, -32768, 32768, 0, 1.0);
smartsystemdesign 2:e9926793544d 136
smartsystemdesign 2:e9926793544d 137 pc.printf(" %d %d %d %d %d %d\r\n", x, y, z, rx, ry, rz);
smartsystemdesign 2:e9926793544d 138
smartsystemdesign 4:dae441214ba4 139 joystick.update(x, y, z, rx, ry, rz, buttons, hat);
smartsystemdesign 2:e9926793544d 140 wait(0.005);
smartsystemdesign 2:e9926793544d 141 }
smartsystemdesign 8:35f1e9541554 142 }