-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw1.py
80 lines (62 loc) · 1.47 KB
/
hw1.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
from temp import Temp_sensor
import time
from push_button import Button
from pir import PIR_sensor
from led_control import LED
from buzzer import Buzzer
import threading
from event_listener import eventListener
import RPi.GPIO as GPIO
if __name__ == "__main__":
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO_PIR = 7
GPIO_BUTT = 19
GPIO_LED = 18
GPIO_BUZZ = 24
GPIO_TEMP = 4
TEMP_THRESH = 30
loop = True
e1 = eventListener(0.05)
e2 = eventListener(5)
pir = PIR_sensor(GPIO_PIR)
pir.initialize()
temp_s = Temp_sensor()
buzzer = Buzzer(GPIO_BUZZ)
L22=None
times=0
def pirCallback():
global times, buzzer
times+=1
if(times>=3):
buzzer.stop()
# print("motion")
pass
def buttCallback(pressed):
global L22
print("pressed", pressed)
if not pressed:
L22 = e2.addListener(tempListener, tempCallback)
pass
def tempListener():
cel, fer = temp_s.read_temp()
print("read temp")
if(cel != None and fer != None):
if cel >= TEMP_THRESH:
return True, {"cel": cel}
else:
return False, {"cel": None}
else:
return False, {"cel": None}
def tempCallback(cel):
global times, buzzer, L22
e2.removeListener(L22)
times = 0
buzzer.buzz(440)
print("cel", cel)
button = Button(GPIO_BUTT)
L11=e1.addListener(pir.getState, pirCallback)
L12=e1.addListener(button.getState, buttCallback)
while loop:
time.sleep(1)