Skip to content

Commit 9a52dd7

Browse files
committed
2 parents 75627e7 + 0a44650 commit 9a52dd7

5 files changed

Lines changed: 142 additions & 122 deletions

File tree

10.microbit/display.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,13 +289,13 @@ def __init__(self):
289289
def stop(self):
290290
self.alive = False
291291
if Display.lock.acquire():
292-
print("stop")
292+
# print("stop")
293293
Display.lock.release()
294294
def scroll(self, val, color=Red, delay=150):
295295
self.stop()
296296
self.alive = True
297297
if Display.lock.acquire():
298-
print("start")
298+
# print("start")
299299
_thread.start_new_thread(self._scroll,([val,color,delay]))
300300
Display.lock.release()
301301
def Disrupt_Col(color):

10.microbit/music.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,12 @@
167167

168168
Letter = 'ABCDEFG#R'
169169

170+
import _thread
171+
170172
class MIDI():
171173

174+
lock = _thread.allocate_lock()
175+
172176
def set_tempo(self, ticks=4, bpm=120):
173177
self.ticks = ticks
174178
self.bpm = bpm
@@ -190,6 +194,7 @@ def stop(self):
190194

191195
def __init__(self):
192196
self.reset()
197+
self.alive = False
193198

194199
def parse(self, tone, dict):
195200
# print(tone)

13.system/task.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
1-
21
import _thread
32

4-
import time
5-
63
class task(object):
7-
84
lock = _thread.allocate_lock()
95

10-
def __init__(self):
6+
def __init__(self, event=(lambda args: print('task running')), args=None):
117
self.alive = False
8+
self.set_cb(event, args)
9+
10+
def set_cb(self, event, args):
11+
self.event = event
12+
self.args = args
1213

1314
def run(self):
1415
if task.lock.acquire():
1516
while self.alive:
16-
time.sleep(0.25)
17-
print("running")
17+
self.event(self.args)
1818
task.lock.release()
1919
_thread.exit()
20-
20+
2121
def stop(self):
2222
self.alive = False
2323
if task.lock.acquire():
24-
print("stop")
24+
# print("stop")
2525
task.lock.release()
2626

2727
def start(self):
2828
self.stop()
2929
self.alive = True
3030
if task.lock.acquire():
31-
print("start")
31+
# print("start")
3232
_thread.start_new_thread(self.run, ())
3333
task.lock.release()
3434

