Command Keypad

If have to admit, the idea of having some small physical keyboard to bind arbitrary commands to control my Raspberry Pi came up long time ago. After that I learned that you could control the Kodi media center running on that small computer with the TV remote via CEC (HDMI control) the plan was dropped again. These days, I finally did it, because I felt like having some easy possibility to quickly pause music or shutdown the Raspberry Pi would after all be very useful.

In short

To realize this I wrote a simple python script called command-keypad to handle my keypad inputs and map them to custom command sets. It is heavily inspired by Peter Dikant’s rpi-ola-controller.

The script is executed whenever the keypad is plugged in and stopped when removed. This is done via udev and systemd. However, if run on an OSMC Pi Box as in my case, you additionally need to hide the keypad from Kodi, otherwise the media center alone will have the power over your command pad.

Hands on!

First, you need a couple of files, to be found in my command-keypad project on github.

Make sure you have a linux system with python installed. In particular the script needs python-evdev available in many distributions repositories. Otherwise install it via pip.

sudo pip install PyYAML evdev

Now you can run the controller with the sample configuration file:

sudo ./ -c conf/simpleconfig.yaml

The evdev library captures the keyboard input on device level and needs root privileges. The above command will search for all available input devices and list them:

Error: You need to specify one of the following input devices:
  /dev/input/event3    ImExPS/2 Generic Explorer Mouse 
  /dev/input/event2    AT Translated Set 2 keyboard    
  /dev/input/event1    Sleep Button                    
  /dev/input/event0    Power Button

Select your keyboard and add the device to the command line:

sudo ./ -c conf/simpleconfig.yaml -i /dev/input/event2

The script will now listen to your keyboard inputs. Try pressing a key to execute the mapped command. If you press a key that is not mapped, the key code will be displayed, so that you can configure this key in the yaml file.

Setup hotplugging

If you are happy with your key mappings, setup some links, so that the script is run, whenever the dedicated command keypad is connected. My OSMC system uses systemd, therefore I prepared two files which will do the trick for such a system. However, you have first to use udevadm to identify you command-keypad:

~$ udevadm info --attribute-walk /dev/input/event0 | less

looking at device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3.2/1-1.3.2:1.1/0003:05A4:9750.0002/input/input1/event1':

looking at parent device '/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3.2/1-1.3.2:1.1/0003:05A4:9750.0002/input/input1':
ATTRS{name}=="NOVATEK USB Keypad"

The bold attributes are well defined if you do not use several keyboards of the same kind, so we use them to setup an udevd rule to unically identify the device. Change those values in the *.rules file, provided. Be aware that udev does not support any sort of line breaks. Now link the rule file to /etc/udev/rules.d/ by issuing

~$ sudo ln -s /path/to/command-keypad/999-command-keypad.rules /etc/udev/rules.d/999-keypad.rules
~$ sudo udevadm control --reload-rules

udev will trigger to execution of the, so link that as well to the appropriate location (udev links our keypad at /dev/command-keypad so we don’t have to worry about device names afterwards).

~$ sudo ln -s /path/to/command-keypad/command-keypad.service /etc/systemd/system/command-keypad.service
~$ sudo systemctl daemon-reload

Also set the path to your configuration layout file in command-keypad.service. Since udev will trigger the service, you don’t have to activate it within systemd.

There you go. You can check whether your daemon is actually started on plugging in the keypad with

~$ sudo journalctl -f
Nov 19 23:09:12 pi kernel: input: NOVATEK USB Keypad as /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3.2/1-1.3.2:1.1/0003:05A4:9750.0016/input/input21
Nov 19 23:09:12 pi kernel: hid-generic 0003:05A4:9750.0016: input,hidraw1: USB HID v1.00 Mouse [NOVATEK USB Keypad] on usb-3f980000.usb-1.3.2/input1
Nov 19 23:09:12 pi systemd[1]: Starting command-keypad daemon...
Nov 19 23:09:12 pi systemd[1]: Started command-keypad daemon.
Nov 19 23:09:10 pi kernel: usb 1-1.3.2: USB disconnect, device number 17
Nov 19 23:09:10 pi[1271]: Device removed. Exiting Script.

Well done. You should now have a customizable hotplugging command keyboard!


Peter Pan. Kann fliegen mit Feenstaub.

Tagged with: , ,
Posted in Linux, Technik
One comment on “Command Keypad
  1. […] verwandelt werden. Ich habe ja früher schon über meine Liebe zum Music Player Daemon, mein Command-Keypad und jüngst über Netflix & Amazon Instant Video Addons berichtet. Ein weiterer Baustein ist […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Posts by topic…
…by month
Have a look at…

%d bloggers like this: