Ries Twisk / Mbed 2 deprecated JoyStick

Dependencies:   USBDevice mbed-rtos mbed

Fork of JoyStick by Ries Twisk

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      JOY_B4 = 16,     
00020 };
00021 
00022 /**
00023  *
00024  * USBJoystick example
00025  * @code
00026  * #include "mbed.h"
00027  * #include "USBJoystick.h"
00028  *
00029  * USBJoystick joystick;
00030  *
00031  * int main(void)
00032  * {
00033  *   while (1)
00034  *   {
00035  *      joystick.move(20, 0);
00036  *      wait(0.5);
00037  *   }
00038  * }
00039  *
00040  * @endcode
00041  *
00042  *
00043  * @code
00044  * #include "mbed.h"
00045  * #include "USBJoystick.h"
00046  * #include <math.h>
00047  *
00048  * USBJoystick joystick;
00049  *
00050  * int main(void)
00051  * {
00052  *   int16_t i = 0;
00053  *   int16_t throttle = 0;
00054  *   int16_t rudder = 0;    
00055  *   int16_t x = 0;
00056  *   int16_t y = 0;
00057  *   int32_t radius = 120;
00058  *   int32_t angle = 0;
00059  *   int8_t button = 0;    
00060  *   int8_t hat = 0;    
00061  *   
00062  *   while (1) {
00063  *       // Basic Joystick
00064  *       throttle = (i >> 8) & 0xFF; // value -127 .. 128
00065  *       rudder = (i >> 8) & 0xFF;   // value -127 .. 128        
00066  *       button = (i >> 8) & 0x0F;   // value    0 .. 15, one bit per button     
00067  *        hat    = (i >> 8) & 0x07;   // value 0..7 or 8 for neutral         
00068  *       i++;        
00069  *       
00070  *       x = cos((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00071  *       y = sin((double)angle*3.14/180.0)*radius;  // value -127 .. 128
00072  *       angle += 3;        
00073  *
00074  *       joystick.update(throttle, rudder, x, y, button, hat);
00075  *
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): 
00094              USBHID(0, 0, vendor_id, product_id, product_release, false)
00095              { 
00096                  _init();
00097                  connect();
00098              };
00099          
00100          /**
00101          * Write a state of the mouse
00102          *
00103          * @param t throttle position
00104          * @param r rudder position         
00105          * @param x x-axis position
00106          * @param y y-axis position
00107          * @param buttons buttons state
00108          * @returns true if there is no error, false otherwise
00109          */
00110          bool update(int16_t t, int16_t r, int16_t x, int16_t y, uint32_t buttons);
00111 
00112          /**
00113          * Write a state of the mouse
00114          *
00115          * @returns true if there is no error, false otherwise
00116          */
00117          bool update();
00118 
00119          /**
00120          * Move the throttle position
00121          *
00122          * @param t throttle position
00123          * @returns true if there is no error, false otherwise
00124          */
00125          bool throttle(int16_t t);
00126          
00127          /**
00128          * Move the rudder position
00129          *
00130          * @param r rudder position
00131          * @returns true if there is no error, false otherwise
00132          */        
00133          bool rudder(int16_t r);         
00134 
00135          /**
00136          * Move the cursor to (x, y)
00137          *
00138          * @param x-axis position
00139          * @param y-axis position
00140          * @returns true if there is no error, false otherwise
00141          */
00142          bool move(int16_t x, int16_t y);
00143          
00144          /**
00145          * Press one or several buttons
00146          *
00147          * @param button button state
00148          * @returns true if there is no error, false otherwise
00149          */
00150          bool button(uint32_t button);
00151                   
00152          /*
00153          * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00154          *
00155          * @returns pointer to the report descriptor
00156          */
00157          virtual uint8_t * reportDesc();
00158 
00159      private:
00160          int16_t _t;     
00161          int16_t _r;              
00162          int16_t _x;                       
00163          int16_t _y;     
00164          uint32_t _button;
00165          
00166          void _init();                 
00167 };
00168 
00169 #endif