'''
Raspberry Pi Pico et VS1053. Tous les fichiers mp3
sur la carte SD sont joués l'un après l'autre.

Pour plus d'infos, consultez le blog:
https://electroniqueamateur.blogspot.com/2022/08/lire-des-fichiers-mp3-avec-raspberry-pi.html

'''

from vs1053_syn import * # https://github.com/peterhinch/micropython-vs1053
from machine import SPI, Pin
import time
import os

# initialisation de SPI
spi = SPI(0, sck=Pin(6), mosi=Pin(7), miso=Pin(4))

# broche CS de la carte SD:
sdcs = Pin(15, Pin.OUT, value=1) 

# broches du module VS1053
reset = Pin(13, Pin.OUT, value=1) 
xcs = Pin(12, Pin.OUT, value=1) 
xdcs = Pin(11, Pin.OUT, value=1)
dreq = Pin(10, Pin.IN)

player = VS1053(spi, reset, dreq, xdcs, xcs, sdcs=sdcs, mp='/fc')
player.volume(-25, -25) # réglage du volume sonore

songs = sorted([x for x in os.listdir('/fc') if x.endswith('.mp3')])

for song in songs:
    print("En train de jouer: ", song) # affichage du titre
    fn = ''.join(('/fc/', song))
    with open(fn, 'rb') as f:
        player.play(f)