Mirror with some correction

Dependencies:   mbed FastIO FastPWM USBDevice

Files at this revision

API Documentation at this revision

Comitter:
mjr
Date:
Tue Oct 17 22:27:48 2017 +0000
Parent:
89:c43cd923401c
Child:
91:ae9be42652bf
Commit message:
Add Rx/Ry/Rz joystick reporting option

Changed in this revision

Plunger/distanceSensor.h Show annotated file Show diff for this revision Revisions of this file
USBJoystick/USBJoystick.cpp Show annotated file Show diff for this revision Revisions of this file
USBJoystick/USBJoystick.h Show annotated file Show diff for this revision Revisions of this file
USBProtocol.h Show annotated file Show diff for this revision Revisions of this file
VL6180X/VL6180X.h Show annotated file Show diff for this revision Revisions of this file
cfgVarMsgMap.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
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Plunger/distanceSensor.h	Fri May 12 17:57:59 2017 +0000
+++ b/Plunger/distanceSensor.h	Tue Oct 17 22:27:48 2017 +0000
@@ -27,8 +27,8 @@
 // precision in testing is closer to 5mm.  Sample times are around 16ms.
 // This makes the sensor acceptable but not great by Pinscape standards;
 // we generally consider 2.5ms read times and .25mm precision to be the
-// minimum standards.  However, this sensor is very inexpensive and easier
-// to set up than most of the better options, so it might be attractive to
+// minimum standards.  However, this sensor is inexpensive and easier to
+// set up than most of the better options, so it might be attractive to
 // some cab builders despite the quality tradeoffs.
 
 #ifndef _DISTANCESENSOR_H_
--- a/USBJoystick/USBJoystick.cpp	Fri May 12 17:57:59 2017 +0000
+++ b/USBJoystick/USBJoystick.cpp	Tue Oct 17 22:27:48 2017 +0000
@@ -526,8 +526,8 @@
     USAGE_PAGE(1), 0x01,            // Generic desktop
     USAGE(1), 0x04,                 // Joystick
     COLLECTION(1), 0x01,            // Application
+    
         // input report (device to host)
-
         USAGE_PAGE(1), 0x06,        // generic device controls - for config status
         USAGE(1), 0x00,             // undefined device control
         LOGICAL_MINIMUM(1), 0x00,   // 8-bit values
@@ -566,6 +566,57 @@
     END_COLLECTION(0)
 };
 
+// Joystick report descriptor with "R" axis reports.  This version
+// uses Rx and Ry for the accelerometer readings and Rz for the
+// plunger, instead of the standard X/Y/Z axes.  This can be used
+// to avoid conflicts with other devices reporting on the normal
+// X/Y/Z axes.
+static const uint8_t reportDescriptorJS_RXRYRZ[] = 
+{         
+    USAGE_PAGE(1), 0x01,            // Generic desktop
+    USAGE(1), 0x04,                 // Joystick
+    COLLECTION(1), 0x01,            // Application
+    
+        // input report (device to host)
+        USAGE_PAGE(1), 0x06,        // generic device controls - for config status
+        USAGE(1), 0x00,             // undefined device control
+        LOGICAL_MINIMUM(1), 0x00,   // 8-bit values
+        LOGICAL_MAXIMUM(1), 0xFF,
+        REPORT_SIZE(1), 0x08,       // 8 bits per report
+        REPORT_COUNT(1), 0x04,      // 4 reports (4 bytes)
+        INPUT(1), 0x02,             // Data, Variable, Absolute
+
+        USAGE_PAGE(1), 0x09,        // Buttons
+        USAGE_MINIMUM(1), 0x01,     // { buttons }
+        USAGE_MAXIMUM(1), 0x20,     // {  1-32   }
+        LOGICAL_MINIMUM(1), 0x00,   // 1-bit buttons - 0...
+        LOGICAL_MAXIMUM(1), 0x01,   // ...to 1
+        REPORT_SIZE(1), 0x01,       // 1 bit per report
+        REPORT_COUNT(1), 0x20,      // 32 reports
+        UNIT_EXPONENT(1), 0x00,     // Unit_Exponent (0)
+        UNIT(1), 0x00,              // Unit (None)                                           
+        INPUT(1), 0x02,             // Data, Variable, Absolute
+       
+        USAGE_PAGE(1), 0x01,        // Generic desktop
+        USAGE(1), 0x33,             // Rx axis ("X rotation")
+        USAGE(1), 0x34,             // Ry axis
+        USAGE(1), 0x35,             // Rz axis
+        LOGICAL_MINIMUM(2), 0x00,0xF0,   // each value ranges -4096
+        LOGICAL_MAXIMUM(2), 0x00,0x10,   // ...to +4096
+        REPORT_SIZE(1), 0x10,       // 16 bits per report
+        REPORT_COUNT(1), 0x03,      // 3 reports (X, Y, Z)
+        INPUT(1), 0x02,             // Data, Variable, Absolute
+         
+        // output report (host to device)
+        REPORT_SIZE(1), 0x08,       // 8 bits per report
+        REPORT_COUNT(1), 0x08,      // output report count - 8-byte LedWiz format
+        0x09, 0x01,                 // usage
+        0x91, 0x01,                 // Output (array)
+
+    END_COLLECTION(0)
+};
+
+
 // 
 // USB HID Report Descriptor - Keyboard/Media Control
 //
