import subprocess
import RPi.GPIO as GPIO

# Shutdown function to be called to safely shutdown the Pi
def shut_down():
	# Switch output pin to provide power to the EN Powerboost Pin in place of the battery
	GPIO.output(shutdown_out_pin, 1)
	subprocess.call(['sh', '/home/pi/es_shutdown.sh'])


# Pin definition
shutdown_in_pin = 23
shutdown_out_pin = 24

# Use "GPIO" pin numbering
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)

# Use built-in internal pullup resistor so the pin is not floating
GPIO.setup(shutdown_in_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(shutdown_out_pin, GPIO.OUT)
GPIO.output(shutdown_out_pin, 0)

# Wait for button depress using an interrupt
channel = GPIO.wait_for_edge(shutdown_in_pin, GPIO.FALLING, bouncetime=200)

if channel is None:
	print('Timeout occurred')
else:
	shut_down()
