USBJoystick updated for 32 buttons and added wait-for-connect.
USBJoystick.cpp@0:e086541742c3, 2017-01-05 (annotated)
- Committer:
- wim
- Date:
- Thu Jan 05 14:22:02 2017 +0000
- Revision:
- 0:e086541742c3
- Child:
- 1:f44ae6479bfe
USBJoystick updated for 32 buttons and added wait-for-connect.
Who changed what in which revision?
User | Revision | Line number | New 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; |
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() { |
wim | 0:e086541742c3 | 125 | _t = -127; |
wim | 0:e086541742c3 | 126 | _r = -127; |
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 |
wim | 0:e086541742c3 | 154 | // 8 bit values |
wim | 0:e086541742c3 | 155 | LOGICAL_MINIMUM(1), 0x81, // -127 |
wim | 0:e086541742c3 | 156 | LOGICAL_MAXIMUM(1), 0x7f, // 127 |
wim | 0:e086541742c3 | 157 | REPORT_SIZE(1), 0x08, |
wim | 0:e086541742c3 | 158 | REPORT_COUNT(1), 0x02, |
wim | 0:e086541742c3 | 159 | INPUT(1), 0x02, // Data, Variable, Absolute |
wim | 0:e086541742c3 | 160 | // 16 bit values |
wim | 0:e086541742c3 | 161 | // LOGICAL_MINIMUM(1), 0x00, // 0 |
wim | 0:e086541742c3 | 162 | // LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 |
wim | 0:e086541742c3 | 163 | // REPORT_SIZE(1), 0x10, |
wim | 0:e086541742c3 | 164 | // REPORT_COUNT(1), 0x02, |
wim | 0:e086541742c3 | 165 | // INPUT(1), 0x02, // Data, Variable, Absolute |
wim | 0:e086541742c3 | 166 | |
wim | 0:e086541742c3 | 167 | END_COLLECTION(0), |
wim | 0:e086541742c3 | 168 | |
wim | 0:e086541742c3 | 169 | #if (HAT4 == 1) |
wim | 0:e086541742c3 | 170 | // 4 Position Hat Switch |
wim | 0:e086541742c3 | 171 | USAGE(1), 0x39, // Usage (Hat switch) |
wim | 0:e086541742c3 | 172 | LOGICAL_MINIMUM(1), 0x00, // 0 |
wim | 0:e086541742c3 | 173 | LOGICAL_MAXIMUM(1), 0x03, // 3 |
wim | 0:e086541742c3 | 174 | PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0) |
wim | 0:e086541742c3 | 175 | PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270) |
wim | 0:e086541742c3 | 176 | UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos) |
wim | 0:e086541742c3 | 177 | REPORT_SIZE(1), 0x04, |
wim | 0:e086541742c3 | 178 | REPORT_COUNT(1), 0x01, |
wim | 0:e086541742c3 | 179 | INPUT(1), 0x02, // Data, Variable, Absolute |
wim | 0:e086541742c3 | 180 | #endif |
wim | 0:e086541742c3 | 181 | #if (HAT8 == 1) |
wim | 0:e086541742c3 | 182 | // 8 Position Hat Switch |
wim | 0:e086541742c3 | 183 | USAGE(1), 0x39, // Usage (Hat switch) |
wim | 0:e086541742c3 | 184 | LOGICAL_MINIMUM(1), 0x00, // 0 |
wim | 0:e086541742c3 | 185 | LOGICAL_MAXIMUM(1), 0x07, // 7 |
wim | 0:e086541742c3 | 186 | PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0) |
wim | 0:e086541742c3 | 187 | PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315) |
wim | 0:e086541742c3 | 188 | UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos) |
wim | 0:e086541742c3 | 189 | REPORT_SIZE(1), 0x04, |
wim | 0:e086541742c3 | 190 | REPORT_COUNT(1), 0x01, |
wim | 0:e086541742c3 | 191 | INPUT(1), 0x02, // Data, Variable, Absolute |
wim | 0:e086541742c3 | 192 | #endif |
wim | 0:e086541742c3 | 193 | |
wim | 0:e086541742c3 | 194 | // Padding 4 bits |
wim | 0:e086541742c3 | 195 | REPORT_SIZE(1), 0x01, |
wim | 0:e086541742c3 | 196 | REPORT_COUNT(1), 0x04, |
wim | 0:e086541742c3 | 197 | INPUT(1), 0x01, // Constant |
wim | 0:e086541742c3 | 198 | |
wim | 0:e086541742c3 | 199 | |
wim | 0:e086541742c3 | 200 | #if (BUTTONS4 == 1) |
wim | 0:e086541742c3 | 201 | // 4 Buttons |
wim | 0:e086541742c3 | 202 | USAGE_PAGE(1), 0x09, // Buttons |
wim | 0:e086541742c3 | 203 | USAGE_MINIMUM(1), 0x01, // 1 |
wim | 0:e086541742c3 | 204 | USAGE_MAXIMUM(1), 0x04, // 4 |
wim | 0:e086541742c3 | 205 | LOGICAL_MINIMUM(1), 0x00, // 0 |
wim | 0:e086541742c3 | 206 | LOGICAL_MAXIMUM(1), 0x01, // 1 |
wim | 0:e086541742c3 | 207 | REPORT_SIZE(1), 0x01, |
wim | 0:e086541742c3 | 208 | REPORT_COUNT(1), 0x04, |
wim | 0:e086541742c3 | 209 | UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0) |
wim | 0:e086541742c3 | 210 | UNIT(1), 0x00, // Unit (None) |
wim | 0:e086541742c3 | 211 | INPUT(1), 0x02, // Data, Variable, Absolute |
wim | 0:e086541742c3 | 212 | |
wim | 0:e086541742c3 | 213 | // Padding 4 bits |
wim | 0:e086541742c3 | 214 | REPORT_SIZE(1), 0x01, |
wim | 0:e086541742c3 | 215 | REPORT_COUNT(1), 0x04, |
wim | 0:e086541742c3 | 216 | INPUT(1), 0x01, // Constant |
wim | 0:e086541742c3 | 217 | |
wim | 0:e086541742c3 | 218 | #endif |
wim | 0:e086541742c3 | 219 | #if (BUTTONS8 == 1) |
wim | 0:e086541742c3 | 220 | // 8 Buttons |
wim | 0:e086541742c3 | 221 | USAGE_PAGE(1), 0x09, // Buttons |
wim | 0:e086541742c3 | 222 | USAGE_MINIMUM(1), 0x01, // 1 |
wim | 0:e086541742c3 | 223 | USAGE_MAXIMUM(1), 0x08, // 8 |
wim | 0:e086541742c3 | 224 | LOGICAL_MINIMUM(1), 0x00, // 0 |
wim | 0:e086541742c3 | 225 | LOGICAL_MAXIMUM(1), 0x01, // 1 |
wim | 0:e086541742c3 | 226 | REPORT_SIZE(1), 0x01, |
wim | 0:e086541742c3 | 227 | REPORT_COUNT(1), 0x08, |
wim | 0:e086541742c3 | 228 | UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0) |
wim | 0:e086541742c3 | 229 | UNIT(1), 0x00, // Unit (None) |
wim | 0:e086541742c3 | 230 | INPUT(1), 0x02, // Data, Variable, Absolute |
wim | 0:e086541742c3 | 231 | #endif |
wim | 0:e086541742c3 | 232 | |
wim | 0:e086541742c3 | 233 | #if (BUTTONS32 == 1) |
wim | 0:e086541742c3 | 234 | // 32 Buttons |
wim | 0:e086541742c3 | 235 | USAGE_PAGE(1), 0x09, // Buttons |
wim | 0:e086541742c3 | 236 | USAGE_MINIMUM(1), 0x01, // 1 |
wim | 0:e086541742c3 | 237 | USAGE_MAXIMUM(1), 0x20, // 32 |
wim | 0:e086541742c3 | 238 | LOGICAL_MINIMUM(1), 0x00, // 0 |
wim | 0:e086541742c3 | 239 | LOGICAL_MAXIMUM(1), 0x01, // 1 |
wim | 0:e086541742c3 | 240 | REPORT_SIZE(1), 0x01, |
wim | 0:e086541742c3 | 241 | REPORT_COUNT(1), 0x20, |
wim | 0:e086541742c3 | 242 | UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0) |
wim | 0:e086541742c3 | 243 | UNIT(1), 0x00, // Unit (None) |
wim | 0:e086541742c3 | 244 | INPUT(1), 0x02, // Data, Variable, Absolute |
wim | 0:e086541742c3 | 245 | #endif |
wim | 0:e086541742c3 | 246 | |
wim | 0:e086541742c3 | 247 | END_COLLECTION(0) |
wim | 0:e086541742c3 | 248 | }; |
wim | 0:e086541742c3 | 249 | |
wim | 0:e086541742c3 | 250 | reportLength = sizeof(reportDescriptor); |
wim | 0:e086541742c3 | 251 | return reportDescriptor; |
wim | 0:e086541742c3 | 252 | } |