Better gamepad handling
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Mon, 8 Mar 2021 03:26:50 +0000 (19:26 -0800)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Mon, 8 Mar 2021 03:28:29 +0000 (19:28 -0800)
CHANGELOG.md
src/py/bbctrl/Jog.py
src/py/inevent/JogHandler.py

index 7eccdb27233e00a3cb1b09d0f010eede053b09c3..b3dc45492c2c77dcdb01b7b2528f3ff544ac3a49 100644 (file)
@@ -13,6 +13,7 @@ Buildbotics CNC Controller Firmware Changelog
  - Added macro buttons.
  - Fixed error setting lower soft limit with virtual keyboard installed. #249
  - Fix numerical overflow causing long (time) linear moves to fail. #258
+ - Better gamepad handling
 
 ## v0.4.16
  - Improved axis under/over warning tooltip.
index 5129b99a37f9a6490b64d99a14066203df715582..4c979671bf47326bc5a2f2c4dd1ae6529ec3b145 100644 (file)
@@ -33,22 +33,13 @@ type2 = [
     'Logitech Logitech RumblePad 2 USB'
 ]
 
-config1 = {
+config = {
     "deadband": 0.1,
-    "axes":     [ABS_X, ABS_Y, ABS_RY, ABS_RX],
-    "dir":      [1, -1, -1, 1],
-    "arrows":   [ABS_HAT0X, ABS_HAT0Y],
-    "speed":    [0x133, 0x130, 0x131, 0x134],
-    "lock":     [0x136, 0x137],
-}
-
-config2 = {
-    "deadband": 0.1,
-    "axes":     [ABS_X, ABS_Y, ABS_RZ, ABS_Z],
-    "dir":      [1, -1, -1, 1],
-    "arrows":   [ABS_HAT0X, ABS_HAT0Y],
-    "speed":    [0x120, 0x121, 0x122, 0x123],
-    "lock":     [0x124, 0x125],
+    "axes":   [[ABS_X], [ABS_Y], [ABS_RY, ABS_RZ], [ABS_RX, ABS_Z]],
+    "dir":    [1, -1, -1, 1],
+    "arrows": [[ABS_HAT0X], [ABS_HAT0Y]],
+    "speed":  [[0x133, 0x120], [0x130, 0x121], [0x131, 0x122], [0x134, 0x123]],
+    "lock":   [[0x136, 0x124], [0x137, 0x125]],
 }
 
 
@@ -69,12 +60,20 @@ class Jog(inevent.JogHandler):
 
 
     # From JobHandler
-    def get_config(self, name):
-        if name in type2: return config2
+    def match_code(self, type, code):
+        if not type in config: return None
+
+        index = 0
+        for codes in config[type]:
+            if code in codes: return index
+            index += 1
+
+
+    def get_config(self, type): return config[type]
+
 
-        # Microsoft X-Box 360 pad
-        # Logitech Gamepad F310
-        return config1
+    def has_code(self, type, code):
+        return self.match_code(type, code) is not None
 
 
     def up(self): self.ctrl.lcd.page_up()
index dd0ee85a510ae7f40ffbc1b05cdc9f43b9805a27..909b5d21278a9e608d062626f6d05f5ff7830aeb 100644 (file)
@@ -87,21 +87,37 @@ class JogHandler:
         self.changed()
 
 
-    def get_config(self, name): raise Exception('No configs')
+    def get_config(self, type): raise Exception('Not implemented')
+    def match_code(self, type, code): raise Exception('Not implemented')
+    def has_code(self, type, code): raise Exception('Not implemented')
 
 
     def event(self, event, state, dev_name):
         if event.type not in [EV_ABS, EV_REL, EV_KEY]: return
 
-        config = self.get_config(dev_name)
         changed = False
+        old_axes = list(self.axes)
 
         # Process event
-        if event.type == EV_ABS and event.code in config['axes']:
-            pass
+        if event.type == EV_ABS and self.has_code('axes', event.code):
+            axis = self.match_code('axes', event.code)
+
+            self.axes[axis] = event.stream.state.abs[event.code]
+            self.axes[axis] *= self.get_config('dir')[axis]
+
+            if abs(self.axes[axis]) < self.get_config('deadband'):
+                self.axes[axis] = 0
+
+            if self.horizontal_lock and axis not in [0, 3]:
+                self.axes[axis] = 0
+
+            if self.vertical_lock and axis not in [1, 2]:
+                self.axes[axis] = 0
+
+            if old_axes[axis] != self.axes[axis]: changed = True
 
-        elif event.type == EV_ABS and event.code in config['arrows']:
-            axis = config['arrows'].index(event.code)
+        elif event.type == EV_ABS and self.has_code('arrows', event.code):
+            axis = self.match_code('arrows', event.code)
 
             if event.value < 0:
                 if axis == 1: self.up()
@@ -111,13 +127,13 @@ class JogHandler:
                 if axis == 1: self.down()
                 else: self.right()
 
-        elif event.type == EV_KEY and event.code in config['speed']:
+        elif event.type == EV_KEY and self.has_code('speed', event.code):
             old_speed = self.speed
-            self.speed = config['speed'].index(event.code) + 1
+            self.speed = self.match_code('speed', event.code) + 1
             if self.speed != old_speed: changed = True
 
-        elif event.type == EV_KEY and event.code in config['lock']:
-            index = config['lock'].index(event.code)
+        elif event.type == EV_KEY and self.has_code('lock', event.code):
+            index = self.match_code('lock', event.code)
 
             self.horizontal_lock, self.vertical_lock = False, False
 
@@ -127,22 +143,4 @@ class JogHandler:
 
         self.log.debug(event_to_string(event, state))
 
-        # Update axes
-        old_axes = list(self.axes)
-
-        for axis in range(4):
-            self.axes[axis] = event.stream.state.abs[config['axes'][axis]]
-            self.axes[axis] *= config['dir'][axis]
-
-            if abs(self.axes[axis]) < config['deadband']:
-                self.axes[axis] = 0
-
-            if self.horizontal_lock and axis not in [0, 3]:
-                self.axes[axis] = 0
-
-            if self.vertical_lock and axis not in [1, 2]:
-                self.axes[axis] = 0
-
-        if old_axes != self.axes: changed = True
-
         if changed: self.changed()