Added joystick backend

This commit is contained in:
Fabio Manganiello 2018-09-18 18:58:19 +02:00
parent 1d50e91b27
commit 98d24b061a
5 changed files with 76 additions and 1 deletions

View file

@ -189,7 +189,8 @@ autodoc_mock_imports = ['googlesamples.assistant.grpc.audio_helpers',
'gevent.wsgi', 'gevent.wsgi',
'Adafruit_IO', 'Adafruit_IO',
'pyperclip', 'pyperclip',
'dbus' 'dbus',
'inputs'
] ]
sys.path.insert(0, os.path.abspath('../..')) sys.path.insert(0, os.path.abspath('../..'))

View file

@ -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:

View file

@ -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:

View file

@ -96,3 +96,6 @@ pyserial
# Support for TP-Link HS100 and similar smart switches/plugins # Support for TP-Link HS100 and similar smart switches/plugins
pyHS100 pyHS100
# Support for joystick backend
inputs

View file

@ -87,6 +87,7 @@ setup(
'Support for MCP3008 analog-to-digital converter plugin': ['adafruit-mcp3008'], 'Support for MCP3008 analog-to-digital converter plugin': ['adafruit-mcp3008'],
'Support for smart cards detection': ['pyscard'], 'Support for smart cards detection': ['pyscard'],
'Support for ICal calendars': ['icalendar', 'python-dateutil'], '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 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'] # 'Support for Flic buttons': ['git+ssh://git@github.com/50ButtonsEach/fliclib-linux-hci']
}, },