intial import from article
This commit is contained in:
51
smartmirror_ir.py
Normal file
51
smartmirror_ir.py
Normal file
@@ -0,0 +1,51 @@
|
||||
#!/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)
|
||||
|
Reference in New Issue
Block a user