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