Skip to content

Instantly share code, notes, and snippets.

@scar45
Created February 8, 2015 02:56
Show Gist options
  • Save scar45/81800a576abeb8070de0 to your computer and use it in GitHub Desktop.
Save scar45/81800a576abeb8070de0 to your computer and use it in GitHub Desktop.
A Python script which is intended to be run as a background process, and listens for + takes action on GPIO button 17, 22, and 23 presses.
#!/usr/bin/env python2.7
# original script by Alex Eames http://RasPi.tv
# http://RasPi.tv/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3
# Modified by George Merlocco http://github.com/scar45
# for Adafruit 2.2" LCD Pi Hat tactile button inputs
import RPi.GPIO as GPIO
import time
import sys
# call is used to enable the execution of any external task
from subprocess import call
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def btn_press_17(channel):
call(["echo", "GPIO button 17 pressed!"])
def btn_press_22(channel):
call(["echo", "GPIO button 22 pressed!"])
def btn_press_23(channel):
call(["echo", "GPIO button 23 pressed!"])
GPIO.add_event_detect(17, GPIO.FALLING, callback=btn_press_17, bouncetime=300)
GPIO.add_event_detect(22, GPIO.FALLING, callback=btn_press_22, bouncetime=300)
GPIO.add_event_detect(23, GPIO.FALLING, callback=btn_press_23, bouncetime=300)
while True:
try:
print "Waiting for presses from GPIOs 17, 22, and 23..."
time.sleep(31536000) # sleep 1 year...zzzzzzzzzzzz...
except KeyboardInterrupt:
GPIO.cleanup() # clean up GPIO on CTRL+C exit
sys.exit()
GPIO.cleanup() # clean up GPIO on normal exit
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment