Skip to content

Commit 3898e89

Browse files
committed
add music_esp32 for scratch3
1 parent 0a44650 commit 3898e89

3 files changed

Lines changed: 257 additions & 15 deletions

File tree

13.system/task.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import _thread
22

3-
class task(object):
3+
from utime import sleep_ms
4+
5+
class Task(object):
46
lock = _thread.allocate_lock()
57

68
def __init__(self, event=(lambda args: print('task running')), args=None):
@@ -12,33 +14,37 @@ def set_cb(self, event, args):
1214
self.args = args
1315

1416
def run(self):
15-
if task.lock.acquire():
17+
if Task.lock.acquire():
1618
while self.alive:
1719
self.event(self.args)
18-
task.lock.release()
20+
Task.lock.release()
1921
_thread.exit()
2022

2123
def stop(self):
22-
self.alive = False
23-
if task.lock.acquire():
24-
# print("stop")
25-
task.lock.release()
24+
if self.alive is True:
25+
self.alive = False
26+
if Task.lock.acquire():
27+
# print("stop")
28+
Task.lock.release()
2629

27-
def start(self):
30+
def start(self, size=2048):
2831
self.stop()
2932
self.alive = True
30-
if task.lock.acquire():
33+
if Task.lock.acquire():
3134
# print("start")
35+
import gc
36+
gc.collect()
37+
_thread.stack_size(size)
3238
_thread.start_new_thread(self.run, ())
33-
task.lock.release()
34-
39+
_thread.stack_size()
40+
Task.lock.release()
3541

3642
if __name__ == "__main__":
3743
import time
3844
def unit_test(args):
39-
time.sleep(0.25)
4045
print('unit_test', args)
41-
tmp = task(unit_test, {'name': 'unit_test_0'})
46+
sleep_ms(500)
47+
tmp = Task(unit_test, {'name': 'unit_test_0'})
4248
tmp.start()
4349
time.sleep(1)
4450
tmp.set_cb(unit_test, {'name': 'unit_test_1'})

14.music/music_bulid_in.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
]
5959

6060
BIRTHDAY = [
61-
'c4:4', 'c:1', 'd:4', 'c:4', 'f', 'e:8', 'c:3', 'c:1', 'd:4', 'c:4', 'g',
62-
'f:8', 'c:3', 'c:1', 'c5:4', 'a4', 'f', 'e', 'd', 'a#:3', 'a#:1', 'a:4',
61+
'c5:4', 'c:1', 'd:4', 'c:4', 'f', 'e:8', 'c:3', 'c:1', 'd:4', 'c:4', 'g',
62+
'f:8', 'c:3', 'c:1', 'c6:4', 'a4', 'f', 'e', 'd', 'a#:3', 'a#:1', 'a:4',
6363
'f', 'g', 'f:8'
6464
]
6565

14.music/music_esp32.py

Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
from task import *
2+
3+
from machine import Pin, PWM
4+
from utime import sleep_ms
5+
6+
from music_bulid_in import *
7+
8+
9+
class RTTTL(object):
10+
11+
normal_tone = {
12+
'A1': 55, 'B1': 62, 'C1': 33, 'D1': 37, 'E1': 41, 'F1': 44, 'G1': 49,
13+
14+
'A2': 110, 'B2': 123, 'C2': 65, 'D2': 73, 'E2': 82, 'F2': 87, 'G2': 98,
15+
16+
'A3': 220, 'B3': 247, 'C3': 131, 'D3': 147, 'E3': 165, 'F3': 175, 'G3': 196,
17+
18+
'A4': 440, 'B4': 494, 'C4': 262, 'D4': 294, 'E4': 330, 'F4': 349, 'G4': 392,
19+
20+
'A5': 880, 'B5': 988, 'C5': 523, 'D5': 587, 'E5': 659, 'F5': 698, 'G5': 784,
21+
22+
'A6': 1760, 'B6': 1976, 'C6': 1047, 'D6': 1175, 'E6': 1319, 'F6': 1397, 'G6': 1568,
23+
24+
'A7': 3520, 'B7': 3951, 'C7': 2093, 'D7': 2349, 'E7': 2637, 'F7': 2794, 'G7': 3135,
25+
26+
'A8': 7040, 'B8': 7902, 'C8': 4186, 'D8': 4699, 'E8': 5274, 'F8': 5588, 'G8': 6271,
27+
28+
'A9': 14080, 'B9': 15804
29+
}
30+
31+
rising_tone = {
32+
'A1': 58, 'C1': 35, 'D1': 39, 'F1': 46, 'G1': 52,
33+
34+
'A2': 117, 'C2': 69, 'D2': 78, 'F2': 93, 'G2': 104,
35+
36+
'A3': 233, 'C3': 139, 'D3': 156, 'F3': 185, 'G3': 208,
37+
38+
'A4': 466, 'C4': 277, 'D4': 311, 'F4': 370, 'G4': 415,
39+
40+
'A5': 932, 'C5': 554, 'D5': 622, 'F5': 740, 'G5': 831,
41+
42+
'A6': 1865, 'C6': 1109, 'D6': 1245, 'F6': 1480, 'G6': 1661,
43+
44+
'A7': 3729, 'C7': 2217, 'D7': 2489, 'F7': 2960, 'G7': 3322,
45+
46+
'A8': 7459, 'C8': 4435, 'D8': 4978, 'F8': 5920, 'G8': 6645,
47+
48+
'A9': 14917
49+
}
50+
51+
falling_tone = {
52+
'B1': 58, 'D1': 35, 'E1': 39, 'G1': 46, 'A1': 52,
53+
54+
'B2': 117, 'D2': 69, 'E2': 78, 'G2': 93, 'A2': 104,
55+
56+
'B3': 233, 'D3': 139, 'E3': 156, 'G3': 185, 'A3': 208,
57+
58+
'B4': 466, 'D4': 277, 'E4': 311, 'G4': 370, 'A4': 415,
59+
60+
'B5': 932, 'D5': 554, 'E5': 622, 'G5': 740, 'A5': 831,
61+
62+
'B6': 1865, 'D6': 1109, 'E6': 1245, 'G6': 1480, 'A6': 1661,
63+
64+
'B7': 3729, 'D7': 2217, 'E7': 2489, 'G7': 2960, 'A7': 3322,
65+
66+
'B8': 7459, 'D8': 4435, 'E8': 4978, 'G8': 5920, 'A8': 6645,
67+
68+
'B9': 14917
69+
}
70+
71+
Letter = 'ABCDEFG#R'
72+
73+
def set_tempo(self, ticks=4, bpm=120):
74+
self.ticks = ticks
75+
self.bpm = bpm
76+
self.beat = 60000 / self.bpm / self.ticks
77+
78+
def set_octave(self, octave=4):
79+
self.octave = octave
80+
81+
def set_duration(self, duration=4):
82+
self.duration = duration
83+
84+
def reset(self):
85+
self.set_duration()
86+
self.set_octave()
87+
self.set_tempo()
88+
89+
def __init__(self):
90+
self.reset()
91+
92+
def parse(self, tone, dict):
93+
# print(tone)
94+
time = self.beat * self.duration
95+
pos = tone.find(':')
96+
if pos != -1:
97+
time = self.beat * int(tone[(pos + 1):])
98+
tone = tone[:pos]
99+
# print(tone)
100+
freq, tone_size = 1, len(tone)
101+
if 'R' in tone:
102+
freq = 1
103+
elif tone_size == 1:
104+
freq = dict[tone[0] + str(self.octave)]
105+
elif tone_size == 2:
106+
freq = dict[tone]
107+
self.set_octave(tone[1:])
108+
# print(int(freq), int(time))
109+
return int(freq), int(time)
110+
111+
def rtttl(self, tone):
112+
# print(tone)
113+
pos = tone.find('#')
114+
if pos != -1:
115+
return self.parse(tone.replace('#', ''), RTTTL.rising_tone)
116+
pos = tone.find('B')
117+
if pos != -1 and pos != 0:
118+
return self.parse(tone.replace('B', ''), RTTTL.falling_tone)
119+
return self.parse(tone, RTTTL.normal_tone)
120+
121+
def set_default(self, tone):
122+
pos = tone.find(':')
123+
if pos != -1:
124+
self.set_duration(int(tone[(pos + 1):]))
125+
# tone = tone[:pos]
126+
127+
128+
class music(RTTTL):
129+
130+
def __init__(self, pin=25):
131+
super(music, self).__init__()
132+
133+
self.flag = ''
134+
self.tune = []
135+
self.task = Task(music.pre, self)
136+
self.pwm = PWM(Pin(pin))
137+
self.out()
138+
139+
def __del__(self):
140+
self.stop()
141+
self.pwm.deinit()
142+
143+
def out(self, freq=0, tim=0):
144+
self.pwm.freq(freq)
145+
self.pwm.duty(tim)
146+
sleep_ms(tim)
147+
148+
def pre(Self):
149+
150+
if Self.flag is 'play' and len(Self.tune) > 0:
151+
# print(Self.tune)
152+
tone = Self.tune[0].upper() # all to upper
153+
if tone[0] in Self.Letter:
154+
tmp = Self.rtttl(tone)
155+
# print(tone)
156+
Self.out(tmp[0], tmp[1])
157+
Self.tune.pop(0)
158+
159+
if Self.flag is 'pitch':
160+
# print(Self.tune)
161+
tmp = Self.tune
162+
for freq in (tmp[0]):
163+
Self.out(freq, tmp[1])
164+
Self.tune = []
165+
166+
if Self.flag != 'stop' and len(Self.tune) == 0:
167+
Self.flag = 'stop'
168+
Self.out()
169+
170+
if Self.flag == 'stop':
171+
sleep_ms(1000)
172+
173+
def play(self, tune, duration=None):
174+
self.task.stop()
175+
self.flag = 'play'
176+
self.tune = list(tune)
177+
# print(self.tune)
178+
if duration is None:
179+
self.set_default(tune[0])
180+
else:
181+
self.set_duration(duration)
182+
self.task.start()
183+
184+
def pitch(self, freq, tim):
185+
self.task.stop()
186+
self.flag = 'pitch'
187+
self.tune = list([freq, tim])
188+
self.task.start()
189+
190+
def stop(self):
191+
self.flag = 'stop'
192+
self.task.stop()
193+
self.out()
194+
195+
def unit_test():
196+
tmp = music()
197+
198+
tmp.play(POWER_UP)
199+
sleep_ms(500)
200+
tmp.play(POWER_UP)
201+
sleep_ms(1000)
202+
tmp.play(POWER_DOWN)
203+
sleep_ms(500)
204+
tmp.play(POWER_DOWN)
205+
sleep_ms(1000)
206+
207+
tmp.pitch(range(880, 1760, 16), 30)
208+
sleep_ms(50)
209+
tmp.pitch(range(1760, 880, -16), 30)
210+
sleep_ms(50)
211+
212+
tmp.play(BIRTHDAY)
213+
sleep_ms(15000)
214+
tmp.play(NYAN)
215+
sleep_ms(10000)
216+
tmp.play(PRELUDE)
217+
sleep_ms(10000)
218+
tmp.play(PYTHON)
219+
sleep_ms(10000)
220+
221+
tmp.__del__()
222+
223+
__music__ = music()
224+
225+
play = __music__.play
226+
pitch = __music__.pitch
227+
set_tempo = __music__.set_tempo
228+
229+
if __name__ == '__main__':
230+
unit_test()
231+
232+
play(NYAN)
233+
sleep_ms(1000)
234+
235+
pitch(range(880, 1760, 16), 30)
236+
sleep_ms(50)

0 commit comments

Comments
 (0)