Implement subclasses for controllers

This commit is contained in:
Marc Riera Irigoyen 2022-10-16 23:59:05 +02:00
parent a5580bdbde
commit ca6105a090
4 changed files with 43 additions and 21 deletions

View file

@ -4,9 +4,15 @@ from hashlib import sha1
class EmulatedGamepad:
class GamepadType(Enum):
DGOC44U = 0
PC2HANDLE = 0
PS1 = 1
class PC2HandleGamepad(EmulatedGamepad):
def __init__(self):
super().__init__()
self.type
self.type = self.GamepadType.PC2HANDLE
def write_input(self):
print("Output to emulated DGOC-44U correct")
return 0

View file

@ -1,12 +1,19 @@
from enum import Enum
from hashlib import sha1
import time
def create_gamepad(vid, pid, name):
match vid, pid:
case 0x0f0d, 0x00c1:
return SwitchGamepad(vid, pid, name)
return PhysicalGamepad(vid, pid, name)
class PhysicalGamepad:
class GamepadType(Enum):
UNKNOWN = 0
CLASSIC = 1
NSWITCH = 2
SWITCH = 2
def __init__(self, vid, pid, name):
super().__init__()
@ -15,7 +22,7 @@ class PhysicalGamepad:
self.name = name
self.id = self._get_gamepad_id()
self.hash = self._get_gamepad_hash()
self.type = self.get_gamepad_type()
self.type = self.GamepadType.UNKNOWN
self.config = []
def _get_gamepad_id(self):
@ -28,8 +35,14 @@ class PhysicalGamepad:
hash = sha1(str(self.id + ":" + self.name).encode('utf-8')).hexdigest()
return hash
def get_gamepad_type(self):
match self.vid, self.pid:
case 0x0f0d, 0x00c1:
return self.GamepadType.NSWITCH
return self.GamepadType.UNKNOWN
class SwitchGamepad(PhysicalGamepad):
def __init__(self, * args):
super().__init__(* args)
self.type = self.GamepadType.SWITCH
self.config = []
def read_input(self):
time.sleep(5)
print("Read from ZKNS-001 correct")
return 0

View file

@ -2,7 +2,8 @@ import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem, QHeaderView
from gui.main_ui import Ui_MainWindow
from gamepads.physical import PhysicalGamepad
import gamepads.physical as gamepad_physical
import gamepads.emulated as gamepad_emulated
from models.gamepad import GamepadModel
class MainWindow(QMainWindow):
@ -22,7 +23,7 @@ class MainWindow(QMainWindow):
self._gui.tableView_physicalControllerList.selectRow(0)
self._gui.pushButton_physicalControllerRefresh.clicked.connect(self.controller_list_refresh)
self._gui.pushButton_emulatedControllerStart.clicked.connect(self.start)
self._gui.pushButton_emulatedControllerStart.clicked.connect(self.controller_emulator_start)
def controller_list_refresh(self):
self.gamepad_model.beginResetModel()
@ -32,12 +33,14 @@ class MainWindow(QMainWindow):
def controller_list_selection_changed(self):
enabled = False
rows = self._gui.tableView_physicalControllerList.selectionModel().selectedRows()
if rows and self.gamepad_model.gamepads[rows[0].row()].type != PhysicalGamepad.GamepadType.UNKNOWN:
if rows and self.gamepad_model.gamepads[rows[0].row()].type != gamepad_physical.PhysicalGamepad.GamepadType.UNKNOWN:
enabled = True
self._gui.pushButton_emulatedControllerStart.setEnabled(enabled)
def start(self):
def controller_emulator_start(self):
self._gui.pushButton_emulatedControllerStart.setEnabled(False)
rows = self._gui.tableView_physicalControllerList.selectionModel().selectedRows()
if rows:
gamepad = self.gamepad_model.gamepads[rows[0].row()]
print(gamepad.name)
emulated_gamepad = gamepad_emulated.PC2HandleGamepad()
self._gamepad_handler.start_gamepad_emulator(gamepad, emulated_gamepad)

View file

@ -1,7 +1,7 @@
from enum import Enum
from evdev import InputDevice, list_devices, ecodes as e, UInput, AbsInfo
from gamepads.physical import PhysicalGamepad
from gamepads.emulated import EmulatedGamepad
import gamepads.physical as gamepad_physical
import gamepads.emulated as gamepad_emulated
class GamepadHandler:
def __init__(self):
@ -11,13 +11,13 @@ class GamepadHandler:
gamepads = []
devices = [InputDevice(path) for path in list_devices()]
for device in devices:
gamepads.append(PhysicalGamepad(device.info.vendor, device.info.product, device.name))
gamepads.append(gamepad_physical.create_gamepad(device.info.vendor, device.info.product, device.name))
return gamepads
def start_gamepad_emulator(gamepad, emulated_gamepad):
realtype = gamepad.type
emutype = emulated_gamepad.type
print(str(realtype)+str(emutype))
for i in range(2):
gamepad.read_input()
emulated_gamepad.write_input()
""" cap = {