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@4:00ac3d093b24, 2016-09-26 (annotated)
- Committer:
- ikuta
- Date:
- Mon Sep 26 14:01:47 2016 +0000
- Revision:
- 4:00ac3d093b24
- Child:
- 8:59dd4860477f
????
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 | 4:00ac3d093b24 | 23 | bool USBJoystick::update(int16_t t, int16_t r, int16_t x, int16_t y, uint8_t button, uint8_t hat) { |
ikuta | 4:00ac3d093b24 | 24 | HID_REPORT report; |
ikuta | 4:00ac3d093b24 | 25 | _t = t; |
ikuta | 4:00ac3d093b24 | 26 | _r = r; |
ikuta | 4:00ac3d093b24 | 27 | _x = x; |
ikuta | 4:00ac3d093b24 | 28 | _y = y; |
ikuta | 4:00ac3d093b24 | 29 | _button = button; |
ikuta | 4:00ac3d093b24 | 30 | _hat = hat; |
ikuta | 4:00ac3d093b24 | 31 | |
ikuta | 4:00ac3d093b24 | 32 | // Fill the report according to the Joystick Descriptor |
ikuta | 4:00ac3d093b24 | 33 | report.data[0] = _t & 0xff; |
ikuta | 4:00ac3d093b24 | 34 | report.data[1] = _r & 0xff; |
ikuta | 4:00ac3d093b24 | 35 | report.data[2] = _x & 0xff; |
ikuta | 4:00ac3d093b24 | 36 | report.data[3] = _y & 0xff; |
ikuta | 4:00ac3d093b24 | 37 | report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; |
ikuta | 4:00ac3d093b24 | 38 | report.length = 5; |
ikuta | 4:00ac3d093b24 | 39 | |
ikuta | 4:00ac3d093b24 | 40 | return send(&report); |
ikuta | 4:00ac3d093b24 | 41 | } |
ikuta | 4:00ac3d093b24 | 42 | |
ikuta | 4:00ac3d093b24 | 43 | bool USBJoystick::update() { |
ikuta | 4:00ac3d093b24 | 44 | HID_REPORT report; |
ikuta | 4:00ac3d093b24 | 45 | |
ikuta | 4:00ac3d093b24 | 46 | // Fill the report according to the Joystick Descriptor |
ikuta | 4:00ac3d093b24 | 47 | report.data[0] = _t & 0xff; |
ikuta | 4:00ac3d093b24 | 48 | report.data[1] = _r & 0xff; |
ikuta | 4:00ac3d093b24 | 49 | report.data[2] = _x & 0xff; |
ikuta | 4:00ac3d093b24 | 50 | report.data[3] = _y & 0xff; |
ikuta | 4:00ac3d093b24 | 51 | report.data[4] = ((_button & 0x0f) << 4) | (_hat & 0x0f) ; |
ikuta | 4:00ac3d093b24 | 52 | report.length = 5; |
ikuta | 4:00ac3d093b24 | 53 | |
ikuta | 4:00ac3d093b24 | 54 | return send(&report); |
ikuta | 4:00ac3d093b24 | 55 | } |
ikuta | 4:00ac3d093b24 | 56 | |
ikuta | 4:00ac3d093b24 | 57 | bool USBJoystick::throttle(int16_t t) { |
ikuta | 4:00ac3d093b24 | 58 | _t = t; |
ikuta | 4:00ac3d093b24 | 59 | return update(); |
ikuta | 4:00ac3d093b24 | 60 | } |
ikuta | 4:00ac3d093b24 | 61 | |
ikuta | 4:00ac3d093b24 | 62 | bool USBJoystick::rudder(int16_t r) { |
ikuta | 4:00ac3d093b24 | 63 | _r = r; |
ikuta | 4:00ac3d093b24 | 64 | return update(); |
ikuta | 4:00ac3d093b24 | 65 | } |
ikuta | 4:00ac3d093b24 | 66 | |
ikuta | 4:00ac3d093b24 | 67 | bool USBJoystick::move(int16_t x, int16_t y) { |
ikuta | 4:00ac3d093b24 | 68 | _x = x; |
ikuta | 4:00ac3d093b24 | 69 | _y = y; |
ikuta | 4:00ac3d093b24 | 70 | return update(); |
ikuta | 4:00ac3d093b24 | 71 | } |
ikuta | 4:00ac3d093b24 | 72 | |
ikuta | 4:00ac3d093b24 | 73 | bool USBJoystick::button(uint8_t button) { |
ikuta | 4:00ac3d093b24 | 74 | _button = button; |
ikuta | 4:00ac3d093b24 | 75 | return update(); |
ikuta | 4:00ac3d093b24 | 76 | } |
ikuta | 4:00ac3d093b24 | 77 | |
ikuta | 4:00ac3d093b24 | 78 | bool USBJoystick::hat(uint8_t hat) { |
ikuta | 4:00ac3d093b24 | 79 | _hat = hat; |
ikuta | 4:00ac3d093b24 | 80 | return update(); |
ikuta | 4:00ac3d093b24 | 81 | } |
ikuta | 4:00ac3d093b24 | 82 | |
ikuta | 4:00ac3d093b24 | 83 | |
ikuta | 4:00ac3d093b24 | 84 | void USBJoystick::_init() { |
ikuta | 4:00ac3d093b24 | 85 | |
ikuta | 4:00ac3d093b24 | 86 | _t = -127; |
ikuta | 4:00ac3d093b24 | 87 | _r = -127; |
ikuta | 4:00ac3d093b24 | 88 | _x = 0; |
ikuta | 4:00ac3d093b24 | 89 | _y = 0; |
ikuta | 4:00ac3d093b24 | 90 | _button = 0x00; |
ikuta | 4:00ac3d093b24 | 91 | _hat = 0x00; |
ikuta | 4:00ac3d093b24 | 92 | } |
ikuta | 4:00ac3d093b24 | 93 | |
ikuta | 4:00ac3d093b24 | 94 | |
ikuta | 4:00ac3d093b24 | 95 | uint8_t * USBJoystick::reportDesc() { |
ikuta | 4:00ac3d093b24 | 96 | static uint8_t reportDescriptor[] = { |
ikuta | 4:00ac3d093b24 | 97 | |
ikuta | 4:00ac3d093b24 | 98 | USAGE_PAGE(1), 0x01, // Generic Desktop |
ikuta | 4:00ac3d093b24 | 99 | LOGICAL_MINIMUM(1), 0x00, // Logical_Minimum (0) |
ikuta | 4:00ac3d093b24 | 100 | USAGE(1), 0x04, // Usage (Joystick) |
ikuta | 4:00ac3d093b24 | 101 | COLLECTION(1), 0x01, // Application |
ikuta | 4:00ac3d093b24 | 102 | USAGE_PAGE(1), 0x02, // Simulation Controls |
ikuta | 4:00ac3d093b24 | 103 | USAGE(1), 0xBB, // Throttle |
ikuta | 4:00ac3d093b24 | 104 | USAGE(1), 0xBA, // Rudder |
ikuta | 4:00ac3d093b24 | 105 | LOGICAL_MINIMUM(1), 0x81, // -127 |
ikuta | 4:00ac3d093b24 | 106 | LOGICAL_MAXIMUM(1), 0x7f, // 127 |
ikuta | 4:00ac3d093b24 | 107 | REPORT_SIZE(1), 0x08, |
ikuta | 4:00ac3d093b24 | 108 | REPORT_COUNT(1), 0x02, |
ikuta | 4:00ac3d093b24 | 109 | INPUT(1), 0x02, // Data, Variable, Absolute |
ikuta | 4:00ac3d093b24 | 110 | USAGE_PAGE(1), 0x01, // Generic Desktop |
ikuta | 4:00ac3d093b24 | 111 | USAGE(1), 0x01, // Usage (Pointer) |
ikuta | 4:00ac3d093b24 | 112 | COLLECTION(1), 0x00, // Physical |
ikuta | 4:00ac3d093b24 | 113 | USAGE(1), 0x30, // X |
ikuta | 4:00ac3d093b24 | 114 | USAGE(1), 0x31, // Y |
ikuta | 4:00ac3d093b24 | 115 | // 8 bit values |
ikuta | 4:00ac3d093b24 | 116 | LOGICAL_MINIMUM(1), 0x81, // -127 |
ikuta | 4:00ac3d093b24 | 117 | LOGICAL_MAXIMUM(1), 0x7f, // 127 |
ikuta | 4:00ac3d093b24 | 118 | REPORT_SIZE(1), 0x08, |
ikuta | 4:00ac3d093b24 | 119 | REPORT_COUNT(1), 0x02, |
ikuta | 4:00ac3d093b24 | 120 | INPUT(1), 0x02, // Data, Variable, Absolute |
ikuta | 4:00ac3d093b24 | 121 | // 16 bit values |
ikuta | 4:00ac3d093b24 | 122 | // LOGICAL_MINIMUM(1), 0x00, // 0 |
ikuta | 4:00ac3d093b24 | 123 | // LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 |
ikuta | 4:00ac3d093b24 | 124 | // REPORT_SIZE(1), 0x10, |
ikuta | 4:00ac3d093b24 | 125 | // REPORT_COUNT(1), 0x02, |
ikuta | 4:00ac3d093b24 | 126 | // INPUT(1), 0x02, // Data, Variable, Absolute |
ikuta | 4:00ac3d093b24 | 127 | |
ikuta | 4:00ac3d093b24 | 128 | END_COLLECTION(0), |
ikuta | 4:00ac3d093b24 | 129 | // 4 Position Hat Switch |
ikuta | 4:00ac3d093b24 | 130 | // USAGE(1), 0x39, // Usage (Hat switch) |
ikuta | 4:00ac3d093b24 | 131 | // LOGICAL_MINIMUM(1), 0x00, // 0 |
ikuta | 4:00ac3d093b24 | 132 | // LOGICAL_MAXIMUM(1), 0x03, // 3 |
ikuta | 4:00ac3d093b24 | 133 | // PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0) |
ikuta | 4:00ac3d093b24 | 134 | // PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270) |
ikuta | 4:00ac3d093b24 | 135 | // UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos) |
ikuta | 4:00ac3d093b24 | 136 | // REPORT_SIZE(1), 0x04, |
ikuta | 4:00ac3d093b24 | 137 | // REPORT_COUNT(1), 0x01, |
ikuta | 4:00ac3d093b24 | 138 | // INPUT(1), 0x02, // Data, Variable, Absolute |
ikuta | 4:00ac3d093b24 | 139 | // 8 Position Hat Switch |
ikuta | 4:00ac3d093b24 | 140 | USAGE(1), 0x39, // Usage (Hat switch) |
ikuta | 4:00ac3d093b24 | 141 | LOGICAL_MINIMUM(1), 0x00, // 0 |
ikuta | 4:00ac3d093b24 | 142 | LOGICAL_MAXIMUM(1), 0x07, // 7 |
ikuta | 4:00ac3d093b24 | 143 | PHYSICAL_MINIMUM(1), 0x00, // Physical_Minimum (0) |
ikuta | 4:00ac3d093b24 | 144 | PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315) |
ikuta | 4:00ac3d093b24 | 145 | UNIT(1), 0x14, // Unit (Eng Rot:Angular Pos) |
ikuta | 4:00ac3d093b24 | 146 | REPORT_SIZE(1), 0x04, |
ikuta | 4:00ac3d093b24 | 147 | REPORT_COUNT(1), 0x01, |
ikuta | 4:00ac3d093b24 | 148 | INPUT(1), 0x02, // Data, Variable, Absolute |
ikuta | 4:00ac3d093b24 | 149 | // |
ikuta | 4:00ac3d093b24 | 150 | USAGE_PAGE(1), 0x09, // Buttons |
ikuta | 4:00ac3d093b24 | 151 | USAGE_MINIMUM(1), 0x01, // 1 |
ikuta | 4:00ac3d093b24 | 152 | USAGE_MAXIMUM(1), 0x04, // 4 |
ikuta | 4:00ac3d093b24 | 153 | LOGICAL_MINIMUM(1), 0x00, // 0 |
ikuta | 4:00ac3d093b24 | 154 | LOGICAL_MAXIMUM(1), 0x01, // 1 |
ikuta | 4:00ac3d093b24 | 155 | REPORT_SIZE(1), 0x01, |
ikuta | 4:00ac3d093b24 | 156 | REPORT_COUNT(1), 0x04, |
ikuta | 4:00ac3d093b24 | 157 | UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0) |
ikuta | 4:00ac3d093b24 | 158 | UNIT(1), 0x00, // Unit (None) |
ikuta | 4:00ac3d093b24 | 159 | INPUT(1), 0x02, // Data, Variable, Absolute |
ikuta | 4:00ac3d093b24 | 160 | END_COLLECTION(0) |
ikuta | 4:00ac3d093b24 | 161 | |
ikuta | 4:00ac3d093b24 | 162 | }; |
ikuta | 4:00ac3d093b24 | 163 | |
ikuta | 4:00ac3d093b24 | 164 | reportLength = sizeof(reportDescriptor); |
ikuta | 4:00ac3d093b24 | 165 | return reportDescriptor; |
ikuta | 4:00ac3d093b24 | 166 | } |
ikuta | 4:00ac3d093b24 | 167 | |
ikuta | 4:00ac3d093b24 | 168 |