-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpi_pir.py
44 lines (36 loc) · 1.14 KB
/
rpi_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
from time import sleep
import RPi.GPIO as GPIO
class PIR:
GPIO.setmode(GPIO.BCM)
gpio_pin = None
action = None
action_args = None
time_lapse = None
stop = False
def __init__(self, gpio_pin, time_lapse, action, action_args=None):
GPIO.setup(gpio_pin, GPIO.IN)
self.gpio_pin = gpio_pin
self.action = action
self.action_args = action_args
self.time_lapse = time_lapse
def __pir_process__(self, stop=False):
while not self.stop:
if GPIO.input(self.gpio_pin):
if self.action_args:
if isinstance(self.action_args, tuple):
self.action(*self.action_args)
else:
self.action(self.action_args)
else:
self.action()
if stop:
self.stop_process()
else:
sleep(self.time_lapse)
def start(self):
self.stop = True
self.__pir_process__(False)
def stop_process(self):
self.stop = True
def one_action(self):
self.__pir_process__(True)