Mamecontroller/joystick device wrapper library

Dependencies:   USBDevice mbed

Committer:
uswickra
Date:
Wed Dec 10 05:51:13 2014 +0000
Revision:
4:4f6e38b6c07e
my controller;

Who changed what in which revision?

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