I've tried to distill this to the essentials. This code works fine.
from evdev import InputDevice, ecodes
dev = InputDevice('/dev/input/event2') # this is the joystick event file
button = {304: 'A', 305: 'B', 307: 'X', 308: 'Y' } # Xbox360 button mappings
exit = False
def joystick(dev):
try:
for event in list(dev.read()):
if button[event.code] == 'A' and event.value == 1:
print('Button A pressed.')
if button[event.code] == 'X' and event.value == 1:
print('Button X pressed.')
return True
except: pass
return False
while exit == False:
exit = joystick(dev)
However, if I move the joystick function to a separate file (including the proper evdev import) and import it the code breaks (function "joystick" will always return false, not recognizing joystick events).
Any ideas why? I know the software/hardware interface can be tricky but this is ridiculous.