Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add /etc-based hook handler for joystick events #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions contrib/joystick-handler/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Event handlers for Sense Hat joystick clicks.

This event handler provides a UNIX-y interface to react to Sense Hat joystick
events.

A daemon (bin/joystick-handler) listens for joystick events using the evdev
Python library. When an event is fired, hooks in the form of executable
programs are executed and passed two command line arguments: STATE first (one
of: press, hold, release) and KEY second (one of: up, down, left, right, push).

Hooks are located in suitable /etc directories, e.g., /etc/joystick.d/press/up/
and executed using run-parts (8). See etc/joystick.d/ for a sample /etc
configuration directory hierarchy and etc/joystick.d/press/hello for a sample
hook.

A systemd unit file (etc/systemd/system/joystick-handler.service) is provided
to start the joystick handler at boot and restart it if needed.

-- Stefano Zacchiroli <zack@upsilon.cc> Mon, 22 Feb 2016 11:25:58 +0100
72 changes: 72 additions & 0 deletions contrib/joystick-handler/bin/joystick-handler
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/python

# Event handler for RPi SenseHat joystick
# Copyright (C) 2015 Stefano Zacchiroli <zack@upsilon.cc>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from sense_hat import SenseStick

import os
import subprocess


HOOK_ROOT = '/etc/joystick.d'

KEY_NAMES = {
SenseStick.KEY_UP: 'up',
SenseStick.KEY_DOWN: 'down',
SenseStick.KEY_LEFT: 'left',
SenseStick.KEY_RIGHT: 'right',
SenseStick.KEY_ENTER: 'push',
}

STATE_NAMES = {
SenseStick.STATE_PRESS: 'press',
SenseStick.STATE_HOLD: 'hold',
SenseStick.STATE_RELEASE: 'release',
}


def run_hooks(state, key):
hook_dir = os.path.join(HOOK_ROOT, state, key)
if os.path.isdir(hook_dir):
subprocess.call(['run-parts',
'--arg', state,
'--arg', key,
hook_dir])


def joystick_loop(j):
for event in j:
key_state = STATE_NAMES[event.state]
key_name = KEY_NAMES[event.key]
run_hooks(key_state, key_name)


if __name__ == '__main__':
joystick_loop(SenseStick())
9 changes: 9 additions & 0 deletions contrib/joystick-handler/etc/joystick.d/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Event handlers for Sense Hat joystick clicks.

Each subdir {press,hold,release}/{up,down,left,right,push}/ contains
executable scripts that will be run by run-parts (8) when the joystick
is pressed/released in the given position.

Each script will receive 2 arguments: STATE, KEY.
STATE is one of: press, hold, release.
KEY is one of: up, down, left, right, push.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
9 changes: 9 additions & 0 deletions contrib/joystick-handler/etc/joystick.d/press/push/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/python

from sense_hat import SenseHat
import sys

if __name__ == '__main__':
(_state, _key) = sys.argv[1:] # ignored
sense = SenseHat()
sense.show_message("Hello, World!")
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[Unit]
Description=Event handler for RPi SenseHat joystick

[Service]
ExecStart=/usr/local/bin/joystick-handler
Restart=always

[Install]
WantedBy=multi-user.target