Initial for Condor Simulator

Dependents:   USBJoystick_2 USBJoystick_NEW

Fork of USBJoystick by Wim Huiskamp

Files at this revision

API Documentation at this revision

Comitter:
Cirrus01
Date:
Sat Sep 29 09:27:44 2018 +0000
Parent:
3:550cca870c98
Child:
5:fa0a30d0ef3d
Commit message:
Descriptor Bug fixes

Changed in this revision

USBJoystick.cpp Show annotated file Show diff for this revision Revisions of this file
USBJoystick.h Show annotated file Show diff for this revision Revisions of this file
--- a/USBJoystick.cpp	Sun Sep 16 09:56:23 2018 +0000
+++ b/USBJoystick.cpp	Sat Sep 29 09:27:44 2018 +0000
@@ -27,17 +27,17 @@
 #include "stdint.h"
 #include "USBJoystick.h"
 
-bool USBJoystick::update(int16_t t, int16_t r, int16_t d, int16_t w, int16_t x, int16_t y, uint32_t buttons, uint8_t hat)
+bool USBJoystick::update(uint16_t x, uint16_t y, uint16_t b, uint16_t f, uint16_t r, uint16_t t, uint8_t hat, uint32_t buttons)
 {
 
-    _t = t;
-    _r = r;
-    _d = d;
-    _w = w;
     _x = x;
     _y = y;
+    _b = b;
+    _f = f;
+    _r = r;
+    _t = t;
+    _hat = hat;
     _buttons = buttons;
-    _hat = hat;
 
     return update();
 }
@@ -48,99 +48,82 @@
     HID_REPORT report;
 
     // Fill the report according to the Joystick Descriptor
-    report.data[pos] = (_t >>  0) & 0xff;
-#if (SWORD == 1)
-    report.data[pos++] = (_t >>  8) & 0xff;
-#endif
-    report.data[pos++] = _r & 0xff;
-#if (SWORD == 1)
-    report.data[pos++] = (_r >>  8) & 0xff;
-#endif
-    report.data[pos++] = _d & 0xff;
-#if (SWORD == 1)
-    report.data[pos++] = (_d >>  8) & 0xff;
-#endif
-    report.data[pos++] = _w & 0xff;
-#if (SWORD == 1)
-    report.data[pos++] = (_w >>  8) & 0xff;
-#endif
-    report.data[pos++] = _x & 0xff;
-#if (SWORD == 1)
-    report.data[pos++] = (_x >>  8) & 0xff;
-#endif
-    report.data[pos++] = _y & 0xff;
-#if (SWORD == 1)
-    report.data[pos++] = (_y >>  8) & 0xff;
-#endif
+    report.data[pos] = (_x >>  0) & 0xff;
+
+    report.data[++pos] = (_x >>  8) & 0xff;
+    report.data[++pos] = _y & 0xff;
+    report.data[++pos] = (_y >>  8) & 0xff;
+    report.data[++pos] = _b & 0xff;
+    report.data[++pos] = (_b >>  8) & 0xff;
+    report.data[++pos] = _f & 0xff;
+    report.data[++pos] = (_f >>  8) & 0xff;
+    report.data[++pos] = _r & 0xff;
+    report.data[++pos] = (_r >>  8) & 0xff;
+    report.data[++pos] = _t & 0xff;
+    report.data[++pos] = (_t >>  8) & 0xff;
 
 #if (HAT4 == 1)
 //Use 4 bit padding for hat4 or hat8
-    report.data[pos++] = (_hat & 0x0f) ;
+    report.data[++pos] = (_hat & 0x0f) ;
 #endif
 
 #if (HAT8 == 1)
 //Use 4 bit padding for hat4 or hat8
-    report.data[pos++] = (_hat & 0xff) ;
+    report.data[++pos] = (_hat & 0x0f) ;
 #endif
 
 #if (BUTTONS4 == 1)
 //Use 4 bit padding for buttons
-    report.data[pos++] = (_buttons & 0x0f) ;
-    report.length = pos++;
+    report.data[++pos] = (_buttons & 0x0f) ;
+    report.length = ++pos;
 #endif
 
 #if (BUTTONS8 == 1)
 //Use 8 bits for buttons
