diff --git a/docs/source/conf.py b/docs/source/conf.py index 2faff381..657938d3 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -189,7 +189,8 @@ autodoc_mock_imports = ['googlesamples.assistant.grpc.audio_helpers', 'gevent.wsgi', 'Adafruit_IO', 'pyperclip', - 'dbus' + 'dbus', + 'inputs' ] sys.path.insert(0, os.path.abspath('../..')) diff --git a/platypush/backend/joystick.py b/platypush/backend/joystick.py new file mode 100644 index 00000000..f3041492 --- /dev/null +++ b/platypush/backend/joystick.py @@ -0,0 +1,48 @@ +import inputs +import time + +from platypush.backend import Backend +from platypush.message.event.joystick import JoystickEvent + + +class JoystickBackend(Backend): + """ + This backend will listen for events from a joystick device and post a + JoystickEvent whenever a new event is captured. + + Triggers: + + * :class:`platypush.message.event.joystick.JoystickEvent` when a new joystick event is received + + Requires: + + * **inputs** (``pip install inputs``) + """ + + def __init__(self, device, *args, **kwargs): + """ + :param device: Path to the joystick device (e.g. `/dev/input/js0`) + :type device_name: str + """ + + super().__init__(*args, **kwargs) + + self.device = device + + def run(self): + super().run() + self.logger.info('Initialized joystick backend on device {}'.format(self.device)) + + while not self.should_stop(): + try: + events = inputs.get_gamepad() + for event in events: + if event.ev_type == 'Key' or event.ev_type == 'Absolute': + self.bus.post(JoystickEvent(code=event.code, state=event.state)) + except Exception as e: + self.logger.exception(e) + time.sleep(1) + + +# vim:sw=4:ts=4:et: + diff --git a/platypush/message/event/joystick.py b/platypush/message/event/joystick.py new file mode 100644 index 00000000..4aca2e7c --- /dev/null +++ b/platypush/message/event/joystick.py @@ -0,0 +1,22 @@ +from platypush.message.event import Event + + +class JoystickEvent(Event): + """ + Event triggered upon joystick event + """ + + def __init__(self, code, state, *args, **kwargs): + """ + :param code: Event code, usually the code of the source key/handle + :type code: str + + :param state: State of the triggering element. Can be 0/1 for a button, -1/0/1 for an axis, a discrete integer for an analog input etc. + :type state: int + """ + + super().__init__(*args, code=code, state=state, **kwargs) + + +# vim:sw=4:ts=4:et: + diff --git a/requirements.txt b/requirements.txt index b14394b0..0fdaab94 100644 --- a/requirements.txt +++ b/requirements.txt @@ -96,3 +96,6 @@ pyserial # Support for TP-Link HS100 and similar smart switches/plugins pyHS100 +# Support for joystick backend +inputs + diff --git a/setup.py b/setup.py index c4ef774a..bcd23f4d 100755 --- a/setup.py +++ b/setup.py @@ -87,6 +87,7 @@ setup( 'Support for MCP3008 analog-to-digital converter plugin': ['adafruit-mcp3008'], 'Support for smart cards detection': ['pyscard'], 'Support for ICal calendars': ['icalendar', 'python-dateutil'], + 'Support for joystick backend': ['inputs'], # 'Support for Leap Motion backend': ['git+ssh://git@github.com:BlackLight/leap-sdk-python3.git'], # 'Support for Flic buttons': ['git+ssh://git@github.com/50ButtonsEach/fliclib-linux-hci'] },