Skip to content

Commit 9209d81

Browse files
authored
Merge pull request #2319 from jepler/jeplayer-gamepadshift-etc
Bring project up to date
2 parents 6a75d4b + 1c059d2 commit 9209d81

File tree

3 files changed

+17
-4867
lines changed

3 files changed

+17
-4867
lines changed

CircuitPython_JEplayer_mp3/code.py

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
import adafruit_bitmap_font.bitmap_font
4040
import adafruit_display_text.label
41-
from adafruit_progressbar.progressbar import ProgressBar
41+
from adafruit_progressbar import ProgressBar
4242
import sdcardio
4343
import analogjoy
4444
import audioio
@@ -48,7 +48,7 @@
4848
import digitalio
4949
import displayio
5050
import terminalio
51-
import gamepadshift
51+
from keypad import ShiftRegisterKeys
5252
import icons
5353
import neopixel
5454
import repeat
@@ -83,7 +83,7 @@ def __init__(self):
8383
for i in range(5, 8):
8484
self.iconbar.icons[i].x += 32
8585
self.label = adafruit_display_text.label.Label(font, line_spacing=1.0)
86-
self.label.y = 6
86+
self.label.y = 4
8787
self._bitmap_filename = None
8888
self._fallback_bitmap = ["/rsrc/background.bmp"]
8989
self._rms = 0.
@@ -256,7 +256,8 @@ def auto_next(self):
256256
@staticmethod
257257
def has_any_mp3s(folder):
258258
"""True if the folder contains at least one item ending in .mp3"""
259-
return any(not fn.startswith(".") and fn.lower().endswith(".mp3") for fn in os.listdir(folder))
259+
return any(not fn.startswith(".") and fn.lower().endswith(".mp3")
260+
for fn in os.listdir(folder))
260261

261262
def choose_folder(self, base='/sd'):
262263
"""Let the user choose a folder within a base directory"""
@@ -269,7 +270,6 @@ def choose_folder(self, base='/sd'):
269270
idx = self.next_choice
270271
else:
271272
idx = menu_choice(choices,
272-
BUTTON_START | BUTTON_A | BUTTON_B | BUTTON_SEL,
273273
sel_idx=self.next_choice,
274274
text_font=terminalio.FONT)
275275
clear_display()
@@ -291,27 +291,21 @@ def choose_folder(self, base='/sd'):
291291
mp3stream = audiomp3.MP3Decoder(open("/rsrc/splash.mp3", "rb"))
292292
speaker.play(mp3stream)
293293

294-
font = adafruit_bitmap_font.bitmap_font.load_font("rsrc/5x8.bdf")
294+
font = adafruit_bitmap_font.bitmap_font.load_font("rsrc/5x8.pcf")
295295
playback_display = PlaybackDisplay()
296296
board.DISPLAY.show(playback_display.group)
297297
font.load_glyphs(range(32, 128))
298298

299-
BUTTON_SEL = const(8)
300-
BUTTON_START = const(4)
301-
BUTTON_A = const(2)
302-
BUTTON_B = const(1)
303-
304-
305299
joystick = analogjoy.AnalogJoystick()
306300

307301
up_key = repeat.KeyRepeat(lambda: joystick.up, rate=0.2)
308302
down_key = repeat.KeyRepeat(lambda: joystick.down, rate=0.2)
309303
left_key = repeat.KeyRepeat(lambda: joystick.left, rate=0.2)
310304
right_key = repeat.KeyRepeat(lambda: joystick.right, rate=0.2)
311305

312-
buttons = gamepadshift.GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
313-
digitalio.DigitalInOut(board.BUTTON_OUT),
314-
digitalio.DigitalInOut(board.BUTTON_LATCH))
306+
buttons = ShiftRegisterKeys(clock=board.BUTTON_CLOCK,
307+
data=board.BUTTON_OUT,
308+
latch=board.BUTTON_LATCH, key_count=4, value_when_pressed=True)
315309
# pylint: enable=invalid-name
316310

317311
def mount_sd():
@@ -332,7 +326,7 @@ def shuffle(seq):
332326
seq[i], seq[j] = seq[j], seq[i]
333327

334328
# pylint: disable=too-many-locals,too-many-statements
335-
def menu_choice(seq, button_ok, button_cancel=0, *, sel_idx=0, text_font=font):
329+
def menu_choice(seq, *, sel_idx=0, text_font=font):
336330
"""Display a menu and allow a choice from it"""
337331
gc.collect()
338332
board.DISPLAY.auto_refresh = True
@@ -365,16 +359,14 @@ def menu_choice(seq, button_ok, button_cancel=0, *, sel_idx=0, text_font=font):
365359
last_scroll_idx = max(0, len(seq) - num_rows)
366360

367361
board.DISPLAY.show(scene)
368-
buttons.get_pressed() # Clear out anything from before now
362+
buttons.events.clear()
369363
i = 0
370364
old_scroll_idx = None
371365

372366
while True:
373367
enable.value = speaker.playing
374-
pressed = buttons.get_pressed()
375-
if button_cancel and (pressed & button_cancel):
376-
return -1
377-
if pressed & button_ok:
368+
event = buttons.events.get()
369+
if event and event.pressed:
378370
return sel_idx
379371

380372
joystick.poll()
@@ -410,11 +402,6 @@ def isdir(x):
410402
"""Return True if 'x' is a directory"""
411403
return os.stat(x)[0] & S_IFDIR
412404

413-
def wait_no_button_pressed():
414-
"""Wait until no button is pressed"""
415-
while buttons.get_pressed():
416-
time.sleep(1/20)
417-
418405
def change_stream(filename):
419406
"""Change the global MP3Decoder object to play a new file"""
420407
old_stream = mp3stream.file
@@ -437,12 +424,10 @@ def play_one_file(idx, filename, folder, title, playlist_size):
437424
board.DISPLAY.refresh()
438425

439426
result = None
440-
wait_no_button_pressed()
441427
file_size = os.stat(filename)[6]
442428
mp3file = change_stream(filename)
443429
playback_display.play(mp3stream)
444430
board.DISPLAY.auto_refresh = True
445-
last_pressed = buttons.get_pressed()
446431

447432
while speaker.playing:
448433

@@ -459,13 +444,9 @@ def play_one_file(idx, filename, folder, title, playlist_size):
459444
if right_key.value:
460445
playback_display.move(1)
461446

462-
pressed = buttons.get_pressed()
463-
rising_edge = pressed & ~last_pressed
464-
last_pressed = pressed
465-
466-
if rising_edge:
447+
event = buttons.events.get()
448+
if event and event.pressed:
467449
return_now = playback_display.press(idx)
468-
wait_no_button_pressed()
469450
if return_now:
470451
result = return_now[0]
471452
break
@@ -514,7 +495,8 @@ def longest_common_prefix(seq):
514495

515496
def play_folder(location):
516497
"""Play everything within a given folder"""
517-
playlist = [d for d in os.listdir(location) if not d.startswith('.') and d.lower().endswith('.mp3')]
498+
playlist = [d for d in os.listdir(location)
499+
if not d.startswith('.') and d.lower().endswith('.mp3')]
518500
if not playlist:
519501
# hmm, no mp3s in a folder? Well, don't crash okay?
520502
del playlist

0 commit comments

Comments
 (0)