#!/usr/bin/python2 import RPi.GPIO as GPIO import time import threading from subprocess import call PIR_PIN = 18 TIMER = None TIMEOUT=60*10 def handleEdge(pin): if(GPIO.input(pin) == 1): handleMotion() else: handleAbsense() def handleMotion(): global TIMER stopTimer() call(['vcgencmd','display_power','1']) def handleAbsense(): startTimer() def stopTimer(): global TIMER if TIMER: TIMER.cancel() TIMER = None def startTimer(): global TIMER stopTimer() TIMER = threading.Timer(TIMEOUT, handleShutdown) TIMER.start() def handleShutdown(): global TIMER TIMER.cancel(); TIMER = None call(['vcgencmd','display_power','0']) def main(): GPIO.setmode(GPIO.BCM) GPIO.setup(PIR_PIN, GPIO.IN) GPIO.add_event_detect(PIR_PIN, GPIO.BOTH, callback=handleEdge) # main loop while True: time.sleep(0.1) GPIO.cleanup() if __name__ == "__main__": main()