更新 mini支持后台播放音乐
This commit is contained in:
@@ -5,6 +5,7 @@ Micropython library for the Music buzzer(Coprocessor I2C communication)
|
|||||||
=======================================================
|
=======================================================
|
||||||
@dahanzimin From the Mixly Team
|
@dahanzimin From the Mixly Team
|
||||||
"""
|
"""
|
||||||
|
import _thread
|
||||||
from time import sleep_ms
|
from time import sleep_ms
|
||||||
|
|
||||||
normal_tone = {
|
normal_tone = {
|
||||||
@@ -24,6 +25,8 @@ class MIDI():
|
|||||||
def __init__(self, bus, volume=100):
|
def __init__(self, bus, volume=100):
|
||||||
self.reset()
|
self.reset()
|
||||||
self._bus = bus
|
self._bus = bus
|
||||||
|
self._play = False
|
||||||
|
self._over = True
|
||||||
self._volume = volume // 5
|
self._volume = volume // 5
|
||||||
|
|
||||||
def set_volume(self, volume):
|
def set_volume(self, volume):
|
||||||
@@ -86,12 +89,16 @@ class MIDI():
|
|||||||
tone = tone[:pos]
|
tone = tone[:pos]
|
||||||
|
|
||||||
def play(self, tune, duration=None):
|
def play(self, tune, duration=None):
|
||||||
|
self._play = True
|
||||||
|
self._over = False
|
||||||
if duration is None:
|
if duration is None:
|
||||||
self.set_default(tune[0])
|
self.set_default(tune[0])
|
||||||
else:
|
else:
|
||||||
self.set_duration(duration)
|
self.set_duration(duration)
|
||||||
for tone in tune:
|
for tone in tune:
|
||||||
tone = tone.upper()
|
tone = tone.upper()
|
||||||
|
if not self._play:
|
||||||
|
break
|
||||||
if tone[0] not in Letter:
|
if tone[0] not in Letter:
|
||||||
continue
|
continue
|
||||||
midi = self.midi(tone)
|
midi = self.midi(tone)
|
||||||
@@ -100,6 +107,15 @@ class MIDI():
|
|||||||
self._bus.buzzer(0)
|
self._bus.buzzer(0)
|
||||||
sleep_ms(1)
|
sleep_ms(1)
|
||||||
sleep_ms(10)
|
sleep_ms(10)
|
||||||
|
self._over = True
|
||||||
|
|
||||||
|
def play_thread(self, tune, duration=None):
|
||||||
|
self._play = False
|
||||||
|
while not self._over:
|
||||||
|
pass
|
||||||
|
if not self._play:
|
||||||
|
_thread.start_new_thread(self.play, (tune, duration))
|
||||||
|
sleep_ms(50)
|
||||||
|
|
||||||
def pitch(self, freq):
|
def pitch(self, freq):
|
||||||
self._bus.buzzer(self._volume, int(freq))
|
self._bus.buzzer(self._volume, int(freq))
|
||||||
@@ -112,6 +128,7 @@ class MIDI():
|
|||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self._bus.buzzer(0)
|
self._bus.buzzer(0)
|
||||||
|
self._play = False
|
||||||
sleep_ms(10)
|
sleep_ms(10)
|
||||||
|
|
||||||
BA_DING=('b5:1','e6:3')
|
BA_DING=('b5:1','e6:3')
|
||||||
|
|||||||
Reference in New Issue
Block a user