-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesd_v1.0.py
133 lines (124 loc) · 3.44 KB
/
esd_v1.0.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
121
122
123
124
125
126
127
128
129
130
131
132
133
from time import sleep
from pitop import Button, LED, UltrasonicSensor, Buzzer
from pitop import Pitop
from pitop.miniscreen import Miniscreen
import datetime
import pyttsx3
# Green LED in port D4
gled = LED("D4")
# Red LED
rled = LED("D5")
# Button on port D2
button = Button("D2")
# Ultrasonic Sensor on port D6
sensor = UltrasonicSensor("D6")
# Buzzer on port D3
buzzer = Buzzer("D3")
# pi-top miniscreen
miniscreen = Miniscreen()
# Initialize pyttsx3
tts = pyttsx3.init()
button.hold_time = 3 # Button hold time required to activate panic feature
sensor.threshold_distance = 0.39 # Sensor distance required to activate alarm when armed
# Initial states of variables
active = False
panic = False
exitdelay = True
# Initial system state is disarmed, turn on green LED and report status in console
gled.on()
print("Alarm off")
# Enable/disable exit delay, triggered by X button
def exit_timer_toggle():
global exitdelay
if exitdelay == False:
exitdelay = True
print("Exit delay on")
else:
exitdelay = False
print("Exit delay off")
# Restart the exit timer, triggered by O button on pi-top
def restartexit():
global active
if not active:
print("You can't restart the exit delay, the alarm is not on")
elif active and not exitdelay:
print("You can't restart the exit delay, the exit delay is disabled")
elif active:
active = False
rled.blink(0.5, 0.5)
gled.blink(0.5, 0.5)
buzzer.blink(0.1,1)
for i in range(15,0,-1):
print(str.format("You have {} seconds to exit", i))
sleep(1)
active = True
rled.on()
gled.off()
buzzer.off()
print("Alarm on")
# Main alarm trigger function
def alarm():
global active
if active:
#buzzer.on()
buzzer.blink(0.1, 0.1, 10)
rled.blink(0.1, 0.1)
print("ALARM TRIGGERED at", datetime.datetime.now())
tts.say("Alarm triggered")
tts.runAndWait()
# Panic alarm, triggered by holding main button
def panic():
global active
global panic
panic = True
active = True
gled.off()
rled.blink(0.1, 0.1)
#buzzer.on()
buzzer.blink(0.1, 0.1)
print("PANIC")
tts.say("Panic alarm activated")
tts.runAndWait()
# Main system toggle function, triggered by main button
def systemtoggle():
global active
global panic
if not active and exitdelay:
tts.say("Alarm active in 15 seconds")
tts.runAndWait()
rled.blink(0.5, 0.5)
gled.blink(0.5, 0.5)
buzzer.blink(0.1,1)
for i in range(15,0,-1):
print(str.format("You have {} seconds to exit", i))
sleep(1)
active = True
rled.on()
gled.off()
buzzer.off()
print("Alarm on")
tts.say("System armed")
tts.runAndWait()
elif not active:
active = True
rled.on()
gled.off()
buzzer.off()
print("Alarm on")
tts.say("System armed")
tts.runAndWait()
elif active == True and panic == False:
active = False
buzzer.off()
rled.off()
gled.on()
print("Alarm off")
tts.say("System disarmed")
tts.runAndWait()
else:
panic = False
button.when_released = systemtoggle
button.when_held = panic
miniscreen.cancel_button.when_released = exit_timer_toggle
miniscreen.select_button.when_released = restartexit
sensor.when_in_range = alarm