Testing 6DOF USB HID joystick device.
Dependencies: USBDevice USBJoystick mbed
main.cpp
00001 /* mbed USBJoystick Library Demo 00002 * Copyright (c) 2012, v01: Initial version, WH, 00003 * Modified USBMouse code ARM Limited. 00004 * (c) 2010-2011 mbed.org, MIT License 00005 * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button 00006 * 00007 * Permission is hereby granted, free of charge, to any person obtaining a copy 00008 * of this software and associated documentation files (the "Software"), to deal 00009 * in the Software without restriction, inclumosig without limitation the rights 00010 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00011 * copies of the Software, and to permit persons to whom the Software is 00012 * furnished to do so, subject to the following conditions: 00013 * 00014 * The above copyright notice and this permission notice shall be included in 00015 * all copies or substantial portions of the Software. 00016 * 00017 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00020 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00022 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00023 * THE SOFTWARE. 00024 */ 00025 00026 // Note: you must connect the usb cable to your computer before the program will proceed 00027 00028 // USB parts from: https://developer.mbed.org/forum/helloworld/topic/3496/ 00029 00030 #include "mbed.h" 00031 #include "USBJoystick.h" 00032 00033 //USBMouse mouse; 00034 USBJoystick joystick; 00035 00036 // Variables for Heartbeat and Status monitoring 00037 PwmOut myled1(LED1); 00038 //PwmOut myled2(LED2); 00039 //PwmOut myled3(LED3); 00040 //DigitalOut heartbeatLED(LED4); 00041 00042 AnalogIn joyXin(A0); 00043 AnalogIn joyYin(A1); 00044 AnalogIn joyZin(A2); 00045 AnalogIn joyRxin(A3); 00046 AnalogIn joyRyin(A4); 00047 AnalogIn joyRzin(A5); 00048 00049 Ticker heartbeat; 00050 Serial pc(USBTX, USBRX); // tx, rx 00051 00052 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 00053 { 00054 return (int16_t)((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min); 00055 } 00056 00057 float mapf(float x, float in_min, float in_max, float out_min, float out_max) 00058 { 00059 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 00060 } 00061 00062 // Heartbeat monitor 00063 //void pulse() 00064 //{ 00065 // heartbeatLED = !heartbeatLED; 00066 //} 00067 // 00068 //void heartbeat_start() 00069 //{ 00070 // heartbeatLED = 1; 00071 // heartbeat.attach(&pulse, 0.5); 00072 //} 00073 // 00074 //void heartbeat_stop() 00075 //{ 00076 // heartbeat.detach(); 00077 //} 00078 00079 00080 int main() 00081 { 00082 uint16_t i = 0; 00083 int16_t x = 0; 00084 int16_t y = 0; 00085 int16_t z = 0; 00086 int16_t rx = 0; 00087 int16_t ry = 0; 00088 int16_t rz = 0; 00089 uint8_t tmp = 0; 00090 uint32_t buttons = 0; 00091 uint8_t hat = 0; 00092 00093 pc.printf("6 DOF Joystick\n\r"); 00094 00095 // myled1 = 0; 00096 // myled2 = 0; 00097 // myled3 = 0; 00098 // heartbeatLED = 0; 00099 // heartbeat_start(); 00100 00101 while (1) { 00102 // 6DOF Joystick with buttons and a hat 00103 00104 #if (BUTTONS4 == 1) 00105 buttons = (i >> 8) & 0x0F; // value 0 .. 15, one bit per button 00106 #endif 00107 #if (BUTTONS8 == 1) 00108 buttons = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button 00109 #endif 00110 #if (BUTTONS32 == 1) 00111 tmp = (i >> 8) & 0xFF; // value 0 .. 255, one bit per button 00112 buttons = (( tmp << 0) & 0x000000FF); 00113 buttons = buttons | ((~tmp << 8) & 0x0000FF00); 00114 buttons = buttons | (( tmp << 16) & 0x00FF0000); 00115 buttons = buttons | ((~tmp << 24) & 0xFF000000); 00116 #endif 00117 00118 #if (HAT4 == 1) 00119 hat = (i >> 8) & 0x03; // value 0, 1, 2, 3 or 4 for neutral 00120 #endif 00121 #if (HAT8 == 1) 00122 hat = (i >> 8) & 0x07; // value 0..7 or 8 for neutral 00123 #endif 00124 i++; 00125 00126 // joystick.move(joyXin.read_u16(), joyYin.read_u16(), joyZin.read_u16(), joyRxin.read_u16(), joyRyin.read_u16(), joyRzin.read_u16()); 00127 00128 x = map(joyXin.read_u16(), 0, 65535, -32768, 32768); // value -32768 .. 32767 00129 y = map(joyYin.read_u16(), 0, 65535, -32768, 32768); 00130 z = map(joyZin.read_u16(), 0, 65535, -32768, 32768); 00131 rx = map(joyRxin.read_u16(), 0, 65535, -32768, 32768); 00132 ry = map(joyRyin.read_u16(), 0, 65535, -32768, 32768); 00133 rz = map(joyRzin.read_u16(), 0, 65535, -32768, 32768); 00134 00135 myled1 = mapf(x, -32768, 32768, 0, 1.0); 00136 00137 pc.printf(" %d %d %d %d %d %d\r\n", x, y, z, rx, ry, rz); 00138 00139 joystick.update(x, y, z, rx, ry, rz, buttons, hat); 00140 wait(0.005); 00141 } 00142 }
Generated on Wed Jul 13 2022 09:48:46 by
1.7.2