Witek Ewert
/
frdmk22f-usbhid-4axis
4-axis USB controller for FPV sims, based on NXP FRDM-K22F
USBJoystick/USBJoystick.cpp@0:9f5a70453c7c, 2020-03-05 (annotated)
- Committer:
- wue
- Date:
- Thu Mar 05 20:57:25 2020 +0000
- Revision:
- 0:9f5a70453c7c
???
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wue | 0:9f5a70453c7c | 1 | /* mbed USBJoystick Library |
wue | 0:9f5a70453c7c | 2 | * Copyright (c) 2012, v01: Initial version, WH, |
wue | 0:9f5a70453c7c | 3 | * Modified USBMouse code ARM Limited. |
wue | 0:9f5a70453c7c | 4 | * (c) 2010-2011 mbed.org, MIT License |
wue | 0:9f5a70453c7c | 5 | * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button |
wue | 0:9f5a70453c7c | 6 | * |
wue | 0:9f5a70453c7c | 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
wue | 0:9f5a70453c7c | 8 | * of this software and associated documentation files (the "Software"), to deal |
wue | 0:9f5a70453c7c | 9 | * in the Software without restriction, inclumosig without limitation the rights |
wue | 0:9f5a70453c7c | 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
wue | 0:9f5a70453c7c | 11 | * copies of the Software, and to permit persons to whom the Software is |
wue | 0:9f5a70453c7c | 12 | * furnished to do so, subject to the following conditions: |
wue | 0:9f5a70453c7c | 13 | * |
wue | 0:9f5a70453c7c | 14 | * The above copyright notice and this permission notice shall be included in |
wue | 0:9f5a70453c7c | 15 | * all copies or substantial portions of the Software. |
wue | 0:9f5a70453c7c | 16 | * |
wue | 0:9f5a70453c7c | 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
wue | 0:9f5a70453c7c | 18 | * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
wue | 0:9f5a70453c7c | 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
wue | 0:9f5a70453c7c | 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
wue | 0:9f5a70453c7c | 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
wue | 0:9f5a70453c7c | 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
wue | 0:9f5a70453c7c | 23 | * THE SOFTWARE. |
wue | 0:9f5a70453c7c | 24 | */ |
wue | 0:9f5a70453c7c | 25 | |
wue | 0:9f5a70453c7c | 26 | |
wue | 0:9f5a70453c7c | 27 | #include "stdint.h" |
wue | 0:9f5a70453c7c | 28 | #include "USBJoystick.h" |
wue | 0:9f5a70453c7c | 29 | |
wue | 0:9f5a70453c7c | 30 | bool USBJoystick::update(int16_t x, int16_t y, int16_t rx, int16_t ry) { |
wue | 0:9f5a70453c7c | 31 | |
wue | 0:9f5a70453c7c | 32 | _x = x; |
wue | 0:9f5a70453c7c | 33 | _y = y; |
wue | 0:9f5a70453c7c | 34 | _rx = rx; |
wue | 0:9f5a70453c7c | 35 | _ry = ry; |
wue | 0:9f5a70453c7c | 36 | |
wue | 0:9f5a70453c7c | 37 | return update(); |
wue | 0:9f5a70453c7c | 38 | } |
wue | 0:9f5a70453c7c | 39 | |
wue | 0:9f5a70453c7c | 40 | bool USBJoystick::update() { |
wue | 0:9f5a70453c7c | 41 | HID_REPORT report; |
wue | 0:9f5a70453c7c | 42 | |
wue | 0:9f5a70453c7c | 43 | // Fill the report according to the Joystick Descriptor |
wue | 0:9f5a70453c7c | 44 | report.data[0] = _x & 0xff; |
wue | 0:9f5a70453c7c | 45 | report.data[1] = (_x >> 8) & 0xff; |
wue | 0:9f5a70453c7c | 46 | report.data[2] = _y & 0xff; |
wue | 0:9f5a70453c7c | 47 | report.data[3] = (_y >> 8) & 0xff; |
wue | 0:9f5a70453c7c | 48 | report.data[4] = _rx & 0xff; |
wue | 0:9f5a70453c7c | 49 | report.data[5] = (_rx >> 8) & 0xff; |
wue | 0:9f5a70453c7c | 50 | report.data[6] = _ry & 0xff; |
wue | 0:9f5a70453c7c | 51 | report.data[7] = (_ry >> 8) & 0xff; |
wue | 0:9f5a70453c7c | 52 | report.length = 8; |
wue | 0:9f5a70453c7c | 53 | |
wue | 0:9f5a70453c7c | 54 | return send(&report); |
wue | 0:9f5a70453c7c | 55 | } |
wue | 0:9f5a70453c7c | 56 | |
wue | 0:9f5a70453c7c | 57 | bool USBJoystick::move(int16_t x, int16_t y, int16_t rx, int16_t ry) { |
wue | 0:9f5a70453c7c | 58 | _x = x; |
wue | 0:9f5a70453c7c | 59 | _y = y; |
wue | 0:9f5a70453c7c | 60 | _rx = rx; |
wue | 0:9f5a70453c7c | 61 | _ry = ry; |
wue | 0:9f5a70453c7c | 62 | return update(); |
wue | 0:9f5a70453c7c | 63 | } |
wue | 0:9f5a70453c7c | 64 | |
wue | 0:9f5a70453c7c | 65 | void USBJoystick::_init() { |
wue | 0:9f5a70453c7c | 66 | _x = 0x0000; |
wue | 0:9f5a70453c7c | 67 | _y = 0x0000; |
wue | 0:9f5a70453c7c | 68 | _rx = 0x0000; |
wue | 0:9f5a70453c7c | 69 | _ry = 0x0000; |
wue | 0:9f5a70453c7c | 70 | } |
wue | 0:9f5a70453c7c | 71 | |
wue | 0:9f5a70453c7c | 72 | |
wue | 0:9f5a70453c7c | 73 | uint8_t * USBJoystick::reportDesc() { |
wue | 0:9f5a70453c7c | 74 | static uint8_t reportDescriptor[] = { |
wue | 0:9f5a70453c7c | 75 | // value in () is the number of bytes. These bytes follow the comma, least significant byte first |
wue | 0:9f5a70453c7c | 76 | // see USBHID_Types.h for more info |
wue | 0:9f5a70453c7c | 77 | USAGE_PAGE(1), 0x01, // Generic Desktop |
wue | 0:9f5a70453c7c | 78 | USAGE(1), 0x04, // Usage (Joystick) |
wue | 0:9f5a70453c7c | 79 | COLLECTION(1), 0x01, // Application |
wue | 0:9f5a70453c7c | 80 | |
wue | 0:9f5a70453c7c | 81 | // 6 Axes of Joystick |
wue | 0:9f5a70453c7c | 82 | USAGE_PAGE(1), 0x01, // Generic Desktop |
wue | 0:9f5a70453c7c | 83 | USAGE(1), 0x01, // Usage (Pointer) |
wue | 0:9f5a70453c7c | 84 | COLLECTION(1), 0x00, // Physical |
wue | 0:9f5a70453c7c | 85 | USAGE(1), 0x30, // X |
wue | 0:9f5a70453c7c | 86 | USAGE(1), 0x31, // Y |
wue | 0:9f5a70453c7c | 87 | USAGE(1), 0x33, // RX |
wue | 0:9f5a70453c7c | 88 | USAGE(1), 0x34, // RY |
wue | 0:9f5a70453c7c | 89 | LOGICAL_MINIMUM(2), 0x00, 0x80, // -32768 (using 2's complement) |
wue | 0:9f5a70453c7c | 90 | LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 (0x7fff, least significant byte first) |
wue | 0:9f5a70453c7c | 91 | REPORT_SIZE(1), 0x10, // REPORT_SIZE describes the number of bits in this element (16, in this case) |
wue | 0:9f5a70453c7c | 92 | REPORT_COUNT(1), 0x04, |
wue | 0:9f5a70453c7c | 93 | INPUT(1), 0x02, // Data, Variable, Absolute |
wue | 0:9f5a70453c7c | 94 | END_COLLECTION(0), |
wue | 0:9f5a70453c7c | 95 | END_COLLECTION(0) |
wue | 0:9f5a70453c7c | 96 | }; |
wue | 0:9f5a70453c7c | 97 | |
wue | 0:9f5a70453c7c | 98 | reportLength = sizeof(reportDescriptor); |
wue | 0:9f5a70453c7c | 99 | return reportDescriptor; |
wue | 0:9f5a70453c7c | 100 | } |