-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_debug.py
More file actions
71 lines (58 loc) · 2.02 KB
/
example_debug.py
File metadata and controls
71 lines (58 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import board
import busio
import time
from DFPlayer import DFPlayer
# Configuration
UART_TX = board.GP0
UART_RX = board.GP1
# Setup UART
uart = busio.UART(UART_TX, UART_RX, baudrate=9600, timeout=0.1)
# Initialize DFPlayer with debug enabled
df = DFPlayer(uart, debug=True, volume= 10, command_delay=0.1)
print("-" * 40)
print("DFPlayer Hardware Diagnostic Tool")
print("-" * 40)
def hex_string(data):
if not data: return "None"
return ' '.join(['%02X' % b for b in data])
def run_test():
print("[1/4] Querying Volume...")
res = df.query_volume()
if res:
print(f"Response: {hex_string(res)}")
print(f"Volume is: {res[6]}")
else:
print("No response for volume query")
print("[2/4] Querying SD Card track count...")
res = df.query_track_count()
if res:
print(f"Response: {hex_string(res)}")
count = (res[5] << 8) | res[6]
print(f"Total tracks on SD: {count}")
else:
print("No response for track count query")
print("[3/4] Trying to play track 1...")
df.play_track(1)
print("[4/4] Monitoring UART for events (e.g. track finished)...")
print("Press Ctrl+C to stop.")
while True:
if uart.in_waiting:
# Read whatever comes in
data = uart.read(uart.in_waiting)
print(f"RAW DATA IN: {hex_string(data)}")
# Check for specific codes if 10 bytes received
if len(data) == 10 and data[0] == 0x7E:
cmd = data[3]
if cmd == 0x3D:
print(f"Event: Track { (data[5] << 8) | data[6] } finished playing (SD)")
elif cmd == 0x3A:
print("Event: SD Card Inserted")
elif cmd == 0x3B:
print("Event: SD Card Removed")
elif cmd == 0x40:
print(f"Error: Module returned error code {data[6]}")
time.sleep(0.1)
try:
run_test()
except KeyboardInterrupt:
print("Stopping diagnostic tool.")