Skip to content

Commit 1524dd2

Browse files
Tinyu-Zhaolbuque
authored andcommitted
libs/unit: Fix crash after switching UART port in audioplayer.
Signed-off-by: [email protected] <[email protected]>
1 parent 8c49548 commit 1524dd2

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

m5stack/libs/unit/audioplayer.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import time
66
import machine
7+
from micropython import schedule
78

89
if sys.platform != "esp32":
910
from typing import Literal
@@ -30,51 +31,34 @@ class AudioPlayerUnit:
3031
audio_player_0 = AudioPlayerUnit(2, port=(33, 32))
3132
"""
3233

34+
_uart_instance = None
35+
3336
def __init__(self, id: Literal[0, 1, 2] = 2, port: list | tuple = None, verbose=False):
37+
if AudioPlayerUnit._uart_instance is not None:
38+
AudioPlayerUnit._uart_instance.deinit()
39+
verbose and print("UART instance exit, deinit uart obj")
40+
3441
self.uart = machine.UART(id, tx=port[1], rx=port[0])
3542
self.uart.init(9600, bits=8, parity=None, stop=1)
3643
self.uart.irq(handler=self._handler, trigger=machine.UART.IRQ_RXIDLE)
37-
self.uart.read()
44+
AudioPlayerUnit._uart_instance = self.uart
3845
self.verbose = verbose
3946
self.raw_message = ""
4047
self.command_num = 0
4148
self.is_recieved = False
4249
self.received_data = [False]
4350
self.play_status = None
51+
self.uart.read()
4452

4553
def _handler(self, uart) -> None:
4654
data = uart.read()
55+
schedule(self._handler_data, data)
56+
57+
def _handler_data(self, data):
4758
if data is not None and len(data) > 5:
4859
self.verbose and print(
4960
"Received message:", " ".join(f"0x{byte:02X}" for byte in data).split()
5061
)
51-
# self.verbose and print(data[0] == self.command)
52-
# self.verbose and print(data[1] == (~self.command) & 0xFF)
53-
# if self.retun_value is not None:
54-
# self.verbose and print(data[2:4] == bytes(self.retun_value))
55-
# else:
56-
# self.verbose and print("True")
57-
# self.verbose and print(
58-
# data[-1]
59-
# == (
60-
# self.command + ~self.command
61-
# & 0xFF
62-
# + sum(data[4:-1])
63-
# + (0 if self.retun_value is None else sum(self.retun_value))
64-
# )
65-
# & 0xFF
66-
# )
67-
# self.verbose and print(
68-
# (
69-
# hex(
70-
# self.command
71-
# + (~self.command & 0xFF)
72-
# + sum(data[4:-1])
73-
# + (0 if self.retun_value is None else sum(self.retun_value))
74-
# & 0xFF
75-
# )
76-
# )
77-
# )
7862
if (data[0] == 0x0A and self.command == 0x05) or (
7963
data[0] == self.command and data[1] == (~self.command & 0xFF)
8064
):
@@ -99,7 +83,7 @@ def _handler(self, uart) -> None:
9983
# self.check_tick_callback()
10084
else:
10185
self.verbose and print("Invalid frame received: header/footer mismatch")
102-
uart.read()
86+
self.uart.read()
10387

10488
def _wait_for_message(self, time_out: int = 500):
10589
self.is_recieved = False
@@ -146,7 +130,7 @@ def check_play_status(self):
146130
audio_player_0.check_play_status()
147131
"""
148132
self._send_message(0x04, [0x00], [0x02, 0x00])
149-
return self._wait_for_message(500)[0]
133+
return self._wait_for_message(600)[0]
150134

151135
def play_audio(self) -> int: # 可从暂停处开始播放
152136
"""Play the audio.
@@ -165,7 +149,7 @@ def play_audio(self) -> int: # 可从暂停处开始播放
165149
audio_player_0.play_audio()
166150
"""
167151
self._send_message(0x04, [0x01], [0x02, 0x00])
168-
return self._wait_for_message(500)[0]
152+
time.sleep(0.6)
169153

170154
def pause_audio(self) -> int: # 暂停播放
171155
"""Pause the audio.

0 commit comments

Comments
 (0)