-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfplayer_control.py
85 lines (66 loc) · 2.2 KB
/
dfplayer_control.py
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
from micropython import const
from machine import Pin
from lib.dfplayermini import Player
from utime import sleep_ms, ticks_ms
PLAYER_TX = const(17)
PLAYER_RX = const(16)
PLAYER_BOOT = const(1000)
BTN_ONE = const(22)
BTN_TWO = const(23)
class SoundPlayer:
def __init__(self, tx: int, rx: int, tracks: int, wait: int = 200):
"""
Sound player constructor
:param tx: value for TX GPIO pin
:param rx: value for RX GPIO pin
:param wait: waiting period in milliseconds to read SD card files
"""
self._tracks_amount = int(tracks)
self._track_current = 1
self.player = Player(pin_TX=int(tx), pin_RX=int(rx))
sleep_ms(int(wait))
self.player.volume(10)
def next(self) -> None:
"""
play next track
:return: None
"""
if self._track_current < self._tracks_amount:
self._track_current += 1
self.player.play(self._track_current)
def prev(self) -> None:
"""
play previous track
:return: None
"""
if self._track_current > 1:
self._track_current -= 1
self.player.play(self._track_current)
class Control:
def __init__(self, player):
"""
control constructor
:param player: DFPlayer object
"""
self._last_time = 0
self._player = player
self.btn_next = Pin(BTN_ONE, Pin.IN, Pin.PULL_UP)
self.btn_prev = Pin(BTN_TWO, Pin.IN, Pin.PULL_UP)
self.btn_next.irq(trigger=Pin.IRQ_FALLING, handler=self._handler)
self.btn_prev.irq(trigger=Pin.IRQ_FALLING, handler=self._handler)
def _handler(self, pin) -> None:
"""
interrupt handler for buttons (next/prev)
:param pin: handler pin object
:return: None
"""
new_time = ticks_ms()
if (new_time - self._last_time) > 200:
self._last_time = new_time
if pin == Pin(BTN_ONE):
self._player.next()
if pin == Pin(BTN_TWO):
self._player.prev()
if __name__ == '__main__':
music = SoundPlayer(tx=PLAYER_TX, rx=PLAYER_RX, tracks=5, wait=PLAYER_BOOT)
btn = Control(player=music)