forked from OjGrooms/project-guest-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguestListApp.py
92 lines (78 loc) · 3.06 KB
/
guestListApp.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
import RPi.GPIO as gpio
from recorder import Recorder
from filename import *
from playback import *
from timer import ButtonTimeout
from time import sleep
gpio.setmode(gpio.BOARD)
class ButtonRecorder(object):
def __init__(self):
# Setting up the button to be used as an input.cd
gpio.setup(18, gpio.IN, pull_up_down=gpio.PUD_DOWN)
gpio.setup(16, gpio.IN, pull_up_down=gpio.PUD_DOWN)
# Creating a timer that will call the falling function after 120 seconds.
self.timer = ButtonTimeout(120, self.handle_timer)
# Creating a Recorder object with 2 channels.
self.recorder = Recorder(channels=2)
self.timerFired = False
self.handsetLowered = False
def start(self):
gpio.add_event_detect(18, gpio.RISING, callback=self.handset_up, bouncetime=50)
def handset_up(self, channel=18):
"""
The handset_up function is called when the handset is lifted from the rest. It starts the real
time clock, sets the handsetLowered variable to False, sets the timerFired variable to False,
removes the event detection on the channel, adds event detection on the channel, and starts
recording
:param channel: the GPIO pin number that the handset is connected to, defaults to 18 (optional)
"""
print('lifted from rest')
self.handsetLowered = False
self.timerFired = False
gpio.remove_event_detect(channel)
gpio.add_event_detect(16, gpio.RISING, callback=self.handset_down, bouncetime=50)
self.start_recording()
def handset_down(self, channel=16):
"""
The handset_down function is called when the handset is returned to the rest position. It sets
the handsetLowered variable to True, and if the timerFired variable is False, it stops recording
:param channel: The GPIO pin number that the handset is connected to, defaults to 16 (optional)
"""
print('returned to rest')
self.handsetLowered = True
if self.timerFired == False:
self.stop_recording()
gpio.remove_event_detect(channel)
gpio.add_event_detect(18, gpio.RISING, callback=self.handset_up, bouncetime=50)
def handle_timer(self):
"""
If the handset is not lowered, stop recording
"""
print('Timer fired')
self.timerFired = True
self.stop_recording()
def stop_recording(self):
"""
It stops the recording, closes the file, and cancels the timer
"""
self.recfile.stop_recording()
self.recfile.close()
self.timer.cancel()
def start_recording(self):
"""
It starts after a 1 second delay, plays a preamble, and then starts recording
"""
sleep(1)
playPreamble()
sleep(0.1)
self.timer.start()
self.recfile = self.recorder.open(fileNameGen(), 'wb')
self.recfile.start_recording()
rec = ButtonRecorder()
rec.start()
try:
while True:
sleep(10)
except KeyboardInterrupt:
pass
gpio.cleanup()