-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathBuzzer.py
More file actions
289 lines (253 loc) · 6.24 KB
/
Buzzer.py
File metadata and controls
289 lines (253 loc) · 6.24 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
# Buzzer.py - Object-oriented implementation of active and passive buzzers
# Author: Arijit Sengupta
"""
import time
from machine import Pin, PWM
from Log import *
class Buzzer:
"""
A simple buzzer class - use it to play and pause different sounds
ranging from fequencies 10 through 10000
default volume is half volume - set it between 0 and 10
"""
def __init__(self, pin, name='Buzzer'):
"""
Base class init - we don't do anything with the pin here
"""
self._name = name
def beep(self, tone=500, duration=150):
"""
Beep the buzzer with the given tone for duration ms
"""
Log.i(f"Beeping {self._name} at {tone}hz for {duration} ms")
self.play(tone)
time.sleep(duration / 1000)
self.stop()
def play(self, tone=500):
""" Stub for playing a tone - implemented in subclasses """
pass
def stop(self):
""" Stub for stopping - implemented in subclasses """
pass
class ActiveBuzzer(Buzzer):
"""
An active buzzer has an internal oscillator that plays a fixed tone when power is applied
Cannot control the tone. Only turn on and off.
"""
def __init__(self, pin, name='Buzzer'):
super().__init__(pin, name)
self._buz = Pin(pin, Pin.OUT)
def play(self, tone=500):
""" Play sound. Tone is ignored. """
Log.i(f"Start playing {self._name}")
self._buz.value(1)
def stop(self):
""" Stop the sound. """
Log.i(f"Stop playing {self._name}")
self._buz.value(0)
class PassiveBuzzer(Buzzer):
"""
A passive buzzer does not have an internal oscillator. MC needs to send a PWM signal
to play tones. The tone is controlled by the frequency of the PWM, and the volume level
is controlled by the duty cycle. Setting duty cycle to 0 stops sound.
"""
MAX = 32767 # Max value for duty cycle
def __init__(self, pin, name='Buzzer'):
Log.i("PassiveBuzzer: constructor")
super().__init__(pin, name)
self._buz = PWM(Pin(pin))
self._volume = 0.5 # Default volume is half
self._playing = False
self.stop()
def play(self, tone=500):
""" play the supplied tone. """
Log.i(f"{self._name}: playing tone {tone}")
self._buz.freq(tone)
self._buz.duty_u16(int(self._volume * self.MAX))
self._playing = True
def stop(self):
""" Stop playing sound """
Log.i(f"{self._name}: stopping tone")
self._buz.duty_u16(0)
self._playing = False
def setVolume(self, volume=0.5):
"""
Change the volume of the sound currently playing and future plays.
Volume is between 0 and 1.
Volume 0 is silent, volume 1 is max volume. Note that this is not a linear
and towards the lower end, the sound drops off quickly. At the higher end,
the sound increases slowly, and might only be noticeable when volume is
set to max.
"""
Log.i(f"{self._name}: changing volume to {volume}")
self._volume = volume
if (self._playing):
self._buz.duty_u16(int(self._volume * self.MAX))
# Known tones from https://github.com/james1236/buzzer_music
tones = {
'C0':16,
'C#0':17,
'D0':18,
'D#0':19,
'E0':21,
'F0':22,
'F#0':23,
'G0':24,
'G#0':26,
'A0':28,
'A#0':29,
'B0':31,
'C1':33,
'C#1':35,
'D1':37,
'D#1':39,
'E1':41,
'F1':44,
'F#1':46,
'G1':49,
'G#1':52,
'A1':55,
'A#1':58,
'B1':62,
'C2':65,
'C#2':69,
'D2':73,
'D#2':78,
'E2':82,
'F2':87,
'F#2':92,
'G2':98,
'G#2':104,
'A2':110,
'A#2':117,
'B2':123,
'C3':131,
'C#3':139,
'D3':147,
'D#3':156,
'E3':165,
'F3':175,
'F#3':185,
'G3':196,
'G#3':208,
'A3':220,
'A#3':233,
'B3':247,
'C4':262,
'C#4':277,
'D4':294,
'D#4':311,
'E4':330,
'F4':349,
'F#4':370,
'G4':392,
'G#4':415,
'A4':440,
'A#4':466,
'B4':494,
'C5':523,
'C#5':554,
'D5':587,
'D#5':622,
'E5':659,
'F5':698,
'F#5':740,
'G5':784,
'G#5':831,
'A5':880,
'A#5':932,
'B5':988,
'C6':1047,
'C#6':1109,
'D6':1175,
'D#6':1245,
'E6':1319,
'F6':1397,
'F#6':1480,
'G6':1568,
'G#6':1661,
'A6':1760,
'A#6':1865,
'B6':1976,
'C7':2093,
'C#7':2217,
'D7':2349,
'D#7':2489,
'E7':2637,
'F7':2794,
'F#7':2960,
'G7':3136,
'G#7':3322,
'A7':3520,
'A#7':3729,
'B7':3951,
'C8':4186,
'C#8':4435,
'D8':4699,
'D#8':4978,
'E8':5274,
'F8':5588,
'F#8':5920,
'G8':6272,
'G#8':6645,
'A8':7040,
'A#8':7459,
'B8':7902,
'C9':8372,
'C#9':8870,
'D9':9397,
'D#9':9956,
'E9':10548,
'F9':11175,
'F#9':11840,
'G9':12544,
'G#9':13290,
'A9':14080,
'A#9':14917,
'B9':15804
}
# Some basic do re mi tones
DO = tones['C4']
RE = tones['D4']
MI = tones['E4']
FA = tones['F4']
SO = tones['G4']
LA = tones['A4']
TI = tones['B4']
DO2 = tones['C5']
### The following code is for testing purposes only
### To use this code, add an Active Buzzer on Pin 14
### and a Passive Buzzer on Pin 15 Then run the module directly
if __name__ == "__main__":
Log.i("Testing Active Buzzer")
active_buzzer = ActiveBuzzer(14, "TestActiveBuzzer")
active_buzzer.play()
time.sleep(1)
active_buzzer.stop()
Log.i("Testing Passive Buzzer")
buzzer = PassiveBuzzer(16, "TestBuzzer")
buzzer.play(DO)
time.sleep(1)
buzzer.stop()
buzzer.setVolume(0.2)
buzzer.play(RE)
time.sleep(1)
buzzer.stop()
buzzer.setVolume(0)
buzzer.play(MI) # Should not sound
time.sleep(1)
buzzer.stop()
# Test volume change
Log.i("Testing volume change")
for v in range(0, 11):
buzzer.setVolume(v / 10)
buzzer.play(FA)
time.sleep(0.5)
# Play do re mi
Log.i("Playing Do Re Mi")
buzzer.setVolume(0.5)
for note in [DO, RE, MI, FA, SO, LA, TI, DO2]:
buzzer.play(note)
time.sleep(0.5)
buzzer.stop()