-    report.data[pos++] = (_buttons & 0xff) ;
-    report.length = pos++;
+    report.data[++pos] = (_buttons & 0xff) ;
+    report.length = ++pos;
 #endif
 
 #if (BUTTONS32 == 1)
 //No bit padding for 32 buttons
-    report.data[pos++] = (_buttons >>  0) & 0xff;
-    report.data[pos++] = (_buttons >>  8) & 0xff;
-    report.data[pos++] = (_buttons >> 16) & 0xff;
-    report.data[pos++] = (_buttons >> 24) & 0xff;
-    report.length = pos++;
+    report.data[++pos] = (_buttons >>  0) & 0xff;
+    report.data[++pos] = (_buttons >>  8) & 0xff;
+    report.data[++pos] = (_buttons >> 16) & 0xff;
+    report.data[++pos] = (_buttons >> 24) & 0xff;
+    report.length = ++pos;
 #endif
 
     return send(&report);
 }
 
-bool USBJoystick::throttle(int16_t t)
-{
-    _t = t;
-    return update();
-}
-
-bool USBJoystick::rudder(int16_t r)
-{
-    _r = r;
-    return update();
-}
-
-bool USBJoystick::diveBreak(int16_t d)
-{
-    _d = d;
-    return update();
-}
-
-bool USBJoystick::flaps(int16_t w)
-{
-    _w = w;
-    return update();
-}
-
-bool USBJoystick::move(int16_t x, int16_t y)
+bool USBJoystick::move(uint16_t x, uint16_t y)
 {
     _x = x;
     _y = y;
     return update();
 }
 
-bool USBJoystick::buttons(uint32_t buttons)
+bool USBJoystick::diveBreak(uint16_t b)
+{
+    _b = b;
+    return update();
+}
+
+bool USBJoystick::flaps(uint16_t f)
 {
-    _buttons = buttons;
+    _f = f;
+    return update();
+}
+
+bool USBJoystick::rudder(uint16_t r)
+{
+    _r = r;
+    return update();
+}
+
+bool USBJoystick::throttle(uint16_t t)
+{
+    _t = t;
     return update();
 }
 
@@ -150,17 +133,24 @@
     return update();
 }
 
+bool USBJoystick::buttons(uint32_t buttons)
+{
+    _buttons = buttons;
+    return update();
+}
+
+
 
 void USBJoystick::_init()
 {
-    _t = -127;
-    _r = -127;
-    _d = -127;
-    _w = -127;
-    _x = 0;
-    _y = 0;
-    _buttons = 0x00000000;
-    _hat = 0x00;
+    _x = 0;                                     // X-Axis
+    _y = 0;                                     // Y-Axis
+    _b = 0;                                     // Breaks
+    _f = 0;                                     // Wing Flaps
+    _r = 0;                                     // Rudder
+    _t = 0;                                     // Throttle
+    _hat = 0x00;                                // POV Hat-Switches
+    _buttons = 0x00000000;                      // Buttons
 }
 
 
