forked from PLdeSousa/micropython-dfplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdfplayer.py
More file actions
165 lines (130 loc) · 4.32 KB
/
dfplayer.py
File metadata and controls
165 lines (130 loc) · 4.32 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
from busio import UART
from microcontroller import Pin
import time
import board
from digitalio import DigitalInOut, Direction, Pull
def sleep_ms(t):
time.sleep(t/1000)
def ticks_ms():
return time.monotonic() * 1000
def ticks_diff(t1, t2):
return t1-t2
Start_Byte = 0x7E
Version_Byte = 0xFF
Command_Length = 0x06
Acknowledge = 0x00
End_Byte = 0xEF
# inherent delays in DFPlayer
CONFIG_LATENCY = 1000
PLAY_LATENCY = 500
VOLUME_LATENCY = 500
def clamp(x, minimum, maximum):
return max(minimum, min(x, maximum))
def split(num):
return num >> 8, num & 0xFF
def kill_time(stamp_ms, kill_ms):
diff_ms = ticks_diff(ticks_ms(), stamp_ms)
if diff_ms < kill_ms:
snooze_ms = kill_ms - diff_ms
sleep_ms(snooze_ms)
return snooze_ms
else:
return 0
class Player():
def __init__(self, uart, busy_pin=None, config=True, volume=0.5, debug = False):
self.debug = debug
self.playtime = None
self._volume = None
self.uart = uart
if busy_pin is not None:
busy_pin.direction = Direction.INPUT
busy_pin.pull = Pull.UP
self.busy_pin = busy_pin
if config:
self.config()
if volume is not None:
self.volume(volume)
def command(self, CMD, Par1, Par2):
self.awaitconfig()
Checksum = -(Version_Byte + Command_Length + CMD + Acknowledge + Par1 + Par2)
HighByte, LowByte = split(Checksum)
CommandLine = bytes([b & 0xFF for b in [
Start_Byte, Version_Byte, Command_Length, CMD, Acknowledge,
Par1, Par2, HighByte, LowByte, End_Byte
]])
if self.debug:
print([hex(c) for c in CommandLine])
self.uart.write(CommandLine)
def query(self, CMD, Par1, Par2):
response = None
while response is None:
self.command(CMD, Par1, Par2)
response = self.uart.read(10)
if self.debug:
print('R', [hex(c) for c in response])
value = (response[5]<<8) + response[6]
return value
def config(self):
self.configtime = ticks_ms()
#self.reset()
self.command(0x3F, 0x00, 0x00)
def play(self, folderNum, trackNum):
self.awaitconfig()
self.playtime = ticks_ms()
self.command(0x0F, folderNum, trackNum)
def finish(self, folderNum, trackNum):
self.play(folderNum, trackNum)
while self.playing():
sleep_ms(50)
def playing(self):
if self.busy_pin is not None:
self.awaitplay()
return not self.busy_pin.value
else:
raise AssertionError("No busy pin provided, cannot detect play status")
def awaitconfig(self):
if self.configtime is not None:
kill_time(self.configtime, CONFIG_LATENCY)
self.configtime = None
def awaitplay(self):
if self.playtime is not None: # handle delay between playing and registering
kill_time(self.playtime, PLAY_LATENCY)
self.playtime = None
def awaitvolume(self):
if self.volumetime is not None: # handle delay between playing and registering
kill_time(self.volumetime, VOLUME_LATENCY)
self.volumetime = None
def repeat(self, repeat=True):
self.awaitconfig()
val = 1 if repeat else 0
self.command(0x11, 0, val)
def _gain(self, gain=1.0):
self.awaitconfig()
gain = float(clamp(gain, 0, 1.0))
val = int(30.0 * gain)
self.command(0x10,0 ,val)
def volume(self, volume=None):
self.awaitconfig()
if volume is None:
return self._volume
else:
self._volume = float(clamp(volume, 0, 1.0))
val = int(30.0 * self._volume)
self.command(0x06,0 ,val)
self.volumetime = ticks_ms()
def standby(self):
self.awaitconfig()
self.command(0x0A, 0x00, 0x00)
def wake(self):
self.awaitconfig()
self.command(0x0B, 0x00, 0x00)
def reset(self):
self.awaitconfig()
self.command(0x0C, 0x00, 0x00)
def stop(self):
self.awaitconfig()
self.command(0x16, 0x00, 0x00)
def query_folders(self):
return self.query(0x4F,0,0)
def query_filesInfolder(self, folder):
return self.query(0x4E,0,folder)