From: Joseph Coffland Date: Mon, 8 Mar 2021 03:26:50 +0000 (-0800) Subject: Better gamepad handling X-Git-Url: https://git.buildbotics.com/?a=commitdiff_plain;h=f45ecd7f6ce9ab4b6cca4af270560bff36f4cf70;p=bbctrl-firmware Better gamepad handling --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eccdb2..b3dc454 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/src/py/bbctrl/Jog.py b/src/py/bbctrl/Jog.py index 5129b99..4c97967 100644 --- a/src/py/bbctrl/Jog.py +++ b/src/py/bbctrl/Jog.py @@ -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() diff --git a/src/py/inevent/JogHandler.py b/src/py/inevent/JogHandler.py index dd0ee85..909b5d2 100644 --- a/src/py/inevent/JogHandler.py +++ b/src/py/inevent/JogHandler.py @@ -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()