From 21d7c4f84a171397ca215b5f865aba94d026f6b5 Mon Sep 17 00:00:00 2001 From: dahanzimin <353767514@qq.com> Date: Tue, 14 Oct 2025 16:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20mini=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=92=AD=E6=94=BE=E9=9F=B3=E4=B9=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../origin/build/lib/musicx.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/boards/default_src/micropython_esp32c2/origin/build/lib/musicx.py b/boards/default_src/micropython_esp32c2/origin/build/lib/musicx.py index d6e5d98e..2f564845 100644 --- a/boards/default_src/micropython_esp32c2/origin/build/lib/musicx.py +++ b/boards/default_src/micropython_esp32c2/origin/build/lib/musicx.py @@ -5,6 +5,7 @@ Micropython library for the Music buzzer(Coprocessor I2C communication) ======================================================= @dahanzimin From the Mixly Team """ +import _thread from time import sleep_ms normal_tone = { @@ -24,6 +25,8 @@ class MIDI(): def __init__(self, bus, volume=100): self.reset() self._bus = bus + self._play = False + self._over = True self._volume = volume // 5 def set_volume(self, volume): @@ -86,12 +89,16 @@ class MIDI(): tone = tone[:pos] def play(self, tune, duration=None): + self._play = True + self._over = False if duration is None: self.set_default(tune[0]) else: self.set_duration(duration) for tone in tune: tone = tone.upper() + if not self._play: + break if tone[0] not in Letter: continue midi = self.midi(tone) @@ -100,6 +107,15 @@ class MIDI(): self._bus.buzzer(0) sleep_ms(1) 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): self._bus.buzzer(self._volume, int(freq)) @@ -112,6 +128,7 @@ class MIDI(): def stop(self): self._bus.buzzer(0) + self._play = False sleep_ms(10) BA_DING=('b5:1','e6:3')