@@ -168,135 +158,107 @@
 {
     static uint8_t reportDescriptor[] = {
 
-        USAGE_PAGE(1), 0x01,           // Generic Desktop
-        LOGICAL_MINIMUM(1), 0x00,      // Logical_Minimum (0)
-        USAGE(1), 0x04,                // Usage (Joystick)
-        COLLECTION(1), 0x01,           // Application
-        USAGE_PAGE(1), 0x02,            // Simulation Controls
-        USAGE(1), 0xBB,                 // Throttle
-        USAGE(1), 0xBA,                 // Rudder
-        LOGICAL_MINIMUM(1), 0x81,       // -127
-        LOGICAL_MAXIMUM(1), 0x7f,       // 127
-        REPORT_SIZE(1), 0x08,
-        REPORT_COUNT(1), 0x02,
-        INPUT(1), 0x02,                 // Data, Variable, Absolute
-        USAGE(1), 0xB6,                 // Dive Breaks
-        USAGE(1), 0xC3,                 // Wing Flaps 
-//  8 bit values
-#if (SBYTE == 1)
-        LOGICAL_MINIMUM(1), 0x81,       // -127
-        LOGICAL_MAXIMUM(1), 0x7f,       // 127
-        REPORT_SIZE(1), 0x08,
-        REPORT_COUNT(1), 0x02,
-        INPUT(1), 0x02,                 // Data, Variable, Absolute
-#endif
-// 16 bit values
-#if (SWORD ==1)
-        LOGICAL_MINIMUM(2), 0x80, 0x01  // -32767
-        LOGICAL_MAXIMUM(2), 0x7f, 0xff, // 32767
-        REPORT_SIZE(1), 0x10,
-        REPORT_COUNT(1), 0x04,
-        INPUT(1), 0x02,                 // Data, Variable, Absolute        
-#endif
-        USAGE_PAGE(1), 0x01,            // Generic Desktop
-        USAGE(1), 0x01,                 // Usage (Pointer)
-        COLLECTION(1), 0x00,            // Physical
-        USAGE(1), 0x30,                 // X
-        USAGE(1), 0x31,                 // Y
-//  8 bit values
-#if (SBYTE == 1)
-        LOGICAL_MINIMUM(1), 0x81,       // -127
-        LOGICAL_MAXIMUM(1), 0x7f,       // 127
-        REPORT_SIZE(1), 0x08,
-        REPORT_COUNT(1), 0x02,
-        INPUT(1), 0x02,                 // Data, Variable, Absolute
-#endif
-// 16 bit values
-#if (SWORD ==1)
-        LOGICAL_MINIMUM(2), 0x80, 0x01  // -32767
-        LOGICAL_MAXIMUM(2), 0x7f, 0xff, // 32767
-        REPORT_SIZE(1), 0x10,
-        REPORT_COUNT(1), 0x04,
-        INPUT(1), 0x02,                 // Data, Variable, Absolute        
-#endif
+        USAGE_PAGE(1), 0x01,                    // Generic Desktop
+        USAGE(1), 0x04,                         // Usage (Joystick)
+        COLLECTION(1), 0x01,                    // Application
+        
+//******* Joystick Axis and Sliders *******
+            USAGE_PAGE(1), 0x01,                // Generic Desktop
+            USAGE(1), 0x01,                     // Usage (Pointer) - (Joystick = 0x04)
+            COLLECTION(1), 0x00,                // Physical                                     Usage in main.cpp
+                USAGE(1), 0x30,                 // X                                            X
+                USAGE(1), 0x31,                 // Y                                            Y
+                USAGE(1), 0x32,                 // Z                                            Breaks
+                USAGE(1), 0x33,                 // Rx                                           Flaps
+//                USAGE(1), 0x34,               // Ry
+//                USAGE(1), 0x35,               // Rz (Rudder on Usage Page 0x02)
+//                USAGE(1), 0x36                // Slider (Throttle on Usage Page 0x02)
+                LOGICAL_MINIMUM(1), 0x00,       // 0
+                LOGICAL_MAXIMUM(2), 0xff, 0xff, // 65536
+                REPORT_SIZE(1), 0x10,
+                REPORT_COUNT(1), 0x04,
+                INPUT(1), 0x02,                 // Data, Variable, Absolute        
+            END_COLLECTION(0),
 
-        END_COLLECTION(0),
+            USAGE_PAGE(1), 0x02,                // Simulation Controls
+            USAGE(1), 0xBA,                     // Usage (Rudder)
+            USAGE(1), 0xBB,                     // Usage (Throttle)
+            LOGICAL_MINIMUM(1), 0x00,           // 0
+            LOGICAL_MAXIMUM(2), 0xff, 0xff,     // 65536
+            REPORT_SIZE(1), 0x10,
+            REPORT_COUNT(1), 0x02,
+            INPUT(1), 0x02,                     // Data, Variable, Absolute
 
+//******* Hat Switches *******
+            USAGE_PAGE(1), 0x01,                // Generic Desktop
+            USAGE(1), 0x39,                     // Usage (Hat switch)
 #if (HAT4 == 1 && HAT4_8 == 0)
 // 4 Position Hat Switch
-        USAGE(1), 0x39,                 // Usage (Hat switch)
-        LOGICAL_MINIMUM(1), 0x00,       // 0
-        LOGICAL_MAXIMUM(1), 0x03,       // 3
-        PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
-        PHYSICAL_MAXIMUM(2), 0x0E, 0x01, // Physical_Maximum (270)
-        UNIT_EXPONENT(1), 0x00,         // Unit Exponent (0)
-        UNIT(1), 0x14,                  // Unit (Degrees)
-        REPORT_SIZE(1), 0x04,
-        REPORT_COUNT(1), 0x01,
-        INPUT(1), 0x42,                 // Data, Variable, Absolute, Null State
-// Padding 4 bits
-        REPORT_SIZE(1), 0x01,
-        REPORT_COUNT(1), 0x04,
-        INPUT(1), 0x01,                 // Constant
+            LOGICAL_MINIMUM(1), 0x00,           // 0
+            LOGICAL_MAXIMUM(1), 0x03,           // 3
+            PHYSICAL_MINIMUM(1), 0x00,          // Physical_Minimum (0)
+            PHYSICAL_MAXIMUM(2), 0x0E, 0x01,    // Physical_Maximum (270)
+            //UNIT_EXPONENT(1), 0x00,           // Unit Exponent (0)
+            UNIT(1), 0x14,                      // Unit (Degrees)
 #endif
 #if (HAT8 == 1) || (HAT4 == 1 && HAT4_8 == 1)
 // 8 Position Hat Switch
-        USAGE(1), 0x39,                 // Usage (Hat switch)
-        LOGICAL_MINIMUM(1), 0x00,       // 0
-        LOGICAL_MAXIMUM(1), 0x07,       // 7
-        PHYSICAL_MINIMUM(1), 0x00,      // Physical_Minimum (0)
-        PHYSICAL_MAXIMUM(2), 0x3B, 0x01, // Physical_Maximum (315)
-        UNIT_EXPONENT(1), 0x00,         // Unit Exponent (0)
-        UNIT(1), 0x14,                  // Unit (Degrees)
-        REPORT_SIZE(1), 0x08,
-        REPORT_COUNT(1), 0x01,
-        INPUT(1), 0x42,                 // Data, Variable, Absolute, Null State
+            LOGICAL_MINIMUM(1), 0x00,          // 0
+            LOGICAL_MAXIMUM(1), 0x07,          // 7
+            PHYSICAL_MINIMUM(1), 0x00,         // Physical_Minimum (0)
+            PHYSICAL_MAXIMUM(2), 0x3B, 0x01,   // Physical_Maximum (315)
+            //UNIT_EXPONENT(1), 0x00,          // Unit Exponent (0)
+            UNIT(1), 0x14,                     // Unit (Degrees)
 #endif
-
+            REPORT_SIZE(1), 0x04,
+            REPORT_COUNT(1), 0x01,
+            INPUT(1), 0x42,                    // Data, Variable, Absolute, Null State
+// Padding 4 bits
+            REPORT_SIZE(1), 0x01,
+            REPORT_COUNT(1), 0x04,
+            INPUT(1), 0x01,                    // Constant
+            
+//******* Buttons *******
+            USAGE_PAGE(1), 0x09,               // Buttons
 #if (BUTTONS4 == 1)
 // 4 Buttons
-        USAGE_PAGE(1), 0x09,            // Buttons
-        USAGE_MINIMUM(1), 0x01,         // 1
-        USAGE_MAXIMUM(1), 0x04,         // 4
-        LOGICAL_MINIMUM(1), 0x00,       // 0
-        LOGICAL_MAXIMUM(1), 0x01,       // 1
-        REPORT_SIZE(1), 0x01,
-        REPORT_COUNT(1), 0x04,
-        UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
-        UNIT(1), 0x00,                  // Unit (None)
-        INPUT(1), 0x02,                 // Data, Variable, Absolute
-
+            USAGE_MINIMUM(1), 0x01,            // 1
+            USAGE_MAXIMUM(1), 0x04,            // 4
+            LOGICAL_MINIMUM(1), 0x00,          // 0
+            LOGICAL_MAXIMUM(1), 0x01,          // 1
+            REPORT_SIZE(1), 0x01,
+            REPORT_COUNT(1), 0x04,
+            //UNIT_EXPONENT(1), 0x00,       // Unit_Exponent (0)
+            UNIT(1), 0x00,                  // Unit (None)
+            INPUT(1), 0x02,                 // Data, Variable, Absolute
 // Padding 4 bits
-        REPORT_SIZE(1), 0x01,
-        REPORT_COUNT(1), 0x04,
-        INPUT(1), 0x01,                 // Constant
-
+            REPORT_SIZE(1), 0x01,
+            REPORT_COUNT(1), 0x04,
+            INPUT(1), 0x01,                 // Constant
 #endif
 #if (BUTTONS8 == 1)
 // 8 Buttons
-        USAGE_PAGE(1), 0x09,            // Buttons
-        USAGE_MINIMUM(1), 0x01,         // 1
-        USAGE_MAXIMUM(1), 0x08,         // 8
-        LOGICAL_MINIMUM(1), 0x00,       // 0
-        LOGICAL_MAXIMUM(1), 0x01,       // 1
-        REPORT_SIZE(1), 0x01,
-        REPORT_COUNT(1), 0x08,
-        UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
-        UNIT(1), 0x00,                  // Unit (None)
-        INPUT(1), 0x02,                 // Data, Variable, Absolute
+            USAGE_MINIMUM(1), 0x01,         // 1
+            USAGE_MAXIMUM(1), 0x08,         // 8
+            LOGICAL_MINIMUM(1), 0x00,       // 0
+            LOGICAL_MAXIMUM(1), 0x01,       // 1
+            REPORT_SIZE(1), 0x01,
+            REPORT_COUNT(1), 0x08,
+            //UNIT_EXPONENT(1), 0x00,       // Unit_Exponent (0)
+            UNIT(1), 0x00,                  // Unit (None)
+            INPUT(1), 0x02,                 // Data, Variable, Absolute
 #endif
 #if (BUTTONS32 == 1)
 // 32 Buttons
-        USAGE_PAGE(1), 0x09,            // Buttons
-        USAGE_MINIMUM(1), 0x01,         // 1
-        USAGE_MAXIMUM(1), 0x20,         // 32
-        LOGICAL_MINIMUM(1), 0x00,       // 0
-        LOGICAL_MAXIMUM(1), 0x01,       // 1
-        REPORT_SIZE(1), 0x01,
-        REPORT_COUNT(1), 0x20,
-        UNIT_EXPONENT(1), 0x00,         // Unit_Exponent (0)
-        UNIT(1), 0x00,                  // Unit (None)
-        INPUT(1), 0x02,                 // Data, Variable, Absolute
+            USAGE_MINIMUM(1), 0x01,         // 1
+            USAGE_MAXIMUM(1), 0x20,         // 32
+            LOGICAL_MINIMUM(1), 0x00,       // 0
+            LOGICAL_MAXIMUM(1), 0x01,       // 1
+            REPORT_SIZE(1), 0x01,
+            REPORT_COUNT(1), 0x20,
+            //UNIT_EXPONENT(1), 0x00,       // Unit_Exponent (0)
+            UNIT(1), 0x00,                  // Unit (None)
+            INPUT(1), 0x02,                 // Data, Variable, Absolute
 #endif
 
         END_COLLECTION(0)
@@ -305,3 +267,4 @@
     reportLength = sizeof(reportDescriptor);
     return reportDescriptor;
 }
+
--- a/USBJoystick.h	Sun Sep 16 09:56:23 2018 +0000
+++ b/USBJoystick.h	Sat Sep 29 09:27:44 2018 +0000
@@ -31,8 +31,10 @@
 #define REPORT_ID_JOYSTICK  4
 
 //Configure Joystick Slider Resolution
-#define SBYTE      1                // 1 Byte, 8 Bit Resolution
-#define SWORD      0                // 2 Byte, 16 Bit Resolution
+/*
+#define SBYTE      0                // 1 Byte, 8 Bit Resolution
+#define SWORD      1                // 2 Byte, 16 Bit Resolution
+*/
 //Configure Joystick Hat Buttons
 #define HAT4      1                 // 4 Hat Buttons; 4 Positions if HAT4_8 = 0
 #define HAT4_8    1                 // 4 Hat Buttons giving 8 Positions if 1; DO NOT USE WITH HAT8!
@@ -75,16 +77,17 @@
 };
 #endif
 