35+
3536
if __name__ == "__main__":
36-
tmp = task()
37+
import time
38+
def unit_test(args):
39+
time.sleep(0.25)
40+
print('unit_test', args)
41+
tmp = task(unit_test, {'name': 'unit_test_0'})
3742
tmp.start()
3843
time.sleep(1)
44+
tmp.set_cb(unit_test, {'name': 'unit_test_1'})
3945
tmp.start()
4046
time.sleep(2)
47+
tmp.set_cb(unit_test, {'name': 'unit_test_2'})
4148
tmp.start()
4249
# time.sleep(3)
4350
tmp.stop()
Lines changed: 11 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,11 @@
11
# Author: qiren123
2-
# This file is part of MicroPython MIDI Music
2+
# This file is part of MicroPython RTTTL Music
33
# Copyright (c) 2018 qiren123
44
#
55
# Licensed under the MIT license:
66
# http://www.opensource.org/licenses/mit-license.php
77
#
8-
9-
DADADADUM = ['r4:2', 'g', 'g', 'g', 'eb:8', 'r:2', 'f', 'f', 'f', 'd:8']
10-
11-
ENTERTAINER = [
12-
'd4:1', 'd#', 'e', 'c5:2', 'e4:1', 'c5:2', 'e4:1', 'c5:3', 'c:1', 'd',
13-
'd#', 'e', 'c', 'd', 'e:2', 'b4:1', 'd5:2', 'c:4'
14-
]
15-
16-
PRELUDE = [
17-
'c4:1', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e', 'c4', 'e', 'g', 'c5', 'e',
18-
'g4', 'c5', 'e', 'c4', 'd', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'c4', 'd',
19-
'g', 'd5', 'f', 'g4', 'd5', 'f', 'b3', 'd4', 'g', 'd5', 'f', 'g4', 'd5',
20-
'f', 'b3', 'd4', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'c4', 'e', 'g', 'c5',
21-
'e', 'g4', 'c5', 'e', 'c4', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e'
22-
]
23-
24-
ODE = [
25-
'e4', 'e', 'f', 'g', 'g', 'f', 'e', 'd', 'c', 'c', 'd', 'e', 'e:6', 'd:2',
26-
'd:8', 'e:4', 'e', 'f', 'g', 'g', 'f', 'e', 'd', 'c', 'c', 'd', 'e', 'd:6',
27-
'c:2', 'c:8'
28-
]
29-
30-
NYAN = [
31-
'f#5:1', 'g#', 'c#:1', 'd#:2', 'b4:1', 'd5:1', 'c#', 'b4:2', 'b', 'c#5',
32-
'd', 'd:1', 'c#', 'b4:1', 'c#5:1', 'd#', 'f#', 'g#', 'd#', 'f#', 'c#', 'd',
33-
'b4', 'c#5', 'b4', 'd#5:2', 'f#', 'g#:1', 'd#', 'f#', 'c#', 'd#', 'b4',
34-
'd5', 'd#', 'd', 'c#', 'b4', 'c#5', 'd:2', 'b4:1', 'c#5', 'd#', 'f#', 'c#',
35-
'd', 'c#', 'b4', 'c#5:2', 'b4', 'c#5', 'b4', 'f#:1', 'g#', 'b:2', 'f#:1',
36-
'g#', 'b', 'c#5', 'd#', 'b4', 'e5', 'd#', 'e', 'f#', 'b4:2', 'b', 'f#:1',
37-
'g#', 'b', 'f#', 'e5', 'd#', 'c#', 'b4', 'f#', 'd#', 'e', 'f#', 'b:2',
38-
'f#:1', 'g#', 'b:2', 'f#:1', 'g#', 'b', 'b', 'c#5', 'd#', 'b4', 'f#', 'g#',
39-
'f#', 'b:2', 'b:1', 'a#', 'b', 'f#', 'g#', 'b', 'e5', 'd#', 'e', 'f#',
40-
'b4:2', 'c#5'
41-
]
42-
43-
RINGTONE = [
44-
'c4:1', 'd', 'e:2', 'g', 'd:1', 'e', 'f:2', 'a', 'e:1', 'f', 'g:2', 'b',
45-
'c5:4'
46-
]
47-
48-
FUNK = [
49-
'c2:2', 'c', 'd#', 'c:1', 'f:2', 'c:1', 'f:2', 'f#', 'g', 'c', 'c', 'g',
50-
'c:1', 'f#:2', 'c:1', 'f#:2', 'f', 'd#'
51-
]
52-
53-
BLUES = [
54-
'c2:2', 'e', 'g', 'a', 'a#', 'a', 'g', 'e', 'c2:2', 'e', 'g', 'a', 'a#',
55-
'a', 'g', 'e', 'f', 'a', 'c3', 'd', 'd#', 'd', 'c', 'a2', 'c2:2', 'e', 'g',
56-
'a', 'a#', 'a', 'g', 'e', 'g', 'b', 'd3', 'f', 'f2', 'a', 'c3', 'd#',
57-
'c2:2', 'e', 'g', 'e', 'g', 'f', 'e', 'd'
58-
]
59-
60-
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',
63-
'f', 'g', 'f:8'
64-
]
65-
66-
WEDDING = [
67-
'c4:4', 'f:3', 'f:1', 'f:8', 'c:4', 'g:3', 'e:1', 'f:8', 'c:4', 'f:3',
68-
'a:1', 'c5:4', 'a4:3', 'f:1', 'f:4', 'e:3', 'f:1', 'g:8'
69-
]
70-
71-
FUNERAL = [
72-
'c3:4', 'c:3', 'c:1', 'c:4', 'd#:3', 'd:1', 'd:3', 'c:1', 'c:3', 'b2:1',
73-
'c3:4'
74-
]
75-
76-
PUNCHLINE = ['c4:3', 'g3:1', 'f#', 'g', 'g#:3', 'g', 'r', 'b', 'c4']
77-
78-
PYTHON = [
79-
'd5:1', 'b4', 'r', 'b', 'b', 'a#', 'b', 'g5', 'r', 'd', 'd', 'r', 'b4',
80-
'c5', 'r', 'c', 'c', 'r', 'd', 'e:5', 'c:1', 'a4', 'r', 'a', 'a', 'g#',
81-
'a', 'f#5', 'r', 'e', 'e', 'r', 'c', 'b4', 'r', 'b', 'b', 'r', 'c5', 'd:5',
82-
'd:1', 'b4', 'r', 'b', 'b', 'a#', 'b', 'b5', 'r', 'g', 'g', 'r', 'd', 'c#',
83-
'r', 'a', 'a', 'r', 'a', 'a:5', 'g:1', 'f#:2', 'a:1', 'a', 'g#', 'a',
84-
'e:2', 'a:1', 'a', 'g#', 'a', 'd', 'r', 'c#', 'd', 'r', 'c#', 'd:2', 'r:3'
85-
]
86-
87-
BADDY = ['c3:3', 'r', 'd:2', 'd#', 'r', 'c', 'r', 'f#:8']
88-
89-
CHASE = [
90-
'a4:1', 'b', 'c5', 'b4', 'a:2', 'r', 'a:1', 'b', 'c5', 'b4', 'a:2', 'r',
91-
'a:2', 'e5', 'd#', 'e', 'f', 'e', 'd#', 'e', 'b4:1', 'c5', 'd', 'c',
92-
'b4:2', 'r', 'b:1', 'c5', 'd', 'c', 'b4:2', 'r', 'b:2', 'e5', 'd#', 'e',
93-
'f', 'e', 'd#', 'e'
94-
]
95-
96-
BA_DING = ['b5:1', 'e6:3']
97-
98-
WAWAWAWAA = ['e3:3', 'r:1', 'd#:3', 'r:1', 'd:4', 'r:1', 'c#:8']
99-
100-
JUMP_UP = ['c5:1', 'd', 'e', 'f', 'g']
101-
102-
JUMP_DOWN = ['g5:1', 'f', 'e', 'd', 'c']
103-
104-
POWER_UP = ['g4:1', 'c5', 'e4', 'g5:2', 'e5:1', 'g5:3']
105-
106-
POWER_DOWN = ['g5:1', 'd#', 'c', 'g4:2', 'b:1', 'c5:3']
8+
from music_bulid_in import *
1079

