-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (48 loc) · 1.63 KB
/
main.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
# Device runs python 3.9.2
from sounds import make_closed_noise, make_mostly_open_noise, make_open_noise, make_slightly_open_noise
from device import potentiometer
import threading
class Main():
current_quadrant = 0
previous_quadrant = 0
timer = None
def run(self):
while True:
self.previous_quadrant = self.current_quadrant
self.current_quadrant = self.get_current_quadrant()
if not self.previous_quadrant == self.current_quadrant:
self.handle_quadrant_change()
def handle_quadrant_change(self):
# We've just moved, fiddle with timers, starting or killing them.
if self.timer:
self.timer.cancel()
self.timer = self.start_countdown(3, self.make_noise)
# start a timer
# if the timer goes off
# run the loud code with the quadrant
return
def make_noise(self):
quadrant_mapping = {
0: make_closed_noise,
1: make_slightly_open_noise,
2: make_mostly_open_noise,
3: make_open_noise
}
quadrant_mapping[self.current_quadrant - 1]()
# return an int based on the closest fraction out of 4
def get_current_quadrant(self):
sensor_value = potentiometer.get_value()
if sensor_value < 0.25:
return 1
elif sensor_value < 0.5:
return 2
elif sensor_value < 0.75:
return 3
else:
return 4
def start_countdown(self, seconds, callback):
timer = threading.Timer(seconds, callback)
timer.start()
return timer
main = Main()
main.run()