From 5fe66eaa164e3a284b9dd355ba2a21cfbe63c3fd Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Mon, 22 Feb 2016 11:37:08 +0100 Subject: [PATCH] add /etc-based hook handler for joystick events --- contrib/joystick-handler/README | 19 +++++ contrib/joystick-handler/bin/joystick-handler | 72 +++++++++++++++++++ .../joystick-handler/etc/joystick.d/README | 9 +++ .../etc/joystick.d/hold/down/.placeholder | 0 .../etc/joystick.d/hold/left/.placeholder | 0 .../etc/joystick.d/hold/push/.placeholder | 0 .../etc/joystick.d/hold/right/.placeholder | 0 .../etc/joystick.d/hold/up/.placeholder | 0 .../etc/joystick.d/press/down/.placeholder | 0 .../etc/joystick.d/press/left/.placeholder | 0 .../etc/joystick.d/press/push/.placeholder | 0 .../etc/joystick.d/press/push/hello | 9 +++ .../etc/joystick.d/press/right/.placeholder | 0 .../etc/joystick.d/press/up/.placeholder | 0 .../etc/joystick.d/release/down/.placeholder | 0 .../etc/joystick.d/release/left/.placeholder | 0 .../etc/joystick.d/release/push/.placeholder | 0 .../etc/joystick.d/release/right/.placeholder | 0 .../etc/joystick.d/release/up/.placeholder | 0 .../systemd/system/joystick-handler.service | 9 +++ 20 files changed, 118 insertions(+) create mode 100644 contrib/joystick-handler/README create mode 100755 contrib/joystick-handler/bin/joystick-handler create mode 100644 contrib/joystick-handler/etc/joystick.d/README create mode 100644 contrib/joystick-handler/etc/joystick.d/hold/down/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/hold/left/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/hold/push/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/hold/right/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/hold/up/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/press/down/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/press/left/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/press/push/.placeholder create mode 100755 contrib/joystick-handler/etc/joystick.d/press/push/hello create mode 100644 contrib/joystick-handler/etc/joystick.d/press/right/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/press/up/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/release/down/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/release/left/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/release/push/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/release/right/.placeholder create mode 100644 contrib/joystick-handler/etc/joystick.d/release/up/.placeholder create mode 100644 contrib/joystick-handler/etc/systemd/system/joystick-handler.service diff --git a/contrib/joystick-handler/README b/contrib/joystick-handler/README new file mode 100644 index 0000000..d05541e --- /dev/null +++ b/contrib/joystick-handler/README @@ -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 Mon, 22 Feb 2016 11:25:58 +0100 diff --git a/contrib/joystick-handler/bin/joystick-handler b/contrib/joystick-handler/bin/joystick-handler new file mode 100755 index 0000000..b358cb1 --- /dev/null +++ b/contrib/joystick-handler/bin/joystick-handler @@ -0,0 +1,72 @@ +#!/usr/bin/python + +# Event handler for RPi SenseHat joystick +# Copyright (C) 2015 Stefano Zacchiroli +# +# 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()) diff --git a/contrib/joystick-handler/etc/joystick.d/README b/contrib/joystick-handler/etc/joystick.d/README new file mode 100644 index 0000000..44c3c13 --- /dev/null +++ b/contrib/joystick-handler/etc/joystick.d/README @@ -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. diff --git a/contrib/joystick-handler/etc/joystick.d/hold/down/.placeholder b/contrib/joystick-handler/etc/joystick.d/hold/down/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/hold/left/.placeholder b/contrib/joystick-handler/etc/joystick.d/hold/left/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/hold/push/.placeholder b/contrib/joystick-handler/etc/joystick.d/hold/push/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/hold/right/.placeholder b/contrib/joystick-handler/etc/joystick.d/hold/right/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/hold/up/.placeholder b/contrib/joystick-handler/etc/joystick.d/hold/up/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/press/down/.placeholder b/contrib/joystick-handler/etc/joystick.d/press/down/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/press/left/.placeholder b/contrib/joystick-handler/etc/joystick.d/press/left/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/press/push/.placeholder b/contrib/joystick-handler/etc/joystick.d/press/push/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/press/push/hello b/contrib/joystick-handler/etc/joystick.d/press/push/hello new file mode 100755 index 0000000..368b0f6 --- /dev/null +++ b/contrib/joystick-handler/etc/joystick.d/press/push/hello @@ -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!") diff --git a/contrib/joystick-handler/etc/joystick.d/press/right/.placeholder b/contrib/joystick-handler/etc/joystick.d/press/right/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/press/up/.placeholder b/contrib/joystick-handler/etc/joystick.d/press/up/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/release/down/.placeholder b/contrib/joystick-handler/etc/joystick.d/release/down/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/release/left/.placeholder b/contrib/joystick-handler/etc/joystick.d/release/left/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/release/push/.placeholder b/contrib/joystick-handler/etc/joystick.d/release/push/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/release/right/.placeholder b/contrib/joystick-handler/etc/joystick.d/release/right/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/joystick.d/release/up/.placeholder b/contrib/joystick-handler/etc/joystick.d/release/up/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/contrib/joystick-handler/etc/systemd/system/joystick-handler.service b/contrib/joystick-handler/etc/systemd/system/joystick-handler.service new file mode 100644 index 0000000..db748d6 --- /dev/null +++ b/contrib/joystick-handler/etc/systemd/system/joystick-handler.service @@ -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