Malte Langermann / USBJoystick_

Dependents:   joy2

Committer:
gagarinlg
Date:
Sun Aug 01 11:49:38 2021 +0000
Revision:
2:de0ca539fa79
Parent:
1:f44ae6479bfe
Child:
3:1f1c9178c55e
bb

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"
gagarinlg 2:de0ca539fa79 29 #include "../buffer_builder.hpp"
wim 0:e086541742c3 30
wim 0:e086541742c3 31 bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons, uint8_t hat) {
wim 0:e086541742c3 32
wim 0:e086541742c3 33 _t = t;
wim 0:e086541742c3 34 _r = r;
wim 0:e086541742c3 35 _x = x;
wim 0:e086541742c3 36 _y = y;
wim 0:e086541742c3 37 _buttons = buttons;
wim 0:e086541742c3 38 _hat = hat;
wim 0:e086541742c3 39
wim 0:e086541742c3 40 return update();
wim 0:e086541742c3 41 }
wim 0:e086541742c3 42
wim 0:e086541742c3 43 bool USBJoystick::update() {
wim 0:e086541742c3 44 HID_REPORT report;
wim 0:e086541742c3 45
gagarinlg 2:de0ca539fa79 46 uint8_t* data = &report.data[0];
gagarinlg 2:de0ca539fa79 47 buffer_builder::put_u16le(data, _t);
gagarinlg 2:de0ca539fa79 48 buffer_builder::put_u16le(data, _r);
gagarinlg 2:de0ca539fa79 49 buffer_builder::put_u16le(data, _x);
gagarinlg 2:de0ca539fa79 50 buffer_builder::put_u16le(data, _y);
gagarinlg 2:de0ca539fa79 51 buffer_builder::put_u16le(data, _t);
gagarinlg 2:de0ca539fa79 52 // buffer_builder::put_u16le(data, 0x5678);
gagarinlg 2:de0ca539fa79 53 #if (BUTTONS4 == 1 && 0)
wim 0:e086541742c3 54 //Hat and 4 Buttons
wim 0:e086541742c3 55 // report.data[4] = ((_buttons & 0x0f) << 4) | (_hat & 0x0f) ;
wim 0:e086541742c3 56 // report.length = 5;
wim 0:e086541742c3 57
wim 0:e086541742c3 58
wim 0:e086541742c3 59 //Use 4 bit padding for hat4 or hat8
gagarinlg 2:de0ca539fa79 60 buffer_builder::put_u8(data, _hat);
wim 0:e086541742c3 61
wim 0:e086541742c3 62 //Use 4 bit padding for buttons
gagarinlg 2:de0ca539fa79 63 buffer_builder::put_u8(data, _buttons);
wim 0:e086541742c3 64 #endif
wim 0:e086541742c3 65
gagarinlg 2:de0ca539fa79 66 #if (BUTTONS8 == 1 && 0)
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
gagarinlg 2:de0ca539fa79 75 buffer_builder::put_u8(data, _hat);
wim 0:e086541742c3 76
wim 0:e086541742c3 77 //Use 8 bits for buttons
gagarinlg 2:de0ca539fa79 78 buffer_builder::put_u8(data, _buttons);
wim 0:e086541742c3 79 #endif
wim 0:e086541742c3 80
gagarinlg 2:de0ca539fa79 81 #if (BUTTONS32 == 1 && 0)
wim 0:e086541742c3 82 //Use 4 bit padding for hat4 or hat8
gagarinlg 2:de0ca539fa79 83 // report.data[4] = (_hat & 0x0f) ;
gagarinlg 2:de0ca539fa79 84 // buffer_builder::put_u8(data, _hat);
wim 0:e086541742c3 85
wim 0:e086541742c3 86 //No bit padding for 32 buttons
gagarinlg 2:de0ca539fa79 87 // buffer_builder::put_u32le(data, _buttons);
wim 0:e086541742c3 88 #endif
gagarinlg 2:de0ca539fa79 89 report.length = data - &report.data[0];
wim 0:e086541742c3 90 return send(&report);
wim 0:e086541742c3 91 }
wim 0:e086541742c3 92
wim 0:e086541742c3 93 bool USBJoystick::throttle(int16_t t) {
wim 0:e086541742c3 94 _t = t;
wim 0:e086541742c3 95 return update();
wim 0:e086541742c3 96 }
wim 0:e086541742c3 97
wim 0:e086541742c3 98 bool USBJoystick::rudder(int16_t r) {
wim 0:e086541742c3 99 _r = r;
wim 0:e086541742c3 100 return update();
wim 0:e086541742c3 101 }
wim 0:e086541742c3 102
wim 0:e086541742c3 103 bool USBJoystick::move(int16_t x, int16_t y) {
wim 0:e086541742c3 104 _x = x;
wim 0:e086541742c3 105 _y = y;
wim 0:e086541742c3 106 return update();
wim 0:e086541742c3 107 }
wim 0:e086541742c3 108
wim 0:e086541742c3 109 bool USBJoystick::buttons(uint32_t buttons) {
wim 0:e086541742c3 110 _buttons = buttons;
wim 0:e086541742c3 111 return update();
wim 0:e086541742c3 112 }
wim 0:e086541742c3 113
wim 0:e086541742c3 114 bool USBJoystick::hat(uint8_t hat) {
wim 0:e086541742c3 115 _hat = hat;
wim 0:e086541742c3 116 return update();
wim 0:e086541742c3 117 }
wim 0:e086541742c3 118
wim 0:e086541742c3 119
wim 0:e086541742c3 120 void USBJoystick::_init() {
wim 0:e086541742c3 121 _t = -127;
wim 0:e086541742c3 122 _r = -127;
wim 0:e086541742c3 123 _x = 0;
wim 0:e086541742c3 124 _y = 0;
wim 0:e086541742c3 125 _buttons = 0x00000000;
wim 0:e086541742c3 126 _hat = 0x00;
wim 0:e086541742c3 127 }
wim 0:e086541742c3 128
wim 0:e086541742c3 129
wim 0:e086541742c3 130 uint8_t * USBJoystick::reportDesc() {
wim 0:e086541742c3 131 static uint8_t reportDescriptor[] = {
wim 0:e086541742c3 132
wim 0:e086541742c3 133 USAGE_PAGE(1), 0x01, // Generic Desktop
wim 0:e086541742c3 134 USAGE(1), 0x04, // Usage (Joystick)
wim 0:e086541742c3 135 COLLECTION(1), 0x01, // Application
gagarinlg 2:de0ca539fa79 136 #if 0
wim 0:e086541742c3 137 USAGE_PAGE(1), 0x02, // Simulation Controls
wim 0:e086541742c3 138 USAGE(1), 0xBB, // Throttle
gagarinlg 2:de0ca539fa79 139 USAGE(1), 0xBA, // Rudder
gagarinlg 2:de0ca539fa79 140
gagarinlg 2:de0ca539fa79 141 /*
wim 0:e086541742c3 142 LOGICAL_MINIMUM(1), 0x81, // -127
wim 0:e086541742c3 143 LOGICAL_MAXIMUM(1), 0x7f, // 127
wim 0:e086541742c3 144 REPORT_SIZE(1), 0x08,
gagarinlg 2:de0ca539fa79 145 */
gagarinlg 2:de0ca539fa79 146 LOGICAL_MINIMUM(2), 0x01, 0x80, // -32767
gagarinlg 2:de0ca539fa79 147 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
gagarinlg 2:de0ca539fa79 148 REPORT_SIZE(1), 0x10,
wim 0:e086541742c3 149 REPORT_COUNT(1), 0x02,
gagarinlg 2:de0ca539fa79 150 INPUT(1), 0x02, // Data, Variable, Absolute
gagarinlg 2:de0ca539fa79 151 #endif
gagarinlg 2:de0ca539fa79 152 #if 1
wim 0:e086541742c3 153 USAGE_PAGE(1), 0x01, // Generic Desktop
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
gagarinlg 2:de0ca539fa79 158 USAGE(1), 0x34, // RY
wim 0:e086541742c3 159 // 8 bit values
gagarinlg 2:de0ca539fa79 160 // LOGICAL_MINIMUM(1), 0x81, // -127
gagarinlg 2:de0ca539fa79 161 // LOGICAL_MAXIMUM(1), 0x7f, // 127
gagarinlg 2:de0ca539fa79 162 // REPORT_SIZE(1), 0x08,
gagarinlg 2:de0ca539fa79 163 // REPORT_COUNT(1), 0x04,
gagarinlg 2:de0ca539fa79 164 // INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 165 // 16 bit values
gagarinlg 2:de0ca539fa79 166 LOGICAL_MINIMUM(2), 0x01, 0x80, // -32767
gagarinlg 2:de0ca539fa79 167 LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767
gagarinlg 2:de0ca539fa79 168 REPORT_SIZE(1), 0x10,
gagarinlg 2:de0ca539fa79 169 REPORT_COUNT(1), 0x05,
gagarinlg 2:de0ca539fa79 170 INPUT(1), 0x02, // Data, Variable, Absolute
wim 0:e086541742c3 171
gagarinlg 2:de0ca539fa79 172 #endif
gagarinlg 2:de0ca539fa79 173 #if (HAT4 == 1 && 0)
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
gagarinlg 2:de0ca539fa79 185 #if (HAT8 == 1 && 0)
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
gagarinlg 2:de0ca539fa79 199 // REPORT_SIZE(1), 0x01,
gagarinlg 2:de0ca539fa79 200 // REPORT_COUNT(1), 0x04,
gagarinlg 2:de0ca539fa79 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
gagarinlg 2:de0ca539fa79 237 #if (BUTTONS32 == 1 && 0)
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 }