-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuzzer_music.py
39 lines (29 loc) · 909 Bytes
/
buzzer_music.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
from rtttl import RTTTL
import songs
import utime
import uasyncio
import door_state
import config
from machine import Pin, PWM
buz_tim = PWM(Pin(config.BUZZER_PIN))
async def play_tone(freq, msec):
print('freq = {:6.1f} msec = {:6.1f}'.format(freq, msec))
if freq > 0:
buz_tim.freq(int(freq))
buz_tim.duty(50)
await uasyncio.sleep_ms(int(msec * 0.9))
buz_tim.duty(0)
utime.sleep_ms(int(msec * 0.1))
async def play_song(song):
tune = RTTTL(songs.find(song))
for freq, msec in tune.notes():
await play_tone(freq, msec)
if not door_state.is_open:
return
async def play_completely(song):
tune = RTTTL(songs.find(song))
for freq, msec in tune.notes():
await play_tone(freq, msec)
def get_random_song():
i = int(utime.localtime()[5] / 3) # dirty, only works with 20 songs
return songs.SONGS[i].split(':')[0]