mirror of
https://github.com/marcriera/ddgo-converter.git
synced 2025-04-19 18:39:29 +02:00
Tests with real input
This commit is contained in:
parent
ca6105a090
commit
5052407e53
4 changed files with 78 additions and 16 deletions
29
ddgo-converter/events/input.py
Normal file
29
ddgo-converter/events/input.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
from enum import IntEnum
|
||||||
|
|
||||||
|
class InputEvent:
|
||||||
|
|
||||||
|
class EventType(IntEnum):
|
||||||
|
RELEASE_BUTTON = 0
|
||||||
|
PRESS_BUTTON = 1
|
||||||
|
BRAKE_NOTCH = 2
|
||||||
|
POWER_NOTCH = 3
|
||||||
|
ERROR = 4
|
||||||
|
|
||||||
|
class Button(IntEnum):
|
||||||
|
BUTTON_SELECT = 0
|
||||||
|
BUTTON_START = 1
|
||||||
|
BUTTON_A = 2
|
||||||
|
BUTTON_B = 3
|
||||||
|
BUTTON_C = 4
|
||||||
|
BUTTON_D = 5
|
||||||
|
BUTTON_UP = 6
|
||||||
|
BUTTON_DOWN = 7
|
||||||
|
BUTTON_LEFT = 8
|
||||||
|
BUTTON_RIGHT = 9
|
||||||
|
BUTTON_LDOOR = 10
|
||||||
|
BUTTON_RDOOR = 11
|
||||||
|
|
||||||
|
|
||||||
|
def __init__(self, type, data):
|
||||||
|
self.type = type
|
||||||
|
self.data = data
|
|
@ -1,9 +1,9 @@
|
||||||
from enum import Enum
|
from enum import IntEnum
|
||||||
from hashlib import sha1
|
from events.input import InputEvent
|
||||||
|
|
||||||
class EmulatedGamepad:
|
class EmulatedGamepad:
|
||||||
|
|
||||||
class GamepadType(Enum):
|
class GamepadType(IntEnum):
|
||||||
PC2HANDLE = 0
|
PC2HANDLE = 0
|
||||||
PS1 = 1
|
PS1 = 1
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@ class PC2HandleGamepad(EmulatedGamepad):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.type = self.GamepadType.PC2HANDLE
|
self.type = self.GamepadType.PC2HANDLE
|
||||||
|
|
||||||
def write_input(self):
|
def write_input(self, event: InputEvent):
|
||||||
print("Output to emulated DGOC-44U correct")
|
print(str(event.type) + ": " + str(event.data))
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
from enum import Enum
|
from enum import IntEnum
|
||||||
|
import evdev
|
||||||
from hashlib import sha1
|
from hashlib import sha1
|
||||||
import time
|
from events.input import InputEvent
|
||||||
|
from select import select
|
||||||
|
|
||||||
def create_gamepad(vid, pid, name):
|
def create_gamepad(vid, pid, name):
|
||||||
match vid, pid:
|
match vid, pid:
|
||||||
|
@ -10,7 +12,7 @@ def create_gamepad(vid, pid, name):
|
||||||
|
|
||||||
class PhysicalGamepad:
|
class PhysicalGamepad:
|
||||||
|
|
||||||
class GamepadType(Enum):
|
class GamepadType(IntEnum):
|
||||||
UNKNOWN = 0
|
UNKNOWN = 0
|
||||||
CLASSIC = 1
|
CLASSIC = 1
|
||||||
SWITCH = 2
|
SWITCH = 2
|
||||||
|
@ -35,14 +37,42 @@ class PhysicalGamepad:
|
||||||
hash = sha1(str(self.id + ":" + self.name).encode('utf-8')).hexdigest()
|
hash = sha1(str(self.id + ":" + self.name).encode('utf-8')).hexdigest()
|
||||||
return hash
|
return hash
|
||||||
|
|
||||||
|
def _get_device(self):
|
||||||
|
for device in [evdev.InputDevice(path) for path in evdev.list_devices()]:
|
||||||
|
dev_name = [device.info.vendor, device.info.product, device.name]
|
||||||
|
if dev_name == [self.vid, self.pid, self.name]:
|
||||||
|
return device
|
||||||
|
|
||||||
class SwitchGamepad(PhysicalGamepad):
|
class SwitchGamepad(PhysicalGamepad):
|
||||||
|
|
||||||
def __init__(self, * args):
|
def __init__(self, * args):
|
||||||
super().__init__(* args)
|
super().__init__(* args)
|
||||||
self.type = self.GamepadType.SWITCH
|
self.type = self.GamepadType.SWITCH
|
||||||
self.config = []
|
self.config = []
|
||||||
|
self.device = self._get_device()
|
||||||
|
|
||||||
def read_input(self):
|
def read_input(self):
|
||||||
time.sleep(5)
|
# time.sleep(5)
|
||||||
print("Read from ZKNS-001 correct")
|
# print("Read from ZKNS-001 correct")
|
||||||
return 0
|
# return InputEvent(InputEvent.EventType.PRESS_BUTTON, InputEvent.Button.BUTTON_A)
|
||||||
|
print(self.device.active_keys())
|
||||||
|
select([self.device], [], [], 5)
|
||||||
|
try:
|
||||||
|
event = self.device.read_one()
|
||||||
|
if event is not None:
|
||||||
|
if event.type == evdev.ecodes.EV_KEY:
|
||||||
|
match event.code:
|
||||||
|
case 304: # Y
|
||||||
|
return InputEvent(InputEvent.EventType(event.value), InputEvent.Button.BUTTON_A)
|
||||||
|
case 305: # B
|
||||||
|
return InputEvent(InputEvent.EventType(event.value), InputEvent.Button.BUTTON_B)
|
||||||
|
case 306: # A
|
||||||
|
return InputEvent(InputEvent.EventType(event.value), InputEvent.Button.BUTTON_C)
|
||||||
|
case 307: # X
|
||||||
|
return InputEvent(InputEvent.EventType(event.value), InputEvent.Button.BUTTON_D)
|
||||||
|
case 312: # MINUS
|
||||||
|
return InputEvent(InputEvent.EventType(event.value), InputEvent.Button.BUTTON_SELECT)
|
||||||
|
case 313: # PLUS
|
||||||
|
return InputEvent(InputEvent.EventType(event.value), InputEvent.Button.BUTTON_START)
|
||||||
|
except OSError:
|
||||||
|
return (InputEvent(InputEvent.EventType.ERROR, None))
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from enum import Enum
|
|
||||||
from evdev import InputDevice, list_devices, ecodes as e, UInput, AbsInfo
|
from evdev import InputDevice, list_devices, ecodes as e, UInput, AbsInfo
|
||||||
|
import events.input as input_events
|
||||||
import gamepads.physical as gamepad_physical
|
import gamepads.physical as gamepad_physical
|
||||||
import gamepads.emulated as gamepad_emulated
|
import gamepads.emulated as gamepad_emulated
|
||||||
|
|
||||||
|
@ -12,12 +12,16 @@ class GamepadHandler:
|
||||||
devices = [InputDevice(path) for path in list_devices()]
|
devices = [InputDevice(path) for path in list_devices()]
|
||||||
for device in devices:
|
for device in devices:
|
||||||
gamepads.append(gamepad_physical.create_gamepad(device.info.vendor, device.info.product, device.name))
|
gamepads.append(gamepad_physical.create_gamepad(device.info.vendor, device.info.product, device.name))
|
||||||
|
gamepads.append(gamepad_physical.create_gamepad(0x0f0d, 0x00c1, "Test gamepad"))
|
||||||
return gamepads
|
return gamepads
|
||||||
|
|
||||||
def start_gamepad_emulator(gamepad, emulated_gamepad):
|
def start_gamepad_emulator(gamepad, emulated_gamepad):
|
||||||
for i in range(2):
|
while True:
|
||||||
gamepad.read_input()
|
event = gamepad.read_input()
|
||||||
emulated_gamepad.write_input()
|
if event is not None:
|
||||||
|
if event.type == input_events.InputEvent.EventType.ERROR:
|
||||||
|
break
|
||||||
|
emulated_gamepad.write_input(event)
|
||||||
|
|
||||||
|
|
||||||
""" cap = {
|
""" cap = {
|
||||||
|
|
Loading…
Add table
Reference in a new issue