52 lines
942 B
Python
52 lines
942 B
Python
#!/usr/bin/env python
|
|
|
|
import RPi.GPIO as GPIO
|
|
import os
|
|
import time
|
|
import sys
|
|
|
|
# PIR pin
|
|
PIR_GPIO = 16
|
|
# Display timeout in seconds
|
|
TIMEOUT = 60
|
|
|
|
# DONT EDIT BELOW THIS LINE
|
|
# UNLESS YOU KNOW WHAT YOU ARE DOING
|
|
|
|
GPIO.setmode(GPIO.BOARD)
|
|
GPIO.setup(PIR_GPIO, GPIO.IN)
|
|
timer = 0
|
|
display = False
|
|
|
|
|
|
def set_display(enable):
|
|
global display
|
|
display = enable
|
|
os.system("vcgencmd display_power " + str(int(enable)))
|
|
|
|
|
|
def check_timeout():
|
|
if timer >= TIMEOUT and display:
|
|
set_display(False)
|
|
elif not display and timer < TIMEOUT:
|
|
set_display(True)
|
|
|
|
|
|
def motion(pin):
|
|
global timer
|
|
timer = 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
GPIO.add_event_detect(PIR_GPIO, GPIO.RISING, callback=motion)
|
|
while True:
|
|
time.sleep(1)
|
|
if timer < TIMEOUT:
|
|
timer += 1
|
|
check_timeout()
|
|
except KeyboardInterrupt:
|
|
GPIO.cleanup()
|
|
sys.exit(0)
|
|
|