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