@@ -670,8 +721,17 @@
         // Otherwise, it's the plain LedWiz control interface.
         if (enableJoystick)
         {
-            len = sizeof(reportDescriptorJS);
-            return reportDescriptorJS;
+            switch (axisFormat)
+            {
+            case AXIS_FORMAT_XYZ:
+            default:
+                len = sizeof(reportDescriptorJS);
+                return reportDescriptorJS;
+                
+            case AXIS_FORMAT_RXRYRZ:
+                len = sizeof(reportDescriptorJS_RXRYRZ);
+                return reportDescriptorJS_RXRYRZ;
+            }
         }
         else
         {
@@ -710,16 +770,19 @@
 
 const uint8_t *USBJoystick::stringIserialDesc() 
 {
-    // set up a buffer with the length prefix and descriptor type
-    const int numChars = 3 + 16 + 1 + 3;
+    // set up a buffer with space for the length prefix byte, descriptor type
+    // byte, and serial number (as a wide-character string)
+    const int numChars = 3 + 16 + 1 + 1 + 3;
     static uint8_t buf[2 + numChars*2];
     uint8_t *dst = buf;
-    *dst++ = sizeof(buf);
+    
+    // store a placeholder for the length, followed by the descriptor type byte
+    *dst++ = 0;
     *dst++ = STRING_DESCRIPTOR;
 
     // Create an ASCII version of our unique serial number string:
     //
-    //   PSCxxxxxxxxxxxxxxxxivvv
+    //   PSCxxxxxxxxxxxxxxxxi[a]vvv
     //
     // where:
     //   
@@ -729,6 +792,9 @@
     //             J = Joystick + LedWiz
     //             K = Keyboard + LedWiz
     //             C = Joystick + Keyboard + LedWiz ("C" for combo)
+    //   a      = joystick axis types:
+    //             <empty> = X,Y,Z, or no joystick interface at all
+    //             A       = Rx,Ry,Rz
     //   vvv    = version suffix
     //
     // The suffix for the interface type resolves a problem on some Windows systems
@@ -740,10 +806,12 @@
     char xbuf[numChars + 1];
     uint32_t x = SIM->UIDML;
     static char ifcCode[] = "LJKC";
-    sprintf(xbuf, "PSC%08lX%08lX%c008",
+    static const char *axisCode[] = { "", "A" };
+    sprintf(xbuf, "PSC%08lX%08lX%c%s008",
         SIM->UIDML, 
         SIM->UIDL, 
-        ifcCode[(enableJoystick ? 0x01 : 0x00) | (useKB ? 0x02 : 0x00)]);
+        ifcCode[(enableJoystick ? 0x01 : 0x00) | (useKB ? 0x02 : 0x00)],
+        axisCode[(enableJoystick ? axisFormat : 0)]);
 
     // copy the ascii bytes into the descriptor buffer, converting to unicode
     // 16-bit little-endian characters
@@ -753,6 +821,9 @@
         *dst++ = '\0';
     }
     
+    // store the final length (in bytes) in the length prefix byte
+    buf[0] = dst - buf;
+    
     // return the buffer    
     return buf;
 }
