USB device stack
Fork of USBDevice by
USBJoystick.cpp@65:e64cf9f9c595, 2016-09-16 (annotated)
- Committer:
- nikitamere
- Date:
- Fri Sep 16 01:03:45 2016 +0000
- Revision:
- 65:e64cf9f9c595
- Parent:
- 64:e01add7a9b25
Modified;
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 | 65:e64cf9f9c595 | 23 | bool USBJoystick::update(int16_t x, int16_t y, int16_t z, int16_t rx, uint16_t ry, uint16_t rz, uint8_t button) { |
nikitamere | 64:e01add7a9b25 | 24 | HID_REPORT report; |
nikitamere | 65:e64cf9f9c595 | 25 | _x = x; |
nikitamere | 64:e01add7a9b25 | 26 | _y = y; |
nikitamere | 65:e64cf9f9c595 | 27 | _z = z; |
nikitamere | 65:e64cf9f9c595 | 28 | _rx = rx; |
nikitamere | 65:e64cf9f9c595 | 29 | _ry = ry; |
nikitamere | 65:e64cf9f9c595 | 30 | _rz = rz; |
nikitamere | 64:e01add7a9b25 | 31 | _button = button; |
nikitamere | 64:e01add7a9b25 | 32 | |
nikitamere | 64:e01add7a9b25 | 33 | // Fill the report according to the Joystick Descriptor |
nikitamere | 65:e64cf9f9c595 | 34 | report.data[0] = _x & 0xff; |
nikitamere | 65:e64cf9f9c595 | 35 | report.data[1] = _y & 0xff; |
nikitamere | 65:e64cf9f9c595 | 36 | report.data[2] = _z & 0xff; |
nikitamere | 65:e64cf9f9c595 | 37 | report.data[3] = _rx & 0xff; |
nikitamere | 65:e64cf9f9c595 | 38 | report.data[4] = _ry & 0xff; |
nikitamere | 65:e64cf9f9c595 | 39 | report.data[5] = _rz & 0xff; |
nikitamere | 65:e64cf9f9c595 | 40 | report.data[6] = _button & 0xff; |
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 | 65:e64cf9f9c595 | 50 | report.data[0] = _x & 0xff; |
nikitamere | 65:e64cf9f9c595 | 51 | report.data[1] = _y & 0xff; |
nikitamere | 65:e64cf9f9c595 | 52 | report.data[2] = _z & 0xff; |
nikitamere | 65:e64cf9f9c595 | 53 | report.data[3] = _rx & 0xff; |
nikitamere | 65:e64cf9f9c595 | 54 | report.data[4] = _ry & 0xff; |
nikitamere | 65:e64cf9f9c595 | 55 | report.data[5] = _rz & 0xff; |
nikitamere | 65:e64cf9f9c595 | 56 | report.data[6] = _button & 0xff; |
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 | 65:e64cf9f9c595 | 63 | |
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 | 65:e64cf9f9c595 | 68 | |
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 | |
nikitamere | 64:e01add7a9b25 | 84 | void USBJoystick::_init() { |
nikitamere | 64:e01add7a9b25 | 85 | |
nikitamere | 64:e01add7a9b25 | 86 | _x = 0; |
nikitamere | 65:e64cf9f9c595 | 87 | _y = 0; |
nikitamere | 65:e64cf9f9c595 | 88 | _z = 0; |
nikitamere | 65:e64cf9f9c595 | 89 | _rx = 0; |
nikitamere | 65:e64cf9f9c595 | 90 | _ry = 0; |
nikitamere | 65:e64cf9f9c595 | 91 | _rz = 0; |
nikitamere | 65:e64cf9f9c595 | 92 | |
nikitamere | 64:e01add7a9b25 | 93 | _button = 0x00; |
nikitamere | 65:e64cf9f9c595 | 94 | |
nikitamere | 64:e01add7a9b25 | 95 | } |
nikitamere | 64:e01add7a9b25 | 96 | |
nikitamere | 64:e01add7a9b25 | 97 | |
nikitamere | 64:e01add7a9b25 | 98 | uint8_t * USBJoystick::reportDesc() { |
nikitamere | 64:e01add7a9b25 | 99 | static uint8_t reportDescriptor[] = { |
nikitamere | 64:e01add7a9b25 | 100 | |
nikitamere | 64:e01add7a9b25 | 101 | USAGE_PAGE(1), 0x01, // Generic Desktop |
nikitamere | 64:e01add7a9b25 | 102 | LOGICAL_MINIMUM(1), 0x00, // Logical_Minimum (0) |
nikitamere | 64:e01add7a9b25 | 103 | USAGE(1), 0x04, // Usage (Joystick) |
nikitamere | 64:e01add7a9b25 | 104 | COLLECTION(1), 0x01, // Application |
nikitamere | 65:e64cf9f9c595 | 105 | // Data, Variable, Absolute |
nikitamere | 64:e01add7a9b25 | 106 | USAGE_PAGE(1), 0x01, // Generic Desktop |
nikitamere | 64:e01add7a9b25 | 107 | USAGE(1), 0x01, // Usage (Pointer) |
nikitamere | 64:e01add7a9b25 | 108 | COLLECTION(1), 0x00, // Physical |
nikitamere | 65:e64cf9f9c595 | 109 | //// 3 Translations and 3 Rotations |
nikitamere | 64:e01add7a9b25 | 110 | USAGE(1), 0x30, // X |
nikitamere | 64:e01add7a9b25 | 111 | USAGE(1), 0x31, // Y |
nikitamere | 65:e64cf9f9c595 | 112 | USAGE(1), 0x32, // Z |
nikitamere | 65:e64cf9f9c595 | 113 | USAGE(1), 0x33, //Rx |
nikitamere | 65:e64cf9f9c595 | 114 | USAGE(1), 0x34, //Ry |
nikitamere | 65:e64cf9f9c595 | 115 | USAGE(1), 0x35, //Rz |
nikitamere | 65:e64cf9f9c595 | 116 | |
nikitamere | 65:e64cf9f9c595 | 117 | //// 8 bit values |
nikitamere | 64:e01add7a9b25 | 118 | LOGICAL_MINIMUM(1), 0x81, // -127 |
nikitamere | 64:e01add7a9b25 | 119 | LOGICAL_MAXIMUM(1), 0x7f, // 127 |
nikitamere | 64:e01add7a9b25 | 120 | REPORT_SIZE(1), 0x08, |
nikitamere | 65:e64cf9f9c595 | 121 | REPORT_COUNT(1), 0x06, |
nikitamere | 64:e01add7a9b25 | 122 | INPUT(1), 0x02, // Data, Variable, Absolute |
nikitamere | 64:e01add7a9b25 | 123 | // 16 bit values |
nikitamere | 64:e01add7a9b25 | 124 | // LOGICAL_MINIMUM(1), 0x00, // 0 |
nikitamere | 64:e01add7a9b25 | 125 | // LOGICAL_MAXIMUM(2), 0xff, 0x7f, // 32767 |
nikitamere | 64:e01add7a9b25 | 126 | // REPORT_SIZE(1), 0x10, |
nikitamere | 65:e64cf9f9c595 | 127 | // REPORT_COUNT(1), 0x06, |
nikitamere | 64:e01add7a9b25 | 128 | // INPUT(1), 0x02, // Data, Variable, Absolute |
nikitamere | 64:e01add7a9b25 | 129 | |
nikitamere | 64:e01add7a9b25 | 130 | END_COLLECTION(0), |
nikitamere | 65:e64cf9f9c595 | 131 | |
nikitamere | 65:e64cf9f9c595 | 132 | // 8 Buttons |
nikitamere | 64:e01add7a9b25 | 133 | USAGE_PAGE(1), 0x09, // Buttons |
nikitamere | 64:e01add7a9b25 | 134 | USAGE_MINIMUM(1), 0x01, // 1 |
nikitamere | 65:e64cf9f9c595 | 135 | USAGE_MAXIMUM(1), 0x08, // 8 |
nikitamere | 64:e01add7a9b25 | 136 | LOGICAL_MINIMUM(1), 0x00, // 0 |
nikitamere | 64:e01add7a9b25 | 137 | LOGICAL_MAXIMUM(1), 0x01, // 1 |
nikitamere | 64:e01add7a9b25 | 138 | REPORT_SIZE(1), 0x01, |
nikitamere | 65:e64cf9f9c595 | 139 | REPORT_COUNT(1), 0x08, |
nikitamere | 64:e01add7a9b25 | 140 | UNIT_EXPONENT(1), 0x00, // Unit_Exponent (0) |
nikitamere | 64:e01add7a9b25 | 141 | UNIT(1), 0x00, // Unit (None) |
nikitamere | 64:e01add7a9b25 | 142 | INPUT(1), 0x02, // Data, Variable, Absolute |
nikitamere | 64:e01add7a9b25 | 143 | |
nikitamere | 64:e01add7a9b25 | 144 | END_COLLECTION(0) |
nikitamere | 64:e01add7a9b25 | 145 | |
nikitamere | 64:e01add7a9b25 | 146 | }; |
nikitamere | 64:e01add7a9b25 | 147 | |
nikitamere | 64:e01add7a9b25 | 148 | reportLength = sizeof(reportDescriptor); |
nikitamere | 64:e01add7a9b25 | 149 | return reportDescriptor; |
nikitamere | 64:e01add7a9b25 | 150 | } |
nikitamere | 64:e01add7a9b25 | 151 | |
nikitamere | 64:e01add7a9b25 | 152 |