USB Joystick library, modified to work as a 6 DOF joystick.

Dependents:   USBJoystick_Test

Fork of USBJoystick by Wim Huiskamp

Committer:
smartsystemdesign
Date:
Fri Jan 13 17:11:32 2017 +0000
Revision:
4:0ffbedd2cb73
Parent:
3:f8be03f31e6c
Fixed the joystick axes' LOGICAL_MINIMUM to be -32768, according to the USB HID Descriptor Tool.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wim 0:e086541742c3 1 /* mbed USBJoystick Library
wim 0:e086541742c3 2 * Copyright (c) 2012, v01: Initial version, WH,
wim 0:e086541742c3 3 * Modified USBMouse code ARM Limited.
wim 0:e086541742c3 4 * (c) 2010-2011 mbed.org, MIT License
wim 0:e086541742c3 5 * 2016, v02: Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button
wim 0:e086541742c3 6 *
wim 0:e086541742c3 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
wim 0:e086541742c3 8 * of this software and associated documentation files (the "Software"), to deal
wim 0:e086541742c3 9 * in the Software without restriction, inclumosig without limitation the rights
wim 0:e086541742c3 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wim 0:e086541742c3 11 * copies of the Software, and to permit persons to whom the Software is
wim 0:e086541742c3 12 * furnished to do so, subject to the following conditions:
wim 0:e086541742c3 13 *
wim 0:e086541742c3 14 * The above copyright notice and this permission notice shall be included in
wim 0:e086541742c3 15 * all copies or substantial portions of the Software.
wim 0:e086541742c3 16 *
wim 0:e086541742c3 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wim 0:e086541742c3 18 * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wim 0:e086541742c3 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wim 0:e086541742c3 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wim 0:e086541742c3 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wim 0:e086541742c3 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wim 0:e086541742c3 23 * THE SOFTWARE.
wim 0:e086541742c3 24 */
wim 0:e086541742c3 25
wim 0:e086541742c3 26 #ifndef USBJOYSTICK_H
wim 0:e086541742c3 27 #define USBJOYSTICK_H
wim 0:e086541742c3 28
wim 0:e086541742c3 29 #include "USBHID.h"
wim 0:e086541742c3 30
wim 0:e086541742c3 31 #define REPORT_ID_JOYSTICK 4
wim 0:e086541742c3 32
wim 0:e086541742c3 33 //Configure Joystick
wim 0:e086541742c3 34 #define HAT4 0
wim 0:e086541742c3 35 #define HAT8 1
wim 0:e086541742c3 36
wim 0:e086541742c3 37 #define BUTTONS4 0
wim 0:e086541742c3 38 #define BUTTONS8 0
wim 0:e086541742c3 39 #define BUTTONS32 1
wim 0:e086541742c3 40
wim 0:e086541742c3 41
wim 0:e086541742c3 42 /* Common usage */
wim 0:e086541742c3 43 enum JOY_BUTTON {
wim 0:e086541742c3 44 JOY_B0 = 1,
wim 0:e086541742c3 45 JOY_B1 = 2,
wim 0:e086541742c3 46 JOY_B2 = 4,
smartsystemdesign 1:8b5f213b169f 47 JOY_B3 = 8,
wim 0:e086541742c3 48 };
wim 0:e086541742c3 49
wim 0:e086541742c3 50 #if (HAT4 == 1)
wim 0:e086541742c3 51 enum JOY_HAT {
wim 0:e086541742c3 52 JOY_HAT_UP = 0,
wim 0:e086541742c3 53 JOY_HAT_RIGHT = 1,
wim 0:e086541742c3 54 JOY_HAT_DOWN = 2,
wim 0:e086541742c3 55 JOY_HAT_LEFT = 3,
smartsystemdesign 1:8b5f213b169f 56 JOY_HAT_NEUTRAL = 4,
wim 0:e086541742c3 57 };
wim 0:e086541742c3 58 #endif
wim 0:e086541742c3 59 #if (HAT8 == 1)
wim 0:e086541742c3 60 enum JOY_HAT {
wim 0:e086541742c3 61 JOY_HAT_UP = 0,
wim 0:e086541742c3 62 JOY_HAT_UP_RIGHT = 1,
wim 0:e086541742c3 63 JOY_HAT_RIGHT = 2,
wim 0:e086541742c3 64 JOY_HAT_RIGHT_DOWN = 3,
wim 0:e086541742c3 65 JOY_HAT_DOWN = 4,
smartsystemdesign 1:8b5f213b169f 66 JOY_HAT_DOWN_LEFT = 5,
smartsystemdesign 1:8b5f213b169f 67 JOY_HAT_LEFT = 6,
smartsystemdesign 1:8b5f213b169f 68 JOY_HAT_LEFT_UP = 7,
smartsystemdesign 1:8b5f213b169f 69 JOY_HAT_NEUTRAL = 8,
wim 0:e086541742c3 70 };
wim 0:e086541742c3 71 #endif
wim 0:e086541742c3 72
wim 0:e086541742c3 73 /* X, Y and T limits */
wim 0:e086541742c3 74 /* These values do not directly map to screen pixels */
wim 0:e086541742c3 75 /* Zero may be interpreted as meaning 'no movement' */
smartsystemdesign 1:8b5f213b169f 76 #define JX_MIN_ABS (-32768) /*!< The maximum value that we can move to the left on the x-axis */
smartsystemdesign 1:8b5f213b169f 77 #define JY_MIN_ABS (-32768) /*!< The maximum value that we can move up on the y-axis */
smartsystemdesign 1:8b5f213b169f 78 #define JT_MIN_ABS (-32768) /*!< The minimum value for the throttle */
smartsystemdesign 1:8b5f213b169f 79 #define JX_MAX_ABS (32767) /*!< The maximum value that we can move to the right on the x-axis */
smartsystemdesign 1:8b5f213b169f 80 #define JY_MAX_ABS (32767) /*!< The maximum value that we can move down on the y-axis */
smartsystemdesign 1:8b5f213b169f 81 #define JT_MAX_ABS (32767) /*!< The maximum value for the throttle */
wim 0:e086541742c3 82
wim 0:e086541742c3 83 class USBJoystick: public USBHID {
wim 0:e086541742c3 84 public:
wim 0:e086541742c3 85
wim 0:e086541742c3 86 /**
wim 0:e086541742c3 87 * Constructor
wim 0:e086541742c3 88 *
wim 0:e086541742c3 89 * @param vendor_id Your vendor_id (default: 0x1234)
wim 0:e086541742c3 90 * @param product_id Your product_id (default: 0x0002)
wim 0:e086541742c3 91 * @param product_release Your product_release (default: 0x0001)
wim 0:e086541742c3 92 */
wim 0:e086541742c3 93 // USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001, int waitForConnect = true): // 4 buttons, no padding on buttons
wim 0:e086541742c3 94 // USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0500, uint16_t product_release = 0x0001, int waitForConnect = true): // 8 buttons, no padding on buttons
smartsystemdesign 4:0ffbedd2cb73 95 USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0702, uint16_t product_release = 0x0001, int waitForConnect = true): // 32 buttons, no padding on buttons
wim 0:e086541742c3 96 USBHID(0, 0, vendor_id, product_id, product_release, false) {
wim 0:e086541742c3 97 _init();
wim 0:e086541742c3 98 connect(waitForConnect);
wim 0:e086541742c3 99 };
wim 0:e086541742c3 100
wim 0:e086541742c3 101 /**
wim 0:e086541742c3 102 * Write state of the joystick
wim 0:e086541742c3 103 *
wim 0:e086541742c3 104 * @param t throttle position
wim 0:e086541742c3 105 * @param r rudder position
wim 0:e086541742c3 106 * @param x x-axis position
wim 0:e086541742c3 107 * @param y y-axis position
wim 0:e086541742c3 108 * @param buttons buttons state
wim 0:e086541742c3 109 * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral)
wim 0:e086541742c3 110 * @returns true if there is no error, false otherwise
wim 0:e086541742c3 111 */
smartsystemdesign 2:1549541d3b4b 112 bool update(int16_t x, int16_t y, int16_t z, int16_t rx, int16_t ry, int16_t rz, uint32_t buttons, uint8_t hat);
wim 0:e086541742c3 113
wim 0:e086541742c3 114 /**
wim 0:e086541742c3 115 * Write state of the joystick
wim 0:e086541742c3 116 *
wim 0:e086541742c3 117 * @returns true if there is no error, false otherwise
wim 0:e086541742c3 118 */
smartsystemdesign 2:1549541d3b4b 119 bool update();
wim 0:e086541742c3 120
wim 0:e086541742c3 121 /**
wim 0:e086541742c3 122 * Move the cursor to (x, y)
wim 0:e086541742c3 123 *
wim 0:e086541742c3 124 * @param x-axis position
wim 0:e086541742c3 125 * @param y-axis position
wim 0:e086541742c3 126 * @returns true if there is no error, false otherwise
wim 0:e086541742c3 127 */
smartsystemdesign 2:1549541d3b4b 128 bool move(int16_t x, int16_t y, int16_t z, int16_t rx, int16_t ry, int16_t rz);
wim 0:e086541742c3 129
wim 0:e086541742c3 130 /**
wim 0:e086541742c3 131 * Press one or several buttons
wim 0:e086541742c3 132 *
wim 0:e086541742c3 133 * @param buttons buttons state
wim 0:e086541742c3 134 * @returns true if there is no error, false otherwise
wim 0:e086541742c3 135 */
wim 0:e086541742c3 136 bool buttons(uint32_t buttons);
wim 0:e086541742c3 137
wim 0:e086541742c3 138 /**
wim 0:e086541742c3 139 * Press hat
wim 0:e086541742c3 140 *
wim 0:e086541742c3 141 * @param hat hat state
wim 0:e086541742c3 142 * @returns true if there is no error, false otherwise
wim 0:e086541742c3 143 */
wim 0:e086541742c3 144 bool hat(uint8_t hat);
wim 0:e086541742c3 145
wim 0:e086541742c3 146 /**
wim 0:e086541742c3 147 * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
wim 0:e086541742c3 148 *
wim 0:e086541742c3 149 * @returns pointer to the report descriptor
wim 0:e086541742c3 150 */
wim 0:e086541742c3 151 virtual uint8_t * reportDesc();
wim 0:e086541742c3 152
wim 0:e086541742c3 153 private:
smartsystemdesign 2:1549541d3b4b 154 int16_t _x;
smartsystemdesign 2:1549541d3b4b 155 int16_t _y;
smartsystemdesign 2:1549541d3b4b 156 int16_t _z;
smartsystemdesign 2:1549541d3b4b 157 int16_t _rx;
smartsystemdesign 4:0ffbedd2cb73 158 int16_t _ry;
smartsystemdesign 2:1549541d3b4b 159 int16_t _rz;
wim 0:e086541742c3 160 uint32_t _buttons;
wim 0:e086541742c3 161 uint8_t _hat;
smartsystemdesign 2:1549541d3b4b 162
wim 0:e086541742c3 163 void _init();
wim 0:e086541742c3 164 };
wim 0:e086541742c3 165
wim 0:e086541742c3 166 #endif