-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuzzer.py
More file actions
37 lines (32 loc) · 1.26 KB
/
Copy pathbuzzer.py
File metadata and controls
37 lines (32 loc) · 1.26 KB
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
def get_frequency(note):
return round(440 * (2 ** ((note - 69) / 12)))
class Buzzer:
def __init__(self, pin, code, frequency_multiplier = 2, stats = None):
self.pin = pin
self.stats = stats
self.code = code
self.is_playing = False
self.note = 0
self.priority = 0
self.frequency_multiplier = frequency_multiplier
def play_note(self, note, priority, time):
self.code.wait_until(time)
self.code.add_loop_code(f"playFrequency({self.pin}, {get_frequency(note) * self.frequency_multiplier});")
self.note = note
self.set_priority(priority)
self.is_playing = True
def stop_playing(self, time):
if self.priority > 75 and self.stats is not None:
self.stats["important notes on"] -= 1
self.code.wait_until(time)
self.code.add_loop_code(f"stopBuzzer({self.pin});")
self.note = 0
self.priority = 0
self.is_playing = False
def set_priority(self, priority):
if self.stats is not None:
if self.priority > 75 >= priority:
self.stats["important notes on"] -= 1
if self.priority <= 75 < priority:
self.stats["important notes on"] += 1
self.priority = priority