Here is how to set Long press and Short press function. I added on my reset ROM button a long press feature that resets the entire system:
Create a script that will be executed at long press
nano /home/pi/switch.sh
Paste the below code and save using CTRL + X, Y
#!/bin/bash
sudo reboot
Make it executable:
chmod +x /home/pi/switch.sh
Edit the code in the first mentioned post resetbutton.py:
nano /home/pi/scripts/resetbutton.py
Replace all text with the below code, save using CTRL + X, Y:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import os
# we will use the pin numbering to match the pins on the Pi, instead of the
# GPIO pin outs (makes it easier to keep track of things)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.IN)
seq_cust = 0
tim_cust = 0
# check to see if button has been pushed
try:
while True:
if GPIO.input(16)==0:
tim_cust = 0
if seq_cust > 0:
os.system("sudo /home/pi/exit.sh") # Short press action
seq_cust = 0
time.sleep(1)
else:
seq_cust = 0
else:
tim_cust = tim_cust + 1
time.sleep(.01)
if tim_cust >= 200: # Time for long press >= x
os.system("sudo /home/pi/switch.sh") # Long press action
tim_cust = 0
time.sleep(1)
else:
seq_cust = 1
finally:
GPIO.cleanup()
I used 0.01 seconds to eliminate delay in button press.
At 200 should result in ~2 seconds.
You can put your own script in switch.sh
Also the script supports more than 2 actions per button with a bit of tweak.
Enjoy ;)