-
Notifications
You must be signed in to change notification settings - Fork 0
/
blank_screen.py
executable file
·120 lines (101 loc) · 3.28 KB
/
blank_screen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# this script should be run by your user's crontab
# see README.md
import atexit
import RPi.GPIO as GPIO
import smtplib
import ssl
import subprocess
from datetime import datetime
from decouple import config
from email.message import EmailMessage
from helper import in_between_days
from time import sleep, time
DEBUG = config('DEBUG', default=False, cast=bool)
ms = config('MOTION_SENSOR')
GPIO.setmode(GPIO.BCM)
# Motion sensor
GPIO.setup(17, GPIO.IN)
if ms == "RCWL-0516":
GPIO.setup(27, GPIO.OUT)
# enable motion sensor
GPIO.output(27, GPIO.HIGH)
# display brightness pwm
GPIO.setup(18, GPIO.OUT)
dim = GPIO.PWM(18, 1000)
@atexit.register
def goodbye():
GPIO.cleanup()
subprocess.run(["/usr/bin/vcgencmd", "display_power",
"1", "2"])
def motion_detect():
motion = GPIO.input(17)
return motion
def dim_display(mode):
if mode == "off":
dim.start(0)
for duty in range(0, 101, 1):
dim.ChangeDutyCycle(duty)
sleep(0.01)
sleep(0.5)
else:
dim.start(100)
for duty in range(100, -1, -1):
dim.ChangeDutyCycle(duty)
sleep(0.01)
sleep(0.5)
def send_mail():
msg = EmailMessage()
msg.set_content("The body of the email is here")
msg["Subject"] = "An Email Alert"
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
context = ssl.create_default_context()
with smtplib.SMTP("mail.example.com", port=587) as smtp:
smtp.starttls(context=context)
smtp.login("[email protected]", "secretpassword")
smtp.send_message(msg)
if __name__ == "__main__":
blanked = False
active = idle = time()
send_alert = False
i = 0
while True:
if motion_detect():
if DEBUG:
print("motion detected")
i = 0
active = time()
if blanked:
# only unblank the display when not during off-hours
if not in_between_days(config('START_DISPLAY_OFF'),
config('END_DISPLAY_OFF')):
subprocess.run(["/usr/bin/vcgencmd", "display_power",
"1", "2"])
dim_display("on")
blanked = False
#now = datetime.now()
#if not send_alert and (start_monitoring <= now <=
#end_monitoring):
#print("send mail")
#send_mail()
#send_alert = True
elif DEBUG:
print("off-hours")
else:
if DEBUG:
if blanked:
print("blanked")
else:
print(f"wait for it: {i}/{config('DISPLAY_TIMEOUT')}")
i += 1
idle = time()
if not blanked and (idle - active > config('DISPLAY_TIMEOUT',
cast=int)):
if DEBUG:
print("blank now")
dim_display("off")
subprocess.run(["/usr/bin/vcgencmd", "display_power",
"0", "2"])
blanked = True
sleep(1)