#simple shutdown script, button attached to GPIO pin 17 (aka pin 11) & ground
import RPi.GPIO as GPIO
from time import sleep
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(17,GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(27,GPIO.OUT)
GPIO.output(27,False)
def Buttonpressed(Channel):
	button_press_timer = 0
	GPIO.output(27,False)
	while True:
		if (GPIO.input(Channel) == False) :
			button_press_timer += 1
			if (button_press_timer > 3) :
				GPIO.output(27,True)

		else:
			if (button_press_timer >= 3) :
				Blink(3)
				os.system("sudo reboot")
				print("rebooting")
				break
			
			elif (button_press_timer > 1 and button_press_timer < 3) :
				Blink(2)
				os.system("sudo shutdown -h now")
				print("shutting down")
				break
			button_press_timer = 0
		sleep(1)
		
def Blink(value):
	GPIO.output(27,False)
	sleep(.3)
	for i in range (0, value):
		GPIO.output(27,True)
		sleep(.3)
		GPIO.output(27,False)
		sleep(.3)	
GPIO.add_event_detect(17,GPIO.FALLING, callback = Buttonpressed, bouncetime = 2000)
Blink(3)
GPIO.output(27,True)
while 1:
	sleep(1)
