Skip to content

Instantly share code, notes, and snippets.

@Trimbell
Last active November 3, 2019 00:46
Show Gist options
  • Save Trimbell/19ebc74560abd91716c68728b07a61d7 to your computer and use it in GitHub Desktop.
Save Trimbell/19ebc74560abd91716c68728b07a61d7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess
from threading import Timer
GPIO.setmode(GPIO.BOARD)
# Time in seconds that power button must be held
SHUTDOWN_HOLD_TIME = 3
# 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)
# use the same pin that is used for the reset button (one button to rule them all!)
GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)
def perform_shutdown():
subprocess.call("shutdown -h now", shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
oldButtonState1 = buttonState1
oldButtonState1 = True
shutdown_timer = shutdown_timer = Timer(SHUTDOWN_HOLD_TIME, perform_shutdown)
while True:
#grab the current button state
buttonState1 = GPIO.input(5)
# check to see if button has been pushed
if buttonState1 != oldButtonState1 and buttonState1 == False:
oldButtonState1 = buttonState1
# shutdown
shutdown_timer.start()
else if buttonState1 != oldButtonState1 and buttonState1 == True:
shutdown_timer.cancel()
time.sleep(.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment