Skip to content

Commit

Permalink
Merge pull request #404 from jpreiss/joystick-id
Browse files Browse the repository at this point in the history
do not assume that joystick 0 must exist
  • Loading branch information
whoenig authored Jun 24, 2021
2 parents 7c2aaeb + c23c1d2 commit dfa154a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
24 changes: 13 additions & 11 deletions ros_ws/src/crazyswarm/scripts/pycrazyswarm/genericJoystick.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ def __init__(self, timeHelper):
# self.buttonWasPressed = False
# joystick.push_handlers(self)
self.timeHelper = timeHelper
self.hasJoystick = False
self.joyID = None

try:
from . import linuxjsdev
self.js = linuxjsdev.Joystick()
dummy = self.js.devices()
if len(dummy) == 0:
devices = self.js.devices()
if len(devices) == 0:
print("Warning: No joystick found!")
else:
self.hasJoystick = True
self.js.open(0)
ids = [dev["id"] for dev in devices]
# For backwards compatibility, always choose device 0 if available.
self.joyID = 0 if 0 in ids else devices[0]["id"]
self.js.open(self.joyID)
except ImportError:
print("Warning: Joystick only supported on Linux.")

Expand All @@ -54,14 +56,14 @@ def __init__(self, timeHelper):
# self.buttonWasPressed = False

def checkIfButtonIsPressed(self):
if self.hasJoystick:
state = self.js.read(0)
if self.joyID is not None:
state = self.js.read(self.joyID)
return state[1][5] == 1
else:
return False

def waitUntilButtonPressed(self):
if self.hasJoystick:
if self.joyID is not None:
while not self.checkIfButtonIsPressed():
self.timeHelper.sleep(0.01)
while self.checkIfButtonIsPressed():
Expand All @@ -77,8 +79,8 @@ def waitUntilButtonPressed(self):


def checkIfAnyButtonIsPressed(self):
if self.hasJoystick:
state = self.js.read(0)
if self.joyID is not None:
state = self.js.read(self.joyID)
if state[1][5] == 1 or state[1][4] == 1 or state[1][3] == 1:
return state[1]
else:
Expand All @@ -87,7 +89,7 @@ def checkIfAnyButtonIsPressed(self):
return None

def waitUntilAnyButtonPressed(self):
if self.hasJoystick:
if self.joyID is not None:
buttons = self.checkIfAnyButtonIsPressed()
while buttons is None:
self.timeHelper.sleep(0.01)
Expand Down
6 changes: 2 additions & 4 deletions ros_ws/src/crazyswarm/scripts/pycrazyswarm/linuxjsdev.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,9 @@ def __init__(self):

def devices(self):
"""
Returns a dict with device_id as key and device name as value of all
the detected devices (result is cached once one or more device are
found).
Returns a list containing an {"id": id, "name": name} dict for each
detected device. Result is cached once one or more devices are found.
"""

if len(self._devices) == 0:
syspaths = glob.glob("/sys/class/input/js*")

Expand Down

0 comments on commit dfa154a

Please sign in to comment.