-
Notifications
You must be signed in to change notification settings - Fork 2
/
pirhdmi
executable file
·67 lines (59 loc) · 1.7 KB
/
pirhdmi
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
#!/usr/bin/env python3
import os
import sys
import subprocess
import time
import threading
from gpiozero import MotionSensor
from signal import pause
pir = MotionSensor(4)
KMSPRINT='/usr/bin/kmsprint'
XSET='/usr/bin/xset'
ON_TIME=120.0
def is_off():
status = subprocess.check_output([KMSPRINT, "-p"]).decode("utf-8")
if " DPMS (2) = 0 (On) [On=0|Standby=1|Suspend=2|Off=3]" in status:
return False
else:
return True
def hdmi_off():
done=False
while not done:
try:
subprocess.run([XSET, 'dpms', 'force', 'standby'], env=dict(os.environ, DISPLAY=":0", XAUTHORITY="/home/martin/.Xauthority"))
print('hdmi_off', file=sys.stderr)
done=True
except:
print('hdmi_off failed', file=sys.stderr)
time.sleep(1)
def start_off_timer():
try:
global timer
timer = threading.Timer(ON_TIME, lambda: hdmi_off())
timer.start()
except:
print('start_off_timer failed', file=sys.stderr)
def restart_off_timer():
try:
global timer
timer.cancel()
start_off_timer()
except:
print('restart_off_timer failed', file=sys.stderr)
def hdmi_on():
done=False
while not done:
try:
if is_off():
subprocess.run([XSET, 'dpms', 'force', 'on'], env=dict(os.environ, DISPLAY=":0", XAUTHORITY="/home/martin/.Xauthority"))
print('hdmi_on', file=sys.stderr)
start_off_timer()
else:
restart_off_timer()
done=True
except:
print('hdmi_on failed', file=sys.stderr)
time.sleep(1)
pir.when_motion = lambda : hdmi_on()
start_off_timer()
pause()