# ATXRaspi interrupt mode script
# This needs to run instead of the shutdowncheck.sh
# Remove the shutdowncheck from /etc/rc.local and add this script instead
# script by Tony Pottier
import RPi.GPIO as GPIO
import os
import sys
import time

GPIO.setmode(GPIO.BCM) 

pulseStart = 0.0
REBOOTPULSEMINIMUM = 0.2	#reboot pulse signal should be at least this long (seconds)
REBOOTPULSEMAXIMUM = 2.0	#reboot pulse signal should be at most this long (seconds)
SHUTDOWN = 7				#GPIO used for shutdown signal
BOOT = 8					#GPIO used for boot signal


# Set up GPIO 8 and write that the PI has booted up
print "\nSystem starting - ATXRaspi IRQ"
GPIO.setup(BOOT, GPIO.OUT, initial=GPIO.HIGH)

# Set up GPIO 7  as interrupt for the shutdown signal to go HIGH
GPIO.setup(SHUTDOWN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


print "ATXRaspi shutdown IRQ script started: asserted pins (", SHUTDOWN, "=input,LOW; ", BOOT, "=output,HIGH). Waiting for GPIO", SHUTDOWN, "to become HIGH..."
try:
	while True:	
		GPIO.wait_for_edge(SHUTDOWN, GPIO.RISING)
		shutdownSignal = GPIO.input(SHUTDOWN)
		pulseStart = time.time() #register time at which the button was pressed
		while shutdownSignal:
			time.sleep(0.2)
			if(time.time() - pulseStart >= REBOOTPULSEMAXIMUM):
				print "ATXRaspi triggered a shutdown signal, halting Rpi ... "
				os.system("sudo poweroff")
				sys.exit()
			shutdownSignal = GPIO.input(SHUTDOWN)
		if time.time() - pulseStart >= REBOOTPULSEMINIMUM:
			print "ATXRaspi triggered a reboot signal, recycling Rpi ... "
			os.system("sudo reboot")
			sys.exit()
		if GPIO.input(SHUTDOWN): #before looping we must make sure the shutdown signal went low
			GPIO.wait_for_edge(SHUTDOWN, GPIO.FALLING)
except:
	pass 
finally:
	GPIO.cleanup()
