This might be an old question, but I thought I might share my solution as well. Basically, my goal is to be able to run an arbitrary shell script using GPIO buttons that come with Adafruit PiTFT. Surprisingly, internet search does not turn up much on this topic. I was about to use a Python daemon as suggeested by Gerben and also described in details here, but then stumbled upon this write up. The basic idea is to treat GPIO pins as a virtual keyboard (apparently Unix already has a support for this), and then map events from this virtual keyboard to commands using 'triggerhappy' module. Triggerhappy comes bundled with Raspbian/Noobs, but if your system does not have it - you can install it with 'sudo apt-get triggerhappy'. Here is what needs to be done:
- Take a note of already available event devices. I have a touch-sensitive PiTFT and a wireless keyboard, so I've got 2 devices.
$ 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/event1: ft6x06_ts
/dev/input/event2: Logitech K400
Select the device event number [0-1]: 0
- Create a device tree overlay gpio.dts file with the following content (I'm mapping buttons to keys that I know I will not be using; the full list is available here):
/dts-v1/;
/plugin/;
/ {
compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";
fragment@0 {
target-path = "/";
__overlay__ {
keypad: pitft_gpio_keys {
compatible = "gpio-keys";
#address-cells = <1>;
#size-cells = <0>;
#autorepeat;
button@17 {
label = "Button 17 down";
linux,code = <85>;
gpios = <&gpio 17 1>;
};
button@22 {
label = "Button 22 down";
linux,code = <93>;
gpios = <&gpio 22 1>;
};
button@27 {
label = "Button 27 down";
linux,code = <94>;
gpios = <&gpio 27 1>;
};
};
};
};
};
- Compile and install the file:
sudo dtc -I dts -O dtb -o /boot/overlays/gpio.dtbo gpio.dts
- Open /boot/config.txt and add the new overlay to the end of the file:
dtoverlay=gpio
- Restart the system and verify you can now see the new even device. You can also press the mapped keys to verify the events are being generated:
$ 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: pitft_gpio_keys
/dev/input/event1: ft6x06_ts
/dev/input/event2: Logitech K400
Select the device event number [0-2]: 0
Input driver version is 1.0.1
Input device ID: bus 0x19 vendor 0x1 product 0x1 version 0x100
Input device name: "pitft_gpio_keys"
Supported events:
Event type 0 (EV_SYN)
Event type 1 (EV_KEY)
Event code 85 (KEY_ZENKAKUHANKAKU)
Event code 93 (KEY_KATAKANAHIRAGANA)
Event code 94 (KEY_MUHENKAN)
Properties:
Testing ... (interrupt to exit)
Event: time 1516058749.600834, type 1 (EV_KEY), code 85 (KEY_ZENKAKUHANKAKU), value 1
Event: time 1516058749.600834, -------------- EV_SYN ------------
Event: time 1516058749.800839, type 1 (EV_KEY), code 85 (KEY_ZENKAKUHANKAKU), value 0
Event: time 1516058749.800839, -------------- EV_SYN ------------
Event: time 1516058751.040851, type 1 (EV_KEY), code 93 (KEY_KATAKANAHIRAGANA), value 1
Event: time 1516058751.040851, -------------- EV_SYN ------------
Event: time 1516058751.360816, type 1 (EV_KEY), code 93 (KEY_KATAKANAHIRAGANA), value 0
Event: time 1516058751.360816, -------------- EV_SYN ------------
Event: time 1516058753.590817, type 1 (EV_KEY), code 94 (KEY_MUHENKAN), value 1
Event: time 1516058753.590817, -------------- EV_SYN ------------
Event: time 1516058753.820817, type 1 (EV_KEY), code 94 (KEY_MUHENKAN), value 0
- Check you can see the events with triggerhappy as well:
$ sudo thd --dump /dev/input/event*
EV_KEY KEY_ZENKAKUHANKAKU 1 /dev/input/event0
# KEY_ZENKAKUHANKAKU 1 command
EV_KEY KEY_ZENKAKUHANKAKU 0 /dev/input/event0
# KEY_ZENKAKUHANKAKU 0 command
EV_KEY KEY_KATAKANAHIRAGANA 1 /dev/input/event0
# KEY_KATAKANAHIRAGANA 1 command
EV_KEY KEY_KATAKANAHIRAGANA 0 /dev/input/event0
# KEY_KATAKANAHIRAGANA 0 command
EV_KEY KEY_MUHENKAN 1 /dev/input/event0
# KEY_MUHENKAN 1 command
EV_KEY KEY_MUHENKAN 0 /dev/input/event0
# KEY_MUHENKAN 0 command
- By default, triggerhappy runs as 'nobody', so we need to give it more rights if we want to execute anything useful (more info).
Open /etc/init.d/triggerhappy and change 'nobody' to 'pi' on DAEMON_ARGS.
- Create a file /etc/triggerhappy/triggers.d/gpio.conf with the following content (can be any shell command, but for this example I'm simply printing event names to a file):
# /etc/triggerhappy/triggers.d/gpio.conf
#
KEY_ZENKAKUHANKAKU 1 echo "Key 17 pressed" >> /home/pi/events.txt
KEY_KATAKANAHIRAGANA 1 echo "Key 22 pressed" >> /home/pi/events.txt
KEY_MUHENKAN 1 echo "Key 27 pressed" >> /home/pi/events.txt
- Reload triggerhappy:
sudo systemctl daemon-reload
service triggerhappy start
service triggerhappy reload
- Open events.txt and keep press the buttons to verify everything works:
tail -f /home/pi/events.txt
TROUBLESHOOTING: If you don't see what's expected at step 5 (either gpio or touchscreen is missing), make sure you are not trying to map the same key that is already mapped. By default, Adafruit maps pin 23 to shutdown, so you have to either not map it in the overlay, or unmap in PiTFT config.