Initial for Condor Simulator

Dependents:   USBJoystick_2 USBJoystick_NEW

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 Slider Resolution
00034 /*
00035 #define SBYTE      0                // 1 Byte, 8 Bit Resolution
00036 #define SWORD      1                // 2 Byte, 16 Bit Resolution
00037 */
00038 //Configure Joystick Hat Buttons
00039 #define HAT4      1                 // 4 Hat Buttons; 4 Positions if HAT4_8 = 0
00040 #define HAT4_8    1                 // 4 Hat Buttons giving 8 Positions if 1; DO NOT USE WITH HAT8!
00041 #define HAT8      0
00042 
00043 //Configure Joystick Buttons
00044 #define BUTTONS4  0
00045 #define BUTTONS8  0
00046 #define BUTTONS32 1
00047 
00048 
00049 /* Common usage */
00050 enum JOY_BUTTON {
00051     JOY_B0 = 1,
00052     JOY_B1 = 2,
00053     JOY_B2 = 4,
00054     JOY_B3 = 8,
00055 };
00056 
00057 #if (HAT4 == 1 && HAT4_8 == 0)
00058 enum JOY_HAT {
00059     JOY_HAT_UP      = 0,
00060     JOY_HAT_RIGHT   = 1,
00061     JOY_HAT_DOWN    = 2,
00062     JOY_HAT_LEFT    = 3,
00063     JOY_HAT_NEUTRAL = 4,
00064 };
00065 #endif
00066 #if (HAT8 == 1 || (HAT4 == 1 && HAT4_8 == 1))
00067 enum JOY_HAT {
00068     JOY_HAT_UP         = 0,
00069     JOY_HAT_UP_RIGHT   = 1,
00070     JOY_HAT_RIGHT      = 2,
00071     JOY_HAT_DOWN_RIGHT = 3,
00072     JOY_HAT_DOWN       = 4,
00073     JOY_HAT_DOWN_LEFT  = 5,
00074     JOY_HAT_LEFT       = 6,
00075     JOY_HAT_UP_LEFT    = 7,
00076     JOY_HAT_NEUTRAL    = 8,
00077 };
00078 #endif
00079 
00080 /* Limits */
00081 /* These values do not directly map to screen pixels */
00082 /* Zero may be interpreted as meaning 'no movement' */
00083 #if (SBYTE == 1)
00084     #define MIN_ABS    (-127)     /*!< The maximum value */
00085     #define MAX_ABS    (127)      /*!< The maximum value */
00086 #endif
00087 #if (SWORD == 1)
00088     #define MIN_ABS    (-32767)     /*!< The maximum value */
00089     #define MAX_ABS    (32767)      /*!< The maximum value */
00090 #endif
00091 /**
00092  *
00093  * USBJoystick example
00094  * @code
00095  * #include "mbed.h"
00096  * #include "USBJoystick.h"
00097  *
00098  * USBJoystick joystick;
00099  *
00100  * int main(void)
00101  * {
00102  *   while (1)
00103  *   {
00104  *      joystick.move(20, 0);
00105  *      wait(0.5);
00106  *   }
00107  * }
00108  *
00109  * @endcode
00110  *
00111  *
00112  * @code
00113  * #include "mbed.h"
00114  * #include "USBJoystick.h"
00115  *
00116  * USBJoystick joystick;
00117  *
00118  * int main(void) {
00119  *   uint16_t i = 0;
00120  *   int16_t throttle = 0;
00121  *   int16_t rudder = 0;
00122  *   int16_t x = 0;
00123  *   int16_t y = 0;
00124  *   int32_t radius = 120;
00125  *   int32_t angle = 0;
00126  *   uint32_t buttons = 0;
00127  *   uint8_t  hat = 0;
00128  *
00129  *   while (1) {
00130  *       // Basic Joystick
00131  *       throttle = (i >> 8) & 0xFF; // value -127 .. 128
00132  *       rudder   = (i >> 8) & 0xFF; // value -127 .. 128
00133  *       buttons  = (i >> 8) & 0x0F; // value    0 .. 15, one bit per button
00134  *       hat      = (i >> 8) & 0x07; // value    0 .. 7 or 8 for neutral
00135  *       i++;
00136  *
00137  *       x = cos((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00138  *       y = sin((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00139  *       angle += 3;
00140  *
00141  *       joystick.update(throttle, rudder, x, y, buttons, hat);
00142  *
00143  *       wait(0.001);
00144  *   }
00145  * }
00146  * @endcode
00147  */
00148 
00149 
00150 class USBJoystick: public USBHID
00151 {
00152 public:
00153 
00154     /**
00155       *   Constructor
00156       *
00157       * @param vendor_id Your vendor_id (default: 0x1234)
00158       * @param product_id Your product_id (default: 0x0604)
00159       * @param product_release Your product_release (default: 0x0001)
00160       */
00161 //     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
00162 //     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
00163     USBJoystick(uint16_t vendor_id = 0x4567, uint16_t product_id = 0x0500, uint16_t product_release = 0x0001, int waitForConnect = true):    // 32 buttons, no padding on buttons
00164         USBHID(0, 0, vendor_id, product_id, product_release, false) {
00165         _init();
00166         connect(waitForConnect);
00167     };
00168 
00169     /**
00170       * Write state of the joystick
00171       *
00172       * @param x x-axis position
00173       * @param y y-axis position
00174       * @param b break position
00175       * @param f flaps position
00176       * @param r rudder position
00177       * @param t throttle position
00178       * @param hat hat state 0 (up), 1 (up right), 2 (right), 3 (down right), 4 (down), 5 (down left), 6 (left), 7 (up left) or 8 (neutral)
00179       * @param buttons buttons state
00180       * @returns true if there is no error, false otherwise
00181       */
00182     bool update(int16_t x, int16_t y, int16_t b, int16_t f, int16_t r, int16_t t, uint8_t hat, uint32_t buttons);
00183 
00184     /**
00185       * Write state of the joystick
00186       *
00187       * @returns true if there is no error, false otherwise
00188       */
00189     bool update();
00190 
00191     /**
00192       * Move the throttle position
00193       *
00194       * @param t throttle position
00195       * @returns true if there is no error, false otherwise
00196       */
00197     bool throttle(int16_t t);
00198 
00199     /**
00200       * Move the rudder position
00201       *
00202       * @param r rudder position
00203       * @returns true if there is no error, false otherwise
00204       */
00205     bool rudder(int16_t r);
00206 
00207     /**
00208       * Move the dive break position
00209       *
00210       * @param b  break position
00211       * @returns true if there is no error, false otherwise
00212       */
00213     bool diveBreak(int16_t b);
00214     
00215      /**
00216        * Move the flaps position
00217        *
00218        * @param f flaps position
00219        * @returns true if there is no error, false otherwise
00220        */        
00221      bool flaps(int16_t f);         
00222 
00223 
00224     /**
00225       * Move the cursor to (x, y)
00226       *
00227       * @param x-axis position
00228       * @param y-axis position
00229       * @returns true if there is no error, false otherwise
00230       */
00231     bool move(int16_t x, int16_t y);
00232 
00233     /**
00234       * Press one or several buttons
00235       *
00236       * @param buttons buttons state
00237       * @returns true if there is no error, false otherwise
00238       */
00239     bool buttons(uint32_t buttons);
00240 
00241     /**
00242       * Press hat
00243       *
00244       * @param hat hat state
00245       * @returns true if there is no error, false otherwise
00246       */
00247     bool hat(uint8_t hat);
00248 
00249     /**
00250       * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00251       *
00252       * @returns pointer to the report descriptor
00253       */
00254     virtual uint8_t * reportDesc();
00255 
00256 private:
00257     int16_t _x;
00258     int16_t _y;
00259     int16_t _b;
00260     int16_t _f;
00261     int16_t _r;
00262     int16_t _t;
00263     uint8_t _hat;
00264     uint32_t _buttons;
00265 
00266     void _init();
00267 };
00268 
00269 #endif