Control program for FzeroX controller via USBHID interface.

Dependencies:   Radio USBDevice mbed

Fork of FzeroXcontroller by Interactive Device Design

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBJoystick.h Source File

USBJoystick.h

00001 /* USBJoystick.h */
00002 /* USB device example: Joystick*/
00003 /* Copyright (c) 2011 ARM Limited. All rights reserved. */
00004 /* Modified Mouse code for Joystick - WH 2012 */
00005 
00006 #ifndef USBJOYSTICK_H
00007 #define USBJOYSTICK_H
00008 
00009 #include "USBHID.h"
00010 
00011 #define REPORT_ID_JOYSTICK  4
00012 
00013 /* Common usage */
00014 enum JOY_BUTTON {
00015      JOY_B0 = 1,
00016      JOY_B1 = 2,
00017      JOY_B2 = 4,
00018      JOY_B3 = 8,     
00019 };
00020 
00021 #if(0)
00022 enum JOY_HAT {
00023      JOY_HAT_UP      = 0,
00024      JOY_HAT_RIGHT   = 1,
00025      JOY_HAT_DOWN    = 2,
00026      JOY_HAT_LEFT    = 3,     
00027      JOY_HAT_NEUTRAL = 4,          
00028 };
00029 #else
00030 enum JOY_HAT {
00031      JOY_HAT_UP         = 0,     
00032      JOY_HAT_UP_RIGHT   = 1,
00033      JOY_HAT_RIGHT      = 2,
00034      JOY_HAT_RIGHT_DOWN = 3,
00035      JOY_HAT_DOWN       = 4,
00036      JOY_HAT_DOWN_LEFT  = 5,     
00037      JOY_HAT_LEFT       = 6,     
00038      JOY_HAT_LEFT_UP    = 7,          
00039      JOY_HAT_NEUTRAL    = 8,          
00040 };
00041 #endif
00042 
00043 /* X, Y and T limits */
00044 /* These values do not directly map to screen pixels */
00045 /* Zero may be interpreted as meaning 'no movement' */
00046 #define JX_MIN_ABS    (-127)     /*!< The maximum value that we can move to the left on the x-axis */
00047 #define JY_MIN_ABS    (-127)     /*!< The maximum value that we can move up on the y-axis */
00048 #define JT_MIN_ABS    (-127)     /*!< The minimum value for the throttle */
00049 #define JX_MAX_ABS    (127)      /*!< The maximum value that we can move to the right on the x-axis */
00050 #define JY_MAX_ABS    (127)      /*!< The maximum value that we can move down on the y-axis */
00051 #define JT_MAX_ABS    (127)      /*!< The maximum value for the throttle */
00052 
00053 /**
00054  *
00055  * USBJoystick example
00056  * @code
00057  * #include "mbed.h"
00058  * #include "USBJoystick.h"
00059  *
00060  * USBJoystick joystick;
00061  *
00062  * int main(void)
00063  * {
00064  *   while (1)
00065  *   {
00066  *      joystick.move(20, 0);
00067  *      wait(0.5);
00068  *   }
00069  * }
00070  *
00071  * @endcode
00072  *
00073  *
00074  * @code
00075  * #include "mbed.h"
00076  * #include "USBJoystick.h"
00077  * #include <math.h>
00078  *
00079  * USBJoystick joystick;
00080  *
00081  * int main(void)
00082  * {
00083  *   int16_t i = 0;
00084  *   int16_t throttle = 0;
00085  *   int16_t rudder = 0;    
00086  *   int16_t x = 0;
00087  *   int16_t y = 0;
00088  *   int32_t radius = 120;
00089  *   int32_t angle = 0;
00090  *   int8_t button = 0;    
00091  *   int8_t hat = 0;    
00092  *   
00093  *   while (1) {
00094  *       // Basic Joystick
00095  *       throttle = (i >> 8) & 0xFF; // value -127 .. 128
00096  *       rudder = (i >> 8) & 0xFF;   // value -127 .. 128        
00097  *       button = (i >> 8) & 0x0F;   // value    0 .. 15, one bit per button     
00098  *        hat    = (i >> 8) & 0x07;   // value 0..7 or 8 for neutral         
00099  *       i++;        
00100  *       
00101  *       x = cos((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00102  *       y = sin((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00103  *       angle += 3;        
00104  *
00105  *       joystick.update(throttle, rudder, x, y, button, hat);
00106  *
00107  *       wait(0.001);
00108  *   }
00109  * }
00110  * @endcode
00111  */
00112 
00113 
00114 class USBJoystick: public USBHID {
00115    public:
00116 
00117         /**
00118          *   Constructor
00119          *
00120          * @param vendor_id Your vendor_id (default: 0x1234)
00121          * @param product_id Your product_id (default: 0x0002)
00122          * @param product_release Your product_release (default: 0x0001)
00123          */
00124          USBJoystick(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001): 
00125              USBHID(0, 0, vendor_id, product_id, product_release, false)
00126              { 
00127                  _init();
00128                  connect();
00129              };
00130          
00131          /**
00132          * Write a state of the mouse
00133          *
00134          * @param t throttle position
00135          * @param r rudder position         
00136          * @param x x-axis position
00137          * @param y y-axis position
00138          * @param buttons buttons state
00139          * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral)
00140          * @returns true if there is no error, false otherwise
00141          */
00142          bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint8_t buttons, uint8_t hat);
00143 
00144          /**
00145          * Write a state of the mouse
00146          *
00147          * @returns true if there is no error, false otherwise
00148          */
00149          bool update();
00150 
00151          /**
00152          * Move the throttle position
00153          *
00154          * @param t throttle position
00155          * @returns true if there is no error, false otherwise
00156          */
00157          bool throttle(int16_t t);
00158          
00159          /**
00160          * Move the rudder position
00161          *
00162          * @param r rudder position
00163          * @returns true if there is no error, false otherwise
00164          */        
00165          bool rudder(int16_t r);         
00166 
00167          /**
00168          * Move the cursor to (x, y)
00169          *
00170          * @param x-axis position
00171          * @param y-axis position
00172          * @returns true if there is no error, false otherwise
00173          */
00174          bool move(int16_t x, int16_t y);
00175          
00176          /**
00177          * Press one or several buttons
00178          *
00179          * @param button button state
00180          * @returns true if there is no error, false otherwise
00181          */
00182          bool button(uint8_t button);
00183          
00184          /**
00185          * Press hat
00186          *
00187          * @param hat hat state
00188          * @returns true if there is no error, false otherwise
00189          */
00190          bool hat(uint8_t hat);
00191          
00192          /*
00193          * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00194          *
00195          * @returns pointer to the report descriptor
00196          */
00197          virtual uint8_t * reportDesc();
00198 
00199      private:
00200          int8_t _t;     
00201          int8_t _r;              
00202          int8_t _x;                       
00203          int8_t _y;     
00204          uint8_t _button;
00205          uint8_t _hat; 
00206          
00207          void _init();                 
00208 };
00209 
00210 #endif