--- a/USBJoystick/USBJoystick.h	Fri May 12 17:57:59 2017 +0000
+++ b/USBJoystick/USBJoystick.h	Tue Oct 17 22:27:48 2017 +0000
@@ -89,7 +89,10 @@
     // Length of our joystick reports.  Important: This must be kept in sync 
     // with the actual joystick report format sent in update().
     static const int reportLen = 14;
-
+    
+    // Joystick axis report format
+    static const int AXIS_FORMAT_XYZ    = 0;    // nudge on X/Y, plunger on Z
+    static const int AXIS_FORMAT_RXRYRZ = 1;    // nudge on Rx/Ry, plunger on Rz
 
     /**
      *   Constructor
@@ -97,14 +100,19 @@
      * @param vendor_id Your vendor_id (default: 0x1234)
      * @param product_id Your product_id (default: 0x0002)
      * @param product_release Your product_release (default: 0x0001)
+     * @param waitforConnect don't return until the connection is established
+     * @param enableJoystick enable the joystick interface (if false, uses the OUT-only LedWiz-style interface)
+     * @param axisFormat an AXIS_FORMAT_xxx value specifying the joystick axis report format
+     * @param useKB enable the USB keyboard reporting interface
      */
     USBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release, 
-        int waitForConnect, bool enableJoystick, bool useKB)
+        int waitForConnect, bool enableJoystick, int axisFormat, bool useKB)
         : USBHID(16, 64, vendor_id, product_id, product_release, false)
     { 
         _init();
         this->useKB = useKB;
         this->enableJoystick = enableJoystick;
+        this->axisFormat = axisFormat;
         connect(waitForConnect);
     };
 
@@ -362,15 +370,31 @@
     // Incoming LedWiz message buffer.  Each LedWiz message is exactly 8 bytes.
     CircBuf<LedWizMsg, 16> lwbuf;
      
+    // enable the joystick interface
     bool enableJoystick;
+    
+    // joystick axis reporting format
+    bool axisFormat;
+    
+    // enable the keyboard interface for button inputs
     bool useKB;
+    
+    // keyboard maximum idle time between reports
     uint8_t kbIdleTime;
+    
+    // media maximum idle time between reports
     uint8_t mediaIdleTime;
+    
+    // current X, Y, Z axis values
     int16_t _x;                       
     int16_t _y;     
     int16_t _z;
+    
+    // joystick button status bits
     uint16_t _buttonsLo;
     uint16_t _buttonsHi;
+    
+    // special status flag bits
     uint16_t _status;
 
     void _init();                 
--- a/USBProtocol.h	Fri May 12 17:57:59 2017 +0000
+++ b/USBProtocol.h	Tue Oct 17 22:27:48 2017 +0000
@@ -809,9 +809,10 @@
 //
 //         byte 3 -> unit number, from 1 to 16
 //
-// 3  -> Enable/disable joystick reports.  
+// 3  -> Joystick report settings.
 //
-//         byte 2 -> 1 to enable, 0 to disable
+//         byte 2 -> Enable joystick interface: 1 to enable, 0 to disable
+//         byte 3 -> Joystick axis format, as a USBJoystick::AXIS_FORMAT_XXX value
 //
 //       When joystick reports are disabled, the device registers as a generic HID 
 //       device, and only sends the private report types used by the Windows config 
