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

Dependents:   USBJoystick_Test

Fork of USBJoystick by Wim Huiskamp

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBJoystick.h Source File

USBJoystick.h

00001 /* mbed USBJoystick Library
00002  * Copyright (c) 2012, v01:  Initial version, WH,
00003  *                           Modified USBMouse code ARM Limited.
00004  *                           (c) 2010-2011 mbed.org, MIT License
00005  *               2016, v02:  Updated USBDevice Lib, Added waitForConnect, Updated 32 bits button 
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a copy
00008  * of this software and associated documentation files (the "Software"), to deal
00009  * in the Software without restriction, inclumosig without limitation the rights
00010  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011  * copies of the Software, and to permit persons to whom the Software is
00012  * furnished to do so, subject to the following conditions:
00013  *
00014  * The above copyright notice and this permission notice shall be included in
00015  * all copies or substantial portions of the Software.
00016  *
00017  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018  * IMPLIED, INCLUmosiG BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00023  * THE SOFTWARE.
00024  */
00025 
00026 #ifndef USBJOYSTICK_H
00027 #define USBJOYSTICK_H
00028 
00029 #include "USBHID.h"
00030 
00031 #define REPORT_ID_JOYSTICK  4
00032 
00033 //Configure Joystick
00034 #define HAT4      0
00035 #define HAT8      1
00036 
00037 #define BUTTONS4  0
00038 #define BUTTONS8  0
00039 #define BUTTONS32 1
00040 
00041 
00042 /* Common usage */
00043 enum JOY_BUTTON {
00044      JOY_B0 = 1,
00045      JOY_B1 = 2,
00046      JOY_B2 = 4,
00047      JOY_B3 = 8,
00048 };
00049 
00050 #if (HAT4 == 1)
00051 enum JOY_HAT {
00052      JOY_HAT_UP      = 0,
00053      JOY_HAT_RIGHT   = 1,
00054      JOY_HAT_DOWN    = 2,
00055      JOY_HAT_LEFT    = 3,     
00056      JOY_HAT_NEUTRAL = 4,
00057 };
00058 #endif
00059 #if (HAT8 == 1)
00060 enum JOY_HAT {
00061      JOY_HAT_UP         = 0,     
00062      JOY_HAT_UP_RIGHT   = 1,
00063      JOY_HAT_RIGHT      = 2,
00064      JOY_HAT_RIGHT_DOWN = 3,
00065      JOY_HAT_DOWN       = 4,
00066      JOY_HAT_DOWN_LEFT  = 5,
00067      JOY_HAT_LEFT       = 6,
00068      JOY_HAT_LEFT_UP    = 7,
00069      JOY_HAT_NEUTRAL    = 8,
00070 };
00071 #endif
00072 
00073 /* X, Y and T limits */
00074 /* These values do not directly map to screen pixels */
00075 /* Zero may be interpreted as meaning 'no movement' */
00076 #define JX_MIN_ABS    (-32768)     /*!< The maximum value that we can move to the left on the x-axis */
00077 #define JY_MIN_ABS    (-32768)     /*!< The maximum value that we can move up on the y-axis */
00078 #define JT_MIN_ABS    (-32768)     /*!< The minimum value for the throttle */
00079 #define JX_MAX_ABS    (32767)      /*!< The maximum value that we can move to the right on the x-axis */
00080 #define JY_MAX_ABS    (32767)      /*!< The maximum value that we can move down on the y-axis */
00081 #define JT_MAX_ABS    (32767)      /*!< The maximum value for the throttle */
00082 
00083 class USBJoystick: public USBHID {
00084    public:
00085 
00086    /**
00087      *   Constructor
00088      *
00089      * @param vendor_id Your vendor_id (default: 0x1234)
00090      * @param product_id Your product_id (default: 0x0002)
00091      * @param product_release Your product_release (default: 0x0001)
00092      */
00093 //     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
00094 //     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
00095      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
00096        USBHID(0, 0, vendor_id, product_id, product_release, false) {
00097          _init();
00098          connect(waitForConnect);
00099      };
00100          
00101      /**
00102        * Write state of the joystick
00103        *
00104        * @param t throttle position
00105        * @param r rudder position         
00106        * @param x x-axis position
00107        * @param y y-axis position
00108        * @param buttons buttons state
00109        * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral)
00110        * @returns true if there is no error, false otherwise
00111        */
00112      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);
00113 
00114      /**
00115        * Write state of the joystick
00116        *
00117        * @returns true if there is no error, false otherwise
00118        */
00119      bool update();       
00120 
00121      /**
00122        * Move the cursor to (x, y)
00123        *
00124        * @param x-axis position
00125        * @param y-axis position
00126        * @returns true if there is no error, false otherwise
00127        */
00128      bool move(int16_t x, int16_t y, int16_t z, int16_t rx, int16_t ry, int16_t rz);
00129          
00130      /**
00131        * Press one or several buttons
00132        *
00133        * @param buttons buttons state
00134        * @returns true if there is no error, false otherwise
00135        */
00136      bool buttons(uint32_t buttons);
00137          
00138      /**
00139        * Press hat
00140        *
00141        * @param hat hat state
00142        * @returns true if there is no error, false otherwise
00143        */
00144      bool hat(uint8_t hat);
00145          
00146      /**
00147        * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00148        *
00149        * @returns pointer to the report descriptor
00150        */
00151        virtual uint8_t * reportDesc();
00152 
00153    private:
00154      int16_t _x;
00155      int16_t _y;
00156      int16_t _z;
00157      int16_t _rx;
00158      int16_t _ry;
00159      int16_t _rz;
00160      uint32_t _buttons;
00161      uint8_t _hat; 
00162      
00163      void _init();                 
00164 };
00165 
00166 #endif