USB Joystick library, modified to work as a 6 DOF joystick.

Dependents:   USBJoystick_Test

Fork of USBJoystick by Wim Huiskamp

Committer:
smartsystemdesign
Date:
Fri Jan 13 05:37:07 2017 +0000
Revision:
1:8b5f213b169f
Parent:
0:e086541742c3
Child:
2:1549541d3b4b
Switching from 8 bit to 16 bit analog controls.  Work in progress.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:e086541742c3 1 /* mbed USBJoystick Library
wim 0:e086541742c3 2 * Copyright (c) 2012, v01: Initial version, WH,
wim 0:e086541742c3 3 * Modified USBMouse code ARM Limited.
wim 0:e086541742c3 4 * (c) 2010-2011 mbed.org, MIT License
wim 0:e086541742c3 5 * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button
wim 0:e086541742c3 6 *
wim 0:e086541742c3 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:e086541742c3 8 * of this software and associated documentation files (the "Software"), to deal
wim 0:e086541742c3 9 * in the Software without restriction, inclumosig without limitation the rights
wim 0:e086541742c3 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:e086541742c3 11 * copies of the Software, and to permit persons to whom the Software is
wim 0:e086541742c3 12 * furnished to do so, subject to the following conditions:
wim 0:e086541742c3 13 *
wim 0:e086541742c3 14 * The above copyright notice and this permission notice shall be included in
wim 0:e086541742c3 15 * all copies or substantial portions of the Software.
wim 0:e086541742c3 16 *
wim 0:e086541742c3 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:e086541742c3 18 * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:e086541742c3 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:e086541742c3 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:e086541742c3 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:e086541742c3 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:e086541742c3 23 * THE SOFTWARE.
wim 0:e086541742c3 24 */
wim 0:e086541742c3 25
wim 0:e086541742c3 26
wim 0:e086541742c3 27 #include "stdint.h"
wim 0:e086541742c3 28 #include "USBJoystick.h"
wim 0:e086541742c3 29
wim 0:e086541742c3 30 bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons, uint8_t hat) {
wim 0:e086541742c3 31
wim 0:e086541742c3 32 _t = t;
wim 0:e086541742c3 33 _r = r;
wim 0:e086541742c3 34 _x = x;
wim 0:e086541742c3 35 _y = y;
wim 0:e086541742c3 36 _buttons = buttons;
wim 0:e086541742c3 37 _hat = hat;
wim 0:e086541742c3 38
wim 0:e086541742c3 39 return update();
wim 0:e086541742c3 40 }
wim 0:e086541742c3 41
wim 0:e086541742c3 42 bool USBJoystick::update() {
wim 0:e086541742c3 43 HID_REPORT report;
wim 0:e086541742c3 44
wim 0:e086541742c3 45 // Fill the report according to the Joystick Descriptor
smartsystemdesign 1:8b5f213b169f 46 report.data[0] = _t & 0xffff;
smartsystemdesign 1:8b5f213b169f 47 report.data[1] = _r & 0xffff;
smartsystemdesign 1:8b5f213b169f 48 report.data[2] = _x & 0xffff;
smartsystemdesign 1:8b5f213b169f 49 report.data[3] = _y & 0xffff;
wim 0:e086541742c3 50
wim 0:e086541742c3 51
wim 0:e086541742c3 52 #if (BUTTONS4 == 1)
wim 0:e086541742c3 53 //Hat and 4 Buttons
wim 0:e086541742c3 54 // report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ;
wim 0:e086541742c3 55 // report.length = 5;
wim 0:e086541742c3 56
wim 0:e086541742c3 57
wim 0:e086541742c3 58 //Use 4 bit padding for hat4 or hat8
wim 0:e086541742c3 59 report.data[4] = (_hat & 0x0f) ;
wim 0:e086541742c3 60
wim 0:e086541742c3 61 //Use 4 bit padding for buttons
wim 0:e086541742c3 62 report.data[5] = (_buttons & 0x0f) ;
wim 0:e086541742c3 63 report.length = 6;
wim 0:e086541742c3 64 #endif
wim 0:e086541742c3 65
wim 0:e086541742c3 66 #if (BUTTONS8 == 1)
wim 0:e086541742c3 67 //Hat and first 4 Buttons
wim 0:e086541742c3 68 // report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ;
wim 0:e086541742c3 69 //
wim 0:e086541742c3 70 //Use bit padding for last 4 Buttons
wim 0:e086541742c3 71 // report.data[5] = (_buttons & 0xf0) >> 4;
wim 0:e086541742c3 72 // report.length = 6;
wim 0:e086541742c3 73
wim 0:e086541742c3 74 //Use 4 bit padding for hat4 or hat8
wim 0:e086541742c3 75 report.data[4] = (_hat & 0x0f) ;
wim 0:e086541742c3 76
wim 0:e086541742c3 77 //Use 8 bits for buttons
wim 0:e086541742c3 78 report.data[5] = (_buttons & 0xff) ;
wim 0:e086541742c3 79 report.length = 6;
wim 0:e086541742c3 80 #endif
wim 0:e086541742c3 81
wim 0:e086541742c3 82 #if (BUTTONS32 == 1)
wim 0:e086541742c3 83 //Use 4 bit padding for hat4 or hat8
wim 0:e086541742c3 84 report.data[4] = (_hat & 0x0f) ;
wim 0:e086541742c3 85
wim 0:e086541742c3 86 //No bit padding for 32 buttons
wim 0:e086541742c3 87 report.data[5] = (_buttons >> 0) & 0xff;
wim 0:e086541742c3 88 report.data[6] = (_buttons >> 8) & 0xff;
wim 0:e086541742c3 89 report.data[7] = (_buttons >> 16) & 0xff;
wim 0:e086541742c3 90 report.data[8] = (_buttons >> 24) & 0xff;
wim 0:e086541742c3 91 report.length = 9;
wim 0:e086541742c3 92 #endif
wim 0:e086541742c3 93
wim 0:e086541742c3 94 return send(&report);
wim 0:e086541742c3 95 }
wim 0:e086541742c3 96
wim 0:e086541742c3 97 bool USBJoystick::throttle(int16_t t) {
wim 0:e086541742c3 98 _t = t;
wim 0:e086541742c3 99 return update();
wim 0:e086541742c3 100 }
wim 0:e086541742c3 101
wim 0:e086541742c3 102 bool USBJoystick::rudder(int16_t r) {
wim 0:e086541742c3 103 _r = r;
wim 0:e086541742c3 104 return update();
wim 0:e086541742c3 105 }
wim 0:e086541742c3 106
wim 0:e086541742c3 107 bool USBJoystick::move(int16_t x, int16_t y) {
wim 0:e086541742c3 108 _x = x;
wim 0:e086541742c3 109 _y = y;
wim 0:e086541742c3 110 return update();
wim 0:e086541742c3 111 }
wim 0:e086541742c3 112
wim 0:e086541742c3 113 bool USBJoystick::buttons(uint32_t buttons) {
wim 0:e086541742c3 114 _buttons = buttons;
wim 0:e086541742c3 115 return update();
wim 0:e086541742c3 116 }
wim 0:e086541742c3 117
wim 0:e086541742c3 118 bool USBJoystick::hat(uint8_t hat) {
wim 0:e086541742c3 119 _hat = hat;
wim 0:e086541742c3 120 return update();
wim 0:e086541742c3 121 }
wim 0:e086541742c3 122
wim 0:e086541742c3 123
wim 0:e086541742c3 124 void USBJoystick::_init() {
smartsystemdesign 1:8b5f213b169f 125 _t = -32768;
smartsystemdesign 1:8b5f213b169f 126 _r = -32768;
wim 0:e086541742c3 127 _x = 0;
wim 0:e086541742c3 128 _y = 0;
wim 0:e086541742c3 129 _buttons = 0x00000000;
wim 0:e086541742c3 130 _hat = 0x00;
wim 0:e086541742c3 131 }
wim 0:e086541742c3 132
wim 0:e086541742c3 133
wim 0:e086541742c3 134 uint8_t * USBJoystick::reportDesc() {
wim 0:e086541742c3 135 static uint8_t reportDescriptor[] = {
wim 0:e086541742c3 136
wim 0:e086541742c3 137 USAGE_PAGE(1), 0x01, // Generic Desktop
wim 0:e086541742c3 138 LOGICAL_MINIMUM(1), 0x00, // Logical_Minimum (0)
wim 0:e086541742c3 139 USAGE(1), 0x04, // Usage (Joystick)
wim 0:e086541742c3 140 COLLECTION(1), 0x01, // Application
wim 0:e086541742c3 141 USAGE_PAGE(1), 0x02, // Simulation Controls
wim 0:e086541742c3 142 USAGE(1), 0xBB, // Throttle
wim 0:e086541742c3 143 USAGE(1), 0xBA, // Rudder
wim 0:e086541742c3 144 LOGICAL_MINIMUM(1), 0x81, // -127
wim 0:e086541742c3 145 LOGICAL_MAXIMUM(1), 0x7f, // 127
wim 0:e086541742c3 146 REPORT_SIZE(1), 0x08,
wim 0:e086541742c3 147 REPORT_COUNT(1), 0x02,
wim 0:e086541742c3 148 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 149 USAGE_PAGE(1), 0x01, // Generic Desktop
wim 0:e086541742c3 150 USAGE(1), 0x01, // Usage (Pointer)
wim 0:e086541742c3 151 COLLECTION(1), 0x00, // Physical
wim 0:e086541742c3 152 USAGE(1), 0x30, // X
wim 0:e086541742c3 153 USAGE(1), 0x31, // Y
smartsystemdesign 1:8b5f213b169f 154 //// 8 bit values
smartsystemdesign 1:8b5f213b169f 155 // LOGICAL_MINIMUM(1), 0x81, // -127
smartsystemdesign 1:8b5f213b169f 156 // LOGICAL_MAXIMUM(1), 0x7f, // 127
smartsystemdesign 1:8b5f213b169f 157 // REPORT_SIZE(1), 0x08,
smartsystemdesign 1:8b5f213b169f 158 // REPORT_COUNT(1), 0x02,
smartsystemdesign 1:8b5f213b169f 159 // INPUT(1), 0x02, // Data, Variable, Absolute
smartsystemdesign 1:8b5f213b169f 160
smartsystemdesign 1:8b5f213b169f 161 // 16 bit values
smartsystemdesign 1:8b5f213b169f 162 LOGICAL_MINIMUM(1), 0x00, // 0
smartsystemdesign 1:8b5f213b169f 163 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
smartsystemdesign 1:8b5f213b169f 164 REPORT_SIZE(1), 0x10,
wim 0:e086541742c3 165 REPORT_COUNT(1), 0x02,
smartsystemdesign 1:8b5f213b169f 166 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 167
wim 0:e086541742c3 168 END_COLLECTION(0),
wim 0:e086541742c3 169
wim 0:e086541742c3 170 #if (HAT4 == 1)
wim 0:e086541742c3 171 // 4 Position Hat Switch
wim 0:e086541742c3 172 USAGE(1), 0x39, // Usage (Hat switch)
wim 0:e086541742c3 173 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 174 LOGICAL_MAXIMUM(1), 0x03, // 3
wim 0:e086541742c3 175 PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0)
wim 0:e086541742c3 176 PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270)
wim 0:e086541742c3 177 UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos)
wim 0:e086541742c3 178 REPORT_SIZE(1), 0x04,
wim 0:e086541742c3 179 REPORT_COUNT(1), 0x01,
wim 0:e086541742c3 180 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 181 #endif
wim 0:e086541742c3 182 #if (HAT8 == 1)
wim 0:e086541742c3 183 // 8 Position Hat Switch
wim 0:e086541742c3 184 USAGE(1), 0x39, // Usage (Hat switch)
wim 0:e086541742c3 185 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 186 LOGICAL_MAXIMUM(1), 0x07, // 7
wim 0:e086541742c3 187 PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0)
wim 0:e086541742c3 188 PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315)
wim 0:e086541742c3 189 UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos)
wim 0:e086541742c3 190 REPORT_SIZE(1), 0x04,
wim 0:e086541742c3 191 REPORT_COUNT(1), 0x01,
wim 0:e086541742c3 192 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 193 #endif
wim 0:e086541742c3 194
wim 0:e086541742c3 195 // Padding 4 bits
wim 0:e086541742c3 196 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 197 REPORT_COUNT(1), 0x04,
wim 0:e086541742c3 198 INPUT(1), 0x01, // Constant
wim 0:e086541742c3 199
wim 0:e086541742c3 200
wim 0:e086541742c3 201 #if (BUTTONS4 == 1)
wim 0:e086541742c3 202 // 4 Buttons
wim 0:e086541742c3 203 USAGE_PAGE(1), 0x09, // Buttons
wim 0:e086541742c3 204 USAGE_MINIMUM(1), 0x01, // 1
wim 0:e086541742c3 205 USAGE_MAXIMUM(1), 0x04, // 4
wim 0:e086541742c3 206 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 207 LOGICAL_MAXIMUM(1), 0x01, // 1
wim 0:e086541742c3 208 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 209 REPORT_COUNT(1), 0x04,
wim 0:e086541742c3 210 UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0)
wim 0:e086541742c3 211 UNIT(1), 0x00, // Unit (None)
wim 0:e086541742c3 212 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 213
wim 0:e086541742c3 214 // Padding 4 bits
wim 0:e086541742c3 215 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 216 REPORT_COUNT(1), 0x04,
wim 0:e086541742c3 217 INPUT(1), 0x01, // Constant
wim 0:e086541742c3 218
wim 0:e086541742c3 219 #endif
wim 0:e086541742c3 220 #if (BUTTONS8 == 1)
wim 0:e086541742c3 221 // 8 Buttons
wim 0:e086541742c3 222 USAGE_PAGE(1), 0x09, // Buttons
wim 0:e086541742c3 223 USAGE_MINIMUM(1), 0x01, // 1
wim 0:e086541742c3 224 USAGE_MAXIMUM(1), 0x08, // 8
wim 0:e086541742c3 225 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 226 LOGICAL_MAXIMUM(1), 0x01, // 1
wim 0:e086541742c3 227 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 228 REPORT_COUNT(1), 0x08,
wim 0:e086541742c3 229 UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0)
wim 0:e086541742c3 230 UNIT(1), 0x00, // Unit (None)
wim 0:e086541742c3 231 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 232 #endif
wim 0:e086541742c3 233
wim 0:e086541742c3 234 #if (BUTTONS32 == 1)
wim 0:e086541742c3 235 // 32 Buttons
wim 0:e086541742c3 236 USAGE_PAGE(1), 0x09, // Buttons
wim 0:e086541742c3 237 USAGE_MINIMUM(1), 0x01, // 1
wim 0:e086541742c3 238 USAGE_MAXIMUM(1), 0x20, // 32
wim 0:e086541742c3 239 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 240 LOGICAL_MAXIMUM(1), 0x01, // 1
wim 0:e086541742c3 241 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 242 REPORT_COUNT(1), 0x20,
wim 0:e086541742c3 243 UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0)
wim 0:e086541742c3 244 UNIT(1), 0x00, // Unit (None)
wim 0:e086541742c3 245 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 246 #endif
wim 0:e086541742c3 247
wim 0:e086541742c3 248 END_COLLECTION(0)
wim 0:e086541742c3 249 };
wim 0:e086541742c3 250
wim 0:e086541742c3 251 reportLength = sizeof(reportDescriptor);
wim 0:e086541742c3 252 return reportDescriptor;
wim 0:e086541742c3 253 }