This is a work in progress. Trying to make a working arcade button to USB interface with the Nucleo F411RE.

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBGamepad.h Source File

USBGamepad.h

00001 /* USBGamepad.h */
00002 /* USB device example: Gamepad*/
00003 /* Arcade style buttons to USB interface for use with RetroPie */
00004 
00005  
00006 #ifndef USBGAMEPAD_H
00007 #define USBGAMEPAD_H
00008  
00009 #include "USBHID.h"
00010  
00011 #define REPORT_ID_JOYSTICK  4
00012  
00013  // Length of our report.  
00014  // this is 3 bytes.  1 byte for report id, 2 bytes for 16 buttons
00015  // this implements a 2 player controller.
00016 #define REPORT_LEN 0x03
00017 
00018 /* Common usage */
00019 enum JOY_BUTTON {
00020      JOY_B0 = 0x0001,
00021      JOY_B1 = 0x0002,
00022      JOY_B2 = 0x0004,
00023      JOY_B3 = 0x0008,
00024      JOY_B4 = 0x0010,
00025      JOY_B5 = 0x0020,
00026      JOY_B6 = 0x0040,
00027      JOY_B7 = 0x0080,
00028      JOY_B8 = 0x0100,
00029      JOY_B9 = 0x0200,
00030      JOY_B10 = 0x0400,
00031      JOY_B11 = 0x0800,
00032      JOY_B12 = 0x1000,
00033      JOY_B13 = 0x2000,
00034      JOY_B14 = 0x4000,
00035      JOY_B15 = 0x8000
00036 };
00037  
00038 /**
00039  *
00040  * USBJoystick example
00041  * @code
00042  * #include "mbed.h"
00043  * #include "USBJoystick.h"
00044  *
00045  * USBJoystick joystick;
00046  *
00047  * int main(void)
00048  * {
00049  *   while (1)
00050  *   {
00051  *      joystick.move(20, 0);
00052  *      wait(0.5);
00053  *   }
00054  * }
00055  *
00056  * @endcode
00057  *
00058  *
00059  * @code
00060  * #include "mbed.h"
00061  * #include "USBJoystick.h"
00062  * #include <math.h>
00063  *
00064  * USBJoystick joystick;
00065  *
00066  * int main(void)
00067  * {   
00068  *   while (1) {
00069  *       // Basic Joystick
00070  *       joystick.update(tx, y, z, buttonBits);
00071  *       wait(0.001);
00072  *   }
00073  * }
00074  * @endcode
00075  */
00076  
00077  
00078 class USBGamepad: public USBHID {
00079    public:
00080  
00081         /**
00082          *   Constructor
00083          *
00084          * @param vendor_id Your vendor_id (default: 0x1234)
00085          * @param product_id Your product_id (default: 0x0002)
00086          * @param product_release Your product_release (default: 0x0001)
00087          */
00088          USBGamepad(uint16_t vendor_id = 0x1234, uint16_t product_id = 0x0100, uint16_t product_release = 0x0001, int waitForConnect = true): 
00089              USBHID(0,REPORT_LEN, vendor_id, product_id, product_release, false)
00090              { 
00091                  _init();
00092                  connect(waitForConnect);
00093              };
00094          
00095          /**
00096          * Write a state of the USBGamepad
00097          *
00098          * @param buttons buttons state, as a bit mask (combination with '|' of JOY_Bn values)
00099          * @returns true if there is no error, false otherwise
00100          */
00101          bool update(uint32_t buttons);
00102          
00103          /**
00104          * Write a state of the USBGamepad
00105          *
00106          * @returns true if there is no error, false otherwise
00107          */
00108          bool update();
00109          
00110          /**
00111          * Press one or several buttons
00112          *
00113          * @param buttons button state, as a bitwise combination of JOY_Bn values
00114          * @returns true if there is no error, false otherwise
00115          */
00116          bool buttons(uint32_t buttons);
00117          
00118          /*
00119          * To define the report descriptor. Warning: this method has to store the length of the report descriptor in reportLength.
00120          *
00121          * @returns pointer to the report descriptor
00122          */
00123          virtual uint8_t * reportDesc();
00124  
00125          /* USB descriptor string overrides */
00126          virtual uint8_t *stringImanufacturerDesc();
00127          virtual uint8_t *stringIserialDesc();
00128          virtual uint8_t *stringIproductDesc();
00129  
00130      private:
00131          uint16_t _buttonsLo;
00132          uint16_t _buttonsHi;
00133          
00134          void _init();                 
00135 };
00136  
00137 #endif