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