Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # shutdown/reboot(/power on) Raspberry Pi with pushbutton
- # 03/16/18 PlataKowai introduced video playback in shutdown/reboot actions
- # 04/18/18 julenvitoria introduced modification to turn on a led for buttons with it
- # 04/21/18 julenvitoria introduced modification to make EmulationStation save metadata aided by cyperghost's bash script
- import RPi.GPIO as GPIO
- from subprocess import call
- from datetime import datetime
- import time
- import os
- # pushbutton connected to this GPIO pin, using pin 5 also has the benefit of
- # waking / powering up Raspberry Pi when button is pressed
- shutdownPin = 5
- # If you have a button with led please set the pin and uncomment
- # ledPin = 8
- # if button pressed for at least this long then shut down. if less then reboot.
- shutdownMinSeconds = 3
- # button debounce time in seconds
- debounceSeconds = 0.01
- #If you have a button with led please uncomment 3th and 4th line of this section
- GPIO.setmode(GPIO.BOARD)
- GPIO.setup(shutdownPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- #GPIO.setup(ledPin, GPIO.OUT)
- #GPIO.output(ledPin, GPIO.HIGH)
- buttonPressedTime = None
- #Uncomment if you desire video playback on reboot/shutdown actions
- #shutdownVideoPath = "/home/pi/MisScript/inicio/shutdown.mp4"
- #restartVideoPath = "/opt/vc/src/hello_pi/hello_video/test.h264"
- def buttonStateChanged(pin):
- global buttonPressedTime
- if not (GPIO.input(pin)):
- # button is down
- if buttonPressedTime is None:
- buttonPressedTime = datetime.now()
- else:
- # button is up
- if buttonPressedTime is not None:
- elapsed = (datetime.now() - buttonPressedTime).total_seconds()
- buttonPressedTime = None
- if elapsed >= shutdownMinSeconds:
- # We call to the omxplayer to watch the restart video before halt the system, uncomment if you desire this option
- # os.system("omxplayer --layer 2 " + restartVideoPath)
- # button pressed for more than specified time, shutdown
- os.system("sudo /home/pi/k.sh")
- time.sleep(2)
- os.system("sudo killall emulationstation")
- time.sleep(5)
- os.system("sudo poweroff")
- elif elapsed >= debounceSeconds:
- # We call to the omxplayer to watch the shutdown video before halt the system, uncomment if you desire this option
- # os.system("omxplayer --layer 2 " + shutdownVideoPath)
- # button pressed for a shorter time, reboot
- os.system("sudo /home/pi/k.sh")
- time.sleep(2)
- os.system("sudo killall emulationstation")
- time.sleep(5)
- os.system("sudo reboot")
- # subscribe to button presses
- GPIO.add_event_detect(shutdownPin, GPIO.BOTH, callback=buttonStateChanged)
- while True:
- # sleep to reduce unnecessary CPU usage
- time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement