Skip to content

Commit 16f6136

Browse files
committed
Add note function
1 parent be0309f commit 16f6136

File tree

1 file changed

+60
-9
lines changed
  • ports/esp32/boards/KidBright32/modules

1 file changed

+60
-9
lines changed

ports/esp32/boards/KidBright32/modules/buzzer.py

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,68 @@
33
from machine import Pin, PWM
44
from time import sleep
55

6-
buzzer = PWM(Pin(13), freq=2000, duty=0)
6+
note_map = {
7+
"C4": 261,
8+
"C#4": 277,
9+
"D4": 293,
10+
"Eb4": 311,
11+
"E4": 329,
12+
"F4": 349,
13+
"F#4": 369,
14+
"G4": 391,
15+
"G#4": 415,
16+
"A4": 440,
17+
"Bb4": 466,
18+
"B4": 493,
19+
"C5": 523,
20+
"C#5": 554,
21+
"D5": 587,
22+
"Eb5": 622,
23+
"E5": 659,
24+
"F5": 698,
25+
"F#5": 740,
26+
"G5": 784,
27+
"G#5": 831,
28+
"A5": 880,
29+
"Bb5": 932,
30+
"B5": 988,
31+
"C6": 1046,
32+
"C#6": 1109,
33+
"D6": 1175,
34+
"Eb6": 1244,
35+
"E6": 1318,
36+
"F6": 1396,
37+
"F#6": 1480,
38+
"G6": 1568,
39+
"G#6": 1661,
40+
"A6": 1760,
41+
"Bb6": 1865,
42+
"B6": 1976,
43+
"C7": 2093,
44+
"SIL": 0
45+
}
746

8-
def tone(freq=2093, duration=0.5, duty=50):
9-
buzzer.freq(freq)
10-
buzzer.duty(duty)
47+
__buzzer = PWM(Pin(13), freq=2000, duty=0)
48+
volume = 50
49+
bpm = 120
50+
51+
def tone(freq=2093, duration=0.5):
52+
__buzzer.freq(int(freq))
53+
__buzzer.duty(int(volume / 100 * 512))
1154
sleep(duration)
12-
buzzer.duty(0)
55+
__buzzer.duty(0)
1356

14-
def on(freq=2000, duty=50):
15-
buzzer.freq(freq)
16-
buzzer.duty(duty)
57+
def on(freq=2000):
58+
__buzzer.freq(int(freq))
59+
__buzzer.duty(int(volume / 100 * 512))
1760

1861
def off():
19-
buzzer.duty(0)
62+
__buzzer.duty(0)
63+
64+
def note(notes, duration=4):
65+
quarter_delay = (60 * 1000) / bpm
66+
delay = quarter_delay * duration
67+
delay = delay / 1000 # mS -> S
68+
for note in notes.split(" "):
69+
tone(note_map[note], delay)
70+

0 commit comments

Comments
 (0)