GPIO keyboard - notro/fbtft-spindle GitHub Wiki

GPIO connected switches can be turned into a keyboard recognized by Linux.
It is also possible to execute commands on the Pi based on keyboard events.

gpio_keys

The gpio_keys module creates a keyboard that listens on GPIO's for keypresses.
Keypresses generates events that other processes can receive.
The driver is configured with the gpio_keys_device module.

Here is an example creating a keyboard with one key:

sudo modprobe gpio_keys

# KEY_POWER 116 /* SC System Power Down */
sudo modprobe gpio_keys_device active_low keys=22:116

Make it permanent by adding it to /etc/modules

gpio_keys
gpio_keys_device active_low keys=22:116

Note: If there is no external pullup/pulldown resistor, the internal ones must be activated with the pullup or pullup argument to gpio_keys_device.

Test this "keyboard":

pi@raspberrypi:~$ evtest
No device specified, trying to scan all of /dev/input/event*
Not running as root, no devices may be available.
Available devices:
/dev/input/event0:      Microsoft Basic Optical Mouse
/dev/input/event1:      LITEON Technology USB Multimedia Keyboard
/dev/input/event2:      LITEON Technology USB Multimedia Keyboard
/dev/input/event3:      ADS7846 Touchscreen
/dev/input/event4:      gpio-keys
Select the device event number [0-4]: 4
Input driver version is 1.0.1
Input device ID: bus 0x19 vendor 0x1 product 0x1 version 0x100
Input device name: "gpio-keys"
Supported events:
  Event type 0 (EV_SYN)
  Event type 1 (EV_KEY)
    Event code 116 (KEY_POWER)
Properties:
Testing ... (interrupt to exit)
Event: time 1390964791.200396, type 1 (EV_KEY), code 116 (KEY_POWER), value 1
Event: time 1390964791.200396, -------------- SYN_REPORT ------------
Event: time 1390964791.260402, type 1 (EV_KEY), code 116 (KEY_POWER), value 0
Event: time 1390964791.260402, -------------- SYN_REPORT ------------

Shields and switches

Some of the display shields have switches or pads for switches.

PiTFT

Pads for four switches. Active low with no pullup resistor.

sudo modprobe gpio_keys_device active_low pullup keys=<gpio>:<code>
RPi-Display

Pads for one switch. Active low with 10k pullup resistor.

sudo modprobe gpio_keys_device active_low keys=22:116
Texy 3.5" display

This display has one joyswitch and one switch configured as a GPIO mouse. Active high with no pulldown resistor.
They can be reconfigured as a keyboard. Remove the gpio_mouse configuration from /etc/modules to do so.

# Arrow keys and Enter key (not tested)
sudo modprobe gpio_keys_device pulldown keys=27:103,22:108,27:105,23:106,4:28

triggerhappy (thd)

The triggerhappy daemon listens for input events, and executes actions based on it's configuration files. In it's default setup, the daemon is run as user nobody. This makes it difficult to execute priviliged commands. By changing the user to pi, we can use sudo for the priviliged commands.

sudo nano /etc/init.d/triggerhappy

change user from nobody to pi

DAEMON_ARGS="--daemon --triggers /etc/triggerhappy/triggers.d/ --socket /var/run/thd.socket --pidfile $PIDFILE --user pi /dev/input/event*"

Now we find the event for our key:

pi@raspberrypi:~$ thd --dump /dev/input/event* | grep ^#
# KEY_POWER     1       command
# KEY_POWER     0       command

Let's make a config file that poweroff the Pi when this key is pressed

sudo nano /etc/triggerhappy/triggers.d/poweroff.conf

content

KEY_POWER	1	sudo /sbin/poweroff

Restart triggerhappy

sudo service triggerhappy restart

If you get: Unable to parse trigger line:, remove trailing empty lines from the config file.

Pressing the switch should now turn off the Pi.

Toggle backlight

by pressing Music key on USB keyboard

thd --dump /dev/input/event* | grep ^#
# KEY_CONFIG    1       command
# KEY_CONFIG    0       command

Make config file

sudo nano /etc/triggerhappy/triggers.d/backlight.conf

content

KEY_CONFIG    1       sudo /usr/local/bin/bl_toggle

Restart daemon

sudo service triggerhappy restart

Backlight toggle script

sudo nano /usr/local/bin/bl_toggle

content

#!/bin/sh

dev=$(echo /sys/class/backlight/*)
state=$(cat $dev/bl_power)

if [ $state -ne 0 ]; then
	sudo sh -c "echo 0 > $dev/bl_power"
else
	sudo sh -c "echo 1 > $dev/bl_power"
fi

Make it executable

sudo chmod +x /usr/local/bin/bl_toggle

piwik

⚠️ **GitHub.com Fallback** ⚠️