-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
piezo.py
89 lines (82 loc) · 1.48 KB
/
piezo.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
"""
Piezo tones.
"""
import simpleio
import board
import time
from nvram import NVRAMValues
class Piezo:
"""
Abstraction of the piezo.
"""
TONES = {
"startup": [
[440, 0.1],
[660, 0.1]
],
"success": [
[660, 0.1],
[None, 0.02],
[660, 0.1],
[None, 0.02],
[660, 0.1],
[880, 0.2]
],
"error": [
[660, 0.2],
[440, 0.4]
],
"idle_warning": [
[700, 0.3],
[None, 0.1],
[700, 0.3],
[None, 0.1],
[700, 0.3]
],
"chime": [
[440, 0.1],
[None, 0.1],
[440, 0.1]
],
"low_battery": [
[660, 0.1],
[550, 0.1],
[440, 0.4]
],
"info": [
[660, 0.1],
[None, 0.3],
[660, 0.1]
],
"shutdown": [
[660, 0.1],
[440, 0.1]
],
"motd": [
[660, 0.2],
[None, 0.05],
[660, 0.1],
[None, 0.05],
[660, 0.1],
[None, 0.05],
[660, 0.3],
[440, 0.3],
[880, 0.3]
]
}
@staticmethod
def tone(name: str, pin = board.A3) -> None:
"""
Play something on the piezo. Note that this method blocks as the piezo plays, so if a given tone takes, say, 5
seconds to play, this method blocks for the entire 5 seconds.
:param name: Tone/melody/chime/etc. to play; use a string from the keys in Piezo.TONES, like "success"
:param pin: PWM pin to which the piezo is connected
"""
if NVRAMValues.PIEZO.get():
data = Piezo.TONES[name]
for i in range(0, len(data)):
frequency, duration = data[i]
if frequency is None:
time.sleep(duration)
else:
simpleio.tone(pin, frequency, duration)