mark w. / Mbed 2 deprecated Nucleo_Gamepad

Dependencies:   USBDevice mbed

Files at this revision

API Documentation at this revision

Comitter:
thetazzbot
Date:
Wed Dec 14 15:30:55 2016 +0000
Parent:
5:39b7e6653dae
Commit message:
Changed Report descriptor to implement a 2 player, 16 buttons each controller.

Changed in this revision

USBGamepad.cpp Show annotated file Show diff for this revision Revisions of this file
USBGamepad.h Show annotated file Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBGamepad.cpp	Wed Dec 14 04:43:31 2016 +0000
+++ b/USBGamepad.cpp	Wed Dec 14 15:30:55 2016 +0000
@@ -25,8 +25,8 @@
 
 bool USBGamepad::update(uint32_t buttons) 
 {
-   _buttonsLo = (uint16_t)(buttons & 0xffff);
-   _buttonsHi = (uint16_t)((buttons >> 16) & 0xffff);
+   _buttonsP1 = (uint16_t)(buttons & 0xffff);
+   _buttonsP2 = (uint16_t)((buttons >> 16) & 0xffff);
    return update();
 }
  
@@ -35,9 +35,20 @@
    HID_REPORT report;
 
    // Fill the report according to the Gamepad Descriptor
-#define put(idx, val) (report.data[idx] = (val) & 0xff, report.data[(idx)+1] = ((val) >> 8) & 0xff)
-   put(0, _buttonsLo); // 0..1 2 bytes
-   put(2, _buttonsHi);//  2..3 2 bytes, 32 buttons
+
+    #define put(idx, val) (report.data[idx] = (val) & 0xff, report.data[(idx)+1] = ((val) >> 8) & 0xff)
+       
+    report.data[0] = 0x01;  //Report id 1
+    put(1, _buttonsP1); // 0..1 2 bytes    
+    
+    send(&report);
+
+    report.data[0] = 0x02;  //Report id 2
+    put(1, _buttonsP1); 
+    
+    send(&report);
+   
+    put(2, _buttonsP2);//  2..3 2 bytes, 32 buttons
    
    // important: keep reportLen in sync with the actual byte length of
    // the reports we build here
@@ -49,50 +60,63 @@
 
 bool USBGamepad::buttons(uint32_t buttons) 
 {
-     _buttonsLo = (uint16_t)(buttons & 0xffff);
-     _buttonsHi = (uint16_t)((buttons >> 16) & 0xffff);
+     _buttonsP1 = (uint16_t)(buttons & 0xffff);
+     _buttonsP2 = (uint16_t)((buttons >> 16) & 0xffff);
      return update();
 }
 
  
 void USBGamepad::_init() {
  
-   _buttonsLo = 0x0000; // 16 buttons
-   _buttonsHi = 0x0000; // 16 buttons
+   _buttonsP1 = 0x0000; // 16 buttons
+   _buttonsP2 = 0x0000; // 16 buttons
    
 }
  
  
 uint8_t * USBGamepad::reportDesc() 
 {    
-    // Descriptor generated by USBHID Descriptor tool
-    static uint8_t reportDescriptor[] = {
-        0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
-        0x15, 0x00,                    // LOGICAL_MINIMUM (0)
-        0x09, 0x05,                    // USAGE (Game Pad)
-        0xa1, 0x01,                    // COLLECTION (Application)
-        0x05, 0x09,                    //   USAGE_PAGE (Button)
-        0x19, 0x01,                    //   USAGE_MINIMUM (Button 1)
-        0x29, 0x20,                    //   USAGE_MAXIMUM (Button 32)
-        0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
-        0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
-        0x75, 0x01,                    //   REPORT_SIZE (1)
-        0x95, 0x20,                    //   REPORT_COUNT (32)
-        0x55, 0x00,                    //   UNIT_EXPONENT (0)
-        0x65, 0x00,                    //   UNIT (None)
-        0x81, 0x02,                    //   INPUT (Data,Var,Abs)
-        0xc0                           // END_COLLECTION
-    };
-    
+
+static const uint8_t reportDescriptor[27] = {
+    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
+    0x09, 0x05,                    // USAGE (Game Pad)
+    0xa1, 0x01,                    // COLLECTION (Application)
+    0xa1, 0x00,                    //   COLLECTION (Physical)
+    0x85, 0x01,                    //     REPORT_ID (1)
+    0x05, 0x09,                    //     USAGE_PAGE (Button)
+    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
+    0x29, 0x10,                    //     USAGE_MAXIMUM (Button 16)
+    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
+    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
+    0x75, 0x01,                    //     REPORT_SIZE (1)
+    0x95, 0x10,                    //     REPORT_COUNT (16)
+    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
+    0xc0                           // END_COLLECTION
+    0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
+    0x09, 0x05,                    // USAGE (Game Pad)
+    0xa1, 0x01,                    // COLLECTION (Application)
+    0xa1, 0x00,                    //   COLLECTION (Physical)
+    0x85, 0x02,                    //     REPORT_ID (2)
+    0x05, 0x09,                    //     USAGE_PAGE (Button)
+    0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
+    0x29, 0x10,                    //     USAGE_MAXIMUM (Button 16)
+    0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
+    0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
+    0x75, 0x01,                    //     REPORT_SIZE (1)
+    0x95, 0x10,                    //     REPORT_COUNT (16)
+    0x81, 0x02,                    //     INPUT (Data,Var,Abs)
+    0xc0                           // END_COLLECTION
+};
+
       reportLength = sizeof(reportDescriptor);
       return reportDescriptor;
 }
  
  uint8_t * USBGamepad::stringImanufacturerDesc() {
     static uint8_t stringImanufacturerDescriptor[] = {
-        0x14,                                            /*bLength*/
+        0x15,                                            /*bLength*/
         STRING_DESCRIPTOR,                               /*bDescriptorType 0x03*/
-        't',0,'h',0,'e',0,'t',0,'a',0,'z',0,'z',0,'b',0,'t',0        /*bString iManufacturer - mjrcorp*/
+        't',0,'h',0,'e',0,'t',0,'a',0,'z',0,'z',0,'b',0,'o',0,'t',0        /*bString iManufacturer - mjrcorp*/
     };
     return stringImanufacturerDescriptor;
 }