-/* X, Y and T limits */
+/* Limits */
 /* These values do not directly map to screen pixels */
 /* Zero may be interpreted as meaning 'no movement' */
-#define JX_MIN_ABS    (-127)     /*!< The maximum value that we can move to the left on the x-axis */
-#define JY_MIN_ABS    (-127)     /*!< The maximum value that we can move up on the y-axis */
-#define JT_MIN_ABS    (-127)     /*!< The minimum value for the throttle */
-#define JX_MAX_ABS    (127)      /*!< The maximum value that we can move to the right on the x-axis */
-#define JY_MAX_ABS    (127)      /*!< The maximum value that we can move down on the y-axis */
-#define JT_MAX_ABS    (127)      /*!< The maximum value for the throttle */
-
+#if (SBYTE == 1)
+    #define MIN_ABS    (-127)     /*!< The maximum value */
+    #define MAX_ABS    (127)      /*!< The maximum value */
+#endif
+#if (SWORD == 1)
+    #define MIN_ABS    (-32767)     /*!< The maximum value */
+    #define MAX_ABS    (32767)      /*!< The maximum value */
+#endif
 /**
  *
  * USBJoystick example
@@ -166,15 +169,17 @@
     /**
       * Write state of the joystick
       *
-      * @param t throttle position
-      * @param r rudder position
       * @param x x-axis position
       * @param y y-axis position
+      * @param b break position
+      * @param f flaps position
+      * @param r rudder position
+      * @param t throttle position
+      * @param hat hat state 0 (up), 1 (up right), 2 (right), 3 (down right), 4 (down), 5 (down left), 6 (left), 7 (up left) or 8 (neutral)
       * @param buttons buttons state
-      * @param hat hat state 0 (up), 1 (right, 2 (down), 3 (left) or 4 (neutral)
       * @returns true if there is no error, false otherwise
       */
