Skip to content

Instantly share code, notes, and snippets.

@aenniw
Last active July 23, 2018 05:04
Show Gist options
  • Save aenniw/54503679c1f3b139b7e666b54835d4cf to your computer and use it in GitHub Desktop.
Save aenniw/54503679c1f3b139b7e666b54835d4cf to your computer and use it in GitHub Desktop.
Waveshare 3.2' [Clone] Buttons handling
#!/usr/bin/env python
# sudo apt-get install python-dev python-rpi.gpio
import RPi.GPIO as GPIO
from time import sleep
import signal, os
buttons = [24, 23, 18]
def button_pressed(channel):
if channel == 24:
print("Top button pressed")
os.system("sudo shutdown -r now")
elif channel == 23:
print("Middle button pressed")
os.system("sudo shutdown -h now")
elif channel == 18:
print("Bottom button pressed")
def unregister_events():
for pin in buttons:
GPIO.remove_event_detect(pin)
if __name__ == '__main__':
signal.signal(signal.SIGINT, unregister_events)
try:
GPIO.setmode(GPIO.BCM)
for pin in buttons:
GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(pin, GPIO.RISING, callback=button_pressed, bouncetime=200)
while True:
print("Waiting for event")
sleep(10)
except Exception as e:
print("Caught exception:", e)
unregister_events()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment