Repository navigation
Expand file tree
/
Copy pathbuzzer.py
More file actions
executable file
·83 lines (69 loc) · 1.75 KB
/
Copy pathbuzzer.py
File metadata and controls
executable file
·83 lines (69 loc) · 1.75 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
#!/usr/bin/python3
"""Class for playing simple melodies with the raspi's pwms.
Demo/Test:
./buzzer.py
"""
import RPi.GPIO as GPIO
import time
class Buzzer(object):
def __init__(self, pin):
self._pin = pin
GPIO.setup(pin, GPIO.OUT)
self._pwm = GPIO.PWM(pin, 440)
self._pwm.start(10)
def playnote(self, freq_hz, duration_ms):
print(freq_hz, duration_ms)
if freq_hz > 0:
self._pwm.ChangeFrequency(freq_hz)
time.sleep(duration_ms * 1000.0)
if freq_hz > 0:
pass
# self._pwm.stop(volume)
def playsong(self, notes):
for note in notes:
self.playnote(*note)
if __name__ == '__main__':
GPIO.setmode(GPIO.BOARD)
_FUER_ELISE = [
(659.255, 240),
(622.254, 240),
(659.255, 240),
(622.254, 240),
(659.255, 240),
(493.883, 240),
(587.330, 240),
(523.251, 240),
(440.000, 720),
(261.626, 240),
(329.628, 240),
(440.000, 240),
(493.883, 720),
(329.628, 240),
(415.305, 240),
(493.883, 240),
(523.251, 720),
(329.628, 240),
(659.255, 240),
(622.254, 240),
(659.255, 240),
(622.254, 240),
(659.255, 240),
(493.883, 240),
(587.330, 240),
(523.251, 240),
(440.000, 720),
(261.626, 240),
(329.628, 240),
(440.000, 240),
(493.883, 720),
(329.628, 240),
(523.251, 240),
(493.883, 240),
(440.000, 720),
]
buzzer = Buzzer(12)
buzzer.playnote(440, 1000)
buzzer.playnote(880, 1000)
buzzer.playnote(1760, 1000)
buzzer.playsong(_FUER_ELISE)
GPIO.cleanup()