2

With a little project in hands I thought it would be a good excuse to learn python. With the gamepad I have here (Logitech F310), the values of axis X and axis Y for the joysticks vary between 0-255, with 127 or 128 when they are "idle" at the center.

With this code (from http://www.lafavre.us/robotics/IoT_LogitechF310.pdf)

from evdev import InputDevice, categorize, ecodes, KeyEvent 
    gamepad = InputDevice('/dev/input/event3') 
for event in gamepad.read_loop(): 
    if event.type == ecodes.EV_ABS: 
        absevent = categorize(event) 
        if ecodes.bytype[absevent.event.type][absevent.event.code] == 'ABS_RZ':
            if absevent.event.value > 128: 
                print 'reverse' 
                print absevent.event.value 
            elif absevent.event.value < 127: 
                print 'forward' 
                print absevent.event.value 
       if ecodes.bytype[absevent.event.type][absevent.event.code] == 'ABS_Z': 
            if absevent.event.value > 128 : 
                print 'right' 
                print absevent.event.value 
            elif absevent.event.value < 127: 
                print 'left' 
                print absevent.event.value

I'm able to get the positions for up, down, right, left; what I've failed to accomplish so far is, how to retrieve the values of X and Y when the joystick it's in between the X axis and the Y axis, which are narrow intervals (4 to be precise).

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jim
  • 21
  • 1
  • 3

2 Answers2

3

Each axis is reported separately, so you'll need to keep current state in some variables.

from evdev import InputDevice, categorize, ecodes, KeyEvent 
    gamepad = InputDevice('/dev/input/event3') 
    last = {
        "ABS_RZ": 128,
        "ABS_Z": 128
    }
for event in gamepad.read_loop(): 
    if event.type == ecodes.EV_ABS: 
        absevent = categorize(event) 
        if ecodes.bytype[absevent.event.type][absevent.event.code] == 'ABS_RZ':
            last["ABS_RZ"] = absevent.event.value

       if ecodes.bytype[absevent.event.type][absevent.event.code] == 'ABS_Z': 
            last["ABS_Z"] = absevent.event.value

        if last["ABS_RZ"] > 128: 
            print 'reverse' 
            print last["ABS_RZ"] 
        elif last["ABS_RZ"] < 127: 
            print 'forward' 
            print last["ABS_RZ"] 

        if last["ABS_Z"] > 128 : 
            print 'right' 
            print last["ABS_Z"] 
        elif last["ABS_Z"] < 127: 
            print 'left' 
            print last["ABS_Z"]
Nick
  • 742
  • 8
  • 14
0

By reading around the documentation a bit I found that you can use absinfo for retrieving a value from a certain joystick: https://python-evdev.readthedocs.io/en/latest/apidoc.html#evdev.device.AbsInfo

For example, to get the value of axes 0, 1, 2 and 5 (transformed between -1 and 1) I use this code:

device = evdev.InputDevice('/dev/input/event26')

def get_axis_value(i):
    return (device.absinfo(i).value - 128) / 128

def check_gamepad():
    [a, b, c, d] = map(get_axis_value, [0, 1, 2, 5])
Isti115
  • 2,418
  • 3
  • 29
  • 35