10810
normal_tone = {
10911
'A1': 55, 'B1': 62, 'C1': 33, 'D1': 37, 'E1': 41, 'F1': 44, 'G1': 49,
@@ -167,7 +69,7 @@
16769

16870
Letter = 'ABCDEFG#R'
16971

170-
class MIDI():
72+
class RTTTL():
17173

17274
def set_tempo(self, ticks=4, bpm=120):
17375
self.ticks = ticks
@@ -207,7 +109,7 @@ def parse(self, tone, dict):
207109
# print(int(freq), int(time))
208110
return int(freq), int(time)
209111

210-
def midi(self, tone):
112+
def RTTTL(self, tone):
211113
# print(tone)
212114
pos = tone.find('#')
213115
if pos != -1:
@@ -237,10 +139,10 @@ def play(self, tune, pin=25, duration=None):
237139
tone = tone.upper() # all to upper
238140
if tone[0] not in Letter:
239141
continue
240-
midi = self.midi(tone)
241-
pwm.freq(midi[0]) # set frequency
242-
pwm.duty(midi[1]) # set duty cycle
243-
sleep_ms(midi[1])
142+
RTTTL = self.RTTTL(tone)
143+
pwm.freq(RTTTL[0]) # set frequency
144+
pwm.duty(RTTTL[1]) # set duty cycle
145+
sleep_ms(RTTTL[1])
244146
finally:
245147
pwm.deinit()
246148

@@ -260,7 +162,7 @@ def pitch(self, freq, tim, pin=25):
260162
def unit_test():
261163
print('The unit test code is as follows')
262164
print('\n\
263-
music = MIDI()\n\
165+
music = RTTTL()\n\
264166
music.play(BIRTHDAY)\n\
265167
music.play(NYAN)\n\
266168
music.play(PRELUDE)\n\
@@ -270,7 +172,7 @@ def unit_test():
270172
for freq in range(1760, 880, -16):\n\
271173
music.pitch(freq, 30)\n\
272174
')
273-
music = MIDI()
175+
music = RTTTL()
274176
music.play(BIRTHDAY)
275177
music.play(NYAN)
276178
music.play(PRELUDE)
@@ -281,7 +183,7 @@ def unit_test():
281183
music.pitch(freq, 30)
282184

283185

284-
__music__ = MIDI()
186+
__music__ = RTTTL()
285187

286188
play = __music__.play
287189
pitch = __music__.pitch

14.music/music_bulid_in.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Author: qiren123
2+
# This file is part of MicroPython MIDI Music
3+
# Copyright (c) 2018 qiren123
4+
#
5+
# Licensed under the MIT license:
6+
# http://www.opensource.org/licenses/mit-license.php
7+
#
8+
9+
DADADADUM = ['r4:2', 'g', 'g', 'g', 'eb:8', 'r:2', 'f', 'f', 'f', 'd:8']
10+
11+
ENTERTAINER = [
12+
'd4:1', 'd#', 'e', 'c5:2', 'e4:1', 'c5:2', 'e4:1', 'c5:3', 'c:1', 'd',
13+
'd#', 'e', 'c', 'd', 'e:2', 'b4:1', 'd5:2', 'c:4'
14+
]
15+
16+
PRELUDE = [
17+
'c4:1', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e', 'c4', 'e', 'g', 'c5', 'e',
18+
'g4', 'c5', 'e', 'c4', 'd', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'c4', 'd',
19+
'g', 'd5', 'f', 'g4', 'd5', 'f', 'b3', 'd4', 'g', 'd5', 'f', 'g4', 'd5',
20+
'f', 'b3', 'd4', 'g', 'd5', 'f', 'g4', 'd5', 'f', 'c4', 'e', 'g', 'c5',
21+
'e', 'g4', 'c5', 'e', 'c4', 'e', 'g', 'c5', 'e', 'g4', 'c5', 'e'
22+
]
23+
24+
ODE = [
25+
'e4', 'e', 'f', 'g', 'g', 'f', 'e', 'd', 'c', 'c', 'd', 'e', 'e:6', 'd:2',
26+
'd:8', 'e:4', 'e', 'f', 'g', 'g', 'f', 'e', 'd', 'c', 'c', 'd', 'e', 'd:6',
27+
'c:2', 'c:8'
28+
]
29+
30+
NYAN = [
31+
'f#5:1', 'g#', 'c#:1', 'd#:2', 'b4:1', 'd5:1', 'c#', 'b4:2', 'b', 'c#5',
32+
'd', 'd:1', 'c#', 'b4:1', 'c#5:1', 'd#', 'f#', 'g#', 'd#', 'f#', 'c#', 'd',
33+
'b4', 'c#5', 'b4', 'd#5:2', 'f#', 'g#:1', 'd#', 'f#', 'c#', 'd#', 'b4',
34+
'd5', 'd#', 'd', 'c#', 'b4', 'c#5', 'd:2', 'b4:1', 'c#5', 'd#', 'f#', 'c#',
35+
'd', 'c#', 'b4', 'c#5:2', 'b4', 'c#5', 'b4', 'f#:1', 'g#', 'b:2', 'f#:1',
36+
'g#', 'b', 'c#5', 'd#', 'b4', 'e5', 'd#', 'e', 'f#', 'b4:2', 'b', 'f#:1',
37+
'g#', 'b', 'f#', 'e5', 'd#', 'c#', 'b4', 'f#', 'd#', 'e', 'f#', 'b:2',
38+
'f#:1', 'g#', 'b:2', 'f#:1', 'g#', 'b', 'b', 'c#5', 'd#', 'b4', 'f#', 'g#',
39+
'f#', 'b:2', 'b:1', 'a#', 'b', 'f#', 'g#', 'b', 'e5', 'd#', 'e', 'f#',
40+
'b4:2', 'c#5'
41+
]
42+
43+
RINGTONE = [
44+
'c4:1', 'd', 'e:2', 'g', 'd:1', 'e', 'f:2', 'a', 'e:1', 'f', 'g:2', 'b',
45+
'c5:4'
46+
]
47+
48+
FUNK = [
49+
'c2:2', 'c', 'd#', 'c:1', 'f:2', 'c:1', 'f:2', 'f#', 'g', 'c', 'c', 'g',
50+
'c:1', 'f#:2', 'c:1', 'f#:2', 'f', 'd#'
51+
]
52+
53+
BLUES = [
54+
'c2:2', 'e', 'g', 'a', 'a#', 'a', 'g', 'e', 'c2:2', 'e', 'g', 'a', 'a#',
55+
'a', 'g', 'e', 'f', 'a', 'c3', 'd', 'd#', 'd', 'c', 'a2', 'c2:2', 'e', 'g',
56+
'a', 'a#', 'a', 'g', 'e', 'g', 'b', 'd3', 'f', 'f2', 'a', 'c3', 'd#',
57+
'c2:2', 'e', 'g', 'e', 'g', 'f', 'e', 'd'
58+
]
59+
60+
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',
63+
'f', 'g', 'f:8'
64+
]
65+
66+
WEDDING = [
67+
'c4:4', 'f:3', 'f:1', 'f:8', 'c:4', 'g:3', 'e:1', 'f:8', 'c:4', 'f:3',
68+
'a:1', 'c5:4', 'a4:3', 'f:1', 'f:4', 'e:3', 'f:1', 'g:8'
69+
]
70+
71+
FUNERAL = [
72+
'c3:4', 'c:3', 'c:1', 'c:4', 'd#:3', 'd:1', 'd:3', 'c:1', 'c:3', 'b2:1',
73+
'c3:4'
74+
]
75+
76+
PUNCHLINE = ['c4:3', 'g3:1', 'f#', 'g', 'g#:3', 'g', 'r', 'b', 'c4']
77+
78+
PYTHON = [
79+
'd5:1', 'b4', 'r', 'b', 'b', 'a#', 'b', 'g5', 'r', 'd', 'd', 'r', 'b4',
80+
'c5', 'r', 'c', 'c', 'r', 'd', 'e:5', 'c:1', 'a4', 'r', 'a', 'a', 'g#',
81+
'a', 'f#5', 'r', 'e', 'e', 'r', 'c', 'b4', 'r', 'b', 'b', 'r', 'c5', 'd:5',
82+
'd:1', 'b4', 'r', 'b', 'b', 'a#', 'b', 'b5', 'r', 'g', 'g', 'r', 'd', 'c#',
83+
'r', 'a', 'a', 'r', 'a', 'a:5', 'g:1', 'f#:2', 'a:1', 'a', 'g#', 'a',
84+
'e:2', 'a:1', 'a', 'g#', 'a', 'd', 'r', 'c#', 'd', 'r', 'c#', 'd:2', 'r:3'
85+
]
86+
87+
BADDY = ['c3:3', 'r', 'd:2', 'd#', 'r', 'c', 'r', 'f#:8']
88+
89+
CHASE = [
90+
'a4:1', 'b', 'c5', 'b4', 'a:2', 'r', 'a:1', 'b', 'c5', 'b4', 'a:2', 'r',
91+
'a:2', 'e5', 'd#', 'e', 'f', 'e', 'd#', 'e', 'b4:1', 'c5', 'd', 'c',
92+
'b4:2', 'r', 'b:1', 'c5', 'd', 'c', 'b4:2', 'r', 'b:2', 'e5', 'd#', 'e',
93+
'f', 'e', 'd#', 'e'
94+
]
95+
96+
BA_DING = ['b5:1', 'e6:3']
97+
98+
WAWAWAWAA = ['e3:3', 'r:1', 'd#:3', 'r:1', 'd:4', 'r:1', 'c#:8']
99+
100+
JUMP_UP = ['c5:1', 'd', 'e', 'f', 'g']
101+
102+
JUMP_DOWN = ['g5:1', 'f', 'e', 'd', 'c']
103+
104+
POWER_UP = ['g4:1', 'c5', 'e4', 'g5:2', 'e5:1', 'g5:3']
105+
106+
POWER_DOWN = ['g5:1', 'd#', 'c', 'g4:2', 'b:1', 'c5:3']

0 commit comments

Comments
 (0)