An input/output controller for virtual pinball machines, with plunger position tracking, accelerometer-based nudge sensing, button input encoding, and feedback device control.

Dependencies:   USBDevice mbed FastAnalogIn FastIO FastPWM SimpleDMA

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 = 0x0001,
00016      JOY_B1 = 0x0002,
00017      JOY_B2 = 0x0004,
00018      JOY_B3 = 0x0008,
00019      JOY_B4 = 0x0010,
00020      JOY_B5 = 0x0020,
00021      JOY_B6 = 0x0040,
00022      JOY_B7 = 0x0080,
00023      JOY_B8 = 0x0100,
00024      JOY_B9 = 0x0200,
00025      JOY_B10 = 0x0400,
00026      JOY_B11 = 0x0800,
00027      JOY_B12 = 0x1000,
00028      JOY_B13 = 0x2000,
00029      JOY_B14 = 0x4000,
00030      JOY_B15 = 0x8000
00031 };
00032  
00033 /* X, Y and T limits */
00034 /* These values do not directly map to screen pixels */
00035 /* Zero may be interpreted as meaning 'no movement' */
00036 #define JX_MIN_ABS    (-127)     /*!< The maximum value that we can move to the left on the x-axis */
00037 #define JY_MIN_ABS    (-127)     /*!< The maximum value that we can move up on the y-axis */
00038 #define JZ_MIN_ABS    (-127)     /*!< The minimum value for the Z axis */
00039 #define JX_MAX_ABS    (127)      /*!< The maximum value that we can move to the right on the x-axis */
00040 #define JY_MAX_ABS    (127)      /*!< The maximum value that we can move down on the y-axis */
00041 #define JZ_MAX_ABS    (127)      /*!< The maximum value for the Z axis */
00042  
00043 /**
00044  *
00045  * USBJoystick example
00046  * @code
00047  * #include "mbed.h"
00048  * #include "USBJoystick.h"
00049  *
00050  * USBJoystick joystick;
00051  *
00052  * int main(void)
00053  * {
00054  *   while (1)
00055  *   {
00056  *      joystick.move(20, 0);
00057  *      wait(0.5);
00058  *   }
00059  * }
00060  *
00061  * @endcode
00062  *
00063  *
00064  * @code
00065  * #include "mbed.h"
00066  * #include "USBJoystick.h"
00067  * #include <math.h>
00068  *
00069  * USBJoystick joystick;
00070  *
00071  * int main(void)
00072  * {   
00073  *   while (1) {
00074  *       // Basic Joystick
00075  *       joystick.update(tx, y, z, buttonBits);
00076  *       wait(0.001);
00077  *   }
00078  * }
00079  * @endcode
00080  */
00081  
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): 
00094              USBHID(16, 64, vendor_id, product_id, product_release, false)
00095              { 
00096                  _init();
00097                  connect(waitForConnect);
00098              };
00099          
00100          /**
00101          * Write a state of the mouse
00102          *
00103          * @param x x-axis position
00104          * @param y y-axis position
00105          * @param z z-axis position
00106          * @param buttons buttons state, as a bit mask (combination with '|' of JOY_Bn values)
00107          * @returns true if there is no error, false otherwise
00108          */
00109          bool update(int16_t x, int16_t y, int16_t z, uint32_t buttons, uint16_t status);
00110          
00111          /**
00112          * Update just the status
00113          */
00114          bool updateStatus(uint32_t stat);
00115          
00116          /**
00117          * Write an exposure report.  We'll fill out a report with as many pixels as
00118          * will fit in the packet, send the report, and update the index to the next
00119          * pixel to send.  The caller should call this repeatedly to send reports for
00120          * all pixels.
00121          *
00122          * @param idx current index in pixel array, updated to point to next pixel to send
00123          * @param npix number of pixels in the overall array
00124          * @param pix pixel array
00125          */
00126          bool updateExposure(int &idx, int npix, const uint16_t *pix);
00127          
00128          /**
00129          * Write a configuration report.
00130          *
00131          * @param numOutputs the number of configured output channels
00132          * @param unitNo the device unit number
00133          */
00134          bool reportConfig(int numOutputs, int unitNo);
00135  
00136          /**
00137          * Write a state of the mouse
00138          *
00139          * @returns true if there is no error, false otherwise
00140          */
00141          bool update();
00142          
00143          /**
00144          * Move the cursor to (x, y)
00145          *
00146          * @param x x-axis position
00147          * @param y y-axis position
00148          * @returns true if there is no error, false otherwise
00149          */
00150          bool move(int16_t x, int16_t y);
00151          
00152          /**
00153          * Set the z position
00154          *
00155          * @param z z-axis osition
00156          */
00157          bool setZ(int16_t z);
00158          
00159          /**
00160          * Press one or several buttons
00161          *
00162          * @param buttons button state, as a bitwise combination of JOY_Bn values
00163          * @returns true if there is no error, false otherwise
00164          */
00165          bool buttons(uint32_t buttons);
00166          
00167          /*
00168          * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00169          *
00170          * @returns pointer to the report descriptor
00171          */
00172          virtual uint8_t * reportDesc();
00173  
00174          /* USB descriptor string overrides */
00175          virtual uint8_t *stringImanufacturerDesc();
00176          virtual uint8_t *stringIserialDesc();
00177          virtual uint8_t *stringIproductDesc();
00178  
00179      private:
00180          int16_t _x;                       
00181          int16_t _y;     
00182          int16_t _z;
00183          uint16_t _buttonsLo;
00184          uint16_t _buttonsHi;
00185          uint16_t _status;
00186          
00187          void _init();                 
00188 };
00189  
00190 #endif