Add initial button handling
This commit is contained in:
@@ -53,6 +53,47 @@ machine.mem32[0x4001c004 + 8*4] = 0x67
|
||||
machine.mem32[0x40030000 + 0x00] = 0x10
|
||||
|
||||
|
||||
class Buttons:
|
||||
def __init__(self, player, pin_volup=17, pin_voldown=19, pin_next=18):
|
||||
self._VOLUP = micropython.const(1)
|
||||
self._VOLDOWN = micropython.const(2)
|
||||
self._NEXT = micropython.const(3)
|
||||
self.player = player
|
||||
self.buttons = {machine.Pin(pin_volup, machine.Pin.IN, machine.Pin.PULL_UP): self._VOLUP,
|
||||
machine.Pin(pin_voldown, machine.Pin.IN, machine.Pin.PULL_UP): self._VOLDOWN,
|
||||
machine.Pin(pin_next, machine.Pin.IN, machine.Pin.PULL_UP): self._NEXT}
|
||||
self.int_flag = asyncio.ThreadSafeFlag()
|
||||
self.pressed = []
|
||||
self.last = {}
|
||||
for button in self.buttons.keys():
|
||||
button.irq(handler=self._interrupt, trigger=machine.Pin.IRQ_FALLING | machine.Pin.IRQ_RISING)
|
||||
|
||||
def _interrupt(self, button):
|
||||
keycode = self.buttons[button]
|
||||
last = self.last.get(keycode, 0)
|
||||
now = time.ticks_ms()
|
||||
self.last[keycode] = now
|
||||
if now - last < 10:
|
||||
# debounce, discard
|
||||
return
|
||||
if button.value() == 0:
|
||||
# print(f'B{keycode} {now}')
|
||||
self.pressed.append(keycode)
|
||||
self.int_flag.set()
|
||||
|
||||
async def task(self):
|
||||
while True:
|
||||
await self.int_flag.wait()
|
||||
while len(self.pressed) > 0:
|
||||
what = self.pressed.pop()
|
||||
if what == self._VOLUP:
|
||||
self.player.set_volume(min(255, self.player.get_volume()+1))
|
||||
elif what == self._VOLDOWN:
|
||||
self.player.set_volume(max(0, self.player.get_volume()-1))
|
||||
elif what == self._NEXT:
|
||||
self.player.play_next()
|
||||
|
||||
|
||||
class SDContext:
|
||||
def __init__(self, mosi, miso, sck, ss, baudrate):
|
||||
self.mosi = mosi
|
||||
@@ -88,9 +129,12 @@ def run():
|
||||
with SDContext(mosi=Pin(3), miso=Pin(4), sck=Pin(2), ss=Pin(5), baudrate=15000000), \
|
||||
AudioContext(Pin(8), Pin(6)) as audioctx:
|
||||
player = MP3Player(audioctx)
|
||||
player.set_volume(128)
|
||||
player.set_volume(32)
|
||||
asyncio.create_task(player.task())
|
||||
|
||||
buttons = Buttons(player)
|
||||
asyncio.create_task(buttons.task())
|
||||
|
||||
# Setup app
|
||||
timer_manager = TimerManager(True)
|
||||
playback_manager = TagPlaybackManager(timer_manager, player)
|
||||
|
||||
@@ -12,6 +12,7 @@ class MP3Player:
|
||||
self.command_event = asyncio.Event()
|
||||
self.playlist = []
|
||||
self.mp3task = None
|
||||
self.volume = 128
|
||||
|
||||
def set_playlist(self, mp3files):
|
||||
"""
|
||||
@@ -52,8 +53,12 @@ class MP3Player:
|
||||
"""
|
||||
Set volume (0..255).
|
||||
"""
|
||||
self.volume = volume
|
||||
self.audiocore.set_volume(volume)
|
||||
|
||||
def get_volume(self) -> int:
|
||||
return self.volume
|
||||
|
||||
def _send_command(self, command: str):
|
||||
self.commands.append(command)
|
||||
self.command_event.set()
|
||||
|
||||
Reference in New Issue
Block a user