#!/usr/bin/env python


import RPi.GPIO as GPIO
import subprocess
import time

def millis():
    milliseconds = int(round(time.time() * 1000))
    return(milliseconds)

def longPress():
    holdTime = 2000
    pressTime = millis()
    while True:
        pinStatus = GPIO.input(3)
        if millis() - pressTime >= holdTime:
            return True
        elif pinStatus == 1:
            return False

def waitForPress():
    GPIO.wait_for_edge(3, GPIO.FALLING)
    if longPress() == True:
        pass
    else:
        waitForPress()
        

GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

waitForPress()

subprocess.call(['shutdown', '-h', 'now'], shell=False)
