Controlling 2 & sensible!

pull/2/head
kaqu 2 years ago
parent 530c940962
commit 94c4d04b20
  1. 2
      .vscode/launch.json
  2. 178
      evalgamepad.py

@ -10,7 +10,7 @@
"request": "launch",
"program": "${file}",
//"args": ["/dev/input/event6"],
"args": ["/dev/input/event6"],
"args": ["/dev/input/event6", "/dev/input/event7"],
"console": "integratedTerminal"
}
]

@ -0,0 +1,178 @@
#!/usr/bin/env python3
#
# evalgamepad.py
# Reading two USB gamepads & making sense of it ...
#
#-----------------------------------------------------------------------
# 'lsusb' ouput: Bus 002 Device 003: ID 0079:0011 DragonRise Inc. Gamepad
# Bus 002 Device 002: ID 0079:0011 DragonRise Inc. Gamepad
#
# input devices: /dev/input/event6
# /dev/input/event7
#
import sys, os, fcntl
import libevdev
# Buttons in 'state<n>'
BTN_TRIGGER = 1 # [Blue/X]
BTN_THUMB = 2 # [Red/A]
BTN_THUMB2 = 4 # [Yellow/B]
BTN_TOP = 8 # [Green/Y]
BTN_TOP2 = 16 # [Frontal left]
BTN_PINKIE = 32 # [Frontal right]
BTN_BASE3 = 64 # [Centre left]
BTN_BASE4 = 128 # [Centre right]
def print_event(e):
print("Event: time {}.{:06d}, ".format(e.sec, e.usec), end='')
if e.matches(libevdev.EV_SYN):
if e.matches(libevdev.EV_SYN.SYN_MT_REPORT):
print("++++++++++++++ {} ++++++++++++".format(e.code.name))
elif e.matches(libevdev.EV_SYN.SYN_DROPPED):
print(">>>>>>>>>>>>>> {} >>>>>>>>>>>>".format(e.code.name))
else:
print("-------------- {} ------------".format(e.code.name))
else:
print("type {:02x} {} code {:03x} {:20s} value {:4d}".format(e.type.value, e.type.name, e.code.value, e.code.name, e.value))
def evalgamepad(dev, state, x, y):
bChanged = False
try:
for e in dev.events():
bChanged = True
#print_event(e)
# Event type 1 (EV_KEY)
# Event code 288 (BTN_TRIGGER) <- 1=[Blue/X]
# Event code 289 (BTN_THUMB) <- 1=[Red/A]
# Event code 290 (BTN_THUMB2) <- 1=[Yellow/B]
# Event code 291 (BTN_TOP) <- 1=[Green/Y]
# Event code 292 (BTN_TOP2) <- 1=[Frontal left]
# Event code 293 (BTN_PINKIE) <- 1=[Frontal right]
# Event code 294 (BTN_BASE)
# Event code 295 (BTN_BASE2)
# Event code 296 (BTN_BASE3) <- 1=[Centre left]
# Event code 297 (BTN_BASE4) <- 1=[Centre right]
if e.type.value == 1: # Non-Cross events
if e.value == 1: # Simple buttons may act as toggles
if e.code.value == 288: # BTN_TRIGGER
state = state ^ BTN_TRIGGER
elif e.code.value == 289: # BTN_THUMB
state = state ^ BTN_THUMB
elif e.code.value == 290: # BTN_THUMB2
state = state ^ BTN_THUMB2
elif e.code.value == 291: # BTN_TOP
state = state ^ BTN_TOP
elif e.code.value == 292: # BTN_TOP2
state = state ^ BTN_TOP2
elif e.code.value == 293: # BTN_PINKIE
state = state ^ BTN_PINKIE
elif e.code.value == 296: # BTN_BASE3
state = state ^ BTN_BASE3
elif e.code.value == 297: # BTN_BASE4
state = state ^ BTN_BASE4
# Event type 3 (EV_ABS)
# Event code 0 (ABS_X) <- Cross: 127=Neutral, 0=[Left], 255=[Right]
# Event code 1 (ABS_Y) <- Cross: 127=Neutral, 0=[Top], 255=[Down]
elif e.type.value == 3: # EV_ABS
if e.code.value == 0: # ABS_X
if e.value == 0:
x = x - 1
elif e.value == 255:
x = x + 1
else: # 127/Neutral
x = 0
else: # ABS_Y
if e.value == 0:
y = y + 1
elif e.value == 255:
y = y - 1
else: # 127/Neutral
y = 0
print("State=",state, "X=", x, "Y=", y)
except libevdev.EventsDroppedException:
print("Dropped!")
for e in dev.sync():
print_event(e)
return bChanged, state, x, y
def evalposition(sum_x, sum_y, delta_x, delta_y):
sum_x = sum_x + delta_x
sum_y = sum_y + delta_y
if sum_x < 0:
sum_x = 0
elif sum_x > 16384:
sum_x = 16384
if sum_y < 0:
sum_y = 0
elif sum_y > 8192:
sum_y = 8192
return sum_x, sum_y
def gameloop(dev1, dev2):
state1 = 0
state2 = 0
x1 = 0
x2 = 0
y1 = 0
y2 = 0
player1_x = 0
player2_x = 0
player1_y = 0
player2_y = 0
while True:
bChanged, state1, x1, y1 = evalgamepad(dev1, state1, x1, y1)
if (state1 & BTN_BASE4) > 0: # Exit?
print("Player #1 aborted game")
break
bChanged, state2, x2, y2 = evalgamepad(dev2, state2, x2, y2) # Exit?
if (state2 & BTN_BASE4) > 0:
print("Player #2 aborted game")
break
player1_x, player1_y = evalposition(player1_x, player1_y, x1, y1)
print("Player #1: ", player1_x," / ",player1_y)
player2_x, player2_y = evalposition(player2_x, player2_y, x2, y2)
print("Player #2: ", player2_x," / ",player2_y)
def main(args):
path1 = args[1]
path2 = args[2]
try:
# Gamepad #1 init.
fd1 = open(path1, "rb")
fcntl.fcntl(fd1, fcntl.F_SETFL, os.O_NONBLOCK)
dev1 = libevdev.Device(fd1)
# Gamepad #2 init.
fd2 = open(path2, "rb")
fcntl.fcntl(fd2, fcntl.F_SETFL, os.O_NONBLOCK)
dev2 = libevdev.Device(fd2)
# Work ...
gameloop(dev1, dev2)
# Quit gracefully
fd2.close()
fd1.close()
except KeyboardInterrupt: # Never comes ... (?!)
pass
except IOError as e:
import errno
if e.errno == errno.EACCES:
print("Insufficient permissions to access {} or {}".format(path1, path2))
elif e.errno == errno.ENOENT:
print("Device {} or {} does not exist".format(path1, path2))
else:
raise e
if __name__ == "__main__":
if len(sys.argv) < 3:
print("usage: ./evalgamepad.py /dev/input/event<n> /dev/input/event<m>")
sys.exit(1)
main(sys.argv)
Loading…
Cancel
Save