Advertisement
julenvitoria

pishutdown

Apr 28th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.76 KB | None | 0 0
  1. #!/usr/bin/python
  2. # shutdown/reboot(/power on) Raspberry Pi with pushbutton
  3. # 03/16/18 PlataKowai introduced video playback in shutdown/reboot actions
  4. # 04/18/18 julenvitoria introduced modification to turn on a led for buttons with it
  5. # 04/21/18 julenvitoria introduced modification to make EmulationStation save metadata aided by cyperghost's bash script
  6.  
  7. import RPi.GPIO as GPIO
  8. from subprocess import call
  9. from datetime import datetime
  10. import time
  11. import os
  12.  
  13. # pushbutton connected to this GPIO pin, using pin 5 also has the benefit of
  14. # waking / powering up Raspberry Pi when button is pressed
  15. shutdownPin = 5
  16.  
  17. # If you have a button with led please set the pin and uncomment
  18. # ledPin = 8
  19.  
  20. # if button pressed for at least this long then shut down. if less then reboot.
  21. shutdownMinSeconds = 3
  22.  
  23. # button debounce time in seconds
  24. debounceSeconds = 0.01
  25.  
  26. #If you have a button with led please uncomment 3th and 4th line of this section
  27. GPIO.setmode(GPIO.BOARD)
  28. GPIO.setup(shutdownPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  29. #GPIO.setup(ledPin, GPIO.OUT)
  30. #GPIO.output(ledPin, GPIO.HIGH)
  31.  
  32. buttonPressedTime = None
  33.  
  34. #Uncomment if you desire video playback on reboot/shutdown actions
  35. #shutdownVideoPath = "/home/pi/MisScript/inicio/shutdown.mp4"
  36. #restartVideoPath = "/opt/vc/src/hello_pi/hello_video/test.h264"
  37.  
  38.  
  39. def buttonStateChanged(pin):
  40.     global buttonPressedTime
  41.  
  42.     if not (GPIO.input(pin)):
  43.         # button is down
  44.         if buttonPressedTime is None:
  45.             buttonPressedTime = datetime.now()
  46.     else:
  47.         # button is up
  48.         if buttonPressedTime is not None:
  49.             elapsed = (datetime.now() - buttonPressedTime).total_seconds()
  50.             buttonPressedTime = None
  51.             if elapsed >= shutdownMinSeconds:
  52.                 # We call to the omxplayer to watch the restart video before halt the system, uncomment if you desire this option
  53.                 # os.system("omxplayer --layer 2 " + restartVideoPath)
  54.                 # button pressed for more than specified time, shutdown
  55.                 os.system("sudo /home/pi/k.sh")
  56.                 time.sleep(2)
  57.                 os.system("sudo killall emulationstation")
  58.                 time.sleep(5)
  59.                 os.system("sudo poweroff")
  60.             elif elapsed >= debounceSeconds:
  61.                 # We call to the omxplayer to watch the shutdown video before halt the system, uncomment if you desire this option
  62.                 # os.system("omxplayer --layer 2 " + shutdownVideoPath)
  63.                 # button pressed for a shorter time, reboot
  64.                 os.system("sudo /home/pi/k.sh")
  65.                 time.sleep(2)
  66.                 os.system("sudo killall emulationstation")
  67.                 time.sleep(5)
  68.                 os.system("sudo reboot")
  69.  
  70.  
  71. # subscribe to button presses
  72. GPIO.add_event_detect(shutdownPin, GPIO.BOTH, callback=buttonStateChanged)
  73.  
  74. while True:
  75.     # sleep to reduce unnecessary CPU usage
  76.     time.sleep(5)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement