Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: USBDevice mbed-rtos mbed
Fork of JoyStick by
USBJoystick.cpp@0:33bc88c4ab31, 2013-08-28 (annotated)
- Committer:
- rvt
- Date:
- Wed Aug 28 02:33:03 2013 +0000
- Revision:
- 0:33bc88c4ab31
- Child:
- 2:ae7a31a3c618
Initial release with RTOS
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rvt | 0:33bc88c4ab31 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
| rvt | 0:33bc88c4ab31 | 2 | * Modified Mouse code for Joystick - WH 2012 |
| rvt | 0:33bc88c4ab31 | 3 | * |
| rvt | 0:33bc88c4ab31 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| rvt | 0:33bc88c4ab31 | 5 | * and associated documentation files (the "Software"), to deal in the Software without |
| rvt | 0:33bc88c4ab31 | 6 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
| rvt | 0:33bc88c4ab31 | 7 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
| rvt | 0:33bc88c4ab31 | 8 | * Software is furnished to do so, subject to the following conditions: |
| rvt | 0:33bc88c4ab31 | 9 | * |
| rvt | 0:33bc88c4ab31 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
| rvt | 0:33bc88c4ab31 | 11 | * substantial portions of the Software. |
| rvt | 0:33bc88c4ab31 | 12 | * |
| rvt | 0:33bc88c4ab31 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| rvt | 0:33bc88c4ab31 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| rvt | 0:33bc88c4ab31 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| rvt | 0:33bc88c4ab31 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| rvt | 0:33bc88c4ab31 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| rvt | 0:33bc88c4ab31 | 18 | */ |
| rvt | 0:33bc88c4ab31 | 19 | |
| rvt | 0:33bc88c4ab31 | 20 | #include "stdint.h" |
| rvt | 0:33bc88c4ab31 | 21 | #include "USBJoystick.h" |
| rvt | 0:33bc88c4ab31 | 22 | |
| rvt | 0:33bc88c4ab31 | 23 | bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint8_t button, uint8_t hat) { |
| rvt | 0:33bc88c4ab31 | 24 | HID_REPORT report; |
| rvt | 0:33bc88c4ab31 | 25 | _t = t; |
| rvt | 0:33bc88c4ab31 | 26 | _r = r; |
| rvt | 0:33bc88c4ab31 | 27 | _x = x; |
| rvt | 0:33bc88c4ab31 | 28 | _y = y; |
| rvt | 0:33bc88c4ab31 | 29 | _button = button; |
| rvt | 0:33bc88c4ab31 | 30 | _hat = hat; |
| rvt | 0:33bc88c4ab31 | 31 | |
| rvt | 0:33bc88c4ab31 | 32 | // Fill the report according to the Joystick Descriptor |
| rvt | 0:33bc88c4ab31 | 33 | report.data[0] = _t & 0xff; |
| rvt | 0:33bc88c4ab31 | 34 | report.data[1] = (_t >> 8) & 0xff; |
| rvt | 0:33bc88c4ab31 | 35 | report.data[2] = _r & 0xff; |
| rvt | 0:33bc88c4ab31 | 36 | report.data[3] = (_r >> 8) & 0xff; |
| rvt | 0:33bc88c4ab31 | 37 | report.data[4] = _x & 0xff; |
| rvt | 0:33bc88c4ab31 | 38 | report.data[5] = (_x >> 8) & 0xff; |
| rvt | 0:33bc88c4ab31 | 39 | report.data[6] = _y & 0xff; |
| rvt | 0:33bc88c4ab31 | 40 | report.data[7] = (_y >> 8) & 0xff; |
| rvt | 0:33bc88c4ab31 | 41 | report.data[8] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; |
| rvt | 0:33bc88c4ab31 | 42 | report.length = 9; |
| rvt | 0:33bc88c4ab31 | 43 | |
| rvt | 0:33bc88c4ab31 | 44 | return send(&report); |
| rvt | 0:33bc88c4ab31 | 45 | } |
| rvt | 0:33bc88c4ab31 | 46 | |
| rvt | 0:33bc88c4ab31 | 47 | bool USBJoystick::update() { |
| rvt | 0:33bc88c4ab31 | 48 | HID_REPORT report; |
| rvt | 0:33bc88c4ab31 | 49 | |
| rvt | 0:33bc88c4ab31 | 50 | // Fill the report according to the Joystick Descriptor |
| rvt | 0:33bc88c4ab31 | 51 | report.data[0] = _t & 0xff; |
| rvt | 0:33bc88c4ab31 | 52 | report.data[1] = (_t >> 8) & 0xff; |
| rvt | 0:33bc88c4ab31 | 53 | report.data[2] = _r & 0xff; |
| rvt | 0:33bc88c4ab31 | 54 | report.data[3] = (_r >> 8) & 0xff; |
| rvt | 0:33bc88c4ab31 | 55 | report.data[4] = _x & 0xff; |
| rvt | 0:33bc88c4ab31 | 56 | report.data[5] = (_x >> 8) & 0xff; |
| rvt | 0:33bc88c4ab31 | 57 | report.data[6] = _y & 0xff; |
| rvt | 0:33bc88c4ab31 | 58 | report.data[7] = (_y >> 8) & 0xff; |
| rvt | 0:33bc88c4ab31 | 59 | report.data[8] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; |
| rvt | 0:33bc88c4ab31 | 60 | report.length = 9; |
| rvt | 0:33bc88c4ab31 | 61 | |
| rvt | 0:33bc88c4ab31 | 62 | return send(&report); |
| rvt | 0:33bc88c4ab31 | 63 | } |
| rvt | 0:33bc88c4ab31 | 64 | |
| rvt | 0:33bc88c4ab31 | 65 | bool USBJoystick::throttle(int16_t t) { |
| rvt | 0:33bc88c4ab31 | 66 | _t = t; |
| rvt | 0:33bc88c4ab31 | 67 | return update(); |
| rvt | 0:33bc88c4ab31 | 68 | } |
| rvt | 0:33bc88c4ab31 | 69 | |
| rvt | 0:33bc88c4ab31 | 70 | bool USBJoystick::rudder(int16_t r) { |
| rvt | 0:33bc88c4ab31 | 71 | _r = r; |
| rvt | 0:33bc88c4ab31 | 72 | return update(); |
| rvt | 0:33bc88c4ab31 | 73 | } |
| rvt | 0:33bc88c4ab31 | 74 | |
| rvt | 0:33bc88c4ab31 | 75 | bool USBJoystick::move(int16_t x, int16_t y) { |
| rvt | 0:33bc88c4ab31 | 76 | _x = x; |
| rvt | 0:33bc88c4ab31 | 77 | _y = y; |
| rvt | 0:33bc88c4ab31 | 78 | return update(); |
| rvt | 0:33bc88c4ab31 | 79 | } |
| rvt | 0:33bc88c4ab31 | 80 | |
| rvt | 0:33bc88c4ab31 | 81 | bool USBJoystick::button(uint8_t button) { |
| rvt | 0:33bc88c4ab31 | 82 | _button = button; |
| rvt | 0:33bc88c4ab31 | 83 | return update(); |
| rvt | 0:33bc88c4ab31 | 84 | } |
| rvt | 0:33bc88c4ab31 | 85 | |
| rvt | 0:33bc88c4ab31 | 86 | bool USBJoystick::hat(uint8_t hat) { |
| rvt | 0:33bc88c4ab31 | 87 | _hat = hat; |
| rvt | 0:33bc88c4ab31 | 88 | return update(); |
| rvt | 0:33bc88c4ab31 | 89 | } |
| rvt | 0:33bc88c4ab31 | 90 | |
| rvt | 0:33bc88c4ab31 | 91 | |
| rvt | 0:33bc88c4ab31 | 92 | void USBJoystick::_init() { |
| rvt | 0:33bc88c4ab31 | 93 | |
| rvt | 0:33bc88c4ab31 | 94 | _t = 0; |
| rvt | 0:33bc88c4ab31 | 95 | _r = 0; |
| rvt | 0:33bc88c4ab31 | 96 | _x = 0; |
| rvt | 0:33bc88c4ab31 | 97 | _y = 0; |
| rvt | 0:33bc88c4ab31 | 98 | _button = 0x00; |
| rvt | 0:33bc88c4ab31 | 99 | _hat = 0x00; |
| rvt | 0:33bc88c4ab31 | 100 | } |
| rvt | 0:33bc88c4ab31 | 101 | |
| rvt | 0:33bc88c4ab31 | 102 | |
| rvt | 0:33bc88c4ab31 | 103 | uint8_t * USBJoystick::reportDesc() { |
| rvt | 0:33bc88c4ab31 | 104 | static uint8_t reportDescriptor[] = { |
| rvt | 0:33bc88c4ab31 | 105 | |
| rvt | 0:33bc88c4ab31 | 106 | USAGE_PAGE(1), 0x01, // Generic Desktop |
| rvt | 0:33bc88c4ab31 | 107 | LOGICAL_MINIMUM(1), 0x00, // Logical_Minimum (0) |
| rvt | 0:33bc88c4ab31 | 108 | USAGE(1), 0x04, // Usage (Joystick) |
| rvt | 0:33bc88c4ab31 | 109 | COLLECTION(1), 0x01, // Application |
| rvt | 0:33bc88c4ab31 | 110 | |
| rvt | 0:33bc88c4ab31 | 111 | USAGE_PAGE(1), 0x02, // Simulation Controls |
| rvt | 0:33bc88c4ab31 | 112 | USAGE(1), 0xBB, // Throttle |
| rvt | 0:33bc88c4ab31 | 113 | USAGE(1), 0xBA, // Rudder |
| rvt | 0:33bc88c4ab31 | 114 | LOGICAL_MINIMUM(2), 0x01, 0x80, // -32767 |
| rvt | 0:33bc88c4ab31 | 115 | LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 |
| rvt | 0:33bc88c4ab31 | 116 | REPORT_SIZE(1), 0x10, |
| rvt | 0:33bc88c4ab31 | 117 | REPORT_COUNT(1), 0x02, |
| rvt | 0:33bc88c4ab31 | 118 | INPUT(1), 0x02, // Data, Variable, Absolute |
| rvt | 0:33bc88c4ab31 | 119 | |
| rvt | 0:33bc88c4ab31 | 120 | USAGE_PAGE(1), 0x01, // Generic Desktop |
| rvt | 0:33bc88c4ab31 | 121 | USAGE(1), 0x01, // Usage (Pointer) |
| rvt | 0:33bc88c4ab31 | 122 | COLLECTION(1), 0x00, // Physical |
| rvt | 0:33bc88c4ab31 | 123 | USAGE(1), 0x30, // X |
| rvt | 0:33bc88c4ab31 | 124 | USAGE(1), 0x31, // Y |
| rvt | 0:33bc88c4ab31 | 125 | // 8 bit values |
| rvt | 0:33bc88c4ab31 | 126 | // LOGICAL_MINIMUM(1), 0x81, // -127 |
| rvt | 0:33bc88c4ab31 | 127 | // LOGICAL_MAXIMUM(1), 0x7f, // 127 |
| rvt | 0:33bc88c4ab31 | 128 | // REPORT_SIZE(1), 0x08, |
| rvt | 0:33bc88c4ab31 | 129 | // REPORT_COUNT(1), 0x02, |
| rvt | 0:33bc88c4ab31 | 130 | // INPUT(1), 0x02, // Data, Variable, Absolute |
| rvt | 0:33bc88c4ab31 | 131 | // 16 bit values |
| rvt | 0:33bc88c4ab31 | 132 | LOGICAL_MINIMUM(2), 0x01, 0x80, // -32767 |
| rvt | 0:33bc88c4ab31 | 133 | LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 |
| rvt | 0:33bc88c4ab31 | 134 | REPORT_SIZE(1), 0x10, |
| rvt | 0:33bc88c4ab31 | 135 | REPORT_COUNT(1), 0x02, |
| rvt | 0:33bc88c4ab31 | 136 | INPUT(1), 0x02, // Data, Variable, Absolute |
| rvt | 0:33bc88c4ab31 | 137 | |
| rvt | 0:33bc88c4ab31 | 138 | |
| rvt | 0:33bc88c4ab31 | 139 | END_COLLECTION(0), |
| rvt | 0:33bc88c4ab31 | 140 | // 4 Position Hat Switch |
| rvt | 0:33bc88c4ab31 | 141 | // USAGE(1), 0x39, // Usage (Hat switch) |
| rvt | 0:33bc88c4ab31 | 142 | // LOGICAL_MINIMUM(1), 0x00, // 0 |
| rvt | 0:33bc88c4ab31 | 143 | // LOGICAL_MAXIMUM(1), 0x03, // 3 |
| rvt | 0:33bc88c4ab31 | 144 | // PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0) |
| rvt | 0:33bc88c4ab31 | 145 | // PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270) |
| rvt | 0:33bc88c4ab31 | 146 | // UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos) |
| rvt | 0:33bc88c4ab31 | 147 | // REPORT_SIZE(1), 0x04, |
| rvt | 0:33bc88c4ab31 | 148 | // REPORT_COUNT(1), 0x01, |
| rvt | 0:33bc88c4ab31 | 149 | // INPUT(1), 0x02, // Data, Variable, Absolute |
| rvt | 0:33bc88c4ab31 | 150 | // 8 Position Hat Switch |
| rvt | 0:33bc88c4ab31 | 151 | USAGE(1), 0x39, // Usage (Hat switch) |
| rvt | 0:33bc88c4ab31 | 152 | LOGICAL_MINIMUM(1), 0x00, // 0 |
| rvt | 0:33bc88c4ab31 | 153 | LOGICAL_MAXIMUM(1), 0x07, // 7 |
| rvt | 0:33bc88c4ab31 | 154 | PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0) |
| rvt | 0:33bc88c4ab31 | 155 | PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315) |
| rvt | 0:33bc88c4ab31 | 156 | UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos) |
| rvt | 0:33bc88c4ab31 | 157 | REPORT_SIZE(1), 0x04, |
| rvt | 0:33bc88c4ab31 | 158 | REPORT_COUNT(1), 0x01, |
| rvt | 0:33bc88c4ab31 | 159 | INPUT(1), 0x02, // Data, Variable, Absolute |
| rvt | 0:33bc88c4ab31 | 160 | // |
| rvt | 0:33bc88c4ab31 | 161 | USAGE_PAGE(1), 0x09, // Buttons |
| rvt | 0:33bc88c4ab31 | 162 | USAGE_MINIMUM(1), 0x01, // 1 |
| rvt | 0:33bc88c4ab31 | 163 | USAGE_MAXIMUM(1), 0x04, // 4 |
| rvt | 0:33bc88c4ab31 | 164 | LOGICAL_MINIMUM(1), 0x00, // 0 |
| rvt | 0:33bc88c4ab31 | 165 | LOGICAL_MAXIMUM(1), 0x01, // 1 |
| rvt | 0:33bc88c4ab31 | 166 | REPORT_SIZE(1), 0x01, |
| rvt | 0:33bc88c4ab31 | 167 | REPORT_COUNT(1), 0x04, |
| rvt | 0:33bc88c4ab31 | 168 | UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0) |
| rvt | 0:33bc88c4ab31 | 169 | UNIT(1), 0x00, // Unit (None) |
| rvt | 0:33bc88c4ab31 | 170 | INPUT(1), 0x02, // Data, Variable, Absolute |
| rvt | 0:33bc88c4ab31 | 171 | END_COLLECTION(0) |
| rvt | 0:33bc88c4ab31 | 172 | |
| rvt | 0:33bc88c4ab31 | 173 | }; |
| rvt | 0:33bc88c4ab31 | 174 | |
| rvt | 0:33bc88c4ab31 | 175 | reportLength = sizeof(reportDescriptor); |
| rvt | 0:33bc88c4ab31 | 176 | return reportDescriptor; |
| rvt | 0:33bc88c4ab31 | 177 | } |
| rvt | 0:33bc88c4ab31 | 178 | |
| rvt | 0:33bc88c4ab31 | 179 |
