USBJoystick updated for 32 buttons and added wait-for-connect.

Dependents:   joy2

Committer:
gagarinlg
Date:
Sun Jul 25 18:01:15 2021 +0000
Revision:
1:f44ae6479bfe
Parent:
0:e086541742c3
Child:
2:de0ca539fa79
tt;

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
wim 0:e086541742c3 46 report.data[0] = _t & 0xff;
wim 0:e086541742c3 47 report.data[1] = _r & 0xff;
wim 0:e086541742c3 48 report.data[2] = _x & 0xff;
wim 0:e086541742c3 49 report.data[3] = _y & 0xff;
gagarinlg 1:f44ae6479bfe 50 report.data[4] = _x & 0xff;
gagarinlg 1:f44ae6479bfe 51 report.data[5] = _y & 0xff;
wim 0:e086541742c3 52
wim 0:e086541742c3 53
wim 0:e086541742c3 54 #if (BUTTONS4 == 1)
wim 0:e086541742c3 55 //Hat and 4 Buttons
wim 0:e086541742c3 56 // report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ;
wim 0:e086541742c3 57 // report.length = 5;
wim 0:e086541742c3 58
wim 0:e086541742c3 59
wim 0:e086541742c3 60 //Use 4 bit padding for hat4 or hat8
wim 0:e086541742c3 61 report.data[4] = (_hat & 0x0f) ;
wim 0:e086541742c3 62
wim 0:e086541742c3 63 //Use 4 bit padding for buttons
wim 0:e086541742c3 64 report.data[5] = (_buttons & 0x0f) ;
wim 0:e086541742c3 65 report.length = 6;
wim 0:e086541742c3 66 #endif
wim 0:e086541742c3 67
wim 0:e086541742c3 68 #if (BUTTONS8 == 1)
wim 0:e086541742c3 69 //Hat and first 4 Buttons
wim 0:e086541742c3 70 // report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ;
wim 0:e086541742c3 71 //
wim 0:e086541742c3 72 //Use bit padding for last 4 Buttons
wim 0:e086541742c3 73 // report.data[5] = (_buttons & 0xf0) >> 4;
wim 0:e086541742c3 74 // report.length = 6;
wim 0:e086541742c3 75
wim 0:e086541742c3 76 //Use 4 bit padding for hat4 or hat8
wim 0:e086541742c3 77 report.data[4] = (_hat & 0x0f) ;
wim 0:e086541742c3 78
wim 0:e086541742c3 79 //Use 8 bits for buttons
wim 0:e086541742c3 80 report.data[5] = (_buttons & 0xff) ;
wim 0:e086541742c3 81 report.length = 6;
wim 0:e086541742c3 82 #endif
wim 0:e086541742c3 83
wim 0:e086541742c3 84 #if (BUTTONS32 == 1)
wim 0:e086541742c3 85 //Use 4 bit padding for hat4 or hat8
gagarinlg 1:f44ae6479bfe 86 report.data[6] = (_hat & 0x0f) ;
wim 0:e086541742c3 87
wim 0:e086541742c3 88 //No bit padding for 32 buttons
gagarinlg 1:f44ae6479bfe 89 report.data[7] = (_buttons >> 0) & 0xff;
gagarinlg 1:f44ae6479bfe 90 report.data[8] = (_buttons >> 8) & 0xff;
gagarinlg 1:f44ae6479bfe 91 report.data[9] = (_buttons >> 16) & 0xff;
gagarinlg 1:f44ae6479bfe 92 report.data[10] = (_buttons >> 24) & 0xff;
gagarinlg 1:f44ae6479bfe 93 report.length = 11;
wim 0:e086541742c3 94 #endif
wim 0:e086541742c3 95
wim 0:e086541742c3 96 return send(&report);
wim 0:e086541742c3 97 }
wim 0:e086541742c3 98
wim 0:e086541742c3 99 bool USBJoystick::throttle(int16_t t) {
wim 0:e086541742c3 100 _t = t;
wim 0:e086541742c3 101 return update();
wim 0:e086541742c3 102 }
wim 0:e086541742c3 103
wim 0:e086541742c3 104 bool USBJoystick::rudder(int16_t r) {
wim 0:e086541742c3 105 _r = r;
wim 0:e086541742c3 106 return update();
wim 0:e086541742c3 107 }
wim 0:e086541742c3 108
wim 0:e086541742c3 109 bool USBJoystick::move(int16_t x, int16_t y) {
wim 0:e086541742c3 110 _x = x;
wim 0:e086541742c3 111 _y = y;
wim 0:e086541742c3 112 return update();
wim 0:e086541742c3 113 }
wim 0:e086541742c3 114
wim 0:e086541742c3 115 bool USBJoystick::buttons(uint32_t buttons) {
wim 0:e086541742c3 116 _buttons = buttons;
wim 0:e086541742c3 117 return update();
wim 0:e086541742c3 118 }
wim 0:e086541742c3 119
wim 0:e086541742c3 120 bool USBJoystick::hat(uint8_t hat) {
wim 0:e086541742c3 121 _hat = hat;
wim 0:e086541742c3 122 return update();
wim 0:e086541742c3 123 }
wim 0:e086541742c3 124
wim 0:e086541742c3 125
wim 0:e086541742c3 126 void USBJoystick::_init() {
wim 0:e086541742c3 127 _t = -127;
wim 0:e086541742c3 128 _r = -127;
wim 0:e086541742c3 129 _x = 0;
wim 0:e086541742c3 130 _y = 0;
wim 0:e086541742c3 131 _buttons = 0x00000000;
wim 0:e086541742c3 132 _hat = 0x00;
wim 0:e086541742c3 133 }
wim 0:e086541742c3 134
wim 0:e086541742c3 135
wim 0:e086541742c3 136 uint8_t * USBJoystick::reportDesc() {
wim 0:e086541742c3 137 static uint8_t reportDescriptor[] = {
wim 0:e086541742c3 138
wim 0:e086541742c3 139 USAGE_PAGE(1), 0x01, // Generic Desktop
wim 0:e086541742c3 140 LOGICAL_MINIMUM(1), 0x00, // Logical_Minimum (0)
wim 0:e086541742c3 141 USAGE(1), 0x04, // Usage (Joystick)
wim 0:e086541742c3 142 COLLECTION(1), 0x01, // Application
wim 0:e086541742c3 143 USAGE_PAGE(1), 0x02, // Simulation Controls
wim 0:e086541742c3 144 USAGE(1), 0xBB, // Throttle
wim 0:e086541742c3 145 USAGE(1), 0xBA, // Rudder
wim 0:e086541742c3 146 LOGICAL_MINIMUM(1), 0x81, // -127
wim 0:e086541742c3 147 LOGICAL_MAXIMUM(1), 0x7f, // 127
wim 0:e086541742c3 148 REPORT_SIZE(1), 0x08,
wim 0:e086541742c3 149 REPORT_COUNT(1), 0x02,
wim 0:e086541742c3 150 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 151 USAGE_PAGE(1), 0x01, // Generic Desktop
wim 0:e086541742c3 152 USAGE(1), 0x01, // Usage (Pointer)
wim 0:e086541742c3 153 COLLECTION(1), 0x00, // Physical
wim 0:e086541742c3 154 USAGE(1), 0x30, // X
wim 0:e086541742c3 155 USAGE(1), 0x31, // Y
gagarinlg 1:f44ae6479bfe 156 USAGE(1), 0x32, // Z
gagarinlg 1:f44ae6479bfe 157 USAGE(1), 0x33, // RX
wim 0:e086541742c3 158 // 8 bit values
wim 0:e086541742c3 159 LOGICAL_MINIMUM(1), 0x81, // -127
wim 0:e086541742c3 160 LOGICAL_MAXIMUM(1), 0x7f, // 127
wim 0:e086541742c3 161 REPORT_SIZE(1), 0x08,
gagarinlg 1:f44ae6479bfe 162 REPORT_COUNT(1), 0x04,
wim 0:e086541742c3 163 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 164 // 16 bit values
wim 0:e086541742c3 165 // LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 166 // LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
wim 0:e086541742c3 167 // REPORT_SIZE(1), 0x10,
wim 0:e086541742c3 168 // REPORT_COUNT(1), 0x02,
wim 0:e086541742c3 169 // INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 170
wim 0:e086541742c3 171 END_COLLECTION(0),
wim 0:e086541742c3 172
wim 0:e086541742c3 173 #if (HAT4 == 1)
wim 0:e086541742c3 174 // 4 Position Hat Switch
wim 0:e086541742c3 175 USAGE(1), 0x39, // Usage (Hat switch)
wim 0:e086541742c3 176 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 177 LOGICAL_MAXIMUM(1), 0x03, // 3
wim 0:e086541742c3 178 PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0)
wim 0:e086541742c3 179 PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270)
wim 0:e086541742c3 180 UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos)
wim 0:e086541742c3 181 REPORT_SIZE(1), 0x04,
wim 0:e086541742c3 182 REPORT_COUNT(1), 0x01,
wim 0:e086541742c3 183 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 184 #endif
wim 0:e086541742c3 185 #if (HAT8 == 1)
wim 0:e086541742c3 186 // 8 Position Hat Switch
wim 0:e086541742c3 187 USAGE(1), 0x39, // Usage (Hat switch)
wim 0:e086541742c3 188 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 189 LOGICAL_MAXIMUM(1), 0x07, // 7
wim 0:e086541742c3 190 PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0)
wim 0:e086541742c3 191 PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315)
wim 0:e086541742c3 192 UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos)
wim 0:e086541742c3 193 REPORT_SIZE(1), 0x04,
wim 0:e086541742c3 194 REPORT_COUNT(1), 0x01,
wim 0:e086541742c3 195 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 196 #endif
wim 0:e086541742c3 197
wim 0:e086541742c3 198 // Padding 4 bits
wim 0:e086541742c3 199 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 200 REPORT_COUNT(1), 0x04,
wim 0:e086541742c3 201 INPUT(1), 0x01, // Constant
wim 0:e086541742c3 202
wim 0:e086541742c3 203
wim 0:e086541742c3 204 #if (BUTTONS4 == 1)
wim 0:e086541742c3 205 // 4 Buttons
wim 0:e086541742c3 206 USAGE_PAGE(1), 0x09, // Buttons
wim 0:e086541742c3 207 USAGE_MINIMUM(1), 0x01, // 1
wim 0:e086541742c3 208 USAGE_MAXIMUM(1), 0x04, // 4
wim 0:e086541742c3 209 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 210 LOGICAL_MAXIMUM(1), 0x01, // 1
wim 0:e086541742c3 211 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 212 REPORT_COUNT(1), 0x04,
wim 0:e086541742c3 213 UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0)
wim 0:e086541742c3 214 UNIT(1), 0x00, // Unit (None)
wim 0:e086541742c3 215 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 216
wim 0:e086541742c3 217 // Padding 4 bits
wim 0:e086541742c3 218 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 219 REPORT_COUNT(1), 0x04,
wim 0:e086541742c3 220 INPUT(1), 0x01, // Constant
wim 0:e086541742c3 221
wim 0:e086541742c3 222 #endif
wim 0:e086541742c3 223 #if (BUTTONS8 == 1)
wim 0:e086541742c3 224 // 8 Buttons
wim 0:e086541742c3 225 USAGE_PAGE(1), 0x09, // Buttons
wim 0:e086541742c3 226 USAGE_MINIMUM(1), 0x01, // 1
wim 0:e086541742c3 227 USAGE_MAXIMUM(1), 0x08, // 8
wim 0:e086541742c3 228 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 229 LOGICAL_MAXIMUM(1), 0x01, // 1
wim 0:e086541742c3 230 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 231 REPORT_COUNT(1), 0x08,
wim 0:e086541742c3 232 UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0)
wim 0:e086541742c3 233 UNIT(1), 0x00, // Unit (None)
wim 0:e086541742c3 234 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 235 #endif
wim 0:e086541742c3 236
wim 0:e086541742c3 237 #if (BUTTONS32 == 1)
wim 0:e086541742c3 238 // 32 Buttons
wim 0:e086541742c3 239 USAGE_PAGE(1), 0x09, // Buttons
wim 0:e086541742c3 240 USAGE_MINIMUM(1), 0x01, // 1
wim 0:e086541742c3 241 USAGE_MAXIMUM(1), 0x20, // 32
wim 0:e086541742c3 242 LOGICAL_MINIMUM(1), 0x00, // 0
wim 0:e086541742c3 243 LOGICAL_MAXIMUM(1), 0x01, // 1
wim 0:e086541742c3 244 REPORT_SIZE(1), 0x01,
wim 0:e086541742c3 245 REPORT_COUNT(1), 0x20,
wim 0:e086541742c3 246 UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0)
wim 0:e086541742c3 247 UNIT(1), 0x00, // Unit (None)
wim 0:e086541742c3 248 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 249 #endif
wim 0:e086541742c3 250
wim 0:e086541742c3 251 END_COLLECTION(0)
wim 0:e086541742c3 252 };
wim 0:e086541742c3 253
wim 0:e086541742c3 254 reportLength = sizeof(reportDescriptor);
wim 0:e086541742c3 255 return reportDescriptor;
wim 0:e086541742c3 256 }