Last active 1700878528

https://blahaj.zone/notes/9mehzr6pjs5zsfs8

Revision 189ee5622dc78d8b379217ba4b0c62222c8e9504

main.py Raw
1import machine
2import utime
3
4class Notes:
5 B3 = 247
6 E4 = 330
7 FS4 = 370
8 GS4 = 415
9 A4 = 440
10 D7 = 2349
11
12buzzer = machine.PWM(machine.Pin(22))
13green = machine.Pin(25, machine.Pin.OUT)
14green.value(0)
15
16rtc=machine.RTC()
17offset = 0
18
19minute = [(Notes.D7, 0.1)]
20hour = [(Notes.D7, 0.4)]
21#minute = [
22# (Notes.D7, 0.1)
23#]
24#hour = [
25# (Notes.GS4, 0.4),
26# (Notes.E4, 0.4),
27# (Notes.FS4, 0.4),
28# (Notes.B3, 0.4),
29# (None, 0.4),
30# (None, 0.4),
31# (Notes.B3, 0.4),
32# (Notes.FS4, 0.4),
33# (Notes.GS4, 0.4),
34# (Notes.E4, 0.4)
35#]
36
37def init_clock():
38 global offset
39 # check if offset pin is pulled high: GP0-11 for 5-60 minute offset
40 for n,pin in enumerate(range(0,12)):
41 inp = machine.Pin(pin, machine.Pin.IN, pull=None)
42 if inp.value():
43 offset += (n+1)*300
44 break
45 m,s = divmod(offset,60)
46 h,m = divmod(m,60)
47 print(offset,h,m,s)
48 t = rtc.datetime()
49 rtc.datetime((t[0],t[1],t[2],t[3],t[4]+h,t[5]+m,t[6]+s,t[7]))
50
51def loop():
52 while True:
53 timestamp=rtc.datetime()
54 print("%04d-%02d-%02d %02d:%02d:%02d.%04d"%(timestamp[0:3] + timestamp[4:8]))
55 song = []
56 # internal RTC starts on 2021-01-01 00:00:01.0000
57 if timestamp[6]-1 == 0:
58 if timestamp[5] == 0:
59 song = hour
60 else:
61 song = minute
62 for note,length in song:
63 if length > 0.5:
64 raise Exception
65 green.value(1)
66 if note:
67 buzzer.freq(note)
68 buzzer.duty_u16(1000)
69 utime.sleep(length)
70 green.value(0)
71 if note:
72 buzzer.duty_u16(0)
73 utime.sleep(0.5-length)
74 if len(song)%2 != 0:
75 utime.sleep(0.5)
76 else:
77 utime.sleep(1)
78
79init_clock()
80loop()