forked from GeekyTim/PiClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpir.py
48 lines (45 loc) · 1.95 KB
/
pir.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
from gpiozero import MotionSensor
import time
import logging
# #############################################################################
# Class: PIR
# Detects whether there has been movement around the clock, and turns the
# matrix off if there has not been any
# #############################################################################
class PIR:
def __init__(self, ledmatrix, pirpin, nomovementforseconds):
logging.info('init motion')
self.__Matrix = ledmatrix
self.__delay = nomovementforseconds
self.__pin = pirpin
logging.info('set up sensor')
self.__PIR = MotionSensor(self.__pin)
logging.info('starting motion')
# -------------------------------------------------------------------------
# wait_for_no_movement
# Waits to see if there is movement within self.__delay seconds
# If there is none, fades the matrix to black and stops it from being
# updated.
# Once movement is seen, fades the screen back on.
# -------------------------------------------------------------------------
def wait_for_no_movement(self):
logging.info('motionsensor')
t = time.time()
while True:
nomovement = self.__PIR.wait_for_no_motion(10)
if nomovement:
logging.info('No movement')
if (time.time() - t) >= self.__delay:
logging.info('Turning off screen')
self.__Matrix.set_draw_on_matrix(False)
if self.__PIR.wait_for_motion():
t = time.time()
logging.info('Turning on screen')
self.__Matrix.set_draw_on_matrix(True)
else:
if self.__PIR.wait_for_motion(10):
logging.info('Motion detected')
t = time.time()
else:
logging.info('Motion detected')
t = time.time()