for whell

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    (-127)     /*!< The maximum value that we can move to the left on the x-axis */
00077 #define JY_MIN_ABS    (-127)     /*!< The maximum value that we can move up on the y-axis */
00078 #define JT_MIN_ABS    (-127)     /*!< The minimum value for the throttle */
00079 #define JX_MAX_ABS    (127)      /*!< The maximum value that we can move to the right on the x-axis */
00080 #define JY_MAX_ABS    (127)      /*!< The maximum value that we can move down on the y-axis */
00081 #define JT_MAX_ABS    (127)      /*!< The maximum value for the throttle */
00082 
00083 #define JS_MAX_ABS    (127)
00084 #define JS_MIN_ABS    (-127)
00085 
00086 /**
00087  *
00088  * USBJoystick example
00089  * @code
00090  * #include "mbed.h"
00091  * #include "USBJoystick.h"
00092  *
00093  * USBJoystick joystick;
00094  *
00095  * int main(void)
00096  * {
00097  *   while (1)
00098  *   {
00099  *      joystick.move(20, 0);
00100  *      wait(0.5);
00101  *   }
00102  * }
00103  *
00104  * @endcode
00105  *
00106  *
00107  * @code
00108  * #include "mbed.h"
00109  * #include "USBJoystick.h"
00110  *
00111  * USBJoystick joystick;
00112  *
00113  * int main(void) {
00114  *   uint16_t i = 0;
00115  *   int16_t throttle = 0;
00116  *   int16_t rudder = 0;    
00117  *   int16_t x = 0;
00118  *   int16_t y = 0;
00119  *   int32_t radius = 120;
00120  *   int32_t angle = 0;
00121  *   uint32_t buttons = 0;    
00122  *   uint8_t  hat = 0;    
00123  *   
00124  *   while (1) {
00125  *       // Basic Joystick
00126  *       throttle = (i >> 8) & 0xFF; // value -127 .. 128
00127  *       rudder   = (i >> 8) & 0xFF; // value -127 .. 128        
00128  *       buttons  = (i >> 8) & 0x0F; // value    0 .. 15, one bit per button     
00129  *       hat      = (i >> 8) & 0x07; // value    0 .. 7 or 8 for neutral         
00130  *       i++;        
00131  *       
00132  *       x = cos((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00133  *       y = sin((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00134  *       angle += 3;        
00135  *
00136  *       joystick.update(throttle, rudder, x, y, buttons, hat);
00137  *
00138  *       wait(0.001);
00139  *   }
00140  * }
00141  * @endcode
00142  */
00143 
00144 
00145 class USBJoystick: public USBHID {
00146    public:
00147 
00148    /**
00149      *   Constructor
00150      *
00151      * @param vendor_id Your vendor_id (default: 0x1234)
00152      * @param product_id Your product_id (default: 0x0002)
00153      * @param product_release Your product_release (default: 0x0001)
00154      */
00155 //     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
00156 //     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
00157      USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0600, uint16_t product_release = 0x0001, int waitForConnect = true):    // 32 buttons, no padding on buttons
00158        USBHID(0, 0, vendor_id, product_id, product_release, false) {
00159          _init();
00160          connect(waitForConnect);
00161      };
00162          
00163      /**
00164        * Write state of the joystick
00165        *
00166        * @param t throttle position
00167        * @param r rudder position         
00168        * @param x x-axis position
00169        * @param y y-axis position
00170        * @param buttons buttons state
00171        * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral)
00172        * @returns true if there is no error, false otherwise
00173        */
00174      bool update(int16_t s);
00175 
00176      /**
00177        * Write state of the joystick
00178        *
00179        * @returns true if there is no error, false otherwise
00180        */
00181      bool update();
00182 
00183      /**
00184        * Move the throttle position
00185        *
00186        * @param t throttle position
00187        * @returns true if there is no error, false otherwise
00188        */
00189      
00190      bool steering(int16_t s);
00191      
00192      bool throttle(int16_t t);
00193          
00194      /**
00195        * Move the rudder position
00196        *
00197        * @param r rudder position
00198        * @returns true if there is no error, false otherwise
00199        */        
00200      bool rudder(int16_t r);         
00201 
00202      /**
00203        * Move the cursor to (x, y)
00204        *
00205        * @param x-axis position
00206        * @param y-axis position
00207        * @returns true if there is no error, false otherwise
00208        */
00209      bool move(int16_t x, int16_t y);
00210          
00211      /**
00212        * Press one or several buttons
00213        *
00214        * @param buttons buttons state
00215        * @returns true if there is no error, false otherwise
00216        */
00217      bool buttons(uint32_t buttons);
00218          
00219      /**
00220        * Press hat
00221        *
00222        * @param hat hat state
00223        * @returns true if there is no error, false otherwise
00224        */
00225      bool hat(uint8_t hat);
00226          
00227      /**
00228        * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00229        *
00230        * @returns pointer to the report descriptor
00231        */
00232        virtual uint8_t * reportDesc();
00233 
00234    private:
00235      int8_t _s;
00236      int8_t _t;     
00237      int8_t _r;              
00238      int8_t _x;                       
00239      int8_t _y;     
00240      uint32_t _buttons;
00241      uint8_t _hat; 
00242          
00243      void _init();                 
00244 };
00245 
00246 #endif