Demo for USBJoystick updated for 32 buttons.

Dependencies:   USBDevice USBJoystick_SIM mbed USBJoystick_2

Dependents:   USBJoystick_2

Fork of USBJoystick_HelloWorld2 by Wim Huiskamp

Revision:
2:967da2faedcd
Parent:
1:b106cf2e99ba
Child:
3:3ddaf1227e1b
diff -r b106cf2e99ba -r 967da2faedcd main.cpp
--- a/main.cpp	Sun Jul 22 10:36:35 2018 +0000
+++ b/main.cpp	Sat Sep 15 07:47:00 2018 +0000
@@ -27,9 +27,6 @@
 #include "config.h"
 #include "USBJoystick.h"
 
-//#define LANDTIGER 1
-
-//USBMouse mouse;
 USBJoystick joystick;
 
 // Variables for Heartbeat and Status monitoring
@@ -205,7 +202,7 @@
     // figure the time elapsed since the last scan
     int dt = hatTimer.read_ms();
     
-    // reset the timef for the next scan
+    // reset the time for the next scan
     hatTimer.reset();
     
     // scan the button list
@@ -253,6 +250,60 @@
         }
     }
     
+    // translate values read to descriptor values
+#if HAT4 && !HAT4_8
+    if (hat == 0x01)
+        hat = JOY_HAT_UP;
+    else if (hat == 0x02)
+        hat = JOY_HAT_RIGHT;
+    else if (hat == 0x04)
+        hat = JOY_HAT_DOWN;
+    else if (hat == 0x08)
+        hat = JOY_HAT_LEFT;
+    else
+        hat = JOY_HAT_NEUTRAL;
+#endif
+#if HAT4 && HAT4_8
+    if (hat == 0x01)
+        hat = JOY_HAT_UP;
+    else if (hat == 0x02)
+        hat = JOY_HAT_RIGHT;
+    else if (hat == 0x03)
+        hat = JOY_HAT_UP_RIGHT;
+    else if (hat == 0x04)
+        hat = JOY_HAT_DOWN;
+    else if (hat == 0x05)
+        hat = JOY_HAT_DOWN_RIGHT;
+    else if (hat == 0x08)
+        hat = JOY_HAT_LEFT;
+    else if (hat == 0x09)
+        hat = JOY_HAT_UP_LEFT;
+    else if (hat == 0x0C)
+        hat = JOY_HAT_DOWN_LEFT;
+    else
+        hat = JOY_HAT_NEUTRAL;
+#endif
+#if HAT8
+    if (hat == 0x01)
+        hat = JOY_HAT_UP;
+    else if (hat == 0x02)
+        hat = JOY_HAT_UP_RIGHT;
+    else if (hat == 0x04)
+        hat = JOY_HAT_RIGHT;
+    else if (hat == 0x08)
+        hat = JOY_HAT_DOWN_RIGHT;
+    else if (hat == 0x10)
+        hat = JOY_HAT_DOWN;
+    else if (hat == 0x20)
+        hat = JOY_HAT_DOWN_LEFT;
+    else if (hat == 0x40)
+        hat = JOY_HAT_LEFT;
+    else if (hat == 0x80)
+        hat = JOY_HAT_UP_LEFT;
+    else
+        hat = JOY_HAT_NEUTRAL;
+#endif
+    
     // return the new button list
     //pc.printf("Hat: %d", hat);
     return hat;
@@ -273,6 +324,15 @@
     //uint32_t tmp = 1;
     uint32_t buttons = 0;
     uint8_t hat = 0;
+    
+#if (SBYTE == 1)
+    const int16_t l = 127;
+    const int16_t m = 256;
+#endif
+#if (SWORD == 1)
+    const int16_t l = 32767;
+    const int16_t m = 65535;
+#endif
 
     pc.printf("Hello World from Joystick!\n\r");
 
@@ -322,38 +382,49 @@
         buttons = readButtons();
         hat = readHat();
         //pc.printf("Hat: %d\n", hat);
+
         
-        throttle = inThrottle.read() * 256 - 128;
-        if(throttle < -127)
-            throttle = -127;
-        if(throttle > 127)
-            throttle = 127;
-        rudder = inRudder.read() * 256 - 128;
-        if(rudder < -127)
-            rudder = -127;
-        if(rudder > 127)
-            rudder = 127;
-        breaks = inBreaks.read() * 256 - 127;
-        if(breaks < -127)
-            breaks = -127;
-        if(breaks > 127)
-            breaks = 127;
-        flaps = inFlaps.read() * 256 - 127;
-        if(flaps < -127)
-            flaps = -127;
-        if(flaps > 127)
-            flaps = 127;
+        throttle = inThrottle.read() * m - l;
+        if(throttle < -l)
+            throttle = -l;
+        if(throttle > l)
+            throttle = l;
+        rudder = inRudder.read() * m - l;
+        if(rudder < -l)
+            rudder = -l;
+        if(rudder > l)
+            rudder = l;
+        breaks = inBreaks.read() * m - l;
+        if(breaks < -l)
+            breaks = -l;
+        if(breaks > l)
+            breaks = l;
+        flaps = inFlaps.read() * m - l;
+        if(flaps < -l)
+            flaps = -l;
+        if(flaps > l)
+            flaps = l;
 
-        x = inX.read() * 256 - 127;
-        if(x < -127)
-            x = -127;
-        if(x > 127)
-            x = 127;
-        y = inY.read() * 254 - 127;
-        if(y < -127)
-            y = -127;
-        if(y < 127)
-            y = 127;
+        x = inX.read() * m - l;
+        if(x < -l)
+            x = -l;
+        if(x > l)
+            x = l;
+            
+        y = inY.read() * m - l;
+        if(y < -l)
+            y = -l;
+        if(y > l)
+            y = l;
+
+
+/*        x = int(((double)inX.read() - 0.5) * 254);
+        y = int(((double)inY.read() - 0.5) * 254);
+        flaps = int(((double)inFlaps.read() - 0.5) * 254);
+        breaks = int(((double)inBreaks.read() - 0.5) * 254);
+        rudder = int(((double)inRudder.read() - 0.5) * 254);
+        throttle = int(((double)inThrottle.read() - 0.5) * 254);
+*/
 
         joystick.update(throttle, rudder, breaks, flaps, x, y, buttons, hat);
         wait(0.01);