-    bool update(int16_t t, int16_t r, int16_t d, int16_t w, int16_t x, int16_t y, uint32_t buttons, uint8_t hat);
+    bool update(uint16_t x, uint16_t y, uint16_t b, uint16_t f, uint16_t r, uint16_t t, uint8_t hat, uint32_t buttons);
 
     /**
       * Write state of the joystick
@@ -189,7 +194,7 @@
       * @param t throttle position
       * @returns true if there is no error, false otherwise
       */
-    bool throttle(int16_t t);
+    bool throttle(uint16_t t);
 
     /**
       * Move the rudder position
@@ -197,23 +202,23 @@
       * @param r rudder position
       * @returns true if there is no error, false otherwise
       */
-    bool rudder(int16_t r);
+    bool rudder(uint16_t r);
 
     /**
       * Move the dive break position
       *
-      * @param d dive break position
+      * @param b  break position
       * @returns true if there is no error, false otherwise
       */
-    bool diveBreak(int16_t d);
+    bool diveBreak(uint16_t b);
     
      /**
        * Move the flaps position
        *
-       * @param w wing flaps position
+       * @param f flaps position
        * @returns true if there is no error, false otherwise
        */        
-     bool flaps(int16_t w);         
+     bool flaps(uint16_t f);         
 
 
     /**
@@ -223,7 +228,7 @@
       * @param y-axis position
       * @returns true if there is no error, false otherwise
       */
-    bool move(int16_t x, int16_t y);
+    bool move(uint16_t x, uint16_t y);
 
     /**
       * Press one or several buttons
@@ -249,14 +254,14 @@
     virtual uint8_t * reportDesc();
 
 private:
-    int16_t _t;
-    int16_t _r;
-    int16_t _d;
-    int16_t _w;
-    int16_t _x;
-    int16_t _y;
+    uint16_t _x;
+    uint16_t _y;
+    uint16_t _b;
+    uint16_t _f;
+    uint16_t _r;
+    uint16_t _t;
+    uint8_t _hat;
     uint32_t _buttons;
-    uint8_t _hat;
 
     void _init();
 };