def run(self): while not self.terminate: if not self.gamepadOK: self.devices = inputs.DeviceManager() try: gamepad = self.devices.gamepads[0] logMessage("Gamepad connected") self.gamepadOK = True self.gamepadUnplugged = False except IndexError: self.gamepadOK = False if self.gamepadUnplugged == False: logMessage("Gamepad not found") self.gamepadUnplugged = True sleep(1) continue try: # Get joystick input events = gamepad.read() for event in events: self.processEvent(event) self.gamepadIOError = False except IOError: self.gamepadOK = False if self.gamepadIOError == False: logMessage("Gamepad I/O error") self.gamepadIOError = True sleep(1) continue
def test_make_event(self, mock_post_init, mock_set_name): """Make_event can make an InputEvent object from evdev details.""" manager = inputs.DeviceManager() # Make sure the manager has key type self.assertEqual(manager.codes['types'][1], 'Key') mock_post_init.assert_called() inputdevice = inputs.InputDevice(manager, KBD_PATH) mock_set_name.assert_called() event = inputdevice._make_event(1535013055, 447534, 1, 30, 1) self.assertEqual(event.device._device_path, KBD_PATH) self.assertEqual(event.timestamp, 1535013055.447534) self.assertEqual(event.ev_type, 'Key') self.assertEqual(event.code, 'KEY_A') self.assertEqual(event.state, 1) # Let's do some more event_1 = inputdevice._make_event(1535013837, 121253, 1, 44, 1) event_2 = inputdevice._make_event(1535013874, 345229, 1, 18, 1) event_3 = inputdevice._make_event(1535013899, 826326, 1, 20, 1) event_4 = inputdevice._make_event(1535013919, 628367, 1, 35, 1) self.assertEqual(event_1.code, 'KEY_Z') self.assertEqual(event_2.code, 'KEY_E') self.assertEqual(event_3.code, 'KEY_T') self.assertEqual(event_4.code, 'KEY_H')
def _get_gamepad(self): """Get a gamepad object.""" try: devices = inputs.DeviceManager() self.gamepad = devices.gamepads[0] except IndexError: raise inputs.UnpluggedError('No gamepad found.')
def test_post_init_win(self, mock_update_all_devices, mock_find_devices_win, mock_find_devices_mac, mock_find_devices): """On Windows, find_devices_win is called and other methods are not.""" inputs.WIN = True inputs.MAC = False inputs.DeviceManager() mock_update_all_devices.assert_called() mock_find_devices_win.assert_called() mock_find_devices.assert_not_called() mock_find_devices_mac.assert_not_called()
def handle_events(self): devices = inputs.DeviceManager() keep_alive = True while keep_alive: try: if len(devices.gamepads) > 0: events = devices.gamepads[0].read() for event in events: self.__process_event(event) else: print('[W] No gamepad connected') time.sleep(3) devices = inputs.DeviceManager() except KeyboardInterrupt: # Quit print('[I] Terminated by keyboard interrupt') keep_alive = False except (inputs.UnpluggedError, IndexError, OSError): print('[E] No gamepad plugged in') time.sleep(3) devices = inputs.DeviceManager()
def test_post_init_linux(self, mock_update_all_devices, mock_find_devices_win, mock_find_devices_mac, mock_find_devices): """On Linux, find_devices is called and the other methods are not.""" inputs.WIN = False inputs.MAC = False # pylint: disable=unused-variable device_manger = inputs.DeviceManager() mock_update_all_devices.assert_called() mock_find_devices.assert_called() mock_find_devices_mac.assert_not_called() mock_find_devices_win.assert_not_called()
def timerEvent(self, event): if (event.timerId() != self.timer.timerId()): return try: states = controllers.monitor_gamepad(self.states) self.states.update(states) self.count += 1 except inputs.UnpluggedError as e: logger.error(e) self.state = 0 logger.debug("Redetecting devices...") inputs.DeviceManager() self.stop_updates()
def run(self): self.count = 0 self.state = 0 self.state = 1 self.gamePad = GamePad() self.gamePad.states["n_agents"] = len(gctronic.elisa_numbers) self.gamePad.states["current_id"] = 0 self.gamePad.states["previous_id"] = 1 while True: try: new_states = controllers.monitor_gamepad(self.gamePad.states) self.gamePad.states.update(new_states) logger.debug("Detected a gamepad action...") self.count += 1 self.gamePad.states.update({'count': self.count}) self.signal.emit(self.gamePad.states) except inputs.UnpluggedError as e: logger.error(e) self.state = 0 logger.debug("Redetecting devices...") inputs.DeviceManager() break self.state = 0
def setUp(self, mock_method): self.device_manger = inputs.DeviceManager() self.mock_method = mock_method