From 5256ae2e59f513afe66203ceecd8759d1bd60a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Ko=C4=8D=C3=AD?= Date: Tue, 18 Jul 2017 13:42:52 +0200 Subject: Another fixes in setup --- setup.py | 4 ++-- turtetris-master/__init__.py | 12 ---------- turtetris-master/__main__.py | 3 --- turtetris-master/usb_input.py | 54 ------------------------------------------- turtetris_master/__init__.py | 12 ++++++++++ turtetris_master/__main__.py | 3 +++ turtetris_master/usb_input.py | 54 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 71 insertions(+), 71 deletions(-) delete mode 100644 turtetris-master/__init__.py delete mode 100644 turtetris-master/__main__.py delete mode 100644 turtetris-master/usb_input.py create mode 100644 turtetris_master/__init__.py create mode 100644 turtetris_master/__main__.py create mode 100644 turtetris_master/usb_input.py diff --git a/setup.py b/setup.py index a233da0..798eb8f 100755 --- a/setup.py +++ b/setup.py @@ -21,10 +21,10 @@ setup( ], keywords='Turris Tetris', - packages=['turtetris'], + packages=['turtetris_master'], entry_points={ 'console_scripts': [ - 'turtetris-master=turtetris-master:main' + 'turtetris-master=turtetris_master:main' ] } ) diff --git a/turtetris-master/__init__.py b/turtetris-master/__init__.py deleted file mode 100644 index 9cf22b1..0000000 --- a/turtetris-master/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -from .usb_input import Gamepad - - -def main(): - "Main function" - inpt = Gamepad() - while True: - print(inpt.check()) - - -if __name__ == '__main__': - main() diff --git a/turtetris-master/__main__.py b/turtetris-master/__main__.py deleted file mode 100644 index 8273c4f..0000000 --- a/turtetris-master/__main__.py +++ /dev/null @@ -1,3 +0,0 @@ -from . import main - -main() diff --git a/turtetris-master/usb_input.py b/turtetris-master/usb_input.py deleted file mode 100644 index 8864ef9..0000000 --- a/turtetris-master/usb_input.py +++ /dev/null @@ -1,54 +0,0 @@ -import usb.core -import usb.util - -# Personal Communication Systems, Inc. SNES Gamepad -CONF_SNES_GAMEPAD = { - "idVendor": 0x0810, - "idProduct": 0xe501, - "iInterface": 0, -} - - -class Gamepad: - "Simple gamepad handle function" - - def __init__(self, conf=CONF_SNES_GAMEPAD): - "Initializes usb subsystem" - self.dev = usb.core.find(idVendor=conf['idVendor'], - idProduct=conf['idProduct']) - if self.dev is None: - raise ValueError('Device not found') - - if self.dev.is_kernel_driver_active(conf['iInterface']) is True: - # Detach any kernel driver so it won't interfere with us - self.dev.detach_kernel_driver(conf['iInterface']) - - # set the active configuration. With no arguments, the first - # configuration will be the active one - self.dev.set_configuration() - - # get an endpoint instance - self.cfg = self.dev.get_active_configuration() - intf = self.cfg[(0, 0)] - - self.ep = usb.util.find_descriptor( - intf, - # match the first IN endpoint - custom_match=lambda e: - usb.util.endpoint_direction(e.bEndpointAddress) == - usb.util.ENDPOINT_IN - ) - assert self.ep is not None - - def check(self): - "Check the input state" - data = self.dev.read(self.ep.bEndpointAddress, - self.ep.wMaxPacketSize*2, 1000).tolist() - return { - "left": data[3] < 120 or bool(data[5] & 0x80), - "right": data[3] > 140 or bool(data[5] & 0x20), - "up": data[4] < 120 or bool(data[5] & 0x10), - "down": data[4] > 140 or bool(data[5] & 0x40), - "select": bool(data[6] & 0x10), - "start": bool(data[6] & 0x20), - } diff --git a/turtetris_master/__init__.py b/turtetris_master/__init__.py new file mode 100644 index 0000000..9cf22b1 --- /dev/null +++ b/turtetris_master/__init__.py @@ -0,0 +1,12 @@ +from .usb_input import Gamepad + + +def main(): + "Main function" + inpt = Gamepad() + while True: + print(inpt.check()) + + +if __name__ == '__main__': + main() diff --git a/turtetris_master/__main__.py b/turtetris_master/__main__.py new file mode 100644 index 0000000..8273c4f --- /dev/null +++ b/turtetris_master/__main__.py @@ -0,0 +1,3 @@ +from . import main + +main() diff --git a/turtetris_master/usb_input.py b/turtetris_master/usb_input.py new file mode 100644 index 0000000..8864ef9 --- /dev/null +++ b/turtetris_master/usb_input.py @@ -0,0 +1,54 @@ +import usb.core +import usb.util + +# Personal Communication Systems, Inc. SNES Gamepad +CONF_SNES_GAMEPAD = { + "idVendor": 0x0810, + "idProduct": 0xe501, + "iInterface": 0, +} + + +class Gamepad: + "Simple gamepad handle function" + + def __init__(self, conf=CONF_SNES_GAMEPAD): + "Initializes usb subsystem" + self.dev = usb.core.find(idVendor=conf['idVendor'], + idProduct=conf['idProduct']) + if self.dev is None: + raise ValueError('Device not found') + + if self.dev.is_kernel_driver_active(conf['iInterface']) is True: + # Detach any kernel driver so it won't interfere with us + self.dev.detach_kernel_driver(conf['iInterface']) + + # set the active configuration. With no arguments, the first + # configuration will be the active one + self.dev.set_configuration() + + # get an endpoint instance + self.cfg = self.dev.get_active_configuration() + intf = self.cfg[(0, 0)] + + self.ep = usb.util.find_descriptor( + intf, + # match the first IN endpoint + custom_match=lambda e: + usb.util.endpoint_direction(e.bEndpointAddress) == + usb.util.ENDPOINT_IN + ) + assert self.ep is not None + + def check(self): + "Check the input state" + data = self.dev.read(self.ep.bEndpointAddress, + self.ep.wMaxPacketSize*2, 1000).tolist() + return { + "left": data[3] < 120 or bool(data[5] & 0x80), + "right": data[3] > 140 or bool(data[5] & 0x20), + "up": data[4] < 120 or bool(data[5] & 0x10), + "down": data[4] > 140 or bool(data[5] & 0x40), + "select": bool(data[6] & 0x10), + "start": bool(data[6] & 0x20), + } -- cgit v1.2.3