--- a/VL6180X/VL6180X.h	Fri May 12 17:57:59 2017 +0000
+++ b/VL6180X/VL6180X.h	Tue Oct 17 22:27:48 2017 +0000
@@ -124,11 +124,14 @@
     // enable the internal pullup resistors.  Set this to false if you're
     // using your own external pullup resistors on the lines.  External
     // pullups are better if you're attaching more than one device to the
-    // same I2C bus; the internal pullups are fine for a single device.
+    // same physical I2C bus; the internal pullups are fine if there's only
+    // one I2C device (in this case the VL6180X) connected to these pins.
     //
-    // Note that the power-on default I2C address is always 0x29.  The
-    // address can be changed during a session, but it's not saved 
-    // persistently; it always resets to 0x29 on the next power cycle.
+    // Note that VL6180X's I2C address is always 0x29 at power-on.  The
+    // address can be changed during a session, but there's no way to save
+    // the value persistently on the VL6180X, so it always resets to 0x29 
+    // on the next power cycle.  As a result, I see little reason to ever
+    // change it during a session.
     VL6180X(PinName sda, PinName scl, uint8_t addr, PinName gpio0,
         bool internalPullups);
     
--- a/cfgVarMsgMap.h	Fri May 12 17:57:59 2017 +0000
+++ b/cfgVarMsgMap.h	Tue Oct 17 22:27:48 2017 +0000
@@ -69,6 +69,7 @@
     case 3:
         // Enable/disable joystick
         v_byte(joystickEnabled, 2);
+        v_byte(joystickAxisFormat, 3);
         break;
         
     case 4:
--- a/config.h	Fri May 12 17:57:59 2017 +0000
+++ b/config.h	Tue Oct 17 22:27:48 2017 +0000
@@ -26,6 +26,8 @@
 #ifndef CONFIG_H
 #define CONFIG_H
 
+#include "USBJoystick.h"
+
 
 // TEST SETTINGS - FOR DEBUGGING PURPOSES ONLY.  The macros below select
 // special option combinations for debugging purposes.
@@ -255,6 +257,9 @@
         // enable joystick reports
         joystickEnabled = true;
         
+        // use the XYZ axis format
+        joystickAxisFormat = USBJoystick::AXIS_FORMAT_XYZ;
+        
         // assume standard orientation, with USB ports toward front of cabinet
         accel.orientation = OrientationFront;
         
@@ -570,6 +575,9 @@
     // use the device as purely an output controller.
     uint8_t joystickEnabled;
     
+    // Joystick axis report format, as a USBJoystick::AXIS_FORMAT_xxx value.
+    uint8_t joystickAxisFormat;
+    
     // Timeout for rebooting the KL25Z when the connection is lost.  On some
     // hosts, the mbed USB stack has problems reconnecting after an initial
     // connection is dropped.  As a workaround, we can automatically reboot
--- a/main.cpp	Fri May 12 17:57:59 2017 +0000
+++ b/main.cpp	Tue Oct 17 22:27:48 2017 +0000
@@ -3361,8 +3361,8 @@
 {
 public:
     MyUSBJoystick(uint16_t vendor_id, uint16_t product_id, uint16_t product_release,
-        bool waitForConnect, bool enableJoystick, bool useKB) 
-        : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, useKB)
+        bool waitForConnect, bool enableJoystick, int axisFormat, bool useKB) 
+        : USBJoystick(vendor_id, product_id, product_release, waitForConnect, enableJoystick, axisFormat, useKB)
     {
         sleeping_ = false;
         reconnectPending_ = false;
@@ -6065,7 +6065,7 @@
     // whether or not we need to present a USB keyboard interface in addition
     // to the joystick interface.
     MyUSBJoystick js(cfg.usbVendorID, cfg.usbProductID, USB_VERSION_NO, false, 
-        cfg.joystickEnabled, kbKeys);
+        cfg.joystickEnabled, cfg.joystickAxisFormat, kbKeys);
         
     // Wait for the USB connection to start up.  Show a distinctive diagnostic
     // flash pattern while waiting.