--- a/USBGamepad.h	Wed Dec 14 04:43:31 2016 +0000
+++ b/USBGamepad.h	Wed Dec 14 15:30:55 2016 +0000
@@ -10,10 +10,10 @@
  
 #define REPORT_ID_JOYSTICK  4
  
- // Length of our report.  This equates to four bytes (32 bits=32 buttons)
-// Important: This must be kept in sync 
-// with the actual joystick report format sent in update().
-#define REPORT_LEN 0x04
+ // Length of our report.  
+ // this is 3 bytes.  1 byte for report id, 2 bytes for 16 buttons
+ // this implements a 2 player controller.
+#define REPORT_LEN 0x03
 
 /* Common usage */
 enum JOY_BUTTON {
--- a/config.h	Wed Dec 14 04:43:31 2016 +0000
+++ b/config.h	Wed Dec 14 15:30:55 2016 +0000
@@ -31,45 +31,42 @@
 // Note: PTD1 (pin J2-12) should NOT be assigned as a button input,
 // as this pin is physically connected on the KL25Z to the on-board
 // indicator LED's blue segment.
+
 PinName buttonMap[] = {
-    PC_10,      // J10 pin 10, joystick button 1
-    PC_11,      // J10 pin 8,  joystick button 2
-    PC_12,      // J10 pin 6,  joystick button 3
-    PD_2,      // J10 pin 4,  joystick button 4
-    
-    PC_13,     // J10 pin 11, joystick button 5
-    PC_14,     // J10 pin 5,  joystick button 6
-    
-    PC_15,      // J9 pin 15,  joystick button 7
-    PC_2,      // J9 pin 13,  joystick button 8
-    PC_3,      // J9 pin 11,  joystick button 9
-    PC_1,      // J9 pin 9,   joystick button 10
-    PC_0,     // J9 pin 7,   joystick button 11
-    PC_4,     // J9 pin 5,   joystick button 12
-    PA_10,      // J9 pin 3,   joystick button 13
-    PB_3,      // J9 pin 1,   joystick button 14
-    
-    PB_5,     // J2 pin 1,   joystick button 15
-    PB_4,     // J2 pin 3,   joystick button 16
-    PB_13,     // J2 pin 5,   joystick button 17
-    PB_14,     // J2 pin 7,   joystick button 18
-    PB_15,     // J2 pin 9,   joystick button 19
-    PB_10,     // J2 pin 11,  joystick button 20
-    PB_1,     // J2 pin 13,  joystick button 21
-    PB_2,      // J2 pin 17,  joystick button 22
-    PC_7,      // J2 pin 19,  joystick button 23
-    
-    PB_6,      // J2 pin 20,  joystick button 24
+    PA_3,      // button 1
+    PA_2,      // button 2
+    PA_10,     // button 3
+    PB_3,      // button 4
+    PB_5,      // button 5
+    PB_4,      // button 6
+    PB_10,     // button 7
+    PA_8,      // button 8
+    PA_9,      // button 9
+    PC_7,      // button 10
+    PB_6,      // button 11
+    PA_7,      // button 12
+    PA_6,      // button 13
+    PA_5,      // button 14
+    PB_9,      // button 15
+    PB_8,      // button 16
+    PC_9,      // button 17
+    PB_4,      // button 18
+    PB_13,     // button 19
+    PB_14,     // button 20
+    PB_15,     // button 21
+    PB_1,      // button 22
+    PB_2,      // button 23
+    PC_5,      // button 24
+    PC_6,      // button 25
+    PC_8,      // button 26
+    PH_1,      // button 27
+    PH_0,      // button 28
+    PC_15,     // button 29
+    PC_14,     // button 30
+    PC_13,     // button 31
+    PB_7       // button 32
+};
 
-    NC,        // not used,   joystick button 25
-    NC,        // not used,   joystick button 26
-    NC,        // not used,   joystick button 27
-    NC,        // not used,   joystick button 28
-    NC,        // not used,   joystick button 29
-    NC,        // not used,   joystick button 30
-    NC,        // not used,   joystick button 31
-    NC         // not used,   joystick button 32
-};
 // STANDARD ID SETTINGS.  These provide full, transparent LedWiz compatibility.
 const uint16_t USB_VENDOR_ID = 0x1209;      
 const uint16_t USB_PRODUCT_ID = 0xACDE;