USB event queue detection improved

master
kaqu 2 years ago
parent 26746a515f
commit 2a8e8218c4
  1. 4
      .vscode/launch.json
  2. 11
      README.md
  3. 26
      game_objects/pong_player.py
  4. 6
      pandemic_pong.py
  5. 2
      utilities/show_gamepad_data.py

@ -9,8 +9,8 @@
"type": "python",
"request": "launch",
"program": "${file}",
"args": ["--sizeable", "--server"],
//"args": ["--sizeable", "--player", "1", "127.0.0.1"],
//"args": ["--sizeable", "--server"],
"args": ["--sizeable", "--player", "1", "127.0.0.1"],
//"args": ["--sizeable", "--viewer", "127.0.0.1"],
"console": "integratedTerminal"
}

@ -21,7 +21,6 @@ Almost forgot: You'll have to have [pyqt5](https://pypi.org/project/PyQt5/) inst
(see also the following passage on virtual environments for 'automatic' installation).
With these installed we're prep'ed & ready!
### Virtual environment setup ###
If you want to keep libraries separate from your other python stuff, a 'virtual environment' comes in handy.
@ -53,6 +52,16 @@ And to reactive the virtual environment later just calling
is sufficient.
Finally, the **playsound** library requires some other libraries installed.
For the virtual environment, the installation is as follows:
pip install vext
pip install vext.gi
which will allow the use of the system gi.repository (Gtk3) from a virtualenv.
With these installed we're prep'ed & ready!
## 2. Game usage & capabilities ##
There are two options to run the game:

@ -11,13 +11,37 @@ import os, fcntl
import libevdev
import struct # Packing/unpacking
import socket # External communication
from glob import glob
import game_objects.pong_constants as pgc # Pong global constants
import game_objects.pong_globalvars as pgv # Pong global variables
UDP_PACKET_SIZE = struct.calcsize(pgc.UDP_TRANSFER_FORMAT) # Size of receiver buffer
serverUDP = None
def pickEventQueues():
"""Figure out gamepad event queues"""
sEventQueues = [None, None]
iQueueIndex = [0, 0]
i = 0
for l in glob("/dev/input/event?"): # Att.: Order not guaranteed!
iQueue = int(l[16:])
try:
with open(l, "rb") as fd:
dev = libevdev.Device(fd)
print("Input device name: {}".format(dev.name)) # 'USB Gamepad'
if dev.name == "USB Gamepad ":
sEventQueues[i] = l
iQueueIndex[i] = iQueue
i += 1
except:
pass # Insufficient access
if iQueueIndex[0] > iQueueIndex[1]: # Wrong order, swap in-place!
sEventQueues[0], sEventQueues[1] = sEventQueues[1], sEventQueues[0]
return sEventQueues[0], sEventQueues[1]
def print_event(e):
"""Sample from libevdev docs, useful for testing ..."""

@ -20,7 +20,7 @@ from playsound import playsound
import game_objects.pong_constants as pgc # Pong global constants
import game_objects.pong_globalvars as pgv # Pong global variables
from game_objects.pong_player import PongPlayer
from game_objects.pong_player import PongPlayer, pickEventQueues
from game_objects.pong_object import PongObject
from game_objects.pong_game import PongGame
import game_objects.pong_viewserver as vs # Pong TCP view server (threads)
@ -72,12 +72,16 @@ if __name__ == '__main__':
print("*** PANDEMIC PONG (local) ***")
# Figure out 2 highest event queues as they are assumed to be the gamepads
# ls -al usb-0079_USB_Gamepad-event-joystick -> ../event7
sEventQueue1, sEventQueue2 = pickEventQueues()
"""
maxEvent = 0
for l in glob("/dev/input/event?"):
if int(l[-1]) > maxEvent:
maxEvent = int(l[-1])
sEventQueue1 = "/dev/input/event" + str(maxEvent-1)
sEventQueue2 = "/dev/input/event" + str(maxEvent)
"""
if pgc.DEVLOCAL == False:
print("I'm a slow starter, please be patient!") # Caching actually ...

@ -150,7 +150,7 @@ def main(args):
raise e
if __name__ == "__main__":
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: {} /dev/input/eventX".format(sys.argv[0]))
sys.exit(1)

Loading…